TechAlpine – The Technology world

Steps to integrate Spring & Java Message Service

Spring and JMS

Spring Series – Learn JMS integration by Example

Overview

In this article we will cover JMS integration with Spring frame work. We will concentrate more on the coding implementation part. This is to help the developers to try hands-on coding and implement JMS in their project.

Java messaging service is used for messaging purpose for most of the Java and J2EE applications. Spring provides an abstraction on the JMS layer that makes it simple to access a message queue or topic (abstractly referred to as a destination) and publish messages to the destination.







Steps to follow 

JmsTemplate is the main class to be used for messaging purport in spring framework. The jmsTemplate property will be wired with an instance of org.springframework.jms.core.JmsTemplate using setter injection

The sendSettlementMessage() method is used in the JmsTemplate’s send() method to send the message. This method takes an instance of org.springframework.jms.core.MessageCreator

Following code snippet shows the wiring part of the configuration

<bean id=”paymentService”

class=”com.springinaction.training.service.PaymentServiceImpl”>

<property name=”jmsTemplate”>

<ref bean=”jmsTemplate”/>

</property>

<bean>

Following are the steps to integrate spring and JMS using any application server.

  • Configure the following JMS resources inside the application server.

ConnectionFactory- jms/connectionFactory and Queue Destination- jms/testQueue

  • Create dynamic web application in the application server
  • Import Spring.jar, Commons-logging.jar and log4j-1.2.15.jar
  • Create the InvoiceQueueSender class in the package jms. The class InvoiceQueueSender is used to send messages. The JMSTemplate is injected into InvoiceQueueSender using IOC container.

package techalpine.jms;

import javax.jms.JMSException;

import javax.jms.Message;

import javax.jms.Session;

import org.springframework.jms.core.JmsTemplate;

import org.springframework.jms.core.MessageCreator;

public class InvoiceQueueSender {

private JmsTemplate jmsTemplate;

public void setJmsTemplate(JmsTemplate jmsTemplate) {

this.jmsTemplate = jmsTemplate;

}

public void sendMesage() {

MessageCreator messageCreator=new MessageCreator() {

public Message createMessage(Session session) throws

JMSException {

return session.createTextMessage(“I am sending Invoice message”);}

};

jmsTemplate.send(“jms/testQueue”, messageCreator);

}

}







  • Create the InvoiceMDB class in the package jms.

package techalpine.jms;

import javax.jms.JMSException;

import javax.jms.Message;

import javax.jms.MessageListener;

import javax.jms.TextMessage;

public class InvoiceMDB implements MessageListener {

public void onMessage(Message message) {

try {

System.out.println(((TextMessage) message).getText());

System.out.println(“Hello JMS”);

} catch (JMSException ex) {

throw new RuntimeException(ex);

}

}

}

  • The following details will be configured inside spring configuration file named as applicationContext.xml.

&ltbean id=”jndiTemplate” class=”org.springframework.jndi.JndiTemplate”>

&ltproperty name=”environment”>

&ltprops>

&ltprop key=”java.naming.factory.initial”&gtweblogic.jndi.WLInitialContextFactory</prop>

&ltprop key=”java.naming.provider.url”&gtt3://localhost:7001</prop>

</props>

</property>

</bean>

  • JndiObjectFactoryBean is used to look up the JNDI object on startup and cache it. This interface is used to configure connection factory

&ltbean id=”queueConnectionFactory” class=”org.springframework.jndi.JndiObjectFactoryBean”>

&ltproperty name=”jndiTemplate”>

&ltref bean=”jndiTemplate” />

</property>

&ltproperty name=”jndiName”>

&ltvalue&gtjms/connectionFactory</value>

</property>

</bean>

  • DestinationResolver is used by JmsTemplate to resolve destination names

&ltbean id=”jmsDestinationResolver” class=”org.springframework.jms.support.destination.JndiDestinationResolver”>

&ltproperty name=”jndiTemplate”>

&ltref bean=”jndiTemplate” />

</property>

&ltproperty name=”cache”>

&ltvalue&gttrue</value>

</property>

</bean>

  • Now the JMSTemplate is used to send messages. Following is a code snippet

&ltbean id=”invoiceQueueTemplate” class=”org.springframework.jms.core.JmsTemplate”>

&ltproperty name=”connectionFactory”>

&ltref bean=”queueConnectionFactory” />

</property>

&ltproperty name=”destinationResolver”>

&ltref bean=”jmsDestinationResolver” />

</property>

</bean>

  • Now the Queue configuration is given below

&ltbean id=”invoiceQueue” class=”org.springframework.jndi.JndiObjectFactoryBean”>

&ltproperty name=”jndiTemplate”>

&ltref bean=”jndiTemplate” />

</property>

&ltproperty name=”jndiName”>

&ltvalue&gtjms/testQueue</value>

</property>

</bean>

  • Now after configuring all the necessary components the final content will be as follows

<?xml version=”1.0″ encoding=”UTF-8″?>

<!DOCTYPE beans PUBLIC “-//SPRING//DTD BEAN//EN”

“http://www.springframework.org/dtd/spring-beans.dtd”>

&ltbeans>

&ltbean id=”invoiceListener” class=”jms.InvoiceMDB” />

&ltbean id=”jndiTemplate” class=”org.springframework.jndi.JndiTemplate”>

&ltproperty name=”environment”>

&ltprops>

&ltprop key=”java.naming.factory.initial”&gtweblogic.jndi.WLInitialContextFactory</prop>

&ltprop key=”java.naming.provider.url”&gtt3://localhost:7001</prop>

</props>

</property>

</bean>

&ltbean id=”queueConnectionFactory” class=”org.springframework.jndi.JndiObjectFactoryBean”>

&ltproperty name=”jndiTemplate”>

&ltref bean=”jndiTemplate” />

</property>

&ltproperty name=”jndiName”>

&ltvalue&gtjms/connectionFactory</value>

</property>

</bean>

&ltbean id=”jmsDestinationResolver” class=”org.springframework.jms.support.destination.JndiDestinationResolver”>

&ltproperty name=”jndiTemplate”>

&ltref bean=”jndiTemplate” />

</property>

&ltproperty name=”cache”>

&ltvalue&gttrue</value>

</property>

</bean>

&ltbean id=”invoiceQueueTemplate” class=”org.springframework.jms.core.JmsTemplate”>

&ltproperty name=”connectionFactory”>

&ltref bean=”queueConnectionFactory” />

</property>

&ltproperty name=”destinationResolver”>

&ltref bean=”jmsDestinationResolver” />

</property>

</bean>

&ltbean id=”jmsInvoiceSender” class=”jms.InvoiceQueueSender”>

&ltproperty name=”jmsTemplate”>

&ltref bean=”invoiceQueueTemplate” />

</property>

</bean>

&ltbean id=”invoiceQueue” class=”org.springframework.jndi.JndiObjectFactoryBean”>

&ltproperty name=”jndiTemplate”>

&ltref bean=”jndiTemplate” />

</property>

&ltproperty name=”jndiName”>

&ltvalue&gtjms/testQueue</value>

</property>

</bean>

&ltbean id=”Invoicelistener” class=”org.springframework.jms.listener.DefaultMessageListenerContainer”>

&ltproperty name=”concurrentConsumers” value=”5″ />

&ltproperty name=”connectionFactory” ref=”queueConnectionFactory” />

&ltproperty name=”destination” ref=”invoiceQueue” />

&ltproperty name=”messageListener” ref=”invoiceListener” />

</bean>

</beans>

  • The last element is the servlet to send messages.

package techalpine.jms;

import java.io.IOException;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import org.springframework.web.context.WebApplicationContext;

import org.springframework.web.context.support.WebApplicationContextUtils;

public class InvoiceSenderServlet extends javax.servlet.http.HttpServlet

implements javax.servlet.Servlet {

protected void service(HttpServletRequest request,

HttpServletResponse response) throws ServletException, IOException {

WebApplicationContext ctx = WebApplicationContextUtils

.getRequiredWebApplicationContext(this.getServletContext());

InvoiceQueueSender sender = (InvoiceQueueSender) ctx

.getBean(“jmsInvoiceSender”);

sender.sendMesage();

}

}

Now configure the web.xml as shown below and run the servlet

<?xml version=”1.0″ encoding=”UTF-8″?>

&ltweb-app id=”WebApp_ID” version=”2.4″ xmlns=”http://java.sun.com/xml/ns/j2ee” xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xsi:schemaLocation=”http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd”>

&ltdisplay-name>

SpringJMS</display-name>

&ltservlet>

&ltdescription>

</description>

&ltdisplay-name>

InvoiceSenderServlet</display-name>

&ltservlet-name&gtInvoiceSenderServlet</servlet-name>

&ltservlet-class>

jms.InvoiceSenderServlet</servlet-class>

</servlet>

&ltservlet-mapping>

&ltservlet-name&gtInvoiceSenderServlet</servlet-name>

&lturl-pattern>/InvoiceSenderServlet</url-pattern>

</servlet-mapping>

&ltwelcome-file-list>

&ltwelcome-file&gtindex.html</welcome-file>

</welcome-file-list>

&ltlistener>

&ltlistener-class&gtorg.springframework.web.context.ContextLoaderListener</listener-class>

</listener>

</web-app>

The console will display the message as below

Hello JMS







Conclusion

This article explains how JMS can be integrated easily with Spring framework. JMS is one of the most important feature used widely in various applications. So, it is good to learn JMS, and also how it can be used with Spring. As, Spring is the most popular framework in the Java world, it is very important to understand and know the integration details.

Tagged on: ,

Leave a Reply

Your email address will not be published. Required fields are marked *


+ 5 = 12

TechAlpine Books
-----------------------------------------------------------