DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Spring, IoC Containers, and Static Code: Design Principles
  • Building a CRUD Application With Spring and SimpleJdbcMapper
  • A Guide for Deploying .NET 10 Applications Using Docker's New Workflow
  • A Beginner's Guide to Essential Commands to Fix Container Setup Issues

Trending

  • 7 Technology Waves I’ve Seen in 30 Years of Software — Will AI Be the Next Real Transformation?
  • DZone's Article Submission Guidelines
  • Persistent Memory for AI Agents Using LangChain's Deep Agents
  • Combining Temporal and Kafka for Resilient Distributed Systems
  1. DZone
  2. Software Design and Architecture
  3. Cloud Architecture
  4. Spring, JMS, Listener Adapters, and Containers

Spring, JMS, Listener Adapters, and Containers

By 
Geraint Jones user avatar
Geraint Jones
·
Feb. 28, 13 · Interview
Likes (2)
Comment
Save
Tweet
Share
72.5K Views

Join the DZone community and get the full member experience.

Join For Free

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>


Spring Framework Container

Published at DZone with permission of Geraint Jones. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Spring, IoC Containers, and Static Code: Design Principles
  • Building a CRUD Application With Spring and SimpleJdbcMapper
  • A Guide for Deploying .NET 10 Applications Using Docker's New Workflow
  • A Beginner's Guide to Essential Commands to Fix Container Setup Issues

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook