trongbang86 / spring-jms-examples

An example on how to connect to ActiveMQ using Spring framework

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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:

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.

Environment Setup

  1. Maven
  2. Apache ActiveMQ
  3. HAWTIO (optional)

Set up Queue for ActiveMQ in HAWTIO

After you run ActiveMQ, you should connect HAWTIO to it. It has a nice web interface to manage remote queues in ActiveMQ. Then log in and open the page for your local ActiveMQ. On that page, you can find localhost>Queue. Open it up and create a new one (TEST.FOO in this example).

Run the Producer for the MessageListener Example

$ cd ./async/message-listener-adapter
$ mvn clean compile
$ mvn -Pproducer exec:java

Run the Consumer for the MessageListener Example

$ cd ./async/message-listener-adapter
$ mvn clean compile
$ mvn -Pconsumer exec:java

Run the Producer for the SessionAwareMessageListener Example

$ cd ./async/message-listener-adapter
$ mvn clean compile
$ mvn -Pproducer exec:java

Run the Consumer for the SessionAwareMessageListener Example

$ cd ./async/message-listener-adapter
$ mvn clean compile
$ mvn -Pconsumer exec:java

Run the Producer for the MessageListenerAdapter Example

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-adapter
$ mvn clean compile
$ 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.

Run the Consumer for the MessageListenerAdapter Example

$ cd ./async/message-listener-adapter
$ mvn clean compile
$ 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.

Run the Consumer for the JmsTemplate Example

$ cd ./sync/jms-template
$ mvn clean compile
$ mvn -Pconsumer exec:java 

Run the Producer for the JmsTemplate Example

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 clean compile
$ mvn -Pproducer exec:java -DsendType=convertAndSend

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:

About

An example on how to connect to ActiveMQ using Spring framework


Languages

Language:Java 100.0%