Spring集成ActiveMQ配置 --转
转自:http://suhuanzheng7784877.iteye.com/blog/969865
- 集成环境
Spring采用2.5.6版本,ActiveMQ使用的是5.4.2,从apache站点可以下载。本文是将Spring集成ActiveMQ来发送和接收JMS消息。
- 集成步骤
将下载的ActiveMQ解压缩后文件夹如下
activemq-all-5.4.2.jar是activemq的所有的类jar包。lib下面是模块分解后的jar包。将lib下面的
- /lib/activation-1.1.jar
- /lib/activemq-camel-5.4.2.jar
- /lib/activemq-console-5.4.2.jar
- /lib/activemq-core-5.4.2.jar
- /lib/activemq-jaas-5.4.2.jar
- /lib/activemq-pool-5.4.2.jar
- /lib/activemq-protobuf-1.1.jar
- /lib/activemq-spring-5.4.2.jar
- /lib/activemq-web-5.4.2.jar
文件全部拷贝到项目中。
而Spring项目所需要的jar包如下
- /lib/spring-beans-2.5.6.jar
- /lib/spring-context-2.5.6.jar
- /lib/spring-context-support-2.5.6.jar
- /lib/spring-core-2.5.6.jar
- /lib/spring-jms-2.5.6.jar
- /lib/spring-tx.jar
当然还需要一些其他的jar文件
- /lib/geronimo-j2ee-management_1.1_spec-1.0.1.jar
- /lib/jms-1.1.jar
- /lib/log4j-1.2.15.jar
- /lib/slf4j-api-1.6.1.jar
- /lib/slf4j-nop-1.6.1.jar
项目的依赖jar都准备好后就可以写配置文件了。
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:context="http://www.springframework.org/schema/context"
- 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"
- default-autowire="byName">
- <!-- 配置connectionFactory -->
- <bean id="jmsFactory" class="org.apache.activemq.pool.PooledConnectionFactory"
- destroy-method="stop">
- <property name="connectionFactory">
- <bean class="org.apache.activemq.ActiveMQConnectionFactory">
- <property name="brokerURL">
- <value>tcp://127.0.0.1:61616</value>
- </property>
- </bean>
- </property>
- <property name="maxConnections" value="100"></property>
- </bean>
- <!-- Spring JMS Template -->
- <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
- <property name="connectionFactory">
- <ref local="jmsFactory" />
- </property>
- <property name="defaultDestinationName" value="subject" />
- <!-- 区别它采用的模式为false是p2p为true是订阅 -->
- <property name="pubSubDomain" value="true" />
- </bean>
- <!-- 发送消息的目的地(一个队列) -->
- <bean id="destination" class="org.apache.activemq.command.ActiveMQTopic">
- <!-- 设置消息队列的名字 -->
- <constructor-arg index="0" value="subject" />
- </bean>
- <!-- 消息监听 -->
- <bean id="listenerContainer"
- class="org.springframework.jms.listener.DefaultMessageListenerContainer">
- <property name="concurrentConsumers" value="10" />
- <property name="connectionFactory" ref="jmsFactory" />
- <property name="destinationName" value="subject" />
- <property name="messageListener" ref="messageReceiver" />
- <property name="pubSubNoLocal" value="false"></property>
- </bean>
- <bean id="messageReceiver"
- class="com.liuyan.jms.consumer.ProxyJMSConsumer">
- <property name="jmsTemplate" ref="jmsTemplate"></property>
- </bean>
- </beans>
编写代码
消息发送者:这里面消息生产者并没有在Spring配置文件中进行配置,这里仅仅使用了Spring中的JMS模板和消息目的而已。
- public class HelloSender {
- /**
- * @param args
- */
- public static void main(String[] args) {
- ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
- new String[] { "classpath:/spring/applicationContext-jms.xml" });
- JmsTemplate template = (JmsTemplate) applicationContext
- .getBean("jmsTemplate");
- Destination destination = (Destination) applicationContext
- .getBean("destination");
- template.send(destination, new MessageCreator() {
- public Message createMessage(Session session) throws JMSException {
- return session
- .createTextMessage("发送消息:Hello ActiveMQ Text Message!");
- }
- });
- System.out.println("成功发送了一条JMS消息");
- }
- }
消息接收
- /**
- * JMS消费者
- *
- *
- * <p>
- * 消息题的内容定义
- * <p>
- * 消息对象 接收消息对象后: 接收到的消息体* <p>
- */
- public class ProxyJMSConsumer {
- public ProxyJMSConsumer() {
- }
- private JmsTemplate jmsTemplate;
- public JmsTemplate getJmsTemplate() {
- return jmsTemplate;
- }
- public void setJmsTemplate(JmsTemplate jmsTemplate) {
- this.jmsTemplate = jmsTemplate;
- }
- /**
- * 监听到消息目的有消息后自动调用onMessage(Message message)方法
- */
- public void recive() {
- ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
- new String[] { "classpath:/spring/applicationContext-jms.xml" });
- Destination destination = (Destination) applicationContext
- .getBean("destination");
- while (true) {
- try {
- TextMessage txtmsg = (TextMessage) jmsTemplate
- .receive(destination);
- if (null != txtmsg) {
- System.out.println("[DB Proxy] " + txtmsg);
- System.out.println("[DB Proxy] 收到消息内容为: "
- + txtmsg.getText());
- } else
- break;
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- }
这里边也是并不是直接使用Spring来初始化建立消息消费者实例,而是在此消费者注入了JMS模板而已。
写一个main入口,初始化消息消费者
- public class JMSTest {
- /**
- * @param args
- */
- public static void main(String[] args) {
- ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
- new String[] { "classpath:/spring/applicationContext-jms.xml" });
- ProxyJMSConsumer proxyJMSConsumer = (ProxyJMSConsumer) applicationContext
- .getBean("messageReceiver");
- System.out.println("初始化消息消费者");
- }
- }
使用的时候先开启ActiveMQ服务,默认是占用了61616端口。之后开启测试程序,开启2个消息消费者监听。之后再运行消息生产者的代码后,消息就可以被消息消费者接收到了。
Spring集成ActiveMQ配置 --转的更多相关文章
- 从零开始学 Java - Spring 集成 ActiveMQ 配置(一)
你家小区下面有没有快递柜 近两年来,我们收取快递的方式好像变了,变得我们其实并不需要见到快递小哥也能拿到自己的快递了.对,我说的就是类似快递柜.菜鸟驿站这类的代收点的出现,把我们原来快递小哥必须拿着快 ...
- 从零开始学 Java - Spring 集成 ActiveMQ 配置(二)
从上一篇开始说起 上一篇从零开始学 Java - Spring 集成 ActiveMQ 配置(一)文章中讲了我关于消息队列的思考过程,现在这一篇会讲到 ActivMQ 与 Spring 框架的整合配置 ...
- spring集成mybatis配置多个数据源,通过aop自动切换
spring集成mybatis,配置多个数据源并自动切换. spring-mybatis.xml如下: <?xml version="1.0" encoding=" ...
- spring 集成mongo配置
spring继承Mongo使用的是spring-data. 如果需要加入两个mongo与spring集成的包,spring-data-commons-1.7.0.RELEASE.jar,spring- ...
- spring集成ActiveMQ居然要依赖这么多包
做spring和ActiveMQ的集成,作maven依赖的时候有感(以前都不在乎,现在不一样了........省略) <!-- https://mvnrepository.com/artifac ...
- SPRING 集成 activemq 的 topic 模式
概要 activemq 支持两种模式: 1.队列模式 2. 发布订阅者模式,topic有一个主题可以有多个订阅者.这种情况可以将一个消息,分发到多个消费者. 比如我有这样一个案例,用户需要同步,而且需 ...
- 160530、memcached集群(spring集成的配置)
第一步:在linux机或windows机上安装memcached服务端(server) linux中安装memcached:centos中命令 yum -y install memcached 如果没 ...
- spring集成activeMQ
1.安装activehttp://activemq.apache.org/activemq-5140-release.html2.运行D:\apache-activemq-5.14.0\bin\win ...
- spring 集成 log4j 配置
在web.xml中增加如下代码: <context-param> <param-name>log4jConfigLocation</param-name> < ...
随机推荐
- 关于在eclipse下的mapreduce工程打包成jar包的问题(包含第三方jar包)
这个问题也是在开发项目中经常遇到的一个问题,网上提供了很多方法,但是我发现很多并不适用,这里推荐两种方法,一种肯定没问题,就是比较麻烦,另一种是适用FatJar来打包,但是我没成功,原因估计出在ubu ...
- sqlmap os shell解析
0x00 Background 最近遇到测试环境,最后利用sqlmap的--os-shell参数取得shell.一直以来,对这个参数的工作原理不是十分的清晰.大致的思想应该是将脚本插入到数据库中,然后 ...
- 1、编译安装Nginx
1.1 如何选择web服务器 在实际工作中,我们需要根据业务需求来选择合适的业务服务软件,有关web服务,选择建议如下: 静态业务:若是高并发场景,尽量采用nginx或lighttpd,二者首选ngi ...
- 内存分哪些区 C++,ios,java
韩梦飞沙 yue31313 韩亚飞 han_meng_fei_sha 313134555@qq.com C/C++编译的程序占用的内存分为以下几个部分 1.栈区(stack)—由编译器自动分配释放,存 ...
- 【20181023T2】行星通道计划【二维BIT】
题面 [错解] 唉好像有规律啊(x2>x1,y2>y1) 唉好像是个偏序啊 然后上CDQ套树状数组 唉怎么大样例跑了十多秒啊 可能有问题吧-- 刷刷刷把T3写了,回来 唉怎么写了个memc ...
- 【欧拉回路】Play On Words(6-16)
[UVA10129]Play On Words 算法入门经典第6章6-16(P169) 题目大意:有一些单词,问能不能将它们串成字符串(只有前缀和后缀相同才能连) 试题分析:很巧妙的一道题,将每个单词 ...
- Problem E: 零起点学算法84——数组中删数II
#include<stdio.h> int main() { ],b[],i,flag=; while(scanf("%d",&n)!=EOF) { ;i< ...
- PHP5.3魔术方法 __invoke
这个魔幻方法被调用的时机是: 当一个对象当做函数调用的时候, 如果对象定义了__invoke魔幻方法则这个函数会被调用, class Callme { public function __invoke ...
- [转]MySQL更改用户密码
grant all privilegeson *.* to root@'localhost'identified by 'root'with grant option; grant all privi ...
- ubuntu中使用apt-get install 安装的软件的一些目录所在地
apt-get 所下载的用于安装的软件包,在 /var/cache/apt/archives中.如果执行过 apt-get clean ,那么原始下载的包就找不到了. 1.下载的软件存放位置/var/ ...