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
spring集成JMS访问ActiveMQ的更多相关文章
- spring整合JMS - 基于ActiveMQ实现
一. 开篇语 继上一篇apache ActiveMQ之初体验后, 由于近期一直在复习spring的东西, 所以本文就使用spring整合下JMS. 二. 环境准备 1. ActiveMQ5.2.0 ( ...
- spring集成Apache的ActiveMQ
1.直接看优秀的博客 http://www.open-open.com/lib/view/open1435496659794.html
- 消息中间件ActiveMQ及Spring整合JMS
一 .消息中间件的基本介绍 1.1 消息中间件 1.1.1 什么是消息中间件 消息中间件利用高效可靠的消息传递机制进行平台无关的数据交流,并基于数据通信来进行分布式系统的集成.通过提供消息传递和消息排 ...
- 从零开始学 Java - Spring 集成 ActiveMQ 配置(一)
你家小区下面有没有快递柜 近两年来,我们收取快递的方式好像变了,变得我们其实并不需要见到快递小哥也能拿到自己的快递了.对,我说的就是类似快递柜.菜鸟驿站这类的代收点的出现,把我们原来快递小哥必须拿着快 ...
- 从零开始学 Java - Spring 集成 ActiveMQ 配置(二)
从上一篇开始说起 上一篇从零开始学 Java - Spring 集成 ActiveMQ 配置(一)文章中讲了我关于消息队列的思考过程,现在这一篇会讲到 ActivMQ 与 Spring 框架的整合配置 ...
- Spring集成ActiveMQ配置 --转
转自:http://suhuanzheng7784877.iteye.com/blog/969865 集成环境 Spring采用2.5.6版本,ActiveMQ使用的是5.4.2,从apache站点可 ...
- ActiveMQ第二弹:使用Spring JMS与ActiveMQ通讯
本文章的完整代码可从我的github中下载:https://github.com/huangbowen521/SpringJMSSample.git 上一篇文章中介绍了如何安装和运行ActiveMQ. ...
- 消费者端的Spring JMS 连接ActiveMQ接收生产者Oozie Server发送的Oozie作业执行结果
一,介绍 Oozie是一个Hadoop工作流服务器,接收Client提交的作业(MapReduce作业)请求,并把该作业提交给MapReduce执行.同时,Oozie还可以实现消息通知功能,只要配置好 ...
- Spring整合JMS(一)——基于ActiveMQ实现
1.1 JMS简介 JMS的全称是Java Message Service,即Java消息服务.它主要用于在生产者和消费者之间进行消息传递,生产者负责产生消息,而消费者负责接收消息.把它应用到 ...
随机推荐
- 牛客练习赛24题解(搜索,DP)
A题,C题不讲,基础题(但是我要抨击一下这次比赛,卡cin,cout,卡的太狠了,根本就不让过的那种,QAQ) 链接:https://www.nowcoder.com/acm/contest/157/ ...
- Codeforces Round #466 (Div. 2) Solution
从这里开始 题目列表 小结 Problem A Points on the line Problem B Our Tanya is Crying Out Loud Problem C Phone Nu ...
- CEF 文件下载
转载:https://blog.csdn.net/liuyan20092009/article/details/53819473?locationNum=6&fps=1 转载:https:// ...
- git如何将一个分支合并到另一个分支?
答: git merge --no-edit <another branch>
- shell编程中的单/双 小括号, 中括号, 大括号
linux shell中的变量类型?分字符串或者数字或者bool类型吗? 参考: http://www.cnblogs.com/nufangrensheng/p/3477281.html 不分! sh ...
- cmd使用管理员权限运行,启动路径不是当前目录
https://stackoverflow.com/questions/672693/windows-batch-file-starting-directory-when-run-as-admin B ...
- (转)Autonomous_Vehicle_Paper_Reading_List
Autonomous_Vehicle_Paper_Reading_List 2018-07-19 10:40:08 Reference:https://github.com/ZRZheng/Auton ...
- C++笔记(2017/2/9)
this指针 this指针作用就是指向成员函数所作用的对象. 非静态成员函数中可以直接使用this来代表指向该函数作用的对象的指针. 静态成员函数中不能使用this指针. 静态成员 static 定义 ...
- 解决Maven管理项目update Maven时,jre自动变为1.5
本文为博主原创,未经允许不得转载: 在搭建一个maven web项目时,项目已经按步骤搭建完好,之后项目上就报了一个错误. 在控制台看到错误提示如下:Dynamic Web Module 3.0 re ...
- MTP 写字机器
目标 无意中看到下面视频,我打算也实现一个类似机器 视频.视频2.视频3 来源于油管Creativity Buzz的创意,顺便了解到有家AxiDraw公司在生产这种机器,淘宝上也有售卖. 想法 观看视 ...