private void UpdateRules(AzureTopicMqSettings settings) { _receiver.RemoveRule("$default"); settings.AzureSubscriptionRules .ToList() .ForEach(x => _receiver.AddRule(x.Key, x.Value)); }
public AzureBusTopicPublisher(AzureTopicMqSettings settings, Action <string> logMessage, Action <string> logError) { _settings = settings; _logMessage = logMessage; _logError = logError; _factory = MessagingFactory.CreateFromConnectionString(settings.ConnectionString); _namespaceManager = NamespaceManager.CreateFromConnectionString(settings.ConnectionString); }
public void ReCreate(AzureTopicMqSettings settings, NamespaceManager namespaceManager) { var topicPath = settings.TopicNameBuilder(typeof(T)); var subscriptionName = $"{topicPath}.{settings.TopicSubscriberId}"; MakeSureTopicExists(namespaceManager, settings, topicPath); MakeSureSubscriptionExists(namespaceManager, settings, topicPath, subscriptionName); UpdateRules(settings); }
private void MakeSureTopicExists(NamespaceManager namespaceManager, AzureTopicMqSettings settings, string topicPath) { if (!namespaceManager.TopicExists(topicPath)) { var queueDescription = new TopicDescription(topicPath); namespaceManager.CreateTopic(settings.TopicBuilderConfig(queueDescription, typeof(T))); _logMessage($"MakeSureTopicExists: Created topic: {topicPath}"); } }
internal Binding( MessagingFactory messagingFactory, NamespaceManager namespaceManager, AzureTopicMqSettings settings, string topicName, Action <string> logMessage, Action <string> logError) { _messagingFactory = messagingFactory; _logMessage = logMessage; _logError = logError; _azureTopicMqSettings = settings; _excludeTopicsFromLogging = new LoggingConfiguration().ExcludeTopicsFromLogging(); if (!namespaceManager.TopicExists(topicName)) { var queueDescription = new TopicDescription(topicName); namespaceManager.CreateTopic(settings.TopicBuilderConfig(queueDescription, typeof(T))); } }
public AzureBusTopicSubscriber(AzureTopicMqSettings settings, Action <string> logMessage, Action <string> logError) { _settings = settings; _logMessage = logMessage; _logError = logError; _factory = MessagingFactory.CreateFromConnectionString(settings.ConnectionString); _namespaceManager = NamespaceManager.CreateFromConnectionString(settings.ConnectionString); _source = new CancellationTokenSource(); Task.Factory.StartNew(() => { while (!_source.IsCancellationRequested) { try { var action = _errorActions.Take(_source.Token); try { action.ReCreate(_settings, _namespaceManager); logMessage($"Recreated subscription."); } catch (MessagingEntityAlreadyExistsException exception) { logError($"Subscription already exists. {exception}"); } catch (Exception exception) { logError($"Unable to recreate subscription. {exception}"); } } catch (OperationCanceledException) { logError($"Stopping {nameof(AzureBusTopicSubscriber)}"); } catch (Exception exception) { logError($"Something went wrong while doing error actions ${exception}."); } } }, _source.Token); }
private void MakeSureSubscriptionExists(NamespaceManager namespaceManager, AzureTopicMqSettings settings, string topicPath, string subscriptionName, bool removePrevious = false) { var subscriptionDescription = new SubscriptionDescription(topicPath, subscriptionName); if (!namespaceManager.SubscriptionExists(topicPath, subscriptionName)) { namespaceManager.CreateSubscription(settings.SubscriptionBuilderConfig(subscriptionDescription, typeof(T))); _logMessage($"MakeSureSubscriptionExists: Created subscription: {subscriptionName}"); } else { if (removePrevious) { namespaceManager.DeleteSubscription(topicPath, subscriptionName); namespaceManager.CreateSubscription(settings.SubscriptionBuilderConfig(subscriptionDescription, typeof(T))); _logMessage($"MakeSureSubscriptionExists: Deleted and created subscription: {subscriptionName}"); } } }
internal Binding(MessagingFactory messagingFactory, NamespaceManager namespaceManager, AzureTopicMqSettings settings, BlockingCollection <IBinding> errorActions, Action <string> logMessage, Action <string> logError) { _errorActions = errorActions; _logError = logError; _logMessage = logMessage; _excludeTopicsFromLogging = new LoggingConfiguration().ExcludeTopicsFromLogging(); var topicPath = settings.TopicNameBuilder(typeof(T)); var subscriptionName = $"{topicPath}.{settings.TopicSubscriberId}"; MakeSureTopicExists(namespaceManager, settings, topicPath); MakeSureSubscriptionExists(namespaceManager, settings, topicPath, subscriptionName, true); _receiver = messagingFactory.CreateSubscriptionClient(topicPath, subscriptionName); UpdateRules(settings); _options = new OnMessageOptions { AutoComplete = true }; _options.ExceptionReceived += OptionsOnExceptionReceived; _receiver.OnMessage(message => { try { var bodyStream = message.GetBody <Stream>(); using (var reader = new StreamReader(bodyStream)) { var body = reader.ReadToEnd(); if (!_excludeTopicsFromLogging.Contains(topicPath)) { logMessage($"Received '{topicPath}': {body}"); } Subject.OnNext(JObject.Parse(body)["data"].ToObject <T>()); } } catch (Exception ex) { logError($"Message {topicPath}': {message} -> consumer error: {ex}"); } }, _options); }