public void Dispose() { if (_amazonSqsClient != null) { _amazonSqsClient.Dispose(); } }
protected override void Dispose(bool disposing) { foreach (string arn in _topicArns) { try { Client.DeleteTopicAsync(new DeleteTopicRequest { TopicArn = arn }).Wait(); } catch { }; } foreach (string url in _queueUrl) { try { sqsClient.DeleteQueueAsync(new DeleteQueueRequest { QueueUrl = url }).Wait(); } catch { }; } if (sqsClient != null) { sqsClient.Dispose(); sqsClient = null; } base.Dispose(disposing); }
protected override void Dispose(bool disposing) { if (disposing && _sqsClient != null) { _sqsClient.Dispose(); } }
/// <summary> /// encasulates the create connection logic /// </summary> /// <param name="config">configuration options</param> /// <returns>redis connection</returns> private ISqsConnection OnCreate(ISqsSettings config) { var sqsConfig = new AmazonSQSConfig { ////sqsConfig.Timeout = TimeSpan.FromSeconds(5); RegionEndpoint = RegionEndpoint.GetBySystemName(config.Region), }; var awsCreds = new BasicAWSCredentials(config.Username, config.Password); AmazonSQSClient?temp = null; try { temp = new AmazonSQSClient(awsCreds, sqsConfig); var connection = new SqsConnection(temp, config, this.Logger); temp = null; return(connection); } finally { temp?.Dispose(); } }
public static IAmazonSQS CreateSqsClient(this IAmazonSqsSettings amazonSqsSettings, ITransactionContext transactionContext) { return(transactionContext.GetOrAdd(AmazonConstaints.SqsClientContextKey, () => { var amazonSqsClient = new AmazonSQSClient(amazonSqsSettings.AmazonCredentialsFactory.Create(), amazonSqsSettings.AmazonSqsConfig); transactionContext.OnDisposed((ITransactionContext context) => amazonSqsClient.Dispose()); return amazonSqsClient; })); }
public async Task StopAsync(CancellationToken cancellationToken) { _logger.LogInformation("Stopping QueuePollingBackgroundService..."); //_cancellationToken.Cancel(); _sqsClient.Dispose(); //_cancellationToken.Dispose(); _logger.LogInformation("QueuePollingBackgroundService service stopped."); await Task.CompletedTask; }
public static void Cleanup() { BaseClean(); if (sqsClient != null) { sqsClient.Dispose(); sqsClient = null; } }
public async ValueTask DisposeAsync() { await _snsClient.DeleteTopicAsync(TopicArn); await _sqsClient.DeleteQueueAsync(QueueUrl); _snsClient.Dispose(); _sqsClient.Dispose(); }
public async Task SendMessage(string emi) { SendMessageRequest message = new SendMessageRequest(); message.QueueUrl = "https://sqs.us-west-2.amazonaws.com/XXXX/MortgageQueue"; //change the XXX with the actual value from aws console sqs service message.MessageBody = $"This is EMI: {emi}"; await sqsClient.SendMessageAsync(message); sqsClient.Dispose(); }
public async Task SendMessage() { ReceiveMessageRequest messageRequest = new ReceiveMessageRequest(); messageRequest.QueueUrl = "https://sqs.us-west-2.amazonaws.com/456186057084/MortgageQueue"; var messageResponse = await sqsClient.ReceiveMessageAsync(messageRequest); sqsClient.Dispose(); EmailService.SendEmail(messageResponse.Messages[0].Body); }
protected virtual void Dispose(bool disposing) { if (!_disposed) { if (disposing) { _sqsClient.Dispose(); _sqsClient = null; _snsClient.Dispose(); _snsClient = null; } _disposed = true; } }
/// <summary> /// implement the disposable pattern. /// </summary> /// <param name="disposing"></param> protected virtual void Dispose(bool disposing) { if (!_Disposed) { if (disposing) { UnSubscribe(); if (null != _AmazonSQSClient) { _AmazonSQSClient.Dispose(); _AmazonSQSClient = null; } } _Disposed = true; } }
public async Task <HttpStatusCode> SendMessage(string messageBody, string queue, int timeOutSec = 300) { try { var config = new AmazonSQSConfig(); config.RegionEndpoint = Amazon.RegionEndpoint.SAEast1; config.Timeout = new TimeSpan(0, 0, timeOutSec); config.MaxErrorRetry = 4; var _client = new AmazonSQSClient("zzzz", "zzzzz", config); SendMessageRequest sendMessageRequest = new SendMessageRequest(); sendMessageRequest.QueueUrl = queue; sendMessageRequest.MessageBody = messageBody; foreach (var attibute in attibutes) { sendMessageRequest.MessageAttributes.Add( attibute.Key, new MessageAttributeValue { DataType = "String", StringValue = attibute.Value } ); } var retornoSQS = _client.SendMessageAsync(sendMessageRequest).Result; _client.Dispose(); return(await Task.FromResult(retornoSQS.HttpStatusCode)); } catch (AmazonSQSException ex) { throw ex; } }
/// <summary> /// Receives the specified queue name. /// </summary> /// <param name="timeoutInMilliseconds">The timeout public Message[] Receive(int timeoutInMilliseconds) { AmazonSQSClient client = null; Amazon.SQS.Model.Message[] sqsMessages = new Amazon.SQS.Model.Message[0]; try { client = new AmazonSQSClient(_connection.Credentials, _connection.Region); var urlResponse = client.GetQueueUrlAsync(_queueName).Result; _logger.Value.DebugFormat("SqsMessageConsumer: Preparing to retrieve next message from queue {0}", urlResponse.QueueUrl); var request = new ReceiveMessageRequest(urlResponse.QueueUrl) { MaxNumberOfMessages = _batchSize, WaitTimeSeconds = (int)TimeSpan.FromMilliseconds(timeoutInMilliseconds).TotalSeconds, MessageAttributeNames = new List<string>() { "All" }, AttributeNames = new List<string>() { "All" } }; var receiveResponse = client.ReceiveMessageAsync(request).Result; sqsMessages = receiveResponse.Messages.ToArray(); } catch (InvalidOperationException ioe) { _logger.Value.DebugFormat("SqsMessageConsumer: Could not determine number of messages to retrieve"); throw new ChannelFailureException("Error connecting to SQS, see inner exception for details", ioe); } catch (OperationCanceledException oce) { _logger.Value.DebugFormat("SqsMessageConsumer: Could not find messages to retrieve"); throw new ChannelFailureException("Error connecting to SQS, see inner exception for details", oce); } catch (Exception e) { _logger.Value.ErrorException("SqsMessageConsumer: There was an error listening to queue {0} ", e, _queueName ); throw; } finally { client?.Dispose(); } if (sqsMessages.Length == 0) { return new Message[] {_noopMessage}; } var messages = new Message[sqsMessages.Length]; for(int i = 0; i < sqsMessages.Length; i++) { var message = new SqsMessageCreator().CreateMessage(sqsMessages[i]); _logger.Value.InfoFormat("SqsMessageConsumer: Received message from queue {0}, message: {1}{2}", _queueName, Environment.NewLine, JsonConvert.SerializeObject(message)); messages[i] = message; } return messages; }
public void Dispose() { client.Dispose(); sqs.Dispose(); s3.Dispose(); }
/// <summary> /// Receives the specified queue name. /// </summary> /// <param name="timeoutInMilliseconds">The timeout in milliseconds. Anytyhing greater than 0 uses long-polling </param> public Message[] Receive(int timeoutInMilliseconds) { AmazonSQSClient client = null; Amazon.SQS.Model.Message[] sqsMessages; try { client = new AmazonSQSClient(_awsConnection.Credentials, _awsConnection.Region); var urlResponse = client.GetQueueUrlAsync(_queueName).GetAwaiter().GetResult(); s_logger.LogDebug("SqsMessageConsumer: Preparing to retrieve next message from queue {URL}", urlResponse.QueueUrl); var request = new ReceiveMessageRequest(urlResponse.QueueUrl) { MaxNumberOfMessages = _batchSize, WaitTimeSeconds = (int)TimeSpan.FromMilliseconds(timeoutInMilliseconds).TotalSeconds, MessageAttributeNames = new List <string>() { "All" }, AttributeNames = new List <string>() { "All" } }; var receiveResponse = client.ReceiveMessageAsync(request).GetAwaiter().GetResult(); sqsMessages = receiveResponse.Messages.ToArray(); } catch (InvalidOperationException ioe) { s_logger.LogDebug("SqsMessageConsumer: Could not determine number of messages to retrieve"); throw new ChannelFailureException("Error connecting to SQS, see inner exception for details", ioe); } catch (OperationCanceledException oce) { s_logger.LogDebug("SqsMessageConsumer: Could not find messages to retrieve"); throw new ChannelFailureException("Error connecting to SQS, see inner exception for details", oce); } catch (Exception e) { s_logger.LogError(e, "SqsMessageConsumer: There was an error listening to queue {ChannelName} ", _queueName); throw; } finally { client?.Dispose(); } if (sqsMessages.Length == 0) { return(new[] { _noopMessage }); } var messages = new Message[sqsMessages.Length]; for (int i = 0; i < sqsMessages.Length; i++) { var message = SqsMessageCreatorFactory.Create(_rawMessageDelivery).CreateMessage(sqsMessages[i]); s_logger.LogInformation("SqsMessageConsumer: Received message from queue {ChannelName}, message: {1}{Request}", _queueName, Environment.NewLine, JsonSerializer.Serialize(message, JsonSerialisationOptions.Options)); messages[i] = message; } return(messages); }
public void Dispose() { _sqsClient?.Dispose(); }
#pragma warning disable 4014 private void SubscriberThread() { if (sqlController.SettingRead(Settings.token).Result != "UNIT_TEST___________________L:32") #region amazon { log.LogStandard(t.GetMethodName("Subscriber"), $"{DateTime.Now.ToString()} - Starting up"); #region setup isActive = true; keepSubscribed = true; string awsAccessKeyId = sqlController.SettingRead(Settings.awsAccessKeyId).Result; string awsSecretAccessKey = sqlController.SettingRead(Settings.awsSecretAccessKey).Result; string awsQueueUrl = sqlController.SettingRead(Settings.awsEndPoint).Result + sqlController.SettingRead(Settings.token).Result; var sqsClient = new AmazonSQSClient(awsAccessKeyId, awsSecretAccessKey, RegionEndpoint.EUCentral1); DateTime lastException = DateTime.MinValue; #endregion while (keepSubscribed) { try { var res = sqsClient.ReceiveMessageAsync(awsQueueUrl).Result; if (res.Messages.Count > 0) { foreach (var message in res.Messages) { #region JSON -> var var parsedData = JRaw.Parse(message.Body); string notificationUId = parsedData["id"].ToString(); int microtingUId = int.Parse(parsedData["microting_uuid"].ToString()); string action = parsedData["text"].ToString(); #endregion log.LogStandard(t.GetMethodName("Subscriber"), "Notification notificationUId : " + notificationUId + " microtingUId : " + microtingUId + " action : " + action); notifications result; switch (action) { case Constants.Notifications.Completed: result = sqlController.NotificationCreate(notificationUId, microtingUId, Constants.Notifications.Completed).Result; bus.SendLocal(new EformCompleted(notificationUId, microtingUId)); break; case Constants.Notifications.EformParsedByServer: bus.SendLocal(new EformParsedByServer(notificationUId, microtingUId)); break; case Constants.Notifications.EformParsingError: bus.SendLocal(new EformParsingError(notificationUId, microtingUId)); break; case Constants.Notifications.RetrievedForm: result = sqlController.NotificationCreate(notificationUId, microtingUId, Constants.Notifications.RetrievedForm).Result; bus.SendLocal(new EformRetrieved(notificationUId, microtingUId)); break; case Constants.Notifications.UnitActivate: result = sqlController.NotificationCreate(notificationUId, microtingUId, Constants.Notifications.UnitActivate).Result; bus.SendLocal(new UnitActivated(notificationUId, microtingUId)); break; case Constants.Notifications.SpeechToTextCompleted: result = sqlController.NotificationCreate(notificationUId, microtingUId, Constants.Notifications.SpeechToTextCompleted).Result; bus.SendLocal(new TranscriptionCompleted(notificationUId, microtingUId)); break; case Constants.Notifications.InSightAnswerDone: result = sqlController.NotificationCreate(notificationUId, microtingUId, Constants.Notifications.InSightAnswerDone).Result; bus.SendLocal(new AnswerCompleted(notificationUId, microtingUId)); break; } sqsClient.DeleteMessageAsync(awsQueueUrl, message.ReceiptHandle); } } else { log.LogStandard(t.GetMethodName("Subscriber"), $"{DateTime.Now.ToString()} - No messages for us right now!"); } } catch (WebException webException) { log.LogWarning(t.GetMethodName("Subscriber"), t.PrintException(t.GetMethodName("Subscriber") + " failed", webException)); // We try to sleep 20 seconds to see if the problem goes away by it self. Thread.Sleep(20000); } catch (Exception ex) { // Log exception log.LogWarning(t.GetMethodName("Subscriber"), t.PrintException(t.GetMethodName("Subscriber") + " failed", ex)); if (DateTime.Compare(lastException.AddMinutes(5), DateTime.Now) > 0) { keepSubscribed = false; log.LogException(t.GetMethodName("Subscriber"), "failed, twice in the last 5 minuts", ex); // TODO handle crash so we could restart!!! } lastException = DateTime.Now; } } log.LogStandard(t.GetMethodName("Subscriber"), "--- WE WHERE TOLD NOT TO CONTINUE TO SUBSCRIBE ---"); sqsClient.Dispose(); //EventMsgClient("Subscriber closed", null); keepSubscribed = false; isActive = false; } #endregion else #region unit test { log.LogStandard(t.GetMethodName("Subscriber"), "Subscriber faked"); isActive = true; keepSubscribed = true; while (keepSubscribed) { Thread.Sleep(100); } keepSubscribed = false; isActive = false; } #endregion }
public void Dispose() { _bodyLogger.LogInformation("Disposing...."); _bodyTimer?.Dispose(); _sqsBodyClient?.Dispose(); }
public override void Dispose() { _client?.Dispose(); }
private static async Task Main(string[] args) { GCSettings.LatencyMode = GCLatencyMode.Batch; Console.Title = EndpointName + " | " + Environment.OSVersion + " | Is64BitProcess:" + Environment.Is64BitProcess + " | LatencyMode:" + GCSettings.LatencyMode + " | IsServerGC:" + GCSettings.IsServerGC; ServicePointManager.Expect100Continue = false; ServicePointManager.UseNagleAlgorithm = false; ServicePointManager.DefaultConnectionLimit = DefaultConnectionLimit; // Querying of DefaultConnectionLimit on dotnet core does not return assigned value var client = new AmazonSQSClient(new AmazonSQSConfig { RegionEndpoint = RegionEndpoint.EUCentral1, /*ServiceURL = "http://sqs.eu-central-1.amazonaws.com"*/ #if !NET461 MaxConnectionsPerServer = 100, /*CacheHttpClient = false, * UseNagleAlgorithm = false*/ #endif }); var queueUrl = await CreateQueue(client); var cancellationTokenSource = new CancellationTokenSource(); var consumerTasks = new List <Task>(); for (var i = 0; i < concurrencyLevel; i++) { consumerTasks.Add(ConsumeMessage(client, queueUrl, i, cancellationTokenSource.Token)); } var sendingTask = Task.Run(async() => { if (sending) { var cts = new CancellationTokenSource(); var sends = new List <Task>(); var sessionId = DateTime.UtcNow.ToString("s"); var batchSize = 100; var count = 0; while (!cts.Token.IsCancellationRequested) { var s = Stopwatch.StartNew(); sends.Clear(); for (var i = 0; i < batchSize; i++) { var id = $"{sessionId}/{++count:D8}"; sends.Add(Item(client, queueUrl, id, cts)); } try { await Task.WhenAll(sends).ConfigureAwait(false); } catch (Exception e) { Console.WriteLine(e); } var elapsed = s.Elapsed; #pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed Console.Out.WriteLineAsync( $"New batch of {batchSize:N0} took {elapsed} at {count:N0} ({batchSize / elapsed.TotalSeconds:N}/s)") .ConfigureAwait(false); #pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed } } }).ConfigureAwait(false); Console.ReadLine(); cancellationTokenSource.Cancel(); client.Dispose(); }
public void Dispose() { _logger.LogInformation("Disposing...."); _timer?.Dispose(); _sqsClient?.Dispose(); }
public void Dispose() { Flush(); _sqsClient.Dispose(); }
public void Dispose() { _client?.Dispose(); }
protected override void DisposeServices() { sqsClient?.Dispose(); }