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

<пасуљ? ид =”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) {

пробати {

Систем.оут.принтлн(((TextMessage) message).getText());

Систем.оут.принтлн(“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”

“хттп://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;

увоз јава.ио.ИОЕкцептион;

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=”хттп://java.sun.com/xml/ns/j2ee” xmlns:xsi=”хттп://www.w3.org/2001/XMLSchema-instance” xsi:schemaLocation=”хттп://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.

Таггед на: ,
============================================= ============================================== Buy best TechAlpine Books on Amazon
============================================== ---------------------------------------------------------------- electrician ct chestnutelectric
error

Enjoy this blog? Please spread the word :)

Follow by Email
LinkedIn
LinkedIn
Share