First, create a producer.
Public class producer {<br/> private jmstemplate; <br/> private destination topic; </P> <p>/** <br/> * Send a JMS message <br/> * @ Param XML message <br/> */<br/> Public void send (string XML) {<br/> jmstemplate. convertandsend (topic, XML); <br/>}</P> <p> Public void setjmstemplate (jmstemplate) {<br/> This. jmstemplate = jmstemplate; <br/>}</P> <p> Public void settopic (destination topic) {<br/> This. topic = topic; <br/>}< br/>}
Another consumer
Public class diyconsumer implements messagelistener {<br/> private logger = logger. getlogger (this. getclass (); </P> <p> Public void onmessage (Message MSG) {<br/> system. out. println (MSG); <br/> string smsg = NULL; <br/> try {<br/> If (MSG instanceof textmessage) {<br/> smsg = (textmessage) MSG ). gettext (); <br/> logger.info ("onmessage:" + smsg ); <br/> // put the code for processing the JMS message here <br/> // convert the received message to XML Format, xmlutil readers can download a file <br/> document DOC = xmlutil. synstring2document (msgxml); <br/> If (Doc = NULL) {<br/> throw new condition entexception ("Doc is null. XML: "+ msgxml); <br/>}< br/> element root = Doc. getrootelement (); <br/> If (! "M ". equals (root. getname () {<br/> throw new condition entexception ("Doc root error. XML: "+ Doc. asxml (); <br/>}< br/> // The result is printed only. <br/> system. out. println (root. attributevalue ("T"); <br/> system. out. println (root. attributevalue ("A"); <br/> system. out. println (root. attributevalue ("St"); <br/> element Eelement = root. element ("e") <br/> // subsequent work will not be described <br/>} else {<br/> logger. error ("MSG: MSG instance Of textmessage return false! "); <Br/>}< br/>} catch (exception e) {<br/> logger. error (E + "MSG:" + smsg); <br/>}< br/>}
Configuration of main applicationContext-jms.xml
<? 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" xmlns: AMQ = "http://activemq.org/config/1.0" <br/> xsi: schemalocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://activemq.org/config/1.0 Co., http://activemq.org/config/1.0."> </P> <p> <! -- A pooling based JMS provider --> <br/> <! -- The client uses the normal transmission mode: TCP: // localhost: 61616 <br/> note that the borkerurl of the listener uses the Failover Transmission Mode: <br/> failover :( TCP: // localhost: 61616 )? Initialreconnectdelay = 100 & amp; <br/> maxreconnectattempts = 5 <br/> failover transport is a reconnection mechanism for reliable transmission. Once the activemq broker is interrupted, the listener will automatically try the connection every ms until the connection is successful or five retry attempts fail. <Br/> failover also supports multiple borkers to provide services at the same time, which can increase system fault tolerance while implementing load balancing. Format: failover :( uri1,..., urin )? Transportoptions <br/> --> </P> <p> <bean id = "jmsfactory" class = "org. apache. activemq. pool. pooledconnectionfactory "Destroy-method =" stop "> <br/> <property name =" connectionfactory "> <br/> <Bean class =" org. apache. activemq. activemqconnectionfactory "> <br/> <property name =" brokerurl "value =" TCP: // localhost: 61616 "/> <br/> <property name =" username "value =" "/> <br/> <property name =" password "value =" "/> <br /> </Bean> <br/> </property> <br/> </bean> </P> <p> <! -- Spring JMS template --> <br/> <! -- Spring JMS template <br/> Once configured, jmstemplate instances are thread-safe. To be clear, jmstemplate <br/> is stateful because it maintains the onfactory reference, but this state is not session state. <Br/> --> </P> <p> <bean id = "myjmstemplate" class = "org. springframework. JMS. core. jmstemplate "> <br/> <property name =" connectionfactory "ref =" jmsfactory "/> <br/> <! -- The jms api can be sent in either of the following ways: the sending mode, the <br/> priority, and the survival time are used as QoS parameters. <br/> the default value is {deliverymode: 2 (1), priority: 4, timetolive: 0} <br/> another method that uses the default value without QoS parameters. <Br/> <property name = "explicitqosenabled" value = "true"/> <br/> <property name = "deliverymode" value = "2"/> <br/> <property name = "Priority" value = "4"/> <br/> <property name = "timetolive" value = "1000"/> <br/> <property name = "receivetimeout" value = "1000"/> <br/> --> </P> <p> </bean> </P> <p> <! -- Create a queue. Here you can also create a topic --> <br/> <bean id = "diyqueue" class = "org. apache. activemq. command. activemqqueue "> <br/> <constructor-Arg Index =" 0 "value =" user_web_queue "/> <br/> </bean> </P> <p> <! -- Send message --> <br/> <bean id = "ossrequest" class = "com. JMS. producer "> <br/> <property name =" jmstemplate "ref =" myjmstemplate "/> <br/> <property name =" topic "ref =" diyqueue "/> <br/> </bean> </P> <p> <! -- Messages sent from the Web Background --> <br/> <bean id = "diyconsumer" class = "com. JMS. diyconsumer "/> </P> <p> <! -- Message listening --> <br/> <bean id = "listenercontainerqueue" <br/> class = "org. springframework. JMS. listener. defaultmessagelistenercontainer "> <br/> <property name =" concurrentconsumers "value =" 5 "/> <br/> <property name =" destination "ref =" diyqueue "/> <br/> <property name = "connectionfactory" ref = "jmsfactory"/> <br/> <property name = "destinationname" value = "user_web_queue"/> <br/> <property name = "messagelistener" ref = "diyconsumer"/> <br/> </bean> <br/> </beans>
Test code
Public class jmstest {<br/> @ test <br/> Public void testjms () throws exception {<br/> applicationcontext context = new classpathxmlapplicationcontext (New String [] {"spring/applicationContext-jms.xml"}); </P> <p> producer = (producer) context. getbean ("ossrequest"); <br/> producer. send ("<m T =/" 8600/"A =/" 10000001/"St =/" 15:42:39/">/N" + <br/> "/T <E id =/"10000/" type =/"1000/"/>/N "+ <br/>" </m> "); <br/> // consume the message <br/>}< br/>}