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