Question

I am using Sprin AMQP's rabbittemplate to send and recieve message via RabbitMQ. I am able to send and receive messages, Howver, I want to attach priority with message.

e.g, If I am pushing 1000 messages, and let's say odd number messages have priority 1, and even number messages have priority 0, and then I am starting my consumer, then consumer should receive odd number messages first, and then even number messages.

Here is my code: Producer:

public void sendMessage(int i) throws IOException {
    Record r = new Record();
    r.setFrom((i + 1));
    r.setTo("infoimage");
    r.setMessage("Hi Pritish.");
    MessageProperties prop = new MessageProperties();
    prop.setPriority(i%2);
    byte[] rByte = serialize(r);
    Message m = new Message(rByte, prop);
    rabbitTemplate.convertAndSend(queueName, m);
}

Consumer:

public MessageListener exampleListener() {
    return new MessageListener() {
        public void onMessage(Message message) {
     //do some job
     }

}; }

Am I doing something wrong? Can anybody please help me to resolve this?

Was it helpful?

Solution

Prior to version 3.5.0, RabbitMQ doesn't support priority out of the box.

There is a plugin, though.

You can also simulate priority by using multiple queues, but that only really works if the number of high priority messages is small, compared to low priority.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top