private async Task ProcessEvent(string eventName, string message) { if (_subscriptionsManager.HasSubscriptionsForEvent(eventName)) { var subscriptionInfos = _subscriptionsManager.GetHandlersForEvent(eventName); foreach (var subInfo in subscriptionInfos) { var handler = _serviceProvider.GetService(subInfo.HandlerServiceType); if (subInfo.IsDynamic) { dynamic eventData = JsonObject.Parse(message); await(handler as IDynamicIntegrationEventHandler).Handle(eventData); } else { var eventType = _subscriptionsManager.GetEventTypeByName(eventName); var integrationEvent = JsonSerializer.DeserializeFromString(message, eventType); var intHandler = handler as IIntegrationEventHandler; var concreteType = typeof(IIntegrationEventHandler <>).MakeGenericType(eventType); await(Task) concreteType.GetMethod("Handle").Invoke(intHandler, new[] { integrationEvent }); } } } }
public void Subscribe <T, TH>() where T : IntegrationEvent where TH : IIntegrationEventHandler <T> { var eventName = _subscriptionsManager.GetEventKey <T>(); if (!_persistentConnection.IsConnected) { _persistentConnection.TryConnect(); } var hasSubs = _subscriptionsManager.HasSubscriptionsForEvent(eventName); if (!hasSubs) { using (var channel = _persistentConnection.CreateModel()) { channel.QueueBind(_queueName, exchange: _brokerName, routingKey: eventName); } } _logger.LogInformation("Subscribing to event {EventName} with {EventHandler}", eventName, typeof(TH).Name); _subscriptionsManager.AddSubscription <T, TH>(); StartBasicConsume(); }
public async Task <MqttClientSubscribeResult> SubscribeAsync(SubscriptionInfo subscriptionInfo, CancellationToken cancellationToken = default) { string topic = subscriptionInfo?.Topic; _logger.LogInformation($"Subscribing to topic {topic} with {subscriptionInfo?.ConsumerType?.Name}"); var containsKey = _subsManager.HasSubscriptionsForEvent(topic); if (!containsKey) { if (await _mqttPersisterConnection.TryRegisterMessageHandlerAsync(MessageReceivedAsync, cancellationToken)) { _subsManager.TryAddSubscription(subscriptionInfo); return(await OnSubscribesAsync(topic, cancellationToken)); } } return(null); }
private void DoInternalSubscription(string eventName) { var containsKey = _subsManager.HasSubscriptionsForEvent(eventName); if (!containsKey) { if (!_persistentConnection.IsConnected) { _persistentConnection.TryConnect(); } using (var channel = _persistentConnection.CreateModel()) { channel.QueueBind(queue: _queueName, exchange: BROKER_NAME, routingKey: eventName); } } }
//Private methods private void DoInternalSubscription(string eventName) { var containsKey = subscriptionManager.HasSubscriptionsForEvent(eventName); if (!containsKey) { if (!connectionmanager.IsConnected) { connectionmanager.TryConnect(); } using (var channel = connectionmanager.CreateModel()) { channel.QueueBind(queue: settings.QueueName, exchange: settings.BrokerName, routingKey: eventName); } } }
public static bool HasSubscriptionsForEvent <T>(this ISubscriptionsManager subscriptionsManager) where T : IntegrationEvent { var eventName = subscriptionsManager.GetEventKey <T>(); return(subscriptionsManager.HasSubscriptionsForEvent(eventName)); }