public SendMessageAsync ( |
||
request | Container for the necessary parameters to execute the SendMessage operation. | |
cancellationToken | System | /// A cancellation token that can be used by other objects or threads to receive notice of cancellation. /// |
return | 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); } }
/// <summary> /// Requeues the specified message. /// </summary> /// <param name="message">The message.</param> /// <param name="delayMilliseconds">Number of milliseconds to delay delivery of the message.</param> public void Requeue(Message message, int delayMilliseconds) { try { Reject(message, false); using (var client = new AmazonSQSClient(_credentials)) { _logger.Value.InfoFormat("SqsMessageConsumer: requeueing the message {0}", message.Id); message.Header.Bag.Remove("ReceiptHandle"); var request = new SendMessageRequest(_queueUrl, JsonConvert.SerializeObject(message)) { DelaySeconds = (int)TimeSpan.FromMilliseconds(delayMilliseconds).TotalSeconds }; client.SendMessageAsync(request).Wait(); } _logger.Value.InfoFormat("SqsMessageConsumer: requeued the message {0}", message.Id); } catch (Exception exception) { _logger.Value.ErrorException("SqsMessageConsumer: Error purging queue {0}", exception, _queueUrl); throw; } }