示例#1
0
 public void Publish(HdMessage @event)
 {
     using (var channel = _connection.CreateChannel())
     {
         channel.QueueDeclare(_exchange, true, false, false, null);
         var body = Encoding.UTF8.GetBytes(JsonSerializer.Serialize(@event));
         RetryPolicy.ExecuteAndCapture <SocketException, BrokerUnreachableException>(5, TimeSpan.FromSeconds(3),
                                                                                     () =>
         {
             IBasicProperties basicProperties = channel.CreateBasicProperties();
             basicProperties.Persistent       = true;
             channel.BasicPublish(_exchange, @event.Name, true, basicProperties, body);
         });
     }
 }
示例#2
0
        private IModel CreateConsumerChannel()
        {
            if (!_connection.IsConnected)
            {
                _connection.Connect();
            }

            var channel = _connection.CreateChannel();

            channel.ExchangeDeclare(_exchange, ExchangeType.Direct, true);
            channel.QueueDeclare(_queueName, true, autoDelete: false, exclusive: false);

            var consumer = new AsyncEventingBasicConsumer(channel);

            consumer.Received += OnMessage;

            channel.BasicConsume(_queueName, false, consumer);

            return(channel);
        }
示例#3
0
        public RpcClient(string exchangeName, IRabbitMqConnection connection)
        {
            _exchangeName = exchangeName;
            _connection   = connection;
            _requestQueue = typeof(TRequest).Name;
            _replyQueue   = typeof(TReply).Name;

            _connection.Connect();
            _consumerChannel = _connection.CreateChannel();

            _consumerChannel.ExchangeDeclare(_exchangeName, ExchangeType.Direct, true);
            _consumerChannel.QueueDeclare(_requestQueue, true, false, false, null);
            _consumerChannel.QueueDeclare(_replyQueue, true, false, false, null);

            _consumer           = new AsyncEventingBasicConsumer(_consumerChannel);
            _consumer.Received += Consumer_Received;
            _consumerChannel.BasicConsume(_replyQueue, true, _consumer);
            _consumerChannel.QueueBind(_replyQueue, _exchangeName, _replyQueue);

            _pendingMessages = new ConcurrentDictionary <Guid, TaskCompletionSource <TReply> >();
        }