2

我浏览了 RabbitTemplate 的 API。它只提供从队列中获取消息的接收方法。但是,无法获取具有特定相关 ID 的消息。你能帮我理解我在这里缺少什么吗?

目前,我正在使用来自 ActiveMQ 的 JMS API 使用以下代码接收消息,该代码使用消息选择器 createConsumer。希望对带有 RabbitMQ 的 Spring AMQP 做同样的事情:

private ObjectMessage receiveMessage(final String readQueue, final UUID correlationId, final boolean isBroadcastMessage, final int readTimeout) throws JMSException
{
    final ActiveMQConnectionFactory connectionFactory = this.findConnectionFactory(readQueue);
    Connection connection = null;
    Session session = null;
    MessageConsumer consumer = null;
    ObjectMessage responseMessage = null;

    try
    {
        connection = connectionFactory.createConnection();
        connection.start();
        session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

        Destination destination = session.createQueue(readQueue);

        consumer = session.createConsumer(destination, "correlationId = '" + correlationId + "'");
        final Message message = consumer.receive(readTimeout);
    }
    finally
    {
        if (consumer != null)
        {
            consumer.close();
        }
        if (session != null)
        {
            session.close();
        }
        if (connection != null)
        {
            connection.close();
        }
    }
    return responseMessage;
} 
4

1 回答 1

1

您在 JMS 中使用messageSelector字符串;RabbitMQ/AMQP 没有等价物。

相反,每个消费者都有自己的队列,您使用代理中的直接或主题交换来进行路由。我建议你看看rabbitmq 网站主题上的教程。

如果您使用correlationId 进行请求/回复处理,请考虑使用模板中的内置sendAndReceiveconvertSendAndReceive方法。有关详细信息,请参阅参考文档

于 2014-05-20T13:13:37.783 回答