spring集成JMS访问ActiveMQ
首先我们搭建一个spring-mvc项目,项目可以参考:spring-mvc 学习笔记
步骤:
- 在pom.xml中加上需要的包
- 修改web.xml,增加IOC容器
- spring配置文件application.xml增加对应的bean
- 生产者Java代码
- 消费者Java代码
1.在pom.xml加上需要的包
<!-- ActiveMQ支持 --> <dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-core</artifactId> <version>5.7.0</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jms</artifactId> <version>4.2.0.RELEASE</version> </dependency> <!-- spring的IOC容器 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.2.0.RELEASE</version> </dependency> <!-- 因为ActiveMQ使用了xbean,所以要引用 --> <dependency> <groupId>org.apache.xbean</groupId> <artifactId>xbean-spring</artifactId> <version>3.16</version> </dependency>
2.web.xml的根路径下增加对spring容器的监听(注意:是增加,不是覆盖)
<web-app> <!-- ContextLoaderListener 加载IOC容器,Spring框架的底层是listener --> <context-param> <param-name>contextConfigLocation</param-name> <!-- 指定Spring的配置文件的路径和名称 --> <param-value>classpath:application.xml</param-value> </context-param> <!-- Bootstraps the root web application context before servlet initialization --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> </web-app>
3.application.xml文件如下:
<?xml version="1.0" encoding="UTF-8"?> <!-- 查找最新的schemaLocation 访问 http://www.springframework.org/schema/ --> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:amq="http://activemq.apache.org/schema/core" xmlns:jms="http://www.springframework.org/schema/jms" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-4.0.xsd http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core-5.8.0.xsd"> <!-- 需要扫描注解的包 --> <context:component-scan base-package="cn.duanjt"></context:component-scan> <!-- 连接工厂,设置地址和用户名密码 --> <amq:connectionFactory id="connectionFactory" brokerURL="tcp://127.0.0.1:61616" userName="" password=""></amq:connectionFactory> <!-- 工厂连接池,池化,提高效率 --> <bean id="cachingConnectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory"> <constructor-arg name="targetConnectionFactory" ref="connectionFactory"></constructor-arg> <property name="sessionCacheSize" value="100"></property> </bean> <!-- 定义生产者 --> <bean id="jmstemplate" class="org.springframework.jms.core.JmsTemplate"> <constructor-arg name="connectionFactory" ref="cachingConnectionFactory"></constructor-arg> <!-- 是否为订阅模式 --> <property name="pubSubDomain" value="false"></property> </bean> <!-- 定义消费者,注册监听器 --> <jms:listener-container destination-type="queue" container-type="default" connection-factory="cachingConnectionFactory" acknowledge="auto"> <jms:listener destination="zd-duanjt" ref="queueReceiver1" /> </jms:listener-container> </beans>
4.生产者的Java代码
package cn.duanjt.controller; import javax.jms.*; import org.springframework.beans.factory.annotation.*; import org.springframework.jms.core.*; import org.springframework.web.bind.annotation.*; @RestController public class ActiveMQController { @Autowired @Qualifier("jmstemplate") private JmsTemplate jmsTemplate; @RequestMapping("/SendMsg") public String SendMsg(String content) { System.out.println("接收到数据:" + content); // zd-duanjt是ActiveMQ中队列的名称 jmsTemplate.send("zd-duanjt", new MessageCreator() { @Override public Message createMessage(Session session) throws JMSException { Message message= session.createTextMessage(content); return message; } }); return "success"; } }
5.消费者的Java代码
package cn.duanjt.controller; import javax.jms.*; import org.springframework.stereotype.Component; // 注入到spring容器之后的名称是 queueReceiver1 @Component public class QueueReceiver1 implements MessageListener { @Override public void onMessage(Message message) { TextMessage textMessage = (TextMessage) message; try { System.out.println("从ActiveMQ获取数据:" + textMessage.getText()); } catch (JMSException e) { e.printStackTrace(); } } }
注意:
1.application.xml文件需要引入ActiveMQ对应的xmlns和xsi:schemaLocation.直接从上面那文件搜索关键字“jms”和"activemq"都是需要引入的。
2.笔者这里是将生产者和消费者写到了一个程序里面,在具体实现的时候可以分开实现
3.关于pom.xml引入了xbean。可以参考http://berdy.iteye.com/blog/815309