Integrate activemq with spring to debug JMS

Source: Internet
Author: User
This article is a simple integration method for spring and activemq. It is only used for reference learning and focuses on understanding concepts.
1: JMS is a set of standard APIs developed by Sun to access message-oriented middleware (MOM). The mom component used in this article is activemq. You can go to [url] routing,
It is very simple to use, extract and directly run the activemq. BAT file under the D:/activemq-4.1.1/bin directory, you can start the service. In addition, we only perform simple tests, so we do not need to configure the content related to JNDI. Service Startup
Then we can see that the corresponding port is started, so that my mom component is ready... 2: Next, we will create a new web project (myeclipse is used) and import related packages. We recommend that you do not use the spring2.0 package that comes with myeclipse, this is because the debugging has been performed on several projects for a long time, because the package has a problem. Haha.
Import spring2.0.jar, apache-activemq-4.1.1.jar, commons-pool-1.2.jar, long4j. jar, commons-logging-1.1.jar file to the lib directory. Next, create two XML files under the WEB-INF <? XML version = "1.0" encoding = "UTF-8"?> <Br/> <beans xmlns = "http://www.springframework.org/schema/beans" <br/> xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" <br/> xmlns: Tx = "http://www.springframework.org/schema/tx" <br/> xsi: schemalocation = "<br/> [url] routing [url] routing <br/> [url] http://www.springframework.org/schema/tx#/url] [url] routing"> </P> <p> <bean id = "connectionfactory" class = "org. apache. activemq. pool. pooledconnectionfactory "> <br/> <property name =" connectionfactory "> <br/> <Bean class =" org. apache. activemq. activemqconnectionfactory "> <br/> <property name =" brokerurl "> <br/> <value> TCP: // localhost: 61616 </value> <br/> </property> <br/> </bean> </P> <p> <bean id = "DEST" class = "org. apache. activemq. command. activemqqueue "> <br/> <constructor-Arg value =" mydest "/> <br/> </bean> </P> <p> <bean id =" jmstemplate" class = "org. springframework. JMS. core. jmstemplate "> <br/> <property name =" connectionfactory "ref =" connectionfactory "> </property> <br/> <property name =" defaultdestination "ref =" DEST" /> <br/> </bean> </P> <p> <bean id = "messagesender" class = "com. bo. impl. messagesender "> <br/> <property name =" jmstemplate "ref =" jmstemplate "> </property> <br/> </bean> <br/> </beans> </P> <p> <? XML version = "1.0" encoding = "UTF-8"?> <Br/> <beans xmlns = "[url] http://www.springframework.org/schema/beans#/url]" <br/> xmlns: xsi = "[url] Comment" <br/> xmlns: tx = "[url] http://www.springframework.org/schema/tx#/url]" <br/> xsi: schemalocation = "<br/> [url] routing [url] routing <br/> [url] http://www.springframework.org/schema/tx#/url] [url] routing"> </P> <p> <bean id = "DEST" class = "org. apache. activemq. command. activemqqueue "> <br/> <constructor-Arg value =" mydest "> </constructor-Arg> <br/> </bean> </P> <p> <Bean id = "connectionfactory" class = "org. apache. activemq. pool. pooledconnectionfactory "> <br/> <property name =" connectionfactory "> <br/> <Bean class =" org. apache. activemq. activemqconnectionfactory "> <br/> <property name =" brokerurl "value =" TCP: // localhost: 61616 "/> <br/> </bean> <br/> </property> <br/> </bean> </P> <p> <bean id =" jmstemplate "class =" org. springframework. JMS. core. jmstemplate "> <br/> <property name =" connectionfactory "ref =" connectionfactory "> </property> <br/> <property name =" defaultdestination "ref =" DEST"> </property> <br/> </bean> </P> <p> <bean id = "messagereceiver" class = "com. bo. impl. messagereceiver "> <br/> <property name =" jmstemplate "ref =" jmstemplate "> </property> <br/> </bean> <br/> </beans> </P> <p>3: Message sending class: Public class messagesender extends jmsgatewaysupport {<br/> Public void sendtextmsg (final string MSG) {<br/> This. getjmstemplate (). send (New messagecreator () {<br/> // a message object is created here, and various attributes of the object can be defined. <br/> private message; <br/> public message createmessage (session) throws jmsexception {<br/> message = session. createtextmessage (MSG); </P> <p> message. setstringproperty ("jmsxuserid", "123456789"); // user code of the message <br/> message. setstringproperty ("jmsxapp1id", "001002"); // the application code of the message </P> <p> return message; <br/>}< br/>}); <br/>}< br/>4: message receiving class: Public class messagereceiver extends jmsgatewaysupport {</P> <p> Public void receivertextmsg () {<br/> textmessage textmsg = (textmessage) This. getjmstemplate (). receive (); </P> <p> try {<br/> // attribute definitions in message headers <br/> system. out. println ("message encoding:" + textmsg. getjmsmessageid (); <br/> system. out. println ("target object:" + textmsg. getjmsdestination (); <br/> system. out. println ("message mode:" + textmsg. getjmsdeliverymode () ); // The message mode is divided into the persistent mode and the non-persistent mode. The default mode is the non-persistent mode (2) </P> <p> long sendtime = textmsg. getjmstimestamp (); <br/> date = new date (sendtime); <br/> dateformat F = new simpledateformat ("yyyy-mm-dd hh: mm: SS "); <br/> string temp = f. format (date); </P> <p> system. out. println ("message sending time:" + temp); <br/> system. out. println ("message expiration time:" + textmsg. getjmsexpiration (); // The value 0 indicates that the message will never expire. <br/> system. out. println ("message Optimization Level 1: "+ textmsg. getjmspriority (); // priority 0 ~ 9, 0 indicates the lowest <br/> system. out. println ("associated encoding:" + textmsg. getjmscorrelationid (); </P> <p> system. out. println ("reply message address:" + textmsg. getjmsreplyto (); // address of the reply message (destination type), set by the sender <br/> system. out. println ("Message Type:" + textmsg. getjmstype (); // JMS does not use this field. The general type is defined by the user. <br/> system. out. println ("accepted:" + textmsg. getjmsredelivered (); // if it is true, it indicates that the client has received the message but has not signed for it. </P> <p> // message properties (properties) <br/> system. out. println ("User Code:" + textmsg. getstringproperty ("jmsxuserid"); <br/> system. out. println ("application code:" + textmsg. getstringproperty ("jmsxapp1id"); <br/> system. out. println ("number of attempts to send messages:" + textmsg. getstringproperty ("jmsxdeliverycount"); </P> <p> // content transmitted in the message body <br/> system. out. println ("message content:" + textmsg. gettext (); </P> <p >}catch (jmsexception e) {<br/> E. printstacktrace (); <br/>}catch (exception e) {<br/> E. printstacktrace (); <br/>}</P> <p>5: test the message sending class:
Public class testmessagesender {<br/> Private Static applicationcontext CTX = NULL; </P> <p> static {<br/> CTX = new filesystemxmlapplicationcontext (New String [] {"webroot/jms_sender.xml "}); <br/>}</P> <p> Public static void senttextmsg () {<br/> messagesender = (messagesender) CTX. getbean ("messagesender"); <br/> messagesender. sendtextmsg ("This world is really helpless! "); <Br/>}</P> <p> Public static void main (string [] ARGs) {<br/> senttextmsg (); <br/>}< br/>6: test the message receiving class: Public class testmessagereceiver {</P> <p> Private Static applicationcontext CTX = NULL; <br/> static {<br/> CTX = new filesystemxmlapplicationcontext (New String [] {"webroot/jms_receiver.xml "}); <br/>}</P> <p> Public static void gettextmsg () {<br/> messagereceiver messagesender = (messagereceiver) CTX. getbean ("messagereceiver"); <br/> messagesender. receivertextmsg (); <br/>}</P> <p> Public static void main (string [] ARGs) {<br/> gettextmsg (); <br/>}< br/>7: Test Result: Message code: ID: hc-369a3f54b2b0-1440-1224731999968-1: 0: 1: 1: 1
Target object: queue: // mydest
Message mode: 2
Message sending time: 11:20:00
Message expiration time: 0
Message priority: 4
Associated encoding: NULL
Reply Message Address: NULL
Message Type: NULL
Accepted or not: false
User ID: 123456789
Application code: 001002
Number of attempts to send messages: 1

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.