如何使用Spring AMQP从RabbitMQ接收correlationid的消息

如何使用Spring AMQP从RabbitMQ接收correlationid的消息,rabbitmq,amqp,spring-amqp,Rabbitmq,Amqp,Spring Amqp,我浏览了RabbitTemplate的API。它只提供从队列中获取消息的接收方法。但是,无法获取具有特定关联id的消息。请您帮助我了解我在此处缺少的内容 目前,我正在使用ActiveMQ中的JMS API,使用以下代码接收消息,该代码使用消息选择器创建Consumer。希望通过RabbitMQ对Spring AMQP执行同样的操作: private ObjectMessage receiveMessage(final String readQueue, final UUID correlatio

我浏览了RabbitTemplate的API。它只提供从队列中获取消息的接收方法。但是,无法获取具有特定关联id的消息。请您帮助我了解我在此处缺少的内容

目前,我正在使用ActiveMQ中的JMS API,使用以下代码接收消息,该代码使用消息选择器创建Consumer。希望通过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;
} 

您正在JMS中使用
messageSelector
字符串;RabbitMQ/AMQP没有等效项

相反,每个使用者都有自己的队列,您可以使用代理中的直接或主题交换来执行路由。我建议你看一看,然后


如果您使用RealRealID进行请求/应答处理,请考虑使用内置的代码> SDENDAND接收或<代码>转换和接收/<代码>方法。有关更多信息,请参阅。

非常感谢Gary我们可以设置ConvertSendReceive返回的超时吗?如果超出超时,convertSendAndReceive应返回某种异常。默认超时为5秒,请使用
setReplyTimeout(长)
(毫秒)调整它。如果超时,则返回空值,而不是异常。