Announcement Announcement Module
No announcement yet.
SimpleMessageListenerContainer - onMessage() VS handleMessage() Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • SimpleMessageListenerContainer - onMessage() VS handleMessage()


    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?

  • #2
    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.


    • #3
      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.


      • #4
        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?


        • #5
          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.