Example #1
0
        private void InitClient()
        {
            var connection = _connectionPool.Rent();

            _channel = connection.CreateModel();

            _channel.ExchangeDeclare(
                exchange: _exchageName,
                type: RabbitMQOptions.ExchangeType,
                durable: true);

            var arguments = new Dictionary <string, object> {
                { "x-message-ttl", (int)_rabbitMQOptions.QueueMessageExpires }
            };

            _channel.QueueDeclare(_queueName,
                                  durable: true,
                                  exclusive: false,
                                  autoDelete: false,
                                  arguments: arguments);

            _connectionPool.Return(connection);
        }
Example #2
0
        public override Task <OperateResult> PublishAsync(string keyName, string content)
        {
            var connection = _connectionPool.Rent();

            try
            {
                using (var channel = connection.CreateModel())
                {
                    var body = Encoding.UTF8.GetBytes(content);

                    channel.ExchangeDeclare(_rabbitMQOptions.TopicExchangeName, RabbitMQOptions.ExchangeType, durable: true);
                    channel.BasicPublish(exchange: _rabbitMQOptions.TopicExchangeName,
                                         routingKey: keyName,
                                         basicProperties: null,
                                         body: body);

                    _logger.LogDebug($"rabbitmq topic message [{keyName}] has been published.");
                }
                return(Task.FromResult(OperateResult.Success));
            }
            catch (Exception ex)
            {
                _logger.LogError($"rabbitmq topic message [{keyName}] has benn raised an exception of sending. the exception is: {ex.Message}");

                return(Task.FromResult(OperateResult.Failed(ex,
                                                            new OperateError()
                {
                    Code = ex.HResult.ToString(),
                    Description = ex.Message
                })));
            }
            finally
            {
                _connectionPool.Return(connection);
            }
        }