Search code examples
javarabbitmqspring-amqpmessage-listener

Spring AMQP : MessageListener not receiving any messages


Currently I'm building a custom library on top of the Spring AMQP project. I've come to the point where I want to implement a message listener to be able to receive message asynchronous. After reading further into the documentation as specified by the project, I management to find that it should be quite easy to implement your own message listener. Just implement the MessageListener class and configure it to fire on incoming messages.

So that is what I did:

public class ReceiveController implements MessageListener
{
    @Override
    public void onMessage(Message message)
    {
        System.out.println("Received a message!");
    }
}

Next I configured it like this:

private SimpleMessageListenerContainer configureMessageListener()
{
    SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
    container.setConnectionFactory(connection);
    container.setQueueNames("test.queue");
    container.setMessageListener(this);
    return container;
}

Both pieces of code are located in the same class called 'ReceiveController'.

Hence the fact that I'm not using any context (annotations or xml). I'm not sure if this is mandatory for the project to function as I can just create instances of the classes myself.

When running some code which uses my library

  • The Consumer client connects to the RabbitMQ broker(and stays connected).
  • The Producer client connects to the RabbitMQ broker, sends his message and disconnects.
  • When I look at the queue using the management plugin I see that a message has been put on the queue, but the message listener did not get triggered.

For some reason the consumer does not receive any messages through it's listener. Could this be related to the fact that the queue was created using a 'amq.direct' exchange and 'test.route' routing key? Or is it something else?


Solution

  • When constructing the container manually (outside of a Spring Application Context), you need to invoke afterPropertiesSet() and start().

    Also, if your listener implements MessageListener or ChannelAwareMessageListener, you don't need an adapter.