How to receive messages for a correlationid from RabbitMQ using Spring AMQP

I went through the API of RabbitTemplate. It provides only receive method which gets the message from queue. However there is no way to get a message with a particular correlation id. Can you please help me understand what I am missing here.

Currently, I am using JMS API's from ActiveMQ to receive messages using the following code which createConsumer with message selector. Looking to do the same with Spring AMQP with RabbitMQ:

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;
} 

You are using a messageSelector string in JMS; RabbitMQ/AMQP does not have an equivalant.

Instead, each consumer gets its own queue and you use a direct or topic exchange in the broker to do the routing. I suggest you take a look at the tutorials on the rabbitmq web site and topics.

If you are using the correlationId for request/reply processing, consider using the inbuilt sendAndReceive or convertSendAndReceive methods in the template. See the reference documentation for more information.

链接地址: http://www.djcxy.com/p/59648.html

上一篇: 在Spring AMQP中向消费者处理无法传递的消息

下一篇: 如何使用Spring AMQP从RabbitMQ接收消息