Beispiel #1
0
 public async Task Publish(IRequest <Unit> @event, Header header, IEndPointConfigurator endPointConfigurator)
 {
     using (var channel = VerifyConnection())
     {
         await RunPublishSettings(@event, header, channel, endPointConfigurator).ConfigureAwait(false);
     }
 }
Beispiel #2
0
        protected virtual void RegistryConsumer(IModel channel, IEndPointConfigurator endPointConfig)
        {
            var consumer = new EventingBasicConsumer(channel);

            AutoRecreateQueue(consumer, endPointConfig.QueueConfiguration);

            consumer.Received += async(model, eventArgs) =>
            {
                try
                {
                    await ProcessEvent(eventArgs, endPointConfig).ConfigureAwait(continueOnCapturedContext: false);

                    channel.BasicAck(deliveryTag: eventArgs.DeliveryTag, multiple: false);
                }
                catch (JsonReaderException ex)
                {
                    var message = eventArgs?.Body == null ? string.Empty : Encoding.UTF8.GetString(eventArgs?.Body);

                    _logger.LogError($"Erro ao deserializar o evento. Motivo: {ex.Message}", ex);
                    channel.BasicNack(deliveryTag: eventArgs.DeliveryTag, multiple: false, requeue: false);
                }
                catch (Exception ex)
                {
                    _logger.LogError($"Erro ao processar o evento: {eventArgs.BasicProperties?.Type?.ToString()}. Motivo: {ex.Message}", ex);
                    channel.BasicNack(deliveryTag: eventArgs.DeliveryTag, multiple: false, requeue: true);
                }
            };

            channel.BasicConsume(queue: endPointConfig.QueueConfiguration.Name, autoAck: false, consumer: consumer, consumerTag: $"{endPointConfig.QueueConfiguration.Name}: MachineName {System.Environment.MachineName}");
        }
Beispiel #3
0
 public void Subscribe <T>(IEndPointConfigurator endPointConfig) where T : IRequest <Unit>
 {
     if (!EndPointConfigurators.ContainsKey(endPointConfig.RoutingKey))
     {
         endPointConfig.MessageType = typeof(T);
         EndPointConfigurators.Add(endPointConfig.RoutingKey, endPointConfig);
     }
 }
Beispiel #4
0
        private async Task Bind(IModel channel, IEndPointConfigurator endPointConfig)
        {
            await Task.Run(() =>
            {
                _logger.LogDebug($"Criando o Bind entre a exchange {endPointConfig.ExchangeConfiguration.Name} e a fila {endPointConfig.QueueConfiguration.Name}");

                channel.QueueBind(endPointConfig.QueueConfiguration.Name, endPointConfig.ExchangeConfiguration.Name, endPointConfig.RoutingKey, endPointConfig.Arguments);
            }).ConfigureAwait(false);
        }
Beispiel #5
0
        protected async Task ProcessEvent(BasicDeliverEventArgs eventArgs, IEndPointConfigurator endPointConfig)
        {
            var message = Encoding.UTF8.GetString(eventArgs.Body);

            var eventType = endPointConfig.MessageType;

            var integrationEvent = (IRequest <Unit>)JsonConvert.DeserializeObject(message, eventType);

            using (var scope = _serviceProvider.CreateScope())
            {
                var mediator = scope.ServiceProvider.GetRequiredService <IMediator>();
                await mediator.Send(integrationEvent).ConfigureAwait(continueOnCapturedContext: false);
            }
        }
Beispiel #6
0
        public async Task Set(IEndPointConfigurator endPointsConfig)
        {
            var endPointCache = await Get(endPointsConfig.QueueConfiguration.Name).ConfigureAwait(false);

            if (endPointCache != null)
            {
                _memoryCache.Remove(endPointCache.QueueConfiguration.Name);
            }

            _logger.LogInformation($"Salvando EndPoint: {endPointsConfig.QueueConfiguration.Name} in Cache.");

            //_memoryCache.Set(endPointsConfig.QueueConfiguration.Name, endPointsConfig);

            await Task.CompletedTask;
        }
Beispiel #7
0
        public async Task <IEndPointConfigurator> Get(string queueName)
        {
            if (queueName == null)
            {
                throw new System.ArgumentNullException(nameof(queueName));
            }

            IEndPointConfigurator endPoint = null;

            return(await Task.Run(() =>
            {
                var result = _memoryCache.TryGetValue(queueName, out endPoint);
                if (result)
                {
                    return endPoint;
                }
                return endPoint;
            }));
        }
Beispiel #8
0
 public async Task CreateEndPoints(IModel channel, IEndPointConfigurator endPointConfig)
 {
     await DeclareExchange(channel, CreateExchangeConfiguration(endPointConfig.ExchangeConfiguration)).ConfigureAwait(false);
     await DeclareQueue(channel, CreateQueueConfiguration(endPointConfig.QueueConfiguration)).ConfigureAwait(false);
     await Bind(channel, endPointConfig).ConfigureAwait(false);
 }
Beispiel #9
0
 public void Publish(IRequest <Unit> @event, Header header, IEndPointConfigurator endPointConfigurator)
 {
     _titanFlashPublisher.Publish(@event, header, endPointConfigurator);
 }
Beispiel #10
0
 public void Publish(IRequest <Unit> @event, IEndPointConfigurator endPointConfigurator) => Publish(@event, Header.Default(), endPointConfigurator);
Beispiel #11
0
 protected async Task CreateEndPoints(IModel channel, IEndPointConfigurator endPointConfig)
 {
     await _titanFlashEndPointFactory.CreateEndPoints(channel, endPointConfig).ConfigureAwait(false);
 }
Beispiel #12
0
        private async Task RunPublishSettings(IRequest <Unit> @event, Header header, IModel channel, IEndPointConfigurator endPointConfigurator)
        {
            await Task.Run(() =>
            {
                CreateEndPoints(channel, endPointConfigurator).GetAwaiter().GetResult();

                var message = JsonConvert.SerializeObject(@event);
                var body    = Encoding.UTF8.GetBytes(message);

                var basicProperties = channel.CreateBasicProperties();

                basicProperties.Persistent  = true;
                basicProperties.ContentType = CONTENT_TYPE;
                basicProperties.Type        = @event.GetType().AssemblyQualifiedName;
                basicProperties.Headers     = header.Values;

                var policy = CreatePolicy();

                policy.Execute(() =>
                {
                    _logger.LogDebug($"Publicando Mensagem na Exchange {endPointConfigurator.ExchangeConfiguration.Name} no RoutingKey {endPointConfigurator.RoutingKey}. Mensagem {message}");

                    channel.BasicPublish(endPointConfigurator.ExchangeConfiguration.Name,
                                         endPointConfigurator.RoutingKey,
                                         basicProperties,
                                         body);
                });
            }).ConfigureAwait(false);
        }
Beispiel #13
0
 public async Task Publish(IRequest <Unit> @event, IEndPointConfigurator endPointConfigurator)
 {
     await Publish(@event, Header.Default(), endPointConfigurator).ConfigureAwait(false);
 }