Exemple #1
0
        public RabbitMQCommandSender(RabbitMQBusContext context)
        {
            Channel         = context.Connection.CreateModel();
            _replyQueueName = Channel.QueueDeclare("", false, false, true).QueueName;
            _logger         = NijnLogger.CreateLogger <RabbitMQCommandSender>();

            _consumer           = new EventingBasicConsumer(Channel);
            _consumer.Received += (model, ea) =>
            {
                if (!_callbackMapper.TryRemove(ea.BasicProperties.CorrelationId, out var tcs))
                {
                    return;
                }
                var body    = ea.Body;
                var message = Encoding.UTF8.GetString(body);

                _logger.LogInformation("Received command answer with correlation id {0}", ea.BasicProperties.CorrelationId);

                var commandResponse =
                    new CommandResponseMessage(message, ea.BasicProperties.Type, ea.BasicProperties.CorrelationId);
                tcs.TrySetResult(commandResponse);
            };

            _logger.LogInformation("Created response queue with name {0}", _replyQueueName);
        }
        public RabbitMQMessageReceiver(RabbitMQBusContext context, string queueName, IEnumerable <string> topicExpressions)
        {
            _log = NijnLogger.CreateLogger <RabbitMQMessageReceiver>();

            QueueName        = queueName;
            TopicExpressions = topicExpressions;
            Channel          = context.Connection.CreateModel();

            ExchangeName = context.ExchangeName;

            _listening     = false;
            _queueDeclared = false;
        }
Exemple #3
0
        /// <summary>
        /// Creates a context with
        ///  - an opened connection (based on HostName, Port, UserName and Password)
        ///  - a declared Topic-Exchange (based on ExchangeName)
        /// </summary>
        /// <returns></returns>
        public RabbitMQBusContext CreateContext(IConnectionFactory factory = null)
        {
            _log.LogInformation("Creating RabbitMQ Connection");
            if (HostName == null)
            {
                throw new ArgumentNullException(nameof(HostName));
            }
            if (HostName == "")
            {
                throw new ArgumentException(nameof(HostName) + " was empty");
            }
            if (Port < 0 || Port > 65535)
            {
                throw new ArgumentOutOfRangeException(nameof(Port));
            }

            factory = factory ?? new ConnectionFactory()
            {
                HostName = HostName,
                UserName = UserName,
                Password = _password,
                Port     = Port,
                DispatchConsumersAsync = true
            };

            try
            {
                var connection = factory.CreateConnection();
                var context    = new RabbitMQBusContext(connection, ExchangeName);

                using (var channel = connection.CreateModel())
                {
                    channel.ExchangeDeclare(ExchangeName, type: "topic");
                }

                _log.LogInformation("RabbitMQ connection succesfully created");
                return(context);
            }
            catch (BrokerUnreachableException e)
            {
                _log.LogError("Could not connect with the RabbitMQ Environment");
                throw e;
            }
        }
Exemple #4
0
 public RabbitMQCommandReceiver(RabbitMQBusContext context, string queueName)
 {
     _channel  = context.Connection.CreateModel();
     QueueName = queueName;
     _log      = NijnLogger.CreateLogger <RabbitMQCommandReceiver>();
 }
 public RabbitMQMessageSender(RabbitMQBusContext context)
 {
     Channel      = context.Connection.CreateModel();
     ExchangeName = context.ExchangeName;
     _log         = NijnLogger.CreateLogger <RabbitMQMessageSender>();
 }