Reputation: 1599
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
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?
Upvotes: 3
Views: 2527
Reputation: 174484
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.
Upvotes: 3