Esempio n. 1
0
        private void SubsManager_OnEventRemoved(object sender, string eventName)
        {
            if (!_persistentConnection.IsConnected)
            {
                _persistentConnection.Connect();
            }

            using (var channel = _persistentConnection.CreateModel())
            {
                channel.QueueUnbind(queue: _queueName,
                                    exchange: BROKER_NAME,
                                    routingKey: eventName);

                if (_subsManager.IsEmpty)
                {
                    _queueName = string.Empty;
                    _consumerChannel.Close();
                }
            }
        }
Esempio n. 2
0
        public void Publish(IEvent @event)
        {
            var eventName = @event.GetType().Name;

            _logger.LogInformation($"Executando { nameof(Publish) }({eventName})");

            if (_connection.Disconnected)
            {
                _connection.Connect();
            }

            var policy = Policy.Handle <BrokerUnreachableException>()
                         .Or <SocketException>()
                         .WaitAndRetry(3, attemp => TimeSpan.FromSeconds(Math.Pow(2, attemp)), (ex, time) =>
            {
                _logger.LogWarning("Não foi possível publicar a '{EventName}': ({ExceptionMessage})", eventName, ex.Message);
            });


            using (var channel = _connection.CreateModel())
            {
                DeclareExchange(channel);

                DeclareQueueAndBind(eventName, channel);

                policy.Execute(() =>
                {
                    _logger.LogInformation("Publicando '{EventName}': {Event}", eventName, @event.ToJson());

                    channel.BasicPublish(
                        exchange: EXCHANGE_NAME,
                        routingKey: eventName,
                        basicProperties: null,
                        body: @event.ToJson(IgnoredProperties).ToBytes()
                        );
                });
            }
        }