Example #1
0
        public void ConfigureChannel(string hostName, string username, string password, int retryCount, string queueName)
        {
            _logger.LogInformation("RabbitMQ is trying to connect");

            var policy = PolicyHelper.CreateRabbitMqPolicy(_logger, retryCount);

            policy.Execute(() =>
            {
                _connectionFactory = new ConnectionFactory()
                {
                    HostName   = hostName
                    , UserName = username
                    , Password = password
                };

                _connection = _connectionFactory.CreateConnection();

                _channel = _connection.CreateModel();

                _channel.QueueDeclare(queueName
                                      , false
                                      , false
                                      , false
                                      , null);

                _consumer = new EventingBasicConsumer(_channel);

                _consumer.Received += OnConsumerOnReceived;

                _channel.BasicConsume(queueName, true, _consumer);

                _logger.LogInformation("RabbitMQ Client is ready to receive messages");
            });
        }
        public void Publish(object file, string hostName, string username, string password, int retryCount, string queueName)
        {
            lock (_syncroot)
            {
                _logger.LogInformation("RabbitMQ is trying to connect.");

                var policy = PolicyHelper.CreateRabbitMqPolicy(_logger, retryCount);

                policy.Execute(() =>
                {
                    var factory = RabbitMqHelper.CreateConnectionFactory(hostName, username, password);

                    using var connection = factory.CreateConnection();

                    using var channel = connection.CreateModel();

                    channel.QueueDeclare(queue: queueName,
                                         durable: false,
                                         exclusive: false,
                                         autoDelete: false,
                                         arguments: null);

                    var body = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(file));

                    var properties = channel.CreateBasicProperties();

                    properties.Persistent = true;

                    channel.BasicPublish(""
                                         , queueName
                                         , properties
                                         , body);

                    _logger.LogInformation("RabbitMQ client is ready do publish messages.");
                });
            }
        }