Steps to integrate Spring & Java Message Service

Spring and JMS

Spring Series – Learn JMS integration by Example

Огляд

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

<боб ID =”paymentService”

клас =”com.springinaction.training.service.PaymentServiceImpl”>

<property name=”jmsTemplate”>

<ref bean=”jmsTemplate”/>

</власність>

<квасоля>

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) {

спробувати {

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

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

} зловити (JMSException ex) {

throw new RuntimeException(ex);

}

}

}

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

&ltbean id=”jndiTemplate” клас =”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://локальний:7001</prop>

</props>

</власність>

</квасоля>

  • 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” клас =”org.springframework.jndi.JndiObjectFactoryBean”>

&ltproperty name=”jndiTemplate”>

&ltref bean=”jndiTemplate” />

</власність>

&ltproperty name=”jndiName”>

&ltvalue&gtjms/connectionFactory</значення>

</власність>

</квасоля>

  • DestinationResolver is used by JmsTemplate to resolve destination names

&ltbean id=”jmsDestinationResolver” клас =”org.springframework.jms.support.destination.JndiDestinationResolver”>

&ltproperty name=”jndiTemplate”>

&ltref bean=”jndiTemplate” />

</власність>

&ltproperty name=”cache”>

&ltvalue&gttrue</значення>

</власність>

</квасоля>

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

&ltbean id=”invoiceQueueTemplate” клас =”org.springframework.jms.core.JmsTemplate”>

&ltproperty name=”connectionFactory”>

&ltref bean=”queueConnectionFactory” />

</власність>

&ltproperty name=”destinationResolver”>

&ltref bean=”jmsDestinationResolver” />

</власність>

</квасоля>

  • Now the Queue configuration is given below

&ltbean id=”invoiceQueue” клас =”org.springframework.jndi.JndiObjectFactoryBean”>

&ltproperty name=”jndiTemplate”>

&ltref bean=”jndiTemplate” />

</власність>

&ltproperty name=”jndiName”>

&ltvalue&gtjms/testQueue</значення>

</власність>

</квасоля>

  • 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” клас =”jms.InvoiceMDB” />

&ltbean id=”jndiTemplate” клас =”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://локальний:7001</prop>

</props>

</власність>

</квасоля>

&ltbean id=”queueConnectionFactory” клас =”org.springframework.jndi.JndiObjectFactoryBean”>

&ltproperty name=”jndiTemplate”>

&ltref bean=”jndiTemplate” />

</власність>

&ltproperty name=”jndiName”>

&ltvalue&gtjms/connectionFactory</значення>

</власність>

</квасоля>

&ltbean id=”jmsDestinationResolver” клас =”org.springframework.jms.support.destination.JndiDestinationResolver”>

&ltproperty name=”jndiTemplate”>

&ltref bean=”jndiTemplate” />

</власність>

&ltproperty name=”cache”>

&ltvalue&gttrue</значення>

</власність>

</квасоля>

&ltbean id=”invoiceQueueTemplate” клас =”org.springframework.jms.core.JmsTemplate”>

&ltproperty name=”connectionFactory”>

&ltref bean=”queueConnectionFactory” />

</власність>

&ltproperty name=”destinationResolver”>

&ltref bean=”jmsDestinationResolver” />

</власність>

</квасоля>

&ltbean id=”jmsInvoiceSender” клас =”jms.InvoiceQueueSender”>

&ltproperty name=”jmsTemplate”>

&ltref bean=”invoiceQueueTemplate” />

</власність>

</квасоля>

&ltbean id=”invoiceQueue” клас =”org.springframework.jndi.JndiObjectFactoryBean”>

&ltproperty name=”jndiTemplate”>

&ltref bean=”jndiTemplate” />

</власність>

&ltproperty name=”jndiName”>

&ltvalue&gtjms/testQueue</значення>

</власність>

</квасоля>

&ltbean id=”Invoicelistener” клас =”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” />

</квасоля>

</боби>

  • The last element is the servlet to send messages.

package techalpine.jms;

Імпорт 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







Висновок

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: ,
============================================= ============================================== Buy best TechAlpine Books on Amazon
============================================== ---------------------------------------------------------------- electrician ct chestnutelectric
error

Enjoy this blog? Please spread the word :)

Follow by Email
LinkedIn
LinkedIn
Share