Skip to main content

Sending JMS message to ActiveMQ using Spring 2.5 from a web application


Last Updated 04/16/2017

In this post, I will show how you can send JMS message to ActiveMQ from your Spring web application. I have a Spring 2.5 + JBOSS Richfaces based web application that runs on Tomcat 6 running on JDK 5. Figure 1. depicts what I intend to do.
F1
Figure - 1 Send JMS Message to ActiveMQ using Spring from a web application


Step 1: Drop in the following jars to WEB-INF/lib
  • activemq-core-5.2.0.jar
  • jms.jar
  • geronimo-j2ee-management_1.0_spec-1.0.jar
Step 2: Create the destination/Queue using the ActiveMQ admin web application.

Launch your browser and point it to - http://localhost:8161/admin/
image
Figure 2 - ActiveMQ Admin Console


Click on 'Queues' to navigate to queue list and create screen. You will need to supply the queue name and click on 'Create' to create a new queue destination on ActiveMQ server. This is shown in Figure 3.
image
Figure 3 - Create a new Queue

Step 3. Create a message producer interface.
This may not be necessary, but since I prefer to implement Program to Interface in my application I define a message producer interface.

This interface defines a single method(can have more, but for now this will suffice) which accepts an object (Java bean) which will be passed to the Queue.

Step 4. An implementation of the MessageProducer interface
This class is responsible for sending a particular JavaBean object to the queue (your goal may be different / may want to send something different). The key is the send method. It uses a MessageCreator to consume the supplied Event object. It then uses the JmsTemplate to send this message to the destination.
Step 5. Provide the message creator implementation


Step 6 - Wire up in Spring configuration

Step 7 - Client of the message producer
Now we need a client to use the message producer. The Event business service class will use this message producer.

Step 8 - Configure in Spring application context.
The message producer is injected by the Spring framework. Here is the configuration snippet from the service configuration.

Step 9 - Test for message
Finally I restart my Tomcat when everything is ready, launch the event entry form, fill in data and click on Send button in the screen. The event details are now in the queue. You can test this in the ActiveMQ admin web console as shown in the Figure - 4 below.
image
Figure 4 - New message now in the Queue.

Comments

Post a Comment

Popular posts from this blog

CKEDITOR 3.x - Simplest Ajax Submit Plugin

  I have assumed that you have downloaded and got started with CKEDITOR. Step 1 – The html file is shown below: <html> <head> <title>Writer</title> <meta content="text/html; charset=utf-8" http-equiv="content-type" /> <script type="text/javascript" src="ckeditor/ckeditor.js"></script> <script type="text/javascript" src="js/jquery-1.4.2.min.js"></script> <style> .cke_contents { height: 400px !important; } </style> </head> <body> <form action="sample_posteddata.php" method="post"> <textarea id="editor" > </textarea> <script type="text/javascript"> //<![CDATA[ CKEDITOR.replace( 'editor', { fullPage : true, uiColor : '#9AB8F3', toolbar : 'MyToolbar' }); //]]> </script> </form> </body> </html> Note that the jquery js

Part 3 - Integrating Tiles, Thymeleaf and Spring MVC 3

In this post I will demonstrate how to integrate Apache Tiles with Thymeleaf. This is very simple. The first step is to include the tiles and thymeleaf-tiles extension dependencies. I will include them in the pom.xml. Note we wil lbe using Tiles 2.2.2 Listing 1 - parent/pom.xml --- thymeleaf-tiles and tiles dependencies <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ --> <!-- Tiles --> <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ --> <dependency> <groupId>org.apache.tiles</groupId> <artifactId>tiles-core</artifactId> <version>${tiles.version}</version> <scope>compile</scope> </dependency> <dependency> <groupId>org.apache.tiles</groupId> <artifactId>tiles-template</artifactId> <version>${tiles.version}</version> <scope>compile</s

Getting started with Prime faces 2

Prime faces is an amazing JSF framework from Cagatay Civici ( http://cagataycivici.wordpress.com/ ). Its wonderful because it is easy to use, minimal dependencies, has probably the widest set of controls among all JSF frameworks, easy to integrate with Spring (including Spring Security) , Java EE EJBs, and last but not the least mobile UI support. So I decided to give Prime faces a try, before selecting it to use in my projects. Step 1 – Create Maven 2 project As a first step to integrating Prime faces, create a Maven 2 project in Eclipse. You will need to select ‘maven-archetype-webapp’. Step 2 – Add repositories and dependencies in pom.xml I will be using Prime faces 2 with JSF 2 on Tomcat 6. Since the dependencies for Prime Faces and JSF 2 (JSF 2.0.3 is required) are available on different repositories, I will add them to my pom file first. The listing below shows my pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/X