Spring JMS Examples
This project provides some examples of using Spring JMS for both asynchronous and synchronous message consumption.
Asynchronous Consumption
All of the examples for asynchronous message consumption utilize the Spring DefaultMessageListenerContainer for message consumption.
Spring JMS supports three types of message listeners including:
- javax.jms.MessageListener
- SessionAwareMessageListener
- [MessageListenerAdapter](http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/jms/listener/adapter/MessageListenerAdapter.html
These three types of message listeners are all demonstrated using examples in the async directory.
Running the Examples
These examples are intended to be run from the command-line using Maven. The consumer should be run in one terminal and the producer should be run in a separate terminal. Below are examples of running each of the three asynchronous examples.
MessageListener
Example
Run the Producer for the $ cd ./async/message-listener
$ mvn -Pproducer exec:java
MessageListener
Example
Run the Consumer for the $ cd ./async/message-listener
$ mvn -Pconsumer exec:java
SessionAwareMessageListener
Example
Run the Producer for the $ cd ./async/message-listener
$ mvn -Pproducer exec:java
SessionAwareMessageListener
Example
Run the Consumer for the $ cd ./async/message-listener
$ mvn -Pconsumer exec:java
MessageListenerAdapter
Example
Run the Producer for the This example requires a couple of arguments, the first one for the destination name and the second one for the message type. The destination name should be either FOO.TEST or FOO.TEST2. The message type should text, bytes, map or object. Below is an example of text:
$ cd ./async/message-listener
$ mvn -Pproducer exec:java -Dexec.args="FOO.TEST2 text"
These arguments allow you to invoke one of two MessageListenerAdapter
examples and to send four different types of messages.
MessageListenerAdapter
Example
Run the Consumer for the $ cd ./async/message-listener
$ mvn -Pconsumer exec:java
Synchronous Consumption
The example for synchronous message consumption uses the Spring JmsTemplate.
Running the Examples
This example is also intended to be run from the command-line using Maven. The consumer should be run in one terminal and the producer should be run in a separate terminal. Below is an example of running the synchronous example.
JmsTemplate
Example
Run the Consumer for the $ cd ./sync/jms-template
$ mvn -Pconsumer exec:java
JmsTemplate
Example
Run the Producer for the This example supports two different types of sends including a simple convertAndSend and also a jmsSend. These two types are specified using an argument when starting up the producer as shown below:
$ cd ./sync/jms-template
$ mvn -Pproducer exec:java -DsendType=converAndSend
Summary
If you have any questions about these examples, please create an issue for the project. This will allow everyone to see all questions and answers and it will notify me when folks enter new issues.
I also blog about JMS occasionally. Here are some popular posts I have written about Spring JMS: