Hey guys, Hope you are doing well.
In this blog I will be focusing on JMS(Java Messaging Service) with an end to end Web Application.
In this process , we will be creating a demo application which will be posting message (Text message) from front end screen. This message will be used by Message producer of JMS to post the message on the Queue.We will be using ActiveMQ as a broker here. JMSTemplate is used in this example for message Posting.Similarly on Other end on the same queue there will be Consumer configured which will consume the message posted by Producer.The whole Message tracking will be logged in a separate Logger named JMSDemo.log in logs directory of your application server.
The tools and techniques used in this project are mentioned below:
- STS 3.5.0 (Spring Tool Suite)
- apache-activemq-5.9.0
- Apache Maven 3.1.1
- Springs
- Tomcat 7
- JDK 1.6
Lets Begin, Below are the steps which describes the implementation.
Step 1> Download apache-activemq-5.9.0 from below link and install on your local machine.
http://activemq.apache.org/activemq-590-release.html
After downloading , execute activemq batch file under bin folder (directory win32/win64).
This will start the ActiveMQ and at the end the URL from which we can access it will be seen in logs.
Please refer below screen shot for reference
On the browser please verify by hitting below URL:
http://localhost:8161/admin/queues.jsp
by default username password is : admin/admin
Step 2> Create a Maven Project with below artifacts information
<groupId>com.x.jms.JMSDemo</groupId>
<artifactId>JMSDemo</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>JMSDemo</name>
<description>JMSDemo</description>
Step 3> Update the pom.xml
Add the below dependencies and plugins in the pom.xml
pom.xml
<project xmlns=”http://maven.apache.org/POM/4.0.0″ xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
xsi:schemaLocation=”http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd”>
<modelVersion>4.0.0</modelVersion>
<groupId>com.x.jms.JMSDemo</groupId>
<artifactId>JMSDemo</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>JMSDemo</name>
<description>JMSDemo</description>
<repositories>
<repository>
<id>java.net2</id>
<name>Repository hosting the jee6 artifacts</name>
<url>http://download.java.net/maven/2</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>3.2.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<!– <version>3.0.5.RELEASE</version> –>
<version>3.2.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>3.2.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>3.2.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
<version>2.5.6</version>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-all</artifactId>
<version>5.9.0</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
<build>
<finalName>JMSDemo</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1-beta-1</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
</project>
Step 4> Create a folder Structure as mentioned in screenshot
Step 5> com.x.jms package files
ConsumerTask.java [Used to Consuming Messages from the Queue]
file: ConsumerTask
ProducerTask.java [Used to Produce Messages on to the Queue]
file: ProducerTask
RequestHandlerServlet.java [Servlet which delegates User Request as a message to ProducerTask]
file: RequestHandlerServlet
Note: Please change the file extension as required.
Step 6> Configuring Spring-config ,context ,log4j ,web.xml file.
spring-config.xml [bean definitions of Producer and Consumer files along with jmsTemplate]
file: spring-config
context.xml [Consist of resources of mqConnectionFactory and Destination details]
file: context
log4j.properties[Logging information and patterns used of logging]
file: log4j
web.xml
file: web_JMSDemo
Note: Please change the file extension as required.
I wont be explaining webapplication related stuff like JSP and Servlet ,since they are complementary in this space and used to
test JMS stuff.
Step 7> jsp files
index.jsp [ Used to Post messages from front end]
success.jsp
Step 8> Add ActiveMq jar in lib folder of Tomcat Lib directory
jar name: activemq-all-5.9.0.jar
Step 9> Build and deploy the project
copy the war file from target folder and deploy it to Tomcat webapps directory, and start the Tomcat server.
Step 10> Verify the web application
Go to the browser and hit http://localhost:8081/JMSDemo/ this should open an UI to post message.
Add some text in there and click POST MESSAGE button to post the message to the Queue.
As for this example i have posted Testing Spring JMS message .
Please verify the message trace in JMSDemo.log in logs directory. Below is the screenshot which verifies the same.
Note: Please mention port name in URL as per your server running on.

Advantages of using JmsTemplate is that its very easy to implement and encapsulate the traditional code of creating ConnectionFactory, Connection, Session, MessageProducer, MessageConsumer, TextMessage etc .
I am done with this article, will be back soon with some more interesting topics, till then enjoy coding 🙂



