Spring provides a SPRING-JMS module that integrates JMS into the spring container and provides a Jmstemplate template class to manipulate JMS, similar to the integrated JDBC database operation.
First, we create a new applicationcontext-qpid.xml to do the Qpid integration operation.
<?xml version= "1.0" encoding= "UTF-8"?><beans xmlns="Http://www.springframework.org/schema/beans"xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation="Http://www.springframework.org/schema/beans http://www.springframework.org/ Schema/beans/spring-beans.xsd "> <!--the JMS tool class provided by spring, which can send messages, receive, and so on <bean id= "jmstemplate" class=" Org.springframework.jms.core.JmsTemplate "> < property name="ConnectionFactory" ref="Jqpidconnectionfactory "/> </Bean> <!--linked to the Java broker Server - <bean id= "jqpidconnectionfactory" class=" Org.apache.qpid.client.AMQConnectionFactory "> <constructor-arg Index="0" value="Amqp://guest:[email protected ]/?brokerlist= ' tcp://localhost:5672 ' " /> </Bean> <!--cache session, guaranteed single link-- <bean id= "jcachingconnectionfactory" class=" Org.springframework.jms.connection.CachingConnectionFactory "> < property name= "targetconnectionfactory" ref=" Jqpidconnectionfactory " /> < property name="Sessioncachesize" value="1" /> < property name="Reconnectonexception" value="true" /> </Bean> <!--subscription path -- <bean id= "jdestination" class=" Org.apache.qpid.client.AMQAnyDestination "> <constructor-arg Index= "0" value="addr:message_queue; {create:always} " /> </Bean> <!--receive event handlers -- <bean id="Jreceiver" class="Com.liuxg.qpid.receiver.MsgReceiver" /> <!--transaction processing -- <bean id= "TransactionManager" class=" Org.springframework.transaction.jta.JtaTransactionManager "/> <!--default message listener -- <bean id= "devicestatuscontainer"class=" Org.springframework.jms.listener.DefaultMessageListenerContainer "> < property name= "connectionfactory" ref=" Jcachingconnectionfactory " /> < property name= "exceptionlistener" ref=" Jcachingconnectionfactory " /> < property name="MessageListener" ref="Jreceiver" /> < property name="Destination" ref="jdestination" /> < property name= "TransactionManager" ref=" TransactionManager "/> </Bean></Beans>
Then you need to add the applicationcontext-qpid.xml to the contextconfiglocation of Web. xml
<context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:META-INF/applicationContext*.xml</param-value></context-param>
Then, on the sending side, you can use the template class to send messages
@Service Public class senderservice { @AutowiredJmstemplate jmstemplate;@AutowiredAmqanydestination devicestatusdestination;/** * Send a message * / Public void sendmsg() {Jmstemplate.send (devicestatusdestination,NewMessagecreator () {@Override PublicMessageCreateMessage(Session session)throwsjmsexception {returnSession.createtextmessage ("Hello world!"); } }); }}
When sent here, you can take advantage of the spring's message converter, which supports string and TextMessage, byte[] and bytesmesssage, as well as Java.util.Map and mapmessage conversions, such as the following
Public voidSendwithconversion () {Map Map = NewHashMap ();Map.Put"Name","Mark");Map.Put"Age",New Integer( -)); Jmstemplate.Convertandsend ("Testqueue",Map,NewMessagepostprocessor () { PublicMessage postprocessmessage (Message message) throws JMSException {message.Setintproperty ("AccountID",1234); Message.Setjmscorrelationid ("123-00001");returnMessage } });}
Finally on the receiving end, you can define a class implementation sessionawaremessagelistener or Devicestatusreceiver, where we implement Sessionawaremessagelistener
publicclass MsgReceiver implements SessionAwareMessageListener<Message> { @Override publicvoid onMessage(Message message, Session session) throws JMSException { JMSTextMessage xx = (JMSTextMessage) message; String text = xx.getText(); System.out.println(text); }}
Spring integrated Apache Qpid Simple Demo