使用spring 的jms发送和接收消息
新建工程
导入spring的jar包,
spring-aop-4.0.6.RELEASE.jar
spring-aspects-4.0.6.RELEASE.jar
spring-beans-4.0.6.RELEASE.jar
spring-build-src-4.0.6.RELEASE.jar
spring-context-4.0.6.RELEASE.jar
spring-context-support-4.0.6.RELEASE.jar
spring-core-4.0.6.RELEASE.jar
spring-expression-4.0.6.RELEASE.jar
spring-framework-bom-4.0.6.RELEASE.jar
spring-instrument-4.0.6.RELEASE.jar
spring-instrument-tomcat-4.0.6.RELEASE.jar
spring-jdbc-4.0.6.RELEASE.jar
spring-jms-4.0.6.RELEASE.jar
spring-messaging-4.0.6.RELEASE.jar
spring-orm-4.0.6.RELEASE.jar
spring-oxm-4.0.6.RELEASE.jar
spring-test-4.0.6.RELEASE.jar
spring-tx-4.0.6.RELEASE.jar
spring-web-4.0.6.RELEASE.jar
spring-webmvc-4.0.6.RELEASE.jar
spring-webmvc-portlet-4.0.6.RELEASE.jar
spring-websocket-4.0.6.RELEASE.jar
导入activeMq的jar包
activemq-all-5.10-SNAPSHOT.jar
将user library中的jar包发布到应用中
右击工程-->properties-->deployment assermbly-->add-->选择对应jar包
tomcat容器集成activieMq,在conf/context.xml文件的配置内容如下:
<!--set activeMq-->
<Resource name="jms/ConnectionFactory"
auth="Container"
type="org.apache.activemq.ActiveMQConnectionFactory"
description="JMS Connection Factory"
factory="org.apache.activemq.jndi.JNDIReferenceFactory"
brokerURL="tcp://localhost:61616"
brokerName="LocalActiveMQBroker" />
<Resource name="jms/Queue"
auth="Container"
type="org.apache.activemq.command.ActiveMQQueue"
description="My Queue"
factory="org.apache.activemq.jndi.JNDIReferenceFactory"
physicalName="TomcatQueue" />
jms的配置文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!-- 连接工厂 -->
<bean id="jmsConnectionFactory"
class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName"
value="java:comp/env/jms/ConnectionFactory"></property>
</bean>
<!-- 消息队列 -->
<bean id="tomcatQueue" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:comp/env/jms/Queue"></property>
</bean>
<!-- 配置JMS的模板 -->
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<!-- 链接工厂 -->
<property name="connectionFactory">
<ref bean="jmsConnectionFactory" />
</property>
<!-- 消息队列 -->
<property name="defaultDestination">
<ref bean="tomcatQueue" />
</property>
</bean>
<!-- 发送消息队列到目的地 -->
<bean id="sender" class="cn.yue.demo.jms.MessageSender">
<property name="jmsTemplate">
<ref bean="jmsTemplate" />
</property>
</bean>
<!-- 接收消息 -->
<bean id="receiver" class="cn.yue.demo.jms.MessageReceiver">
<property name="jmsTemplate">
<ref bean="jmsTemplate" />
</property>
</bean>
<!-- 默认的消息监听容器 -->
<bean id="listenerContainer"
class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory">
<ref bean="jmsConnectionFactory" />
</property>
<property name="destination">
<ref bean="tomcatQueue" />
</property>
<property name="messageListener">
<ref bean="receiver" />
</property>
</bean>
</beans>
spring中文件中的配置文件如下:
<?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:util="http://www.springframework.org/schema/util"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:amq="http://activemq.apache.org/schema/core"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://activemq.apache.org/schema/core
http://activemq.apache.org/schema/core/activemq-core.xsd">
<!-- 引入jms配置文件 -->
<import resource="classpath:jmsconfig.xml" />
<!-- 自动扫描 -->
<context:component-scan base-package="cn.yue.demo" />
</beans>
web.xml的配置文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>SpringJmsDemo</display-name>
<!-- 设置spring配置文件路径 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- 设置spring监听,在应用启动时启动spring容器 -->
<listener>
<listener-
class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 防止延迟加载 -->
<filter>
<filter-name>Spring OpenEntityManagerInView</filter-name>
<filter-
class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class>
</filter>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
java代码如下:
/**
* Copyright (C) 2016
*
* FileName:MessageSender.java
*
* Author:<a href="mailto:zhenhuayue@sina.com">Retacn</a>
*
* CreateTime: 2016-3-10
*/
// Package Information
package cn.yue.demo.jms;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
/**
* 消息发送
*
* @version
* @Description:
* @author <a href="mailto:zhenhuayue@sina.com">Retacn</a>
* @since 2016-3-10
*/
public class MessageSender {
// 消息模版
private JmsTemplate jmsTemplate;
/**
* 发送消息
*
* @param text 消息内容
*/
public void send(final String text) {
jmsTemplate.send(new MessageCreator() {
public Message createMessage(Session session) throws JMSException {
return session.createTextMessage(text);
}
});
}
public JmsTemplate getJmsTemplate() {
return jmsTemplate;
}
public void setJmsTemplate(JmsTemplate jmsTemplate) {
this.jmsTemplate = jmsTemplate;
}
}
/**
* Copyright (C) 2016
*
* FileName:MessageReceiver.java
*
* Author:<a href="mailto:zhenhuayue@sina.com">Retacn</a>
*
* CreateTime: 2016-3-10
*/
// Package Information
package cn.yue.demo.jms;
import org.springframework.jms.core.JmsTemplate;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
/**
* 消息接收
*
* @version
* @Description:
* @author <a href="mailto:zhenhuayue@sina.com">Retacn</a>
* @since 2016-3-10
*/
public class MessageReceiver implements MessageListener {
// 消息模版
private JmsTemplate jmsTemplate;
/**
* 接收消息
*
* @throws JMSException
*/
public void receive() throws JMSException {
TextMessage text = (TextMessage) this.jmsTemplate.receive();
System.out.println("receive=" + text.getText());
}
/**
* 监听回调函数
*/
public void onMessage(Message message) {
if (message instanceof TextMessage) {
TextMessage text = (TextMessage) message;
try {
System.out.println(text.getText());
} catch (Exception e) {
}
}
}
public JmsTemplate getJmsTemplate() {
return jmsTemplate;
}
public void setJmsTemplate(JmsTemplate jmsTemplate) {
this.jmsTemplate = jmsTemplate;
}
}
/**
* Copyright (C) 2016
*
* FileName:MessageTest.java
*
* Author:<a href="mailto:zhenhuayue@sina.com">Retacn</a>
*
* CreateTime: 2016-3-10
*/
// Package Information
package cn.yue.demo.jms;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import javax.jms.JMSException;
/**
* 测试消息接收
*
* @version
* @Description:
* @author <a href="mailto:zhenhuayue@sina.com">Retacn</a>
* @since 2016-3-10
*/
public class MessageTest {
/**
* 测试消息的发送与接收
*
* @throws JMSException
*/
public void test() throws JMSException {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(
"applicationContext.xml");
MessageSender sender = (MessageSender) ctx.getBean("sender");
MessageReceiver receive = (MessageReceiver) ctx.getBean("receiver");
sender.send("helloworld");
receive.receive();
}
}
启动activeMq
启动tomcat,运行该程序