public GetQueueUrlAsync ( |
||
request | Container for the necessary parameters to execute the GetQueueUrl operation. | |
cancellationToken | System | /// A cancellation token that can be used by other objects or threads to receive notice of cancellation. /// |
Результат | Task |
public async Task SendAsync(string message, CancellationToken token) { // Console.WriteLine("Sending messages:"); using (var client = new Amazon.SQS.AmazonSQSClient(accessKey, secretKey, region)) { var queueUrlResult = await client.GetQueueUrlAsync(queueName, token); var queueUrl = queueUrlResult.QueueUrl; var req = new SendMessageRequest(queueUrl, message); if (isFIFO) { // required req.MessageGroupId = "group"; req.MessageDeduplicationId = message; } req.MessageAttributes.Add("CorrelationID", new MessageAttributeValue() { DataType = "string", StringValue = Guid.NewGuid().ToString() }); //req.DelaySeconds = 10; var r = await client.SendMessageAsync(req); Console.WriteLine("[" + DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss.fff") + "] [P] " + message + " | " + r.MessageId); } }
public async Task ConsumeAsync(int maxMessages = 5, CancellationToken token = default) { Console.WriteLine("\nConsuming messages:"); using (var client = new Amazon.SQS.AmazonSQSClient(accessKey, secretKey, region)) { var queueUrlResult = await client.GetQueueUrlAsync(queueName, token); var queueUrl = queueUrlResult.QueueUrl; while (true) { /// Console.WriteLine("Fetching messages:"); var req = new ReceiveMessageRequest(queueUrl); req.WaitTimeSeconds = 20; // long pooling req.MaxNumberOfMessages = maxMessages; var r = await client.ReceiveMessageAsync(req, token); if (r.Messages.Count > 0) { foreach (var m in r.Messages) { Console.WriteLine("[" + DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss.fff") + "] [C] " + m.Body + " | " + m.MessageId); // ack await client.DeleteMessageAsync(queueUrl, m.ReceiptHandle); } } } } }
/// <summary> /// Creates the SQS queue. If the queue already exists, the existing URL is returned. /// </summary> /// <param name="awsCredentials">The AWS credentials for access the queue.</param> /// <returns>System.String.</returns> /// <exception cref="System.InvalidOperationException">Unable to create SQS queue</exception> private string GetSqsQueueUrl(AWSCredentials awsCredentials) { Logger.LogDebug($"Getting SQS Queue: {AwsSettings.Sqs.Name}"); Logger.LogDebug($"Using AWS Region: {AwsSettings.Sqs.Region}"); sqsClient = new SQS.AmazonSQSClient(awsCredentials, RegionEndpoint.GetBySystemName(AwsSettings.Sqs.Region)); var queueUrlResponse = AsyncTaskHelper.RunSync(() => sqsClient.GetQueueUrlAsync(AwsSettings.Sqs.Name)); Logger.LogTrace($"SQS Queue Response[Status:{queueUrlResponse.HttpStatusCode}], [QueueUrl:{queueUrlResponse.QueueUrl}]"); if (queueUrlResponse.HttpStatusCode != HttpStatusCode.OK) { Logger.LogError($"Error getting SQS Queue. Status code: {queueUrlResponse.HttpStatusCode}. Aborting."); throw new InvalidOperationException("Unable to open SQS queue"); } return(queueUrlResponse.QueueUrl); }
/// <summary> /// /// </summary> /// <param name="messages"></param> /// <param name="token"></param> /// <returns></returns> public async Task SendBatchAsync(string[] messages, CancellationToken token) { Console.WriteLine(string.Format("Sending {0} messages: ", messages.Length)); using (var client = new Amazon.SQS.AmazonSQSClient(accessKey, secretKey, region)) { var queueUrlResult = await client.GetQueueUrlAsync(queueName, token); var queueUrl = queueUrlResult.QueueUrl; var entries = new List <SendMessageBatchRequestEntry>(); foreach (var message in messages) { var req = new SendMessageBatchRequestEntry(Guid.NewGuid().ToString(), message); if (isFIFO) { // required req.MessageGroupId = "group"; // fifo atua sobre o grupo req.MessageDeduplicationId = Guid.NewGuid().ToString(); } entries.Add(req); } var batch = new SendMessageBatchRequest(queueUrl, entries); var r = await client.SendMessageBatchAsync(batch); if (r.Failed != null && r.Failed.Count > 0) { foreach (var f in r.Failed) { Console.WriteLine("Failed : ", f.Message); } } } }