[activemq]+spring的使用
一、生产端(Producer)
applicationContext.xml中的配置:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <!-- 真正可以产生Connection的ConnectionFactory,由对应的 JMS服务厂商提供 -->
<bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL" value="tcp://192.168.137.1:61616" />
<property name="userName" value="afeng" />
<property name="password" value="111111" />
</bean> <!-- Spring用于管理真正的ConnectionFactory的ConnectionFactory -->
<bean id="connectionFactory"
class="org.springframework.jms.connection.SingleConnectionFactory">
<!-- 目标ConnectionFactory对应真实的可以产生JMS Connection的ConnectionFactory -->
<property name="targetConnectionFactory" ref="targetConnectionFactory" />
</bean> <!-- 定义JmsTemplate的Queue类型 -->
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<constructor-arg ref="connectionFactory"/>
<!-- 非pub/sub模型(发布/订阅),即队列模式 -->
<property name="pubSubDomain" value="false"/>
</bean> <bean id="queueSender" class="com.afeng.utils.activeMq.QueueSender">
<property name="JmsTemplate" ref="jmsTemplate"/>
</bean> <!-- 定义JmsTemplate的Topic类型 -->
<bean id="jmsTopicTemplate" class="org.springframework.jms.core.JmsTemplate">
<!-- 这个connectionFactory对应的是我们定义的Spring提供的那个ConnectionFactory对象 -->
<constructor-arg ref="connectionFactory"/>
<!-- pub/sub模型(发布/订阅) -->
<property name="pubSubDomain" value="true"/>
</bean> <bean id="topicSender" class="com.afeng.utils.activeMq.TopicSender">
<property name="jmsTemplate" ref="jmsTopicTemplate"/>
</bean> </beans>
两个封装的类:QueueSender
package com.afeng.utils.activeMq; import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Component; import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session; @Component
public class QueueSender { private JmsTemplate jmsTemplate;//通过@Qualifier修饰符来注入对应的bean public JmsTemplate getJmsTemplate() {
return jmsTemplate;
} public void setJmsTemplate(JmsTemplate jmsTemplate) {
this.jmsTemplate = jmsTemplate;
} /**
* 发送一条消息到指定的队列(目标)
* @param queueName 队列名称
* @param message 消息内容
*/
public void send(String queueName,final String message){
jmsTemplate.send(queueName, new MessageCreator() {
public Message createMessage(Session session) throws JMSException {
return session.createTextMessage(message);
}
});
}
}
TopicSender
package com.afeng.utils.activeMq; import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Component; import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session; @Component
public class TopicSender {
private JmsTemplate jmsTemplate; public JmsTemplate getJmsTemplate() {
return jmsTemplate;
} public void setJmsTemplate(JmsTemplate jmsTemplate) {
this.jmsTemplate = jmsTemplate;
} public void send(String queueName, final String message) {
this.jmsTemplate.send(queueName, new MessageCreator() {
@Override
public Message createMessage(Session session) throws JMSException {
return session.createTextMessage(message);
}
});
}
}
在实际场景中的使用:
@Autowired
private QueueSender queueSender; queueSender.send("spring-queue","要发送的数据");
二、消费端(Comsumer)
applicationContext.xml中的配置:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <!-- 真正可以产生Connection的ConnectionFactory,由对应的 JMS服务厂商提供 -->
<bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL" value="tcp://192.168.137.1:61616" />
<property name="userName" value="afeng" />
<property name="password" value="111111" />
</bean> <!-- Spring用于管理真正的ConnectionFactory的ConnectionFactory -->
<bean id="connectionFactory"
class="org.springframework.jms.connection.SingleConnectionFactory">
<!-- 目标ConnectionFactory对应真实的可以产生JMS Connection的ConnectionFactory -->
<property name="targetConnectionFactory" ref="targetConnectionFactory" />
</bean> <!--这个是队列目的地,点对点的 -->
<bean id="queueDestination" class="org.apache.activemq.command.ActiveMQQueue">
<constructor-arg>
<value>spring-queue</value>
</constructor-arg>
</bean>
<!--这个是主题目的地,一对多的 -->
<bean id="topicDestination" class="org.apache.activemq.command.ActiveMQTopic">
<constructor-arg value="spring-topic" />
</bean> <!--监听 数据发生变化-->
<bean id="itemChangeListener" class="com.afeng.listener.ItemChangeListener" />
<bean class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="connectionFactory" />
<property name="destination" ref="queueDestination" />
<property name="messageListener" ref="itemChangeListener" />
</bean> </beans>
监听的类ItemChangeListener:
package com.afeng.listener; import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage; public class ItemChangeListener implements MessageListener {
@Override
public void onMessage(Message message) {
try {
TextMessage textMessage = null;
String queuetxt = "";
//取商品id
if (message instanceof TextMessage) {
textMessage = (TextMessage) message;
queuetxt = textMessage.getText();
}
System.err.println("queuetxt="+queuetxt);
} catch (Exception e) {
e.printStackTrace();
}
}
}
[activemq]+spring的使用的更多相关文章
- activemq spring 配置
Apache ActiveMQ是最流行和最强大的开源消息集成模式服务器.Apache ActiveMQ是速度快,支持多跨语言的客户端和协议,带有易于使用企业集成模式和许多先进的功能在充分支持JMS 1 ...
- activemq spring 集成与测试
1.下载安装activemq 2.pom依赖配置 3.spring配置 4.生产消息,消费消息(同步消费),监听消息(异步消费) 4.测试 5.参考博客 http://www.cnblogs.com/ ...
- ActiveMQ Spring 集成配置
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-jms&l ...
- Activemq -- Spring 整合
转自: https://www.cnblogs.com/jaycekon/p/ActiveMq.html
- ActiveMQ spring (一)
在5.8.0版本下 配置成功. 参考文档:http://yinbinhome.iteye.com/blog/1273228
- ActiveMQ学习笔记(5)——使用Spring JMS收发消息
摘要 ActiveMQ学习笔记(四)http://my.oschina.net/xiaoxishan/blog/380446 中记录了如何使用原生的方式从ActiveMQ中收发消息.可以看出,每次 ...
- ActiveMQ(5.10.0) - Spring Support
Maven Dependency: <dependencies> <dependency> <groupId>org.apache.activemq</gro ...
- spring整合JMS - 基于ActiveMQ实现
一. 开篇语 继上一篇apache ActiveMQ之初体验后, 由于近期一直在复习spring的东西, 所以本文就使用spring整合下JMS. 二. 环境准备 1. ActiveMQ5.2.0 ( ...
- ActiveMQ学习笔记(6)----ActiveMQ整合Spring开发
1. 添加依赖 spring 提供了对JMS的支持,需要添加Spring支持jms的包和Spring的核心包,如下: <dependency> <groupId>org.apa ...
随机推荐
- 爬虫如何发现更多的url呢,怎么动态收集新的url连接
大家在做爬虫采集数据的时候很多都会遇到增量采集的问题,有些时候是通过过滤url来进行的,有些是通过爬取网页后再进行分析判断, 以上这些过程也许大部分做爬虫的都会这么做,各位有没有想过, 除了以上的常用 ...
- TLS1.3&TLS1.2形式化分析(二)
1.下面是TLS1.2和TLS1.3握手协议过程 ,明显的可以看出存在不同 . 我们先说TLS1.2的握手过程明显是比TLS1.3的握手过多了一次.在TLS1.3中舍弃了之前RSA的协商过程,然后基于 ...
- cookies, session, token
Cookie 是由客户端(通常是浏览器)保存的小型文本信息,其内容是一系列的键值对,是由 HTTP 服务器设置并保存在浏览器上的信息. 在post请求的瞬间,cookie会被浏览器自动添加到请求头中. ...
- C++---初识《通过g++ / makefile 编译和调用动态库so文件》(ubuntu)
C++---初识<通过g++ / makefile 编译和调用动态库so文件>(ubuntu) ------------------------目录------------------- ...
- PHP返回JSON数据及中文编码问题的解决方案
在处理app接口的时候 ,中文在经过json_encode之后 变成\ \格式 想在返回接口的时候 中文不被转换 解决办法 第一种解决办法 exit(json_encode($result,JSON ...
- bitset 来计算位
在stl中提供了 bitset<N> 来计算位,非常方便. 尤其是计算键盘的状态时游泳 ; bitset<> bs = s; cout<<bs[]<<e ...
- mysql workbench使用技巧,使用workbench导出部分表
最近在刚开始用workbench导出数据的时候,需要导出部分表数据,找来半天找不到,原来是选中库之后,不要要点右边的字母,然后表才显示出来 点左边的对勾的话,右边的表是不会显示出来的!
- C语言I作业12一学期总结
一.我学到的内容 二.我的收获 作业 收获 C语言I博客作业01 学会了编程"Hello word" C语言I博客作业02 安装编译器,将代码建立在自己的文件里面 C语言I博客作业 ...
- django 重定向如何解决iframe页面嵌套问题
出现问题背景:从登录页进入到首页后,如出现后台重启或者用户清除cookie,或者session过期,token验证等问题,会重定向到登录页.由于使用的是iframe,出现登录页面嵌套在首页框架下.很是 ...
- Miller-Rabin素性测试
有时候我们想快速的知道一个数是不是素数,而这个数又特别的大导致 $O(\sqrt n)$ 的算法也难以通过,这时候我们可以对其进行 Miller-Rabin 素数测试,可以很大概率测出其是否为素数. ...