public ServiceBusMessageBus(IStringMinifier stringMinifier, ILoggerFactory loggerFactory, IPerformanceCounterManager performanceCounterManager, IOptions <MessageBusOptions> optionsAccessor, IOptions <ServiceBusScaleoutOptions> scaleoutConfigurationAccessor) : base(stringMinifier, loggerFactory, performanceCounterManager, optionsAccessor, scaleoutConfigurationAccessor) { var configuration = scaleoutConfigurationAccessor.Value; if (configuration == null) { throw new ArgumentNullException("configuration"); } if (String.IsNullOrEmpty(configuration.TopicPrefix)) { throw new InvalidOperationException("TopixPrefix is invalid"); } _logger = loggerFactory.CreateLogger <ServiceBusMessageBus>(); _connection = new ServiceBusConnection(configuration, _logger); _topics = Enumerable.Range(0, configuration.TopicCount) .Select(topicIndex => SignalRTopicPrefix + "_" + configuration.TopicPrefix + "_" + topicIndex) .ToArray(); _connectionContext = new ServiceBusConnectionContext(configuration, _topics, _logger, OnMessage, OnError, Open); ThreadPool.QueueUserWorkItem(Subscribe); }
public ReceiverContext(int topicIndex, MessageReceiver receiver, ServiceBusConnectionContext connectionContext, TimeSpan defaultReadTimeout) { TopicIndex = topicIndex; Receiver = receiver; ReceiveTimeout = defaultReadTimeout; ConnectionContext = connectionContext; }
private void CreateTopic(ServiceBusConnectionContext connectionContext, int topicIndex) { lock (connectionContext.TopicClientsLock) { if (connectionContext.IsDisposed) { return; } string topicName = connectionContext.TopicNames[topicIndex]; if (!_namespaceManager.TopicExists(topicName)) { try { _logger.LogInformation("Creating a new topic {0} in the service bus...", topicName); _namespaceManager.CreateTopic(topicName); _logger.LogInformation("Creation of a new topic {0} in the service bus completed successfully.", topicName); } catch (MessagingEntityAlreadyExistsException) { // The entity already exists _logger.LogInformation("Creation of a new topic {0} threw an MessagingEntityAlreadyExistsException.", topicName); } } // Create a client for this topic TopicClient topicClient = TopicClient.CreateFromConnectionString(_connectionString, topicName); if (_options.RetryPolicy != null) { topicClient.RetryPolicy = _options.RetryPolicy; } else { topicClient.RetryPolicy = RetryExponential.Default; } connectionContext.SetTopicClients(topicClient, topicIndex); _logger.LogInformation("Creation of a new topic client {0} completed successfully.", topicName); } CreateSubscription(connectionContext, topicIndex); }
private void CreateSubscription(ServiceBusConnectionContext connectionContext, int topicIndex) { lock (connectionContext.SubscriptionsLock) { if (connectionContext.IsDisposed) { return; } string topicName = connectionContext.TopicNames[topicIndex]; // Create a random subscription string subscriptionName = Guid.NewGuid().ToString(); try { var subscriptionDescription = new SubscriptionDescription(topicName, subscriptionName); // This cleans up the subscription while if it's been idle for more than the timeout. subscriptionDescription.AutoDeleteOnIdle = _idleSubscriptionTimeout; _namespaceManager.CreateSubscription(subscriptionDescription); _logger.LogInformation("Creation of a new subscription {0} for topic {1} in the service bus completed successfully.", subscriptionName, topicName); } catch (MessagingEntityAlreadyExistsException) { // The entity already exists _logger.LogInformation("Creation of a new subscription {0} for topic {1} threw an MessagingEntityAlreadyExistsException.", subscriptionName, topicName); } // Create a receiver to get messages string subscriptionEntityPath = SubscriptionClient.FormatSubscriptionPath(topicName, subscriptionName); MessageReceiver receiver = _factory.CreateMessageReceiver(subscriptionEntityPath, ReceiveMode.ReceiveAndDelete); _logger.LogInformation("Creation of a message receive for subscription entity path {0} in the service bus completed successfully.", subscriptionEntityPath); connectionContext.SetSubscriptionContext(new SubscriptionContext(topicName, subscriptionName, receiver), topicIndex); var receiverContext = new ReceiverContext(topicIndex, receiver, connectionContext, DefaultReadTimeout); ProcessMessages(receiverContext); // Open the stream connectionContext.OpenStream(topicIndex); } }
public void Subscribe(ServiceBusConnectionContext connectionContext) { if (connectionContext == null) { throw new ArgumentNullException("connectionContext"); } _logger.LogInformation("Subscribing to {0} topic(s) in the service bus...", connectionContext.TopicNames.Count); connectionContext.NamespaceManager = _namespaceManager; for (var topicIndex = 0; topicIndex < connectionContext.TopicNames.Count; ++topicIndex) { Retry(() => CreateTopic(connectionContext, topicIndex)); } _logger.LogInformation("Subscription to {0} topics in the service bus Topic service completed successfully.", connectionContext.TopicNames.Count); }