Steps to integrate Spring & Java Message Service

Spring and JMS

Serie Spring - Scopri JMS integrazione con l'esempio,,en,In questo articolo andremo a coprire l'integrazione JMS con il lavoro telaio Primavera,,en,servizio di messaggistica Java viene utilizzato per scopi di messaggistica per la maggior parte del Java e applicazioni J2EE,,en,Spring fornisce un'astrazione sullo strato di JMS che rende semplice per accedere a una coda di messaggi o un argomento,,en,astrattamente definito come una destinazione,,en,e pubblicare i messaggi alla destinazione,,en,Passi da seguire,,en,JmsTemplate è la classe principale da utilizzare per la messaggistica purport in primavera quadro,,en,La proprietà jmsTemplate sarà cablato con un'istanza di org.springframework.jms.core.JmsTemplate usando iniezione setter,,en,il sendSettlementMessage,,en,il metodo è usato in invio del JmsTemplate,,en,il metodo per inviare il messaggio,,en,Questo metodo richiede un'istanza di org.springframework.jms.core.MessageCreator,,en

Panoramica

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

Seguente frammento di codice mostra la parte di cablaggio della configurazione,,en,paymentService,,en,com.springinaction.training.service.PaymentServiceImpl,,en,jmsTemplate,,no,Di seguito sono riportati i passi per integrare la primavera e JMS utilizzando qualsiasi application server,,en,Configurare le seguenti risorse JMS all'interno del server di applicazione,,en,ConnectionFactory,,en,jms / ConnectionFactory e coda Destinazione,,en,etc / test,,et,Creare un'applicazione Web dinamica del server di applicazione,,en,Importa spring.jar,,en,Commons-logging.jar e log4j-1.2.15.jar,,en,Creare la classe InvoiceQueueSender nel pacchetto,,en,eccetera,,et,L'InvoiceQueueSender classe viene utilizzato per inviare messaggi,,en,Il JMSTemplate viene iniettato in InvoiceQueueSender utilizzando contenitore IOC,,en,techalpine.jms pacchetto,,en,importazione javax.jms.JMSException,,en,importazione javax.jms.Message,,en,importazione javax.jms.Session,,en,importazione org.springframework.jms.core.JmsTemplate,,en,importazione org.springframework.jms.core.MessageCreator,,en

<bean id =”paymentService”

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

<property name =”jmsTemplate”>

<ref bean=”jmsTemplate”/>

</proprietà>

<fagiolo>

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

provare {

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

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

} prendere (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</puntello>

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

</props>

</proprietà>

</fagiolo>

  • JndiObjectFactoryBean viene utilizzato per cercare l'oggetto JNDI all'avvio e memorizzare nella cache è,,en,Questa interfaccia viene utilizzata per configurare produzione connessioni,,en,QueueConnectionFactory,,en,org.springframework.jndi.JndiObjectFactoryBean,,en,ltref fagiolo =,,en,JndiName,,lt,ltvalue,,en,gtjms / ConnectionFactory,,en,DestinationResolver viene utilizzato da JmsTemplate per risolvere i nomi di destinazione,,en,jmsDestinationResolver,,da,org.springframework.jms.support.destination.JndiDestinationResolver,,en,nascondiglio,,en,gttrue,,af,Ora il JMSTemplate viene utilizzato per inviare messaggi,,en,Di seguito è riportato un frammento di codice,,en,invoiceQueueTemplate,,en,org.springframework.jms.core.JmsTemplate,,en,ConnectionFactory,,en,destinationResolver,,en,Ora la configurazione della coda è riportata qui sotto,,en,invoiceQueue,,en,gtjms / test,,en,Ora, dopo la configurazione di tutti i componenti necessari al contenuto finale sarà il seguente,,en,fagioli DOCTYPE PUBBLICA,,en,SPRING // DTD BEAN // EN,,en,www.springframework.org/dtd/spring-beans.dtd,,en,ltbeans,,en,invoiceListener,,en,jms.InvoiceMDB,,en,jmsInvoiceSender,,en. This interface is used to configure connection factory

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

&ltproperty name=”jndiTemplate”>

&ltref bean=”jndiTemplate” />

</proprietà>

&ltproperty name=”jndiName”>

&ltvalue&gtjms/connectionFactory</valore>

</proprietà>

</fagiolo>

  • 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” />

</proprietà>

&ltproperty name=”cache”>

&ltvalue&gttrue</valore>

</proprietà>

</fagiolo>

  • 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” />

</proprietà>

&ltproperty name=”destinationResolver”>

&ltref bean=”jmsDestinationResolver” />

</proprietà>

</fagiolo>

  • Now the Queue configuration is given below

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

&ltproperty name=”jndiTemplate”>

&ltref bean=”jndiTemplate” />

</proprietà>

&ltproperty name=”jndiName”>

&ltvalue&gtjms/testQueue</valore>

</proprietà>

</fagiolo>

  • 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</puntello>

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

</props>

</proprietà>

</fagiolo>

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

&ltproperty name=”jndiTemplate”>

&ltref bean=”jndiTemplate” />

</proprietà>

&ltproperty name=”jndiName”>

&ltvalue&gtjms/connectionFactory</valore>

</proprietà>

</fagiolo>

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

&ltproperty name=”jndiTemplate”>

&ltref bean=”jndiTemplate” />

</proprietà>

&ltproperty name=”cache”>

&ltvalue&gttrue</valore>

</proprietà>

</fagiolo>

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

&ltproperty name=”connectionFactory”>

&ltref bean=”queueConnectionFactory” />

</proprietà>

&ltproperty name=”destinationResolver”>

&ltref bean=”jmsDestinationResolver” />

</proprietà>

</fagiolo>

&ltbean id=”jmsInvoiceSender” class =”jms.InvoiceQueueSender,,en,Invoicelistener,,en,org.springframework.jms.listener.DefaultMessageListenerContainer,,en,concurrentConsumers,,en,destinazione,,en,messageListener,,en,L'ultimo elemento è il servlet per inviare messaggi,,en,importazione javax.servlet.ServletException,,en,importazione javax.servlet.http.HttpServletRequest,,en,importazione javax.servlet.http.HttpServletResponse,,en,importazione org.springframework.web.context.WebApplicationContext,,en,org.springframework.web.context.support.WebApplicationContextUtils importazione,,en,public class InvoiceSenderServlet estende javax.servlet.http.HttpServlet,,en,implementa javax.servlet.Servlet,,en,servizio di vuoto protetto,,en,getta ServletException,,en,WebApplicationContext CTX = WebApplicationContextUtils,,en,getRequiredWebApplicationContext,,en,this.getServletContext,,en,InvoiceQueueSender mittente =,,en,InvoiceQueueSender,,en,CTX,,en,getBean,,en,sender.sendMesage,,en,Ora configurare il web.xml come illustrato di seguito ed eseguire il servlet,,en,ltweb-app id =,,en,WebApp_ID,,en,version =,,en,java.sun.com/xml/ns/j2ee,,en,java.sun.com/xml/ns/j2ee http,,en”>

&ltproperty name=”jmsTemplate”>

&ltref bean=”invoiceQueueTemplate” />

</proprietà>

</fagiolo>

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

&ltproperty name=”jndiTemplate”>

&ltref bean=”jndiTemplate” />

</proprietà>

&ltproperty name=”jndiName”>

&ltvalue&gtjms/testQueue</valore>

</proprietà>

</fagiolo>

&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” />

</fagiolo>

</fagioli>

  • The last element is the servlet to send messages.

package techalpine.jms;

importazione 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,,en,ltdisplay-nome,,en,SpringJMS,,en,nome da visualizzare,,en,ltservlet,,no,ltdescription,,en,InvoiceSenderServlet,,en,ltservlet-nome,,en,gtInvoiceSenderServlet,,da,servlet-name,,en,ltservlet-class,,en,jms.InvoiceSenderServlet,,en,servlet-class,,en,servlet,,en,ltservlet-mapping,,en,lturl-pattern,,en,url-pattern,,en,servlet-mapping,,en,ltwelcome-file-list,,en,ltwelcome file,,en,gtindex.html,,en,benvenuto file,,en,welcome-file-list,,en,ltlistener,,no,ltlistener-class,,en,gtorg.springframework.web.context.ContextLoaderListener,,en,La console visualizzerà il messaggio come di seguito,,en,Questo articolo spiega come JMS possono essere integrati facilmente con framework Spring,,en,JMS è una delle caratteristiche più importanti ampiamente utilizzato in varie applicazioni,,en,è bene imparare JMS,,en,e anche come esso può essere utilizzato con la Primavera,,en,La primavera è il quadro più popolare nel mondo Java,,en,è molto importante per capire e conoscere i dettagli di integrazione,,en”>

&ltdisplay-name>

SpringJMS</display-name>

&ltservlet>

&ltdescription>

</descrizione>

&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







Conclusione

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.

Taggato su: ,
============================================= ============================================== Acquista i migliori libri di tecnologia su Amazon,en,ELETTRICI CT COSTRALETTRICO,en
============================================== ---------------------------------------------------------------- electrician ct chestnutelectric
error

Enjoy this blog? Please spread the word :)

Follow by Email
LinkedIn
LinkedIn
Share