Spring Consolidated JMS-based on ACTIVEMQ implementation

Source: Internet
Author: User
Tags message queue

I. Opening language

Following the first experience of Apache ACTIVEMQ, I've been reviewing spring's stuff recently, so this article uses spring to integrate JMS.


Two. Environmental preparedness

1. ActiveMQ5.2.0 (Activemq-all-5.2.0.jar)

2. spring2.5 (Spring.jar)

3. JavaEE5

4. JDK1.6


Note: Please start activemqserver before testing


Three. The code test (peer)

1. Msgsender: Message producer

/** * Message sender */public class Msgsender {public static void main (string[] args) throws Exception {//load XML and CR Eate Bean Factoryapplicationcontext ctx = new Classpathxmlapplicationcontext ("/applicationcontext.xml");//Get Jmstemplate object from spring containerjmstemplate jmstemplate = (jmstemplate) ctx.getbean ("jmstemplate");//Get  Destination object from spring containerdestination Destination = (Destination) ctx.getbean ("Destination");//Send MSG to ActiveMQ serverjmstemplate.send (destination, new Messagecreator () {textmessage message = Null;public message CreateMessage (Session session) {try {String str = "Hello activemq!"; message = Session.createtextmessage (str); System.out.println ("Send:" + str);} catch (Exception e) {throw new RuntimeException ("Error happens ...", e);} return message;}});}

2. Msgreceiver: Message Consumers

/** * Message receiver */public class Msgreceiver {public static void main (string[] args) throws Exception {//load XML a D Create bean Factoryapplicationcontext CTX = new Classpathxmlapplicationcontext ("/applicationcontext.xml");//Get Jmstemplate object from spring containerjmstemplate jmstemplate = (jmstemplate) ctx.getbean ("jmstemplate");//Get Destination object from spring containerdestination Destination = (Destination) ctx.getbean ("Destination"), while (true) {//Receive MSG from ActiveMQ servertextmessage txtmsg = (textmessage) jmstemplate.receive (destination); if (null! = Txtmsg {System.out.println ("Receive:" + txtmsg.gettext ());} Else{break;}}}}

3. Configure Applicationcontext.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-2.5.xsd http ://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd " ><!--config JMS connection factory--><bean id= "ConnectionFactory" class= " Org.apache.activemq.spring.ActiveMQConnectionFactory "><property name=" Brokerurl "value=" tcp://localhost : 61616 "/></bean><!--config JMS template--><bean id=" jmstemplate "class=" Org.springframework.jms.core.JmsTemplate "><property name=" ConnectionFactory "ref=" ConnectionFactory "/> </bean><!--Config message send destination (queue)--><bean id= "Destination" class= " Org.apache.activemq.command.ActiveMQQueue "><!--Set the name of message queue--><constructor-arg index= "0" value= "myqueue"/></bean></beans>

4 . Source code: http://download.csdn.net/detail/zdp072/7422385

Spring Consolidated JMS-based on ACTIVEMQ implementation

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.