RabbitMQ持久队列不起作用(RPC-Server,RPC-Client)

时间:2015-07-12 16:13:15

标签: c# rabbitmq

我想知道为什么我的RabbitMQ RPC-Client在重启后总是处理死信息。 _channel.QueueDeclare(queue, false, false, false, null);应禁用缓冲区。如果我重载RPC-Client中的QueueDeclare,我就无法连接到服务器。这里有问题吗?知道如何解决这个问题吗?

RPC-服务器

new Thread(() =>
{
    var factory = new ConnectionFactory { HostName = _hostname };
    if (_port > 0)
        factory.Port = _port;
    _connection = factory.CreateConnection();
    _channel = _connection.CreateModel();

    _channel.QueueDeclare(queue, false, false, false, null);
    _channel.BasicQos(0, 1, false);
    var consumer = new QueueingBasicConsumer(_channel);
    _channel.BasicConsume(queue, false, consumer);
    IsRunning = true;
    while (IsRunning)
    {
        BasicDeliverEventArgs ea;
        try {
            ea = consumer.Queue.Dequeue();
        }
        catch (Exception ex) {
            IsRunning = false;
        }
        var body = ea.Body;
        var props = ea.BasicProperties;
        var replyProps = _channel.CreateBasicProperties();
        replyProps.CorrelationId = props.CorrelationId;

        var xmlRequest = Encoding.UTF8.GetString(body);

        var messageRequest = XmlSerializer.DeserializeObject(xmlRequest, typeof(Message)) as Message;
        var messageResponse = handler(messageRequest);

        _channel.BasicPublish("", props.ReplyTo, replyProps,
                                messageResponse);
        _channel.BasicAck(ea.DeliveryTag, false);
    }
}).Start();

RPC客户机

public void Start()
{
    if (IsRunning)
        return;
    var factory = new ConnectionFactory { 
        HostName = _hostname,
        Endpoint = _port <= 0 ? new AmqpTcpEndpoint(_endpoint) 
                              : new AmqpTcpEndpoint(_endpoint, _port)
    };
    _connection = factory.CreateConnection();
    _channel = _connection.CreateModel();
    _replyQueueName = _channel.QueueDeclare(); // Do not connect any more
    _consumer = new QueueingBasicConsumer(_channel);
    _channel.BasicConsume(_replyQueueName, true, _consumer);
    IsRunning = true;
}

public Message Call(Message message)
{
    if (!IsRunning)
        throw new Exception("Connection is not open.");
    var corrId = Guid.NewGuid().ToString().Replace("-", "");
    var props = _channel.CreateBasicProperties();
    props.ReplyTo = _replyQueueName;
    props.CorrelationId = corrId;

    if (!String.IsNullOrEmpty(_application))
        props.AppId = _application;

    message.InitializeProperties(_hostname, _nodeId, _uniqueId, props);

    var messageBytes = Encoding.UTF8.GetBytes(XmlSerializer.ConvertToString(message));
    _channel.BasicPublish("", _queue, props, messageBytes);

    try 
    {
        while (IsRunning)
        {
            var ea = _consumer.Queue.Dequeue();
            if (ea.BasicProperties.CorrelationId == corrId)
            {
                var xmlResponse = Encoding.UTF8.GetString(ea.Body);
                try
                {
                    return XmlSerializer.DeserializeObject(xmlResponse, typeof(Message)) as Message;
                }
                catch(Exception ex)
                {
                    IsRunning = false;
                    return null;
                }
            }
        }
    }
    catch (EndOfStreamException ex)
    {
        IsRunning = false;
        return null;
    }
    return null;
}

0 个答案:

没有答案