PDA

View Full Version : SimpleMessageListenerContainer - onMessage() VS handleMessage()



ponsu
Mar 28th, 2011, 02:27 AM
Hi,

In consumer application, for listening for asynchronous messages, we can use either onMessage() or handleMessage() method while setting 'SimpleMessageListenerContainer.setMessageListener ()'.

What is the difference between these two methods and when to use these methods?

Dave Syer
Mar 28th, 2011, 09:00 AM
onMessage() is defined in an interface (MessageListener). handleMessage() is just a naming convention (the default method name in a POJO if you don't specify). It's the same in Spring JMS, which is where the names come from, to make it easy to learn Spring AMQP if you already know JMS.

Mark Fisher
Mar 28th, 2011, 09:08 AM
Also, if you are using a POJO, you can use any method name you want as long as you provide it via the "defaultListenerMethod" property of the MessageListenerAdapter or the "method" attribute of the <listener> element if using the namespace support.

ponsu
Mar 30th, 2011, 01:10 AM
Mark and Dave, Thanks for your clarifications.

I have another query on using onMessage(Message msg) method.

In onMessage() method, how can I find the name of the Queue from which the message was sent?

Dave Syer
Mar 31st, 2011, 11:23 AM
I don't think you can (it's not supported by the protocol). You can find the receivedRoutingKey and receivedExchange in the MessageProperties, but as a consumer you are supposed to know which queue you are listening to.