异步进程通信是面向服务架构(SOA)一个重要的组成部分,因为企业里很多系统通信,特别是与外部组织间的通信,实质上都是异步的。Java消息服务(JMS)是用于编写使用异步消息传递的JEE应用程序的API。传统的使用JMS API进行消息传递的实现包括多个步骤,例如JNDI查询队列连接工厂和Queue资源,在实际发送和接收消息前创建一个JMS会话。
发送消息:
测试:
Spring框架则简化了使用JEE组件(包括JMS)的任务。它提供的模板机制隐藏了典型的JMS实现的细节,这样开发人员可以集中精力放在处理消息的实际工作中,而不用担心如何去创建,访问或清除JMS资源。
集成环境
Spring采用2.5.6版本,ActiveMQ使用的是5.5.0,从apache站点可以下载。本文是将Spring集成ActiveMQ来发送和接收JMS消息。
环境所需JAR包
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/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
- 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>
- <!-- 配置Jms模板 -->
- <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>
<?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/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
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>
<!-- 配置Jms模板 -->
<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>
- 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 jmsTemplate;
- Destination destination;
- public void receive() {
- TextMessage message = (TextMessage)jmsTemplate.receive();
- try {
- System.out.println(">>接收到的消息>>"+message.getText());
- } catch (JMSException e) {
- e.printStackTrace();
- }
- }
- public void setJmsTemplate(JmsTemplate jmsTemplate) {
- this.jmsTemplate = jmsTemplate;
- }
- public void setDestination(Destination destination) {
- this.destination = destination;
- }
- }
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 jmsTemplate;
Destination destination;
public void receive() {
TextMessage message = (TextMessage)jmsTemplate.receive();
try {
System.out.println(">>接收到的消息>>"+message.getText());
} catch (JMSException e) {
e.printStackTrace();
}
}
public void setJmsTemplate(JmsTemplate jmsTemplate) {
this.jmsTemplate = jmsTemplate;
}
public void setDestination(Destination destination) {
this.destination = destination;
}
}
发送消息:
- 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 jmsTemplate;
- Destination destination;
- public void send() {
- MessageCreator messageCreator = new MessageCreator(){
- public Message createMessage(Session 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 jmsTemplate) {
- this.jmsTemplate = jmsTemplate;
- }
- public void setDestination(Destination destination) {
- this.destination = destination;
- }
- }
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 jmsTemplate;
Destination destination;
public void send() {
MessageCreator messageCreator = new MessageCreator(){
public Message createMessage(Session 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 jmsTemplate) {
this.jmsTemplate = jmsTemplate;
}
public void setDestination(Destination destination) {
this.destination = destination;
}
}
测试:
- 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();
- }
- }
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();
}
}
执行Test类的main方法打印信息如下:
>>接收到的消息>>你好 Hello