Thursday 13 October 2011

Spring, JMS, Listener Adapters and Containers

In order to receive JMS messages, Spring provides the concept of message listener containers. These are beans that can be tied to receive messages that arrive at certain destinations. This post will examine the different ways in which containers can be configured.

A simple example is below where the DefaultMessageListenerContianer has been configured to watch one queue (the property jms.queue.name) and has a reference to a myMessageListener bean which implements the MessageListener interface (ie onMessage):

<bean id="jmsContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
    <property name="connectionFactory" ref="connectionFactory" /> 
    <property name="destinationName" value="${jms.queue.name}" /> 
    <property name="messageListener" ref="myMessageListener" />
</bean> 


This is all very well but means that the myMessageListener bean will have to handle the JMS Message object and process accordingly depending upon the type of javax.jms.Message and its payload. For example:

if (message instanceof MapMessage) {
    // cast, get object, do something
}


An alternative is to use a MessageListenerAdapter. This class abstracts away the above processing and leaves your code to deal with just the message's payload. For example:

<bean id="jmsContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
    <property name="connectionFactory" ref="connectionFactory" />
    <property name="destinationName" value="${jms.queue.name}" />
    <property name="messageListener" ref="myMessageAdapter" />
</bean> 

<bean id="myMessageAdapter" class="org.springframework.jms.listener.adapter.MessageListenerAdapter">
    <property name="delegate" ref="myMessageReceiverDelegate" />
    <property name="defaultListenerMethod" value="processMessage" />
</bean>


The delegate is a reference to a myMessageReceiverDelegate bean which has one or more methods called processMessage. It does not need to implement the MessageListener interface. This method can be overload to handle different payload types. Spring behind the scenes will determine which gets called. For example:

public void processMessage(final HashMap message) {
    // do something
}

public void processMessage(final String message) {
    // do something
}


For the given approach though, only one queue can be tied to the container. Another approach is to tie many listeners (therefore many queues) to the one container, The below Spring XML, using the jms namespace, shows how two listeners for different queues can be tied to one container:

<jms:listener-container container-type="default" 
  connection-factory="connectionFactory" acknowledge="auto">  
    <jms:listener destination="${jms.queue.name1}" ref="myMessageReceiverDelegate" method="processMessage" />  
    <jms:listener destination="${jms.queue.name2}" ref="myMessageReceiverDelegate" method="processMessage" />  
</jms:listener-container>


The myMessageReceiverDelegate bean is treated as an adapter delegate, therefore does not need to implement the MessageListener interface. Each listener can have a different delegate but for the above example, all messages arriving at the two queues are processed by the one receiver bean ie myMessageReceiverDelegate.

If there is a need to check the message type and extract the payload, then the listener can use a class which implements the MessageListener interface (eg the myMessageListener bean used in the first example). The onMessage method will then be called when messages arrive at the specified destination:

<jms:listener-container container-type="default" 
  connection-factory="connectionFactory" acknowledge="auto">  
    <jms:listener destination="${jms.queue.name1}" ref="myMessageListener" />  
    <jms:listener destination="${jms.queue.name2}" ref="myMessageListener" />  
</jms:listener-container>

7 comments:

  1. Completely agree, Spring is the best framework available now be it JMS, JDBC or Remoting its quite easy to use them by using Spring framework with the help of JMSTemplate and JdbcTemplate etc.

    Thanks
    Javin
    String vs StringBuffer vs StringBuilder in Java

    ReplyDelete
  2. Nice post.
    But this Spring container works for simple cases.

    What about reconnections with JMS? delivery types and etc.

    ReplyDelete
  3. Very nice post.

    Is it possible to specify multiple listeners with the SAME destination WITHIN the same listener-container ?

    ReplyDelete
    Replies
    1. Yes it is possible but if the queue in question is point to point then only one of the listeners will receive the message. If the queue is publish subscribe then all the listeners for that same destination will receive the message.

      Delete
  4. I am trying to use the MessageListener with Tomcat trying to connect to Websphere MQ. But when I do that I am consistently getting the exception
    Exception in thread "main" java.lang.AbstractMethodError: com.ibm.mq.jms.MQQueueConnectionFactory.createConnection(Ljava/lang/String;Ljava/lang/String;)Ljavax/jms/Connection;
    at org.springframework.jms.connection.UserCredentialsConnectionFactoryAdapter.doCreateConnection(UserCredentialsConnectionFactoryAdapter.java:175)


    Any help will be highly appreciated

    Here is the snippet of my spring configuration















    HOST
    PORT
    QMGR
    CHANNEL
    1

    ReplyDelete
  5. Hi Ramesh

    My best guess would be the jar versions. At runtime, your code is accessing a different Websphere MQ jar to the one that the code got compiled with. Check the classpath Tomcat is using.

    Geraint

    ReplyDelete
  6. Geraint, any help in setting a different listener container than using the default container?

    ReplyDelete

Note: only a member of this blog may post a comment.