SubscriptionClient(ServiceBusNamespaceConnection serviceBusConnection, string topicPath, string subscriptionName, ReceiveMode receiveMode, RetryPolicy retryPolicy) : base(ClientEntity.GenerateClientId(nameof(SubscriptionClient), $"{topicPath}/{subscriptionName}"), retryPolicy) { MessagingEventSource.Log.SubscriptionClientCreateStart(serviceBusConnection?.Endpoint.Authority, topicPath, subscriptionName, receiveMode.ToString()); this.ServiceBusConnection = serviceBusConnection ?? throw new ArgumentNullException(nameof(serviceBusConnection)); this.OperationTimeout = this.ServiceBusConnection.OperationTimeout; this.syncLock = new object(); this.TopicPath = topicPath; this.SubscriptionName = subscriptionName; this.Path = EntityNameHelper.FormatSubscriptionPath(this.TopicPath, this.SubscriptionName); this.ReceiveMode = receiveMode; this.TokenProvider = TokenProvider.CreateSharedAccessSignatureTokenProvider( serviceBusConnection.SasKeyName, serviceBusConnection.SasKey); this.CbsTokenProvider = new TokenProviderAdapter(this.TokenProvider, serviceBusConnection.OperationTimeout); MessagingEventSource.Log.SubscriptionClientCreateStop(serviceBusConnection.Endpoint.Authority, topicPath, subscriptionName, this.ClientId); }
internal SubscriptionClient CreateSubscriptionClient(string topicPath, string subscriptionName, ReceiveMode mode) { MessagingEventSource.Log.SubscriptionClientCreateStart(this.Endpoint.Host, topicPath, subscriptionName, mode.ToString()); AmqpSubscriptionClient subscriptionClient = new AmqpSubscriptionClient(this, topicPath, subscriptionName, mode); MessagingEventSource.Log.SubscriptionClientCreateStop(this.Endpoint.Host, topicPath, subscriptionName, subscriptionClient.ClientId); return(subscriptionClient); }
internal QueueClient CreateQueueClient(string entityPath, ReceiveMode mode) { MessagingEventSource.Log.QueueClientCreateStart(this.Endpoint.Host, entityPath, mode.ToString()); AmqpQueueClient queueClient = new AmqpQueueClient(this, entityPath, mode); MessagingEventSource.Log.QueueClientCreateStop(this.Endpoint.Host, entityPath, queueClient.ClientId); return(queueClient); }
/// <summary> /// Creates a new instance of the Queue client on a given <see cref="ServiceBusConnection"/> /// </summary> /// <param name="serviceBusConnection">Connection object to the service bus namespace.</param> /// <param name="entityPath">Queue path.</param> /// <param name="receiveMode">Mode of receive of messages. Default to <see cref="ReceiveMode"/>.PeekLock.</param> /// <param name="retryPolicy">Retry policy for queue operations. Defaults to <see cref="RetryPolicy.Default"/></param> public QueueClient(ServiceBusConnection serviceBusConnection, string entityPath, ReceiveMode receiveMode, RetryPolicy retryPolicy) : base(nameof(QueueClient), entityPath, retryPolicy) { MessagingEventSource.Log.QueueClientCreateStart(serviceBusConnection?.Endpoint.Authority, entityPath, receiveMode.ToString()); if (string.IsNullOrWhiteSpace(entityPath)) { throw Fx.Exception.ArgumentNullOrWhiteSpace(entityPath); } this.ServiceBusConnection = serviceBusConnection ?? throw new ArgumentNullException(nameof(serviceBusConnection)); this.syncLock = new object(); this.QueueName = entityPath; this.ReceiveMode = receiveMode; this.ownsConnection = false; if (this.ServiceBusConnection.TokenProvider != null) { this.CbsTokenProvider = new TokenProviderAdapter(this.ServiceBusConnection.TokenProvider, this.ServiceBusConnection.OperationTimeout); } else { throw new ArgumentNullException($"{nameof(ServiceBusConnection)} doesn't have a valid token provider"); } MessagingEventSource.Log.QueueClientCreateStop(serviceBusConnection.Endpoint.Authority, entityPath, this.ClientId); }
internal MessageReceiver CreateMessageReceiver(string entityPath, ReceiveMode mode) { MessagingEventSource.Log.MessageReceiverCreateStart(this.Endpoint.Host, entityPath, mode.ToString()); TokenProvider tokenProvider = TokenProvider.CreateSharedAccessSignatureTokenProvider(this.SasKeyName, this.SasKey); var cbsTokenProvider = new TokenProviderAdapter(tokenProvider, this.OperationTimeout); AmqpMessageReceiver messageReceiver = new AmqpMessageReceiver(entityPath, null, mode, this.PrefetchCount, this, cbsTokenProvider, RetryPolicy.Default); MessagingEventSource.Log.MessageReceiverCreateStop(this.Endpoint.Host, entityPath); return(messageReceiver); }
public void AmqpSessionClientAcceptMessageSessionStart(string clientId, string entityPath, ReceiveMode receiveMode, int prefetchCount, string sessionId) { if (this.IsEnabled()) { this.AmqpSessionClientAcceptMessageSessionStart(clientId, entityPath, receiveMode.ToString(), prefetchCount, sessionId ?? string.Empty); } }
private async Task OnSessionTestAsync(bool partitioned, bool sessionEnabled, int maxConcurrentCalls, ReceiveMode mode, bool autoComplete) { await ServiceBusScope.UsingQueueAsync(partitioned, sessionEnabled, async queueName => { TestUtility.Log($"Queue: {queueName}, MaxConcurrentCalls: {maxConcurrentCalls}, Receive Mode: {mode.ToString()}, AutoComplete: {autoComplete}"); var queueClient = new QueueClient(TestUtility.NamespaceConnectionString, queueName, mode); try { var handlerOptions = new SessionHandlerOptions(ExceptionReceivedHandler) { MaxConcurrentSessions = maxConcurrentCalls, MessageWaitTimeout = TimeSpan.FromSeconds(5), AutoComplete = autoComplete }; var testSessionHandler = new TestSessionHandler( queueClient.ReceiveMode, handlerOptions, queueClient.InnerSender, queueClient.SessionPumpHost); // Send messages to Session first await testSessionHandler.SendSessionMessages(); // Register handler testSessionHandler.RegisterSessionHandler(handlerOptions); // Verify messages were received. await testSessionHandler.VerifyRun(); testSessionHandler.ClearData(); } finally { await queueClient.CloseAsync(); } }); // test UnregisterSessionHandler can wait for message handling upto the timeout user defined. await ServiceBusScope.UsingQueueAsync(partitioned, sessionEnabled, async queueName => { TestUtility.Log($"Queue: {queueName}, MaxConcurrentCalls: {maxConcurrentCalls}, Receive Mode: {mode.ToString()}, AutoComplete: {autoComplete}"); var queueClient = new QueueClient(TestUtility.NamespaceConnectionString, queueName, mode); try { var sessionHandlerOptions = new SessionHandlerOptions(ExceptionReceivedHandler) { MaxConcurrentSessions = maxConcurrentCalls, MessageWaitTimeout = TimeSpan.FromSeconds(5), AutoComplete = autoComplete }; var testSessionHandler = new TestSessionHandler( queueClient.ReceiveMode, sessionHandlerOptions, queueClient.InnerSender, queueClient.SessionPumpHost); // Send messages to Session first await testSessionHandler.SendSessionMessages(); // Register handler var count = 0; testSessionHandler.RegisterSessionHandler( async(session, message, token) => { await Task.Delay(TimeSpan.FromSeconds(8)); TestUtility.Log($"Received Session: {session.SessionId} message: SequenceNumber: {message.SystemProperties.SequenceNumber}"); if (queueClient.ReceiveMode == ReceiveMode.PeekLock && !sessionHandlerOptions.AutoComplete) { await session.CompleteAsync(message.SystemProperties.LockToken); } Interlocked.Increment(ref count); }, sessionHandlerOptions); await Task.Delay(TimeSpan.FromSeconds(2)); // UnregisterSessionHandler should wait up to the provided timeout to finish the message handling tasks await testSessionHandler.UnregisterSessionHandler(TimeSpan.FromSeconds(10)); Assert.True(count == maxConcurrentCalls); testSessionHandler.ClearData(); } finally { await queueClient.CloseAsync(); } }); // test UnregisterSessionHandler can close down in time when message handling takes longer than wait timeout user defined. await ServiceBusScope.UsingQueueAsync(partitioned, sessionEnabled, async queueName => { TestUtility.Log($"Queue: {queueName}, MaxConcurrentCalls: {maxConcurrentCalls}, Receive Mode: {mode.ToString()}, AutoComplete: {autoComplete}"); var queueClient = new QueueClient(TestUtility.NamespaceConnectionString, queueName, mode); try { var sessionHandlerOptions = new SessionHandlerOptions(ExceptionReceivedHandler) { MaxConcurrentSessions = maxConcurrentCalls, MessageWaitTimeout = TimeSpan.FromSeconds(5), AutoComplete = autoComplete }; var testSessionHandler = new TestSessionHandler( queueClient.ReceiveMode, sessionHandlerOptions, queueClient.InnerSender, queueClient.SessionPumpHost); // Send messages to Session first await testSessionHandler.SendSessionMessages(); // Register handler var count = 0; testSessionHandler.RegisterSessionHandler( async(session, message, token) => { await Task.Delay(TimeSpan.FromSeconds(8)); TestUtility.Log($"Received Session: {session.SessionId} message: SequenceNumber: {message.SystemProperties.SequenceNumber}"); if (queueClient.ReceiveMode == ReceiveMode.PeekLock && !sessionHandlerOptions.AutoComplete) { await session.CompleteAsync(message.SystemProperties.LockToken); } Interlocked.Increment(ref count); }, sessionHandlerOptions); await Task.Delay(TimeSpan.FromSeconds(2)); // UnregisterSessionHandler should wait up to the provided timeout to finish the message handling tasks await testSessionHandler.UnregisterSessionHandler(TimeSpan.FromSeconds(2)); Assert.True(count == 0); testSessionHandler.ClearData(); } finally { await queueClient.CloseAsync(); } }); }
async Task OnSessionTestAsync(string topicName, int maxConcurrentCalls, ReceiveMode mode, bool autoComplete) { TestUtility.Log($"Topic: {topicName}, MaxConcurrentCalls: {maxConcurrentCalls}, Receive Mode: {mode.ToString()}, AutoComplete: {autoComplete}"); var topicClient = new TopicClient(TestUtility.NamespaceConnectionString, topicName); var subscriptionClient = new SubscriptionClient( TestUtility.NamespaceConnectionString, topicClient.TopicName, this.SubscriptionName, ReceiveMode.PeekLock); try { var sessionHandlerOptions = new SessionHandlerOptions(ExceptionReceivedHandler) { MaxConcurrentSessions = 5, MessageWaitTimeout = TimeSpan.FromSeconds(5), AutoComplete = true }; var testSessionHandler = new TestSessionHandler( subscriptionClient.ReceiveMode, sessionHandlerOptions, topicClient.InnerSender, subscriptionClient.SessionPumpHost); // Send messages to Session await testSessionHandler.SendSessionMessages(); // Register handler testSessionHandler.RegisterSessionHandler(sessionHandlerOptions); // Verify messages were received. await testSessionHandler.VerifyRun(); } finally { await subscriptionClient.CloseAsync(); await topicClient.CloseAsync(); } }
async Task OnSessionTestAsync(string queueName, int maxConcurrentCalls, ReceiveMode mode, bool autoComplete) { TestUtility.Log($"Queue: {queueName}, MaxConcurrentCalls: {maxConcurrentCalls}, Receive Mode: {mode.ToString()}, AutoComplete: {autoComplete}"); var queueClient = new QueueClient(TestUtility.NamespaceConnectionString, queueName, mode); try { SessionHandlerOptions handlerOptions = new SessionHandlerOptions(ExceptionReceivedHandler) { MaxConcurrentSessions = maxConcurrentCalls, MessageWaitTimeout = TimeSpan.FromSeconds(5), AutoComplete = autoComplete }; TestSessionHandler testSessionHandler = new TestSessionHandler( queueClient.ReceiveMode, handlerOptions, queueClient.InnerSender, queueClient.SessionPumpHost); // Send messages to Session first await testSessionHandler.SendSessionMessages(); // Register handler testSessionHandler.RegisterSessionHandler(handlerOptions); // Verify messages were received. await testSessionHandler.VerifyRun(); } finally { await queueClient.CloseAsync(); } }
/// <summary> /// Creates a new instance of the Subscription client on a given <see cref="ServiceBusConnection"/> /// </summary> /// <param name="serviceBusConnection">Connection object to the service bus namespace.</param> /// <param name="topicPath">Topic path.</param> /// <param name="subscriptionName">Subscription name.</param> /// <param name="receiveMode">Mode of receive of messages. Defaults to <see cref="ReceiveMode"/>.PeekLock.</param> /// <param name="retryPolicy">Retry policy for subscription operations. Defaults to <see cref="RetryPolicy.Default"/></param> public SubscriptionClient(ServiceBusConnection serviceBusConnection, string topicPath, string subscriptionName, ReceiveMode receiveMode, RetryPolicy retryPolicy) : base(nameof(SubscriptionClient), $"{topicPath}/{subscriptionName}", retryPolicy) { if (string.IsNullOrWhiteSpace(topicPath)) { throw Fx.Exception.ArgumentNullOrWhiteSpace(topicPath); } if (string.IsNullOrWhiteSpace(subscriptionName)) { throw Fx.Exception.ArgumentNullOrWhiteSpace(subscriptionName); } MessagingEventSource.Log.SubscriptionClientCreateStart(serviceBusConnection?.Endpoint.Authority, topicPath, subscriptionName, receiveMode.ToString()); this.ServiceBusConnection = serviceBusConnection ?? throw new ArgumentNullException(nameof(serviceBusConnection)); this.syncLock = new object(); this.TopicPath = topicPath; this.SubscriptionName = subscriptionName; this.Path = EntityNameHelper.FormatSubscriptionPath(this.TopicPath, this.SubscriptionName); this.ReceiveMode = receiveMode; this.diagnosticSource = new ServiceBusDiagnosticSource(this.Path, serviceBusConnection.Endpoint); this.OwnsConnection = false; this.ServiceBusConnection.ThrowIfClosed(); if (this.ServiceBusConnection.TokenProvider != null) { this.CbsTokenProvider = new TokenProviderAdapter(this.ServiceBusConnection.TokenProvider, this.ServiceBusConnection.OperationTimeout); } else { throw new ArgumentNullException($"{nameof(ServiceBusConnection)} doesn't have a valid token provider"); } MessagingEventSource.Log.SubscriptionClientCreateStop(serviceBusConnection.Endpoint.Authority, topicPath, subscriptionName, this.ClientId); }
QueueClient(ServiceBusNamespaceConnection serviceBusConnection, string entityPath, ReceiveMode receiveMode, RetryPolicy retryPolicy) : base(nameof(QueueClient), entityPath, retryPolicy) { MessagingEventSource.Log.QueueClientCreateStart(serviceBusConnection?.Endpoint.Authority, entityPath, receiveMode.ToString()); this.ServiceBusConnection = serviceBusConnection ?? throw new ArgumentNullException(nameof(serviceBusConnection)); this.syncLock = new object(); this.QueueName = entityPath; this.ReceiveMode = receiveMode; MessagingEventSource.Log.QueueClientCreateStop(serviceBusConnection.Endpoint.Authority, entityPath, this.ClientId); }
private async Task OnSessionTestAsync(bool partitioned, bool sessionEnabled, int maxConcurrentCalls, ReceiveMode mode, bool autoComplete) { await ServiceBusScope.UsingQueueAsync(partitioned, sessionEnabled, async queueName => { TestUtility.Log($"Queue: {queueName}, MaxConcurrentCalls: {maxConcurrentCalls}, Receive Mode: {mode.ToString()}, AutoComplete: {autoComplete}"); var queueClient = new QueueClient(TestUtility.NamespaceConnectionString, queueName, mode); try { var handlerOptions = new SessionHandlerOptions(ExceptionReceivedHandler) { MaxConcurrentSessions = maxConcurrentCalls, MessageWaitTimeout = TimeSpan.FromSeconds(5), AutoComplete = autoComplete }; var testSessionHandler = new TestSessionHandler( queueClient.ReceiveMode, handlerOptions, queueClient.InnerSender, queueClient.SessionPumpHost); // Send messages to Session first await testSessionHandler.SendSessionMessages(); // Register handler testSessionHandler.RegisterSessionHandler(handlerOptions); // Verify messages were received. await testSessionHandler.VerifyRun(); testSessionHandler.ClearData(); } finally { await queueClient.CloseAsync(); } }); // test UnregisterSessionHandler can wait for message handling upto the timeout user defined. await ServiceBusScope.UsingQueueAsync(partitioned, sessionEnabled, async queueName => { TestUtility.Log($"Queue: {queueName}, MaxConcurrentCalls: {maxConcurrentCalls}, Receive Mode: {mode.ToString()}, AutoComplete: {autoComplete}"); var queueClient = new QueueClient(TestUtility.NamespaceConnectionString, queueName, mode); try { var sessionHandlerOptions = new SessionHandlerOptions(ExceptionReceivedHandler) { MaxConcurrentSessions = maxConcurrentCalls, MessageWaitTimeout = TimeSpan.FromSeconds(5), AutoComplete = autoComplete }; var testSessionHandler = new TestSessionHandler( queueClient.ReceiveMode, sessionHandlerOptions, queueClient.InnerSender, queueClient.SessionPumpHost); // Send messages to Session first await testSessionHandler.SendSessionMessages(); // Register handler testSessionHandler.RegisterSessionHandlerAndRecordReceivedMessageCount(queueClient.ReceiveMode == ReceiveMode.PeekLock, 8); // Session handler set up has greater latency than message handler. Delay here to enable some processing time of the session pump. await Task.Delay(TimeSpan.FromSeconds(2)); // UnregisterSessionHandler should wait up to the provided timeout to finish the message handling tasks await testSessionHandler.UnregisterSessionHandler(TimeSpan.FromSeconds(10)); Assert.True(testSessionHandler.ReceivedMessageCount == maxConcurrentCalls); // Reregister won't have any problems testSessionHandler.RegisterSessionHandlerAndRecordReceivedMessageCount(queueClient.ReceiveMode == ReceiveMode.PeekLock, 0); await testSessionHandler.WaitForAllMessagesReceived(ExpectedMessageCount); Assert.True(testSessionHandler.ReceivedMessageCount == ExpectedMessageCount); testSessionHandler.ClearData(); } finally { await queueClient.CloseAsync(); } }); // test UnregisterSessionHandler can close down in time when message handling takes longer than wait timeout user defined. await ServiceBusScope.UsingQueueAsync(partitioned, sessionEnabled, async queueName => { TestUtility.Log($"Queue: {queueName}, MaxConcurrentCalls: {maxConcurrentCalls}, Receive Mode: {mode.ToString()}, AutoComplete: {autoComplete}"); var queueClient = new QueueClient(TestUtility.NamespaceConnectionString, queueName, mode); try { var sessionHandlerOptions = new SessionHandlerOptions(ExceptionReceivedHandler) { MaxConcurrentSessions = maxConcurrentCalls, MessageWaitTimeout = TimeSpan.FromSeconds(5), AutoComplete = autoComplete }; var testSessionHandler = new TestSessionHandler( queueClient.ReceiveMode, sessionHandlerOptions, queueClient.InnerSender, queueClient.SessionPumpHost); // Send messages to Session first await testSessionHandler.SendSessionMessages(); // Register handler testSessionHandler.RegisterSessionHandlerAndRecordReceivedMessageCount(queueClient.ReceiveMode == ReceiveMode.PeekLock, 8); // UnregisterSessionHandler should wait up to the provided timeout to finish the message handling tasks await testSessionHandler.UnregisterSessionHandler(TimeSpan.FromSeconds(2)); Assert.True(testSessionHandler.ReceivedMessageCount == 0); // Reregister won't have any problems testSessionHandler.RegisterSessionHandlerAndRecordReceivedMessageCount(queueClient.ReceiveMode == ReceiveMode.PeekLock, 0); await testSessionHandler.WaitForAllMessagesReceived(ExpectedMessageCount); Assert.True(testSessionHandler.ReceivedMessageCount == ExpectedMessageCount); testSessionHandler.ClearData(); } finally { await queueClient.CloseAsync(); } }); }
SubscriptionClient(ServiceBusNamespaceConnection serviceBusConnection, string topicPath, string subscriptionName, ReceiveMode receiveMode, RetryPolicy retryPolicy) : base(nameof(SubscriptionClient), $"{topicPath}/{subscriptionName}", retryPolicy) { MessagingEventSource.Log.SubscriptionClientCreateStart(serviceBusConnection?.Endpoint.Authority, topicPath, subscriptionName, receiveMode.ToString()); this.ServiceBusConnection = serviceBusConnection ?? throw new ArgumentNullException(nameof(serviceBusConnection)); this.OperationTimeout = this.ServiceBusConnection.OperationTimeout; this.syncLock = new object(); this.TopicPath = topicPath; this.SubscriptionName = subscriptionName; this.Path = EntityNameHelper.FormatSubscriptionPath(this.TopicPath, this.SubscriptionName); this.ReceiveMode = receiveMode; this.diagnosticSource = new ServiceBusDiagnosticSource(this.Path, serviceBusConnection.Endpoint); MessagingEventSource.Log.SubscriptionClientCreateStop(serviceBusConnection.Endpoint.Authority, topicPath, subscriptionName, this.ClientId); }
QueueClient(ServiceBusNamespaceConnection serviceBusConnection, string entityPath, ReceiveMode receiveMode, RetryPolicy retryPolicy) : base(ClientEntity.GenerateClientId(nameof(QueueClient), entityPath), retryPolicy) { MessagingEventSource.Log.QueueClientCreateStart(serviceBusConnection?.Endpoint.Authority, entityPath, receiveMode.ToString()); this.ServiceBusConnection = serviceBusConnection ?? throw new ArgumentNullException(nameof(serviceBusConnection)); this.OperationTimeout = this.ServiceBusConnection.OperationTimeout; this.syncLock = new object(); this.QueueName = entityPath; this.ReceiveMode = receiveMode; this.TokenProvider = TokenProvider.CreateSharedAccessSignatureTokenProvider( serviceBusConnection.SasKeyName, serviceBusConnection.SasKey); this.CbsTokenProvider = new TokenProviderAdapter(this.TokenProvider, serviceBusConnection.OperationTimeout); MessagingEventSource.Log.QueueClientCreateStop(serviceBusConnection.Endpoint.Authority, entityPath, this.ClientId); }