示例#1
0
        public void Run()
        {
            if (_messageProcessor != null)
            {
                var connection = _connectionFactory.CreateConnection();
                var channel    = connection.CreateModel();

                channel.ExchangeDeclare(_exchangeName, ExchangeType.Direct, true);
                channel.QueueDeclare(_queueName, true, false, false, null);
                channel.QueueBind(_queueName, _exchangeName, _queueName, null);

                var consumer = _consumerFactory.CreateEventConsumer(channel);
                consumer.Received += (model, ea) =>
                {
                    var body    = ea.Body;
                    var message = Encoding.UTF8.GetString(body);
                    _messageProcessor.Process(message);
                    channel.BasicAck(ea.DeliveryTag, false);
                };

                channel.BasicConsume(_queueName, false, consumer);
            }
            else
            {
                _logger.LogError($"Message proccesor for type '{_type.ToString()}' not found");
            }
        }
示例#2
0
        public void Run()
        {
            _logger.LogError($"Listener for type '{_type}' have been started");
            if (_service != null)
            {
                var connection = _connectionFactory.CreateConnection();
                var channel    = connection.CreateModel();

                channel.ExchangeDeclare(_exchangeName, ExchangeType.Direct, true);
                channel.QueueDeclare(_queueName, true, false, false, null);
                channel.QueueBind(_queueName, _exchangeName, _queueName, null);

                var consumer = _consumerFactory.CreateEventConsumer(channel);
                consumer.Received += (model, ea) =>
                {
                    var body    = ea.Body;
                    var message = JsonConvert.DeserializeObject <EmailMessage>(Encoding.UTF8.GetString(body));
                    _service.SendEmail(message.Email, message.Title, message.Body);
                    channel.BasicAck(ea.DeliveryTag, false);
                };

                channel.BasicConsume(_queueName, false, consumer);
            }
            else
            {
                _logger.LogError($"Email service for type '{_type}' not found");
            }
        }