Asynchronous process communication is an important component of the Service-Oriented Architecture (SOA), because many systems in an enterprise communicate, especially with external organizations, in essence, asynchronously. Java Message Service (JMS) is an API used to write Jee applications that use asynchronous message transmission. The traditional implementation of Message Passing using the jms api involves multiple steps, such as querying the queue connection factory and queue resources in JNDI, and creating a JMS session before actually sending and receiving messages.
The Spring framework simplifies the use of Jee components (including JMS) tasks. The template mechanism provided by it hides the details of typical JMS implementation, so that developers can focus on the actual work of Message Processing without worrying about how to create it, access or clear JMS resources.
Integrated Environment
Spring uses version 2.5.6 and activemq uses version 5.5.0, which can be downloaded from the Apache site. This article integrates spring with activemq to send and receive JMS messages.
Jar packages required by the Environment
ApplicationContext-service.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" xmlns: AOP = "http://www.springframework.org/schema/aop" xmlns: Tx = "http://www.springframework.org/schema/tx" xsi: schemalocation = "http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans- 2.5.xsdhttp: // metrics http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd "> <bean id =" producerservice "class =" test. producerserviceimpl "> <property name = "Jmstemplate" ref = "jmstemplate"/> <property name = "destination" ref = "destination"/> </bean> <bean id = "consumerservice" class = "Test. consumerserviceimpl "> <property name =" jmstemplate "ref =" jmstemplate "/> <property name =" destination "ref =" destination "/> </bean> <! -- Configure the JMS template --> <bean id = "jmstemplate" class = "org. springframework. JMS. core. jmstemplate "> <property name =" connectionfactory "ref =" connectionfactory "/> <property name =" defaultdestination "ref =" destination "/> <property name =" receivetimeout "value = "10000"/> </bean> <bean id = "connectionfactory" class = "org. apache. activemq. activemqconnectionfactory "> <property name =" brokerurl "value =" TCP: // localhost: 61616 "/> </bean> <bean id =" destination "class =" org. apache. activemq. command. activemqqueue "> <constructor-Arg Index =" 0 "value =" test "/> </bean> </beans>
Receive message:
Package test; public interface consumerservice {public void receive ();} package test; import javax. JMS. destination; import javax. JMS. jmsexception; import javax. JMS. textmessage; import Org. springframework. JMS. core. jmstemplate; public class consumerserviceimpl implements consumerservice {jmstemplate; destination; Public void receive () {textmessage message = (textmessage) jmstemplate. receive (); try {system. out. println ("> received message>" + message. gettext ();} catch (jmsexception e) {e. printstacktrace () ;}} public void setjmstemplate (jmstemplate) {This. jmstemplate = jmstemplate;} public void setdestination (destination) {This. destination = destination ;}}
Send message:
Package test; public interface producerservice {public void send ();} package test; import javax. JMS. destination; import javax. JMS. jmsexception; import javax. JMS. message; import javax. JMS. session; import javax. JMS. textmessage; import Org. springframework. JMS. core. jmstemplate; import Org. springframework. JMS. core. messagecreator; public class producerserviceimpl implements producerservice {jmstemplate; destination; Public void send () {inclumessagecreator = new messagecreator () {public message createmessage (session) {textmessage message = NULL; try {message = session. createtextmessage ("hello");} catch (jmsexception e) {e. printstacktrace ();} return message ;}}; jmstemplate. send (this. destination, messagecreator);} public void setjmstemplate (jmstemplate) {This. jmstemplate = jmstemplate;} public void setdestination (destination) {This. destination = destination ;}}
Test:
package test; import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test { private static ApplicationContext appContext = new ClassPathXmlApplicationContext("applicationContext-service.xml"); private static void send(){ProducerService producerService = (ProducerService)appContext.getBean("producerService");producerService.send();} private static void receive(){ConsumerService consumerService = (ConsumerService)appContext.getBean("consumerService");consumerService.receive();} /** * @param args */public static void main(String[] args) {send();receive();} }
Click activemq. bat under apache-activemq-5.5.0 \ bin to start the service
Execute the main method of the test class to print the following information:
> Received message> hello, hello
Finished!
If you have any questions, please join the group number for discussion: 173711587