首先我们搭建一个spring-mvc项目,项目可以参考:spring-mvc 学习笔记

步骤:

  1. 在pom.xml中加上需要的包
  2. 修改web.xml,增加IOC容器
  3. spring配置文件application.xml增加对应的bean
  4. 生产者Java代码
  5. 消费者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的更多相关文章

  1. spring整合JMS - 基于ActiveMQ实现

    一. 开篇语 继上一篇apache ActiveMQ之初体验后, 由于近期一直在复习spring的东西, 所以本文就使用spring整合下JMS. 二. 环境准备 1. ActiveMQ5.2.0 ( ...

  2. spring集成Apache的ActiveMQ

    1.直接看优秀的博客 http://www.open-open.com/lib/view/open1435496659794.html

  3. 消息中间件ActiveMQ及Spring整合JMS

    一 .消息中间件的基本介绍 1.1 消息中间件 1.1.1 什么是消息中间件 消息中间件利用高效可靠的消息传递机制进行平台无关的数据交流,并基于数据通信来进行分布式系统的集成.通过提供消息传递和消息排 ...

  4. 从零开始学 Java - Spring 集成 ActiveMQ 配置(一)

    你家小区下面有没有快递柜 近两年来,我们收取快递的方式好像变了,变得我们其实并不需要见到快递小哥也能拿到自己的快递了.对,我说的就是类似快递柜.菜鸟驿站这类的代收点的出现,把我们原来快递小哥必须拿着快 ...

  5. 从零开始学 Java - Spring 集成 ActiveMQ 配置(二)

    从上一篇开始说起 上一篇从零开始学 Java - Spring 集成 ActiveMQ 配置(一)文章中讲了我关于消息队列的思考过程,现在这一篇会讲到 ActivMQ 与 Spring 框架的整合配置 ...

  6. Spring集成ActiveMQ配置 --转

    转自:http://suhuanzheng7784877.iteye.com/blog/969865 集成环境 Spring采用2.5.6版本,ActiveMQ使用的是5.4.2,从apache站点可 ...

  7. ActiveMQ第二弹:使用Spring JMS与ActiveMQ通讯

    本文章的完整代码可从我的github中下载:https://github.com/huangbowen521/SpringJMSSample.git 上一篇文章中介绍了如何安装和运行ActiveMQ. ...

  8. 消费者端的Spring JMS 连接ActiveMQ接收生产者Oozie Server发送的Oozie作业执行结果

    一,介绍 Oozie是一个Hadoop工作流服务器,接收Client提交的作业(MapReduce作业)请求,并把该作业提交给MapReduce执行.同时,Oozie还可以实现消息通知功能,只要配置好 ...

  9. Spring整合JMS(一)——基于ActiveMQ实现

    1.1     JMS简介 JMS的全称是Java Message Service,即Java消息服务.它主要用于在生产者和消费者之间进行消息传递,生产者负责产生消息,而消费者负责接收消息.把它应用到 ...

随机推荐

  1. Linux CentOS 7的图形界面安装(GNOME、KDE等)

    转载于:https://jingyan.baidu.com/article/0964eca26fc3b38284f53642.html 今天为大家介绍一下CentOS 7的图像界面安装(虚拟机和硬盘安 ...

  2. 20145325张梓靖 《网络对抗技术》 Web安全基础实践

    20145325张梓靖 <网络对抗技术> Web安全基础实践 实验内容 使用webgoat进行XSS攻击.CSRF攻击.SQL注入 XSS攻击:Stored XSS Attacks.Ref ...

  3. centos7 install rabbtimq

    yum install deltarpm erlang wget https://dl.bintray.com/rabbitmq/all/rabbitmq-server/3.7.8/rabbitmq- ...

  4. 0x17二叉堆之超市

    题目链接:https://www.acwing.com/problem/content/147/ 容易想到一个贪心策略:在最优解中,对于每个时间(天数) t,应该在保证不卖出过期商品的前提下,尽量卖出 ...

  5. Spring 学习——Bean容器

    Bean容器初始化 基础 org.springframework.beans org.springframework.context BeanFactory提供配置结构和基本功能,加载并初始化Bean ...

  6. Luncene学习二《搜索索引》

    搜索索引的流程 第一步:创建一个Directory对象,也就是索引库存放的位置 第二步:创建一个IndexReader对象,需要指定Directory对象 第三步:创建一个indexsearcher对 ...

  7. Hbase与Oracle比较(列式数据库与行式数据库)

    Hbase与Oracle比较(列式数据库与行式数据库) 1 主要区别 Hbase适合大量插入同时又有读的情况 Hbase的瓶颈是硬盘传输速度,Oracle的瓶颈是硬盘寻道时间.   Hbase本质上只 ...

  8. Eclipse卸载插件SpringSoource-tool-suite

    Eclipse卸载插件SpringSoource-tool-suite **系统环境:**Mac OS X 引子: 一直在纠结的一个问题,就是Eclipse开发java 项目在配置了InternalR ...

  9. 一.移动app测试与质量保证

    1.典型的互联网产品的研发流程,及其核心做法.这里并不是简单的套用敏捷等流程方法,而是经过时间摸索和不断调整,找到最适合自己产品的流程做法,这是质量实践质量保证的基础. 2.系统功能测试实践.包涵需求 ...

  10. arcgis 要素服务增删改查

    两种方式: 第一种 要素服务的增删改操作,在ArcGIS API for JS中给我们提供了三个类用于要素的增Add,删Delete,改Update 添加draw和要素服务 //用于操作的要素图层,注 ...