JMS整合Spirng实现异步发送消息,简单实例:
1、下载ActiveMQ:http://activemq.apache.org/download.html
2、新建java project,添加spring功能,由于我不太信任Myeclipse添加的spring的jar包,所以我自己添加了spring的jar包,只用它给我生成的那个applicationContext.xml文件,另外加上:activemq-all-5.3.1.jar、log4j-1.2.15.jar、slf4j-api-1.5.8.jar、slf4j-log4j12-1.5.6.jar
3、下面贴上所有代码:
--------MessageProducer :
package com.starit.jmsspring;
import org.springframework.jms.core.JmsTemplate;
/**
* MessageProducer
* @author lisanlai
*
*/
public class MessageProducer {
private JmsTemplate jmsTemplate;
public void setJmsTemplate(JmsTemplate jmsTemplate) {
this.jmsTemplate = jmsTemplate;
}
/**
* 发送消息
* @param msg
*/
public void sendMsg(Object msg){
this.jmsTemplate.convertAndSend(msg);
}
}
------------MessageListener:
package com.starit.jmsspring;
/**
* MessageListener
* @author lisanlai
*
*/
public class MessageListener {
/**
* 接收消息
* @param msg
*/
public void getMessage(Object msg){
if(msg instanceof String){
System.out.println("监听到消息:"+msg);
}
}
}
----------applicationContext.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <!-- 属性文件的配置 --> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>jms_spring.properties</value> </list> </property> </bean> <!-- beans配置开始 --> <!-- JMS ConnectionFactory --> <bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"> <property name="brokerURL" value="${jms.brokerURL}" /> </bean> <!-- JMS Queue Template --> <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate102"> <property name="connectionFactory" ref="connectionFactory" /> <property name="timeToLive" value="${jms.timeToLive}" /> <property name="defaultDestinationName" value="${jms.destinationName}" /> <property name="receiveTimeout" value="${jms.receiveTimeout}" /> </bean> <!-- Message porducer 消息生产者--> <bean id="msgProducer" class="com.starit.jmsspring.MessageProducer"> <property name="jmsTemplate" ref="jmsTemplate" /> </bean> <!-- 消息监听器 --> <bean id="messageListener" class="org.springframework.jms.listener.adapter.MessageListenerAdapter"> <constructor-arg> <bean class="com.starit.jmsspring.MessageListener" /> </constructor-arg> <property name="defaultListenerMethod" value="getMessage" /> </bean> <bean id="listenerContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer"> <property name="connectionFactory" ref="connectionFactory" /> <property name="destinationName" value="${jms.destinationName}" /> <property name="messageListener" ref="messageListener" /> </bean> </beans>
-----------jms_spring.properties:
jms.brokerURL=tcp://localhost:61616
jms.receiveTimeout=3000
jms.destinationName=starit.queue
jms.timeToLive=86400000
------------测试类Test:
package com.starit.jmsspring;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* 测试类
* @author lisanlai
*
*/
public class Test {
public static void main(String[] args) {
AbstractApplicationContext ctx = new ClassPathXmlApplicationContext(new String[] { "applicationContext.xml" });
MessageProducer msgProducer = (MessageProducer) ctx.getBean("msgProducer");
msgProducer.sendMsg("Hello JMS and Spring!这是一个JMS整合Spring的DEMO");
for(int i=1;i<=10;i++){
msgProducer.sendMsg("#这是第"+i+"条消息……");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
msgProducer.sendMsg("消息成功 发送完成!");
}
}