A small example of sending and receiving JMS messages in spring

Source: Internet
Author: User

Spring provides jmstemplate a thin layer of encapsulation of native JMS APIs, which is very convenient to use.

I use the JMS message Agent plugin is Apache ACTIVEMQ, it is recommended to install the latest version, because I previously installed the old version, a variety of incompatible, various bugs, The latest version of the Activemq-all-5.9.1.jar bag has a slf4j.impl bag, before it was this hole ... Add this jar package to the Lib directory, there will be no various ClassNotFound exception and class conflict of the bug.

Download ACTIVEMQ unzip and then run the Activemq.bat under the bin to run. You can visit http://127.0.0.1:8161/to see if the service is open properly.

The default account password is Admin/admin

The following example is a post Office system, including the front and rear two subsystems, the current station received an e-mail message, it sends this message to the background, while sending a JMS message to the background subsystem, reminding the new message.

There are two types of message communication models for JMS:

1), point-to-point

2), Pub/sub

Here we are of course the first model to use.

First we create a mail class:

Package Com.apress.springrecipes.post;public class Mail {private string mailid;private string Country;private double Weight;public Mail () {}public mail (string mailid, String country, double weight) {this.mailid = Mailid;this.country = Coun Try;this.weight = weight;} Public String Getmailid () {return mailid;} public void Setmailid (String mailid) {this.mailid = Mailid;} Public String Getcountry () {return country;} public void Setcountry (String country) {this.country = country;} Public double Getweight () {return weight;} public void Setweight (double weight) {this.weight = weight;}}

A foreground subsystem interface:

Package Com.apress.springrecipes.post;public interface Frontdesk {public void SendMail (mail mail);}

and its implementation class:

package com.apress.springrecipes.post;import javax.jms.destination;import  javax.jms.jmsexception;import javax.jms.mapmessage;import javax.jms.message;import  javax.jms.session;import org.springframework.beans.factory.annotation.autowired;import  org.springframework.jms.core.jmstemplate;import org.springframework.jms.core.messagecreator;public  class frontdeskimpl2 implements frontdesk {@Autowiredprivate  JmsTemplate  Jmstemplate, @Autowiredprivate  Destination destination; @Overridepublic  void sendmail (final  mail mail)  {jmstemplate.send (Destination,new messagecreator ()  {@Overridepublic   Message createmessage (session session)  throws jmsexception {mapmessage message  = session.createmapmessage (); Message.setstring ("Mailid",  mail.getmailid ()); Message.setString (" Country ",  mail.getcountry ()); Message.setdouble (" Weight ", Mail.getweight ()); return message;}});}} 

Interface for a backend subsystem:

Package Com.apress.springrecipes.post;public interface BackOffice {public Mail receivemail ();}

and its implementation class:

package com.apress.springrecipes.post;import javax.jms.destination;import  javax.jms.jmsexception;import javax.jms.mapmessage;import  org.springframework.beans.factory.annotation.autowired;import org.springframework.jms.core.jmstemplate; Import org.springframework.jms.support.jmsutils;public class backofficeimpl2 implements  backoffice {@Autowiredprivate  JmsTemplate jmsTemplate; @Autowiredprivate  destination  destination; @Overridepublic  mail receivemail ()  {MapMessage message =  ( Mapmessage)  jmstemplate.receive (destination);try {if  (message == null)  {return  null;} Mail mail = new mail (); Mail.setmailid (message.getstring ("Mailid")); Mail.setCountry ( Message.getstring ("country")); Mail.setweight (message.getdouble ("weight")); return mail;}  catch  (jmsexception e)  {throw jmsutils.convertjmsaccessexception (e);}}

The XML configuration file for the foreground subsystem bean-front.xml:

<?xml version= "1.0"  encoding= "UTF-8"? ><beans xmlns= "http://www.springframework.org/ Schema/beans "xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance "xmlns:context="/HTTP/ Www.springframework.org/schema/context "xsi:schemalocation=" http://www.springframework.org/schema/beans  Http://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context-3.0.xsd "><context:annotation-config/ ><bean id= "Frontdesk" class= "COM.APRESS.SPRINGRECIPES.POST.FRONTDESKIMPL2"  /><bean  Id= "ConnectionFactory" class= "Org.apache.activemq.ActiveMQConnectionFactory" ><property name= " Brokerurl " value=" tcp://localhost:61616 "/></bean><bean id=" Destination "class=" Org.apache.activemq.command.ActiveMQQueue "><constructor-arg value=" Mail.queue " /></bean ><bean id= "Jmstemplate" class= "Org.springframewoRk.jms.core.JmsTemplate "><property name=" ConnectionFactory " ref=" ConnectionFactory "/></ Bean></beans>

XML configuration file for the background subsystem bean-back.xml:

<?xml version= "1.0"  encoding= "UTF-8"? ><beans xmlns= "http://www.springframework.org/ Schema/beans "xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance "xmlns:context="/HTTP/ Www.springframework.org/schema/context "xsi:schemalocation=" http://www.springframework.org/schema/beans  Http://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context-3.0.xsd "> <context: Annotation-config /><bean id= "BackOffice" class= "COM.APRESS.SPRINGRECIPES.POST.BACKOFFICEIMPL2"  /><bean id= "ConnectionFactory" class= "Org.apache.activemq.ActiveMQConnectionFactory" >< Property name= "Brokerurl"  value= "tcp://localhost:61616"  /></bean><bean id= " Destination "class=" Org.apache.activemq.command.ActiveMQQueue "><constructor-arg value=" Mail.queue "  /></bean><bean id= "Jmstemplate" Class= "Org.springframework.jms.core.JmsTemplate" ><property name= "ConnectionFactory"  ref= " ConnectionFactory " /><property name=" ReceiveTimeout " value=" 10000 " /></bean ></beans>

Two test classes, one to send messages, and one to accept messages.

Package Com.apress.springrecipes.post;import Org.springframework.context.applicationcontext;import Org.springframework.context.support.classpathxmlapplicationcontext;public class Frontdeskmain {public static void Main (string[] args) {ApplicationContext context = new Classpathxmlapplicationcontext ("Beans-front.xml"); Frontdesk Frontdesk = (frontdesk) context.getbean ("Frontdesk"), Frontdesk.sendmail (New Mail ("1234", "US", 1.5));}}

Package Com.apress.springrecipes.post;import Org.springframework.context.applicationcontext;import Org.springframework.context.support.classpathxmlapplicationcontext;public class Backofficemain {public static void Main (string[] args) {ApplicationContext context = new Classpathxmlapplicationcontext ("Beans-back.xml"); BackOffice BackOffice = (BackOffice) context.getbean ("BackOffice"); Mail mail = Backoffice.receivemail (); System.out.println ("Mail #" + mail.getmailid () + "received");}}

After you run Frontdeskmain, you can simply visit http://127.0.0.1:8161/admin/queueGraph.jsp and http://127.0.0.1:8161/admin/queues.jsp to see what happens when you send a message. Then run Backofficemain to observe the changes of these two pages.


A small example of sending and receiving JMS messages in spring

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.