Esempio n. 1
0
        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 void Publish(IntegrationEvent @event)
        {
            TryConnectIfDisconnected();
            var addToQueuePolicy = RetryPolicy.Handle <BrokerUnreachableException>()
                                   .Or <SocketException>()
                                   .WaitAndRetry(_config.MaxRetries,
                                                 retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)),
                                                 (ex, time) => _logger.LogWarning(ex.ToString()));

            using (var channel = _persistentConnection.CreateModel())
            {
                var eventName = _subscriptionsManager.GetEventKey(@event.GetType());

                channel.ExchangeDeclare(_config.ExhangeName, _config.ExchangeType);
                var message = JsonSerializer.SerializeToString(@event);
                var body    = Encoding.UTF8.GetBytes(message);

                addToQueuePolicy.Execute(() =>
                {
                    var properties          = channel.CreateBasicProperties();
                    properties.DeliveryMode = 2; // persistent

                    channel.BasicPublish(exchange: _config.ExhangeName,
                                         routingKey: eventName,
                                         mandatory: true,
                                         basicProperties: properties,
                                         body: body);
                });
            }
        }
Esempio n. 3
0
        public void Subscribe <T, TH>() where T : DefaultEvent where TH : IEventHandler <T>
        {
            var eventName = subscriptionManager.GetEventKey <T>();

            DoInternalSubscription(eventName);

            subscriptionManager.AddSubscription <T, TH>();
            StartConsumer();
        }
Esempio n. 4
0
        public void Subscribe <T, TH>()
            where T : Event
            where TH : IEventHandler <T>
        {
            var eventName = _subsManager.GetEventKey <T>();

            DoInternalSubscription(eventName);

            _logger.LogInformation("Subscribing to event {EventName} with {EventHandler}", eventName, typeof(TH).Name);

            _subsManager.AddSubscription <T, TH>();
            StartBasicConsume();
        }
Esempio n. 5
0
        public void Subscribe <T, TH>()
            where T : EventArguments
            where TH : IEventHandler <T>
        {
            var eventName = _subsManager.GetEventKey <T>();

            DoInternalSubscription(eventName);
            _subsManager.AddSubscription <T, TH>();
        }
Esempio n. 6
0
        public void Subscribe <TIntegrationEvent, TIntegrationEventHandler>()
            where TIntegrationEvent : IntegrationEvent
            where TIntegrationEventHandler : IIntegrationEventHandler <TIntegrationEvent>
        {
            var eventName = _subscriptionsManager.GetEventKey <TIntegrationEvent>();

            DoInternalSubscription(eventName);
            _subscriptionsManager.AddSubscription <TIntegrationEvent, TIntegrationEventHandler>();

            throw new NotImplementedException();
        }
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="TEvent"></typeparam>
        /// <param name="event"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task <bool> PublishEventAsync <TEvent>(
            TEvent @event,
            CancellationToken cancellationToken = default)
        {
            var result = await eventStore.PushAsync(new EventWrapper
            {
                Name    = subscriptionsManager.GetEventKey <TEvent>(),
                Message = JsonSerializer.Serialize(@event),
            });

            logger.LogInformation($"Publish event {(result ? "success" : "failed")}");

            if (task == null)
            {
                DoConsume();
            }

            return(result);
        }
Esempio n. 8
0
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="TEvent"></typeparam>
        /// <typeparam name="TEventHandler"></typeparam>
        public void RegisterEventHandler <TEvent, TEventHandler>()
            where TEventHandler : IEventHandler <TEvent>
        {
            var eventName = subscriptionsManager.GetEventKey <TEvent>();

            if (subscriptionsManager.HasSubscriptions(eventName))
            {
                return;
            }

            if (!connection.IsConnected)
            {
                connection.TryConnect();
            }

            using var model = connection.CreateModel();
            model.QueueBind(
                queue: settings.QueueName,
                exchange: BROKER_NAME,
                routingKey: eventName);

            subscriptionsManager.AddSubscription <TEvent, TEventHandler>();
        }
 public static string GetEventKey <T>(this ISubscriptionsManager subscriptionsManager)
 {
     return(subscriptionsManager.GetEventKey(typeof(T)));
 }
        public static bool HasSubscriptionsForEvent <T>(this ISubscriptionsManager subscriptionsManager) where T : IntegrationEvent
        {
            var eventName = subscriptionsManager.GetEventKey <T>();

            return(subscriptionsManager.HasSubscriptionsForEvent(eventName));
        }
        public static IEnumerable <SubscriptionInfo> GetHandlersForEvent <T>(this ISubscriptionsManager subscriptionsManager) where T : IntegrationEvent
        {
            var eventName = subscriptionsManager.GetEventKey <T>();

            return(subscriptionsManager.GetHandlersForEvent(eventName));
        }