private static async Task <ReadResult> DoReadOperation(string connectionString, string path, TimeSpan timeout, bool useCached, Func <MessageReceiver, Task <ReadResult> > operation) { ServiceBusConnection connection = null; MessageReceiver requestClient = null; try { if (useCached) { requestClient = ServiceBusMessagingFactory.Instance.GetMessageReceiver(connectionString, path, timeout); } else { connection = ServiceBusMessagingFactory.CreateConnectionWithTimeout(connectionString, timeout); requestClient = new MessageReceiver(connection, path); } return(await operation.Invoke(requestClient).ConfigureAwait(false)); } finally { await(requestClient?.CloseAsync() ?? Task.CompletedTask); await(connection?.CloseAsync() ?? Task.CompletedTask); } }
public async Task Should_get_cached_sender_per_destination_for_send_via() { var pool = new MessageSenderPool(new ServiceBusConnectionStringBuilder(connectionString), null, null); var connection = new ServiceBusConnection(connectionString); try { var firstMessageSenderDest1 = pool.GetMessageSender("dest1", (connection, "via")); pool.ReturnMessageSender(firstMessageSenderDest1); var firstMessageSenderDest2 = pool.GetMessageSender("dest2", (connection, "via")); pool.ReturnMessageSender(firstMessageSenderDest2); var secondMessageSenderDest1 = pool.GetMessageSender("dest1", (connection, "via")); var secondMessageSenderDest2 = pool.GetMessageSender("dest2", (connection, "via")); Assert.AreSame(firstMessageSenderDest1, secondMessageSenderDest1); Assert.AreSame(firstMessageSenderDest2, secondMessageSenderDest2); Assert.AreNotSame(firstMessageSenderDest1, firstMessageSenderDest2); Assert.AreNotSame(secondMessageSenderDest1, secondMessageSenderDest2); } finally { await pool.Close(); await connection.CloseAsync(); } }
public async Task Should_return_correctly_configured_sender() { var pool = new MessageSenderPool(new ServiceBusConnectionStringBuilder(connectionString), null, null); var connection = new ServiceBusConnection(connectionString); try { // unfortunately it is not possible to assert the token provider var nonSendViaSender = pool.GetMessageSender("dest1", (null, null)); var sendViaSender = pool.GetMessageSender("dest2", (connection, "via")); Assert.AreEqual("dest1", nonSendViaSender.Path); Assert.IsNull(nonSendViaSender.TransferDestinationPath); Assert.IsNull(nonSendViaSender.ViaEntityPath); Assert.IsTrue(nonSendViaSender.OwnsConnection); Assert.AreEqual("via", sendViaSender.ViaEntityPath); Assert.AreEqual("via", sendViaSender.Path); Assert.AreEqual("dest2", sendViaSender.TransferDestinationPath); Assert.IsFalse(sendViaSender.OwnsConnection); } finally { await pool.Close(); await connection.CloseAsync(); } }
public void Dispose() { // a bit unnecessary, but good to remember if using different patters of Start / Stop _failureEmitter?.CloseAsync(); _successEmitter?.CloseAsync(); _failureConn?.CloseAsync(); _successConn?.CloseAsync(); }
public async ValueTask DisposeAsync() { var address = _connection.Endpoint.ToString(); TransportLogMessages.DisconnectHost(address); await _managementClient.CloseAsync().ConfigureAwait(false); await _connection.CloseAsync().ConfigureAwait(false); TransportLogMessages.DisconnectedHost(address); }
public async Task TransactionCommitWorksAcrossClientsUsingSameConnectionToSameEntity() { var connection = new ServiceBusConnection(ConnectionString); var sender = new MessageSender(connection, QueueName); var receiver = new MessageReceiver(connection, QueueName); try { string body1 = Guid.NewGuid().ToString("N"); string body2 = Guid.NewGuid().ToString("N"); var message = new Message(body1.GetBytes()); var message2 = new Message(body2.GetBytes()); await sender.SendAsync(message).ConfigureAwait(false); var receivedMessage = await receiver.ReceiveAsync(ReceiveTimeout); Assert.NotNull(receivedMessage); Assert.Equal(body1, receivedMessage.Body.GetString()); using (var ts = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)) { await receiver.CompleteAsync(receivedMessage.SystemProperties.LockToken); await sender.SendAsync(message2).ConfigureAwait(false); ts.Complete(); } // Adding delay since transaction Commit/Rollback is an asynchronous operation. // Operating on the same message should not be done. await Task.Delay(TimeSpan.FromSeconds(2)); // Assert that complete did succeed await Assert.ThrowsAsync <MessageLockLostException>(async() => await receiver.CompleteAsync(receivedMessage.SystemProperties.LockToken)); // Assert that send did succeed receivedMessage = await receiver.ReceiveAsync(ReceiveTimeout); Assert.NotNull(receivedMessage); Assert.Equal(body2, receivedMessage.Body.GetString()); await receiver.CompleteAsync(receivedMessage.SystemProperties.LockToken); } finally { await sender.CloseAsync(); await receiver.CloseAsync(); await connection.CloseAsync(); } }
/// <inheritdoc /> public async Task StopAsync(CancellationToken cancellationToken) { _logger.LogTrace($"Stopping [{nameof(ShiftRegistrationQueueMonitor)}]..."); if (_configuration == null || !_configuration.MonitoringEnabled || _monitoringConfiguration == null) { _logger.LogTrace($"Monitoring not configured or disabled."); return; } await _queueClient.CloseAsync(); await _conn.CloseAsync(); _logger.LogTrace($"[{nameof(ShiftRegistrationQueueMonitor)}] stopped."); }
public async Task TransactionRollbackWorksAcrossClientsUsingSameConnectionToSameEntity() { var connection = new ServiceBusConnection(ConnectionString); var sender = new MessageSender(connection, QueueName); var receiver = new MessageReceiver(connection, QueueName); try { string body1 = Guid.NewGuid().ToString("N"); string body2 = Guid.NewGuid().ToString("N"); var message = new Message(body1.GetBytes()); var message2 = new Message(body2.GetBytes()); await sender.SendAsync(message).ConfigureAwait(false); var receivedMessage = await receiver.ReceiveAsync(ReceiveTimeout); Assert.NotNull(receivedMessage); Assert.Equal(body1, receivedMessage.Body.GetString()); using (var ts = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)) { await receiver.CompleteAsync(receivedMessage.SystemProperties.LockToken); await sender.SendAsync(message2).ConfigureAwait(false); ts.Dispose(); } // Following should succeed without exceptions await receiver.CompleteAsync(receivedMessage.SystemProperties.LockToken); // Assert that send failed receivedMessage = await receiver.ReceiveAsync(ReceiveTimeout); Assert.Null(receivedMessage); } finally { await sender.CloseAsync(); await receiver.CloseAsync(); await connection.CloseAsync(); } }
public async Task <T> RecibeMensajeSesion(string cadenaConexion, string nombreCola, string sesionId, int segundos, int intentos) { int contador = 0; T respuesta = default(T); var serviceBusConnectionStringBuilder = new ServiceBusConnectionStringBuilder(cadenaConexion); var serviceBusConnection = new ServiceBusConnection(serviceBusConnectionStringBuilder); var sessionClient = new SessionClient(serviceBusConnection, nombreCola, ReceiveMode.PeekLock, null, 0); var messageSession = await sessionClient.AcceptMessageSessionAsync(sesionId, TimeSpan.FromMinutes(1)); if (messageSession != null) { while (contador < intentos) { Message message = await messageSession.ReceiveAsync(TimeSpan.FromSeconds(5)); if (message != null) { var bodyText = System.Text.Encoding.UTF8.GetString(message.Body); respuesta = JsonConvert.DeserializeObject <T>(bodyText); await messageSession.CompleteAsync(message.SystemProperties.LockToken); contador = intentos; } else { await Task.Delay(segundos * 1000); } contador++; } } await messageSession.CloseAsync(); await sessionClient.CloseAsync(); await serviceBusConnection.CloseAsync(); return(respuesta); }
private async Task PerformSendAndReceiveOperation(string topicName, string subscriptionName, ServiceBusConnectionStringBuilder csBuilder) { var connection = new ServiceBusConnection(csBuilder); var sender = new MessageSender(connection, topicName); var receiver = new MessageReceiver(connection, EntityNameHelper.FormatSubscriptionPath(topicName, subscriptionName)); try { await sender.SendAsync(new Message() { MessageId = "1" }); Console.WriteLine("Sent message successfully"); } catch (UnauthorizedException) { Console.WriteLine($"Could not send message due to authorization failure"); } try { var msg = await receiver.ReceiveAsync(); Console.WriteLine("Received message successfully"); } catch (UnauthorizedException) { Console.WriteLine($"Could not receive message due to authorization failure"); } await sender.CloseAsync(); await receiver.CloseAsync(); await connection.CloseAsync(); }
public void GlobalCleanup() { connection.CloseAsync().GetAwaiter().GetResult(); }
private async Task RunReceiveLoopAsync() { var client = new ServiceBusConnection(this.NamespaceName, TransportType.Amqp); client.TokenProvider = new TokenCredentialTokenProvider(new DefaultAzureCredential()); long messageCount = 0; var sw = Stopwatch.StartNew(); var bagStart = sw.ElapsedMilliseconds; Console.WriteLine($"Receiving from entity '{EntityName}' in namespace '{NamespaceName}'"); var timer = new Timer(state => { var snapshot = Interlocked.Exchange(ref messageCount, 0); var bagDuration = sw.ElapsedMilliseconds - bagStart; bagStart = sw.ElapsedMilliseconds; Console.ResetColor(); Console.WriteLine($"\nReceived {snapshot / (bagDuration / 1000.0)} msg/sec, {snapshot} in {bagDuration} ms"); }, null, 10000, 10000); var receiver = new MessageReceiver(client, EntityName, this.ReceiveDelete ? ReceiveMode.ReceiveAndDelete : ReceiveMode.PeekLock, RetryPolicy.Default, PrefetchCount); Console.CancelKeyPress += async(a, o) => { await receiver.CloseAsync(); }; Message message = null; do { message = await receiver.ReceiveAsync(); if (message != null) { try { if (!string.IsNullOrEmpty(message.SessionId) || !string.IsNullOrEmpty(message.Label)) { int color = Math.Abs(string.IsNullOrEmpty(message.SessionId) ? message.Label.GetHashCode() : message.SessionId.GetHashCode()); Console.BackgroundColor = ConsoleColor.Black; Console.ForegroundColor = (ConsoleColor)((color % 14) + 1); } //Console.Write("[]"); Interlocked.Increment(ref messageCount); if (!this.ReceiveDelete) { receiver.CompleteAsync(message.SystemProperties.LockToken); } } catch { if (!this.ReceiveDelete) { await receiver.AbandonAsync(message.SystemProperties.LockToken); } } } }while (message != null); timer.Dispose(); await receiver.CloseAsync(); await client.CloseAsync(); }
private async Task Listen(CancellationToken cancellationToken) { var connection = new ServiceBusConnection(connectionString); var messageReceivers = new List <BatchMessageReceiver>(); messageReceivers.AddRange(Enumerable.Range(0, 3) .Select(i => new BatchMessageReceiver(connection, EndpointName))); var errorQueueSender = new MessageSender(connection, errorQueueName, RetryPolicy.Default); try { while (!cancellationToken.IsCancellationRequested) { try { var pipeLineStopwatch = Stopwatch.StartNew(); var receiveTimer = Stopwatch.StartNew(); var receiveTasks = messageReceivers.Select(receiver => ReceiveMessages(receiver, cancellationToken)).ToList(); await Task.WhenAll(receiveTasks).ConfigureAwait(false); var messages = receiveTasks.SelectMany(task => task.Result).ToList(); receiveTimer.Stop(); if (!messages.Any()) { await Task.Delay(2000, cancellationToken); continue; } RecordMetric("ReceiveMessages", receiveTimer.ElapsedMilliseconds, messages.Count); var groupedMessages = new Dictionary <Type, List <(object Message, BatchMessageReceiver MessageReceiver, Message ReceivedMessage)> >(); foreach (var message in messages) { cancellationToken.ThrowIfCancellationRequested(); var key = message.Message.GetType(); var applicationMessages = groupedMessages.ContainsKey(key) ? groupedMessages[key] : groupedMessages[key] = new List <(object Message, BatchMessageReceiver MessageReceiver, Message ReceivedMessage)>(); applicationMessages.Add(message); } var stopwatch = Stopwatch.StartNew(); await Task.WhenAll(groupedMessages.Select(group => ProcessMessages(group.Key, group.Value, cancellationToken))); stopwatch.Stop(); RecordProcessedAllBatchesTelemetry(stopwatch.ElapsedMilliseconds, messages.Count); pipeLineStopwatch.Stop(); RecordPipelineTelemetry(pipeLineStopwatch.ElapsedMilliseconds, messages.Count); } catch (TaskCanceledException) { logger.LogWarning("Cancelling communication listener."); return; } catch (OperationCanceledException) { logger.LogWarning("Cancelling communication listener."); return; } catch (Exception ex) { logger.LogError($"Error listening for message. Error: {ex.Message}", ex); } } } finally { await Task.WhenAll(messageReceivers.Select(receiver => receiver.Close())).ConfigureAwait(false); if (!connection.IsClosedOrClosing) { await connection.CloseAsync(); } } }
private async Task Listen(CancellationToken cancellationToken) { var connection = new ServiceBusConnection(connectionString); var messageReceiver = new MessageReceiver(connection, EndpointName, ReceiveMode.PeekLock, RetryPolicy.Default, 0); var errorQueueSender = new MessageSender(connection, errorQueueName, RetryPolicy.Default); try { while (!cancellationToken.IsCancellationRequested) { try { var pipeLineStopwatch = Stopwatch.StartNew(); var messages = new List <Message>(); for (var i = 0; i < 10 && messages.Count <= 200; i++) { cancellationToken.ThrowIfCancellationRequested(); var receivedMessages = await messageReceiver.ReceiveAsync(200, TimeSpan.FromSeconds(2)) .ConfigureAwait(false); if (receivedMessages == null || !receivedMessages.Any()) { break; } messages.AddRange(receivedMessages); } if (!messages.Any()) { await Task.Delay(2000, cancellationToken); continue; } var groupedMessages = new Dictionary <Type, List <(string, object)> >(); foreach (var message in messages) { cancellationToken.ThrowIfCancellationRequested(); try { var applicationMessage = DeserializeMessage(message); var key = applicationMessage.GetType(); var applicationMessages = groupedMessages.ContainsKey(key) ? groupedMessages[key] : groupedMessages[key] = new List <(string, object)>(); applicationMessages.Add((message.SystemProperties.LockToken, applicationMessage)); } catch (Exception e) { logger.LogError($"Error deserialising the message. Error: {e.Message}", e); //TODO: should use the error queue instead of dead letter queue await messageReceiver.DeadLetterAsync(message.SystemProperties.LockToken) .ConfigureAwait(false); } } var stopwatch = Stopwatch.StartNew(); await Task.WhenAll(groupedMessages.Select(group => ProcessMessages(group.Key, group.Value, messageReceiver, cancellationToken))); stopwatch.Stop(); //RecordAllBatchProcessTelemetry(stopwatch.ElapsedMilliseconds, messages.Count); pipeLineStopwatch.Stop(); //RecordPipelineTelemetry(pipeLineStopwatch.ElapsedMilliseconds, messages.Count); } catch (TaskCanceledException) { logger.LogWarning("Cancelling communication listener."); return; } catch (OperationCanceledException) { logger.LogWarning("Cancelling communication listener."); return; } catch (Exception ex) { logger.LogError($"Error listening for message. Error: {ex.Message}", ex); } } } finally { if (!messageReceiver.IsClosedOrClosing) { await messageReceiver.CloseAsync(); } if (!connection.IsClosedOrClosing) { await connection.CloseAsync(); } } }
public void Dispose() { _connection.CloseAsync().Wait(); }