【ActiveMQ】Spring Jms集成ActiveMQ学习记录
Spring Jms集成ActiveMQ学习记录。
引入依赖包
无论生产者还是消费者均引入这些包:
<properties>
<spring.version>3.0.5.RELEASE</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-core</artifactId>
<version>5.7.0</version>
</dependency>
</dependencies>
生产者
先注册连接工厂、QueueTemplate等Bean:
<?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"
xmlns:jms="http://www.springframework.org/schema/jms"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-3.0.xsd">
<context:component-scan base-package="com.nicchagil" />
<!-- ActiveMQ的连接工厂 -->
<bean id="activeMQConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL" value="tcp://192.168.1.101:61616"/>
</bean>
<!-- Spring的连接工厂 -->
<bean id="connectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory">
<property name="targetConnectionFactory" ref="activeMQConnectionFactory"></property>
<!-- Session缓存数量 -->
<property name="sessionCacheSize" value="100" />
</bean>
<!-- 队列模式 -->
<bean id="jmsQueueTemplate" class="org.springframework.jms.core.JmsTemplate">
<constructor-arg ref="connectionFactory" />
<!-- 非发布/订阅模型,即队列模式 -->
<property name="pubSubDomain" value="false" />
</bean>
<!-- 发布/订阅模式 -->
<bean id="jmsTopicTemplate" class="org.springframework.jms.core.JmsTemplate">
<constructor-arg ref="connectionFactory" />
<!-- 发布/订阅模式 -->
<property name="pubSubDomain" value="true" />
</bean>
<!-- 队列 -->
<bean id="testQueue" class="org.apache.activemq.command.ActiveMQQueue">
<!-- 队列名 -->
<constructor-arg>
<value>testQueue</value>
</constructor-arg>
</bean>
</beans>
此类完全模拟正常的Service
package com.nicchagil;
import javax.annotation.Resource;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Service;
@Service
public class ProducerService {
@Resource(name="jmsQueueTemplate")
private JmsTemplate jmsTemplate;
public void sendMessage(Destination destination, final String message) {
jmsTemplate.send(destination, new MessageCreator() {
public Message createMessage(Session session) throws JMSException {
return session.createTextMessage(message);
}
});
}
}
这里模拟调用Service去发送一条消息:
package com.nicchagil;
import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.nicchagil.ProducerService;
public class HowToUse {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"spring-activemq.xml"});
context.start();
ProducerService producerService = (ProducerService)context.getBean("producerService");
ActiveMQQueue activeMQQueue = (ActiveMQQueue)context.getBean("testQueue");
producerService.sendMessage(activeMQQueue, "hello.");
}
}
消费者
注册连接工厂、监听器等Bean:
<?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"
xmlns:jms="http://www.springframework.org/schema/jms"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-3.0.xsd">
<context:component-scan base-package="com.nicchagil" />
<!-- ActiveMQ的连接工厂 -->
<bean id="activeMQConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL" value="tcp://192.168.1.101:61616"/>
</bean>
<!-- Spring的连接工厂 -->
<bean id="connectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory">
<property name="targetConnectionFactory" ref="activeMQConnectionFactory"></property>
<!-- Session缓存数量 -->
<property name="sessionCacheSize" value="100" />
</bean>
<!-- 队列 -->
<bean id="testQueue" class="org.apache.activemq.command.ActiveMQQueue">
<!-- 队列名 -->
<constructor-arg>
<value>testQueue</value>
</constructor-arg>
</bean>
<!-- 监听器 -->
<bean id="queueMessageListener" class="com.nicchagil.QueueMessageListener" />
<!-- 消息监听容器 -->
<bean id="queueListenerContainer"
class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="connectionFactory" />
<property name="destination" ref="testQueue" />
<property name="messageListener" ref="queueMessageListener" />
</bean>
</beans>
消费者的主要业务逻辑,这里只简单地打印消息:
package com.nicchagil;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
public class QueueMessageListener implements MessageListener {
@Override
public void onMessage(Message message) {
TextMessage textMessage = (TextMessage) message;
try {
System.out.println("处理消息:" + textMessage.getText());
} catch (JMSException e) {
e.printStackTrace();
}
}
}
消费者启动类:
package com.nicchagil;
import java.io.IOException;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Boot {
public static void main(String[] args) throws IOException {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"spring-activemq.xml"});
context.start();
System.in.read();
}
}
运行Boot和HowToUse可看效果。
【ActiveMQ】Spring Jms集成ActiveMQ学习记录的更多相关文章
- 在Spring下集成ActiveMQ
1.参考文献 Spring集成ActiveMQ配置 Spring JMS异步发收消息 ActiveMQ 2.环境 在前面的一篇ActiveMQ入门实例中我们实现了消息的异步传送,这篇博文将如何在spr ...
- Spring boot 集成ActiveMQ(包含双向队列实现)
集百家之长,成一家之言. 1. 下载ActiveMQ https://mirrors.tuna.tsinghua.edu.cn/apache/activemq/5.15.9/apache-activ ...
- ActiveMQ第二弹:使用Spring JMS与ActiveMQ通讯
本文章的完整代码可从我的github中下载:https://github.com/huangbowen521/SpringJMSSample.git 上一篇文章中介绍了如何安装和运行ActiveMQ. ...
- spring boot集成activemq
spring boot集成activemq 转自:https://blog.csdn.net/maiyikai/article/details/77199300
- 消费者端的Spring JMS 连接ActiveMQ接收生产者Oozie Server发送的Oozie作业执行结果
一,介绍 Oozie是一个Hadoop工作流服务器,接收Client提交的作业(MapReduce作业)请求,并把该作业提交给MapReduce执行.同时,Oozie还可以实现消息通知功能,只要配置好 ...
- Spring下集成ActiveMQ推送
本文是将ActiveMQ消息制造者集成进spring,通过spring后台推送消息的实现. 首先是spring的applicationContext的配置,如下 <?xml version=&q ...
- 86. Spring Boot集成ActiveMQ【从零开始学Spring Boot】
在Spring Boot中集成ActiveMQ相对还是比较简单的,都不需要安装什么服务,默认使用内存的activeMQ,当然配合ActiveMQ Server会更好.在这里我们简单介绍怎么使用,本节主 ...
- ActiveMQ学习笔记(5)——使用Spring JMS收发消息
摘要 ActiveMQ学习笔记(四)http://my.oschina.net/xiaoxishan/blog/380446 中记录了如何使用原生的方式从ActiveMQ中收发消息.可以看出,每次 ...
- 【ActiveMQ入门-9】ActiveMQ学习-与Spring集成2
概述: 下面将介绍如何在Spring下集成ActiveMQ. 消费者:同步接收: 目的地:Queue 环境: 共5个文件 Receiver.java ReceiverTest.java Sender. ...
随机推荐
- onActivityResult 传递数据
onActivityResult 传递数据 http://www.cnblogs.com/sipher/articles/2435078.html 如下图所示.当菜单项变多时,出现了垂直的滚动条,选项 ...
- JavaScript 风格指南
来源于: https://github.com/alivebao/clean-code-js 目录 介绍 变量 函数 对象和数据结构 类 测试 并发 错误处理 格式化 注释 介绍 作者根据 Rober ...
- [JavaScript模块演化简史]摘要
来源于:https://zhuanlan.zhihu.com/p/26231889 # JavaScript 模块化 早期的JavaScript并没有模块化解决方案.随着单页应用与富客户端的流行,出现 ...
- winform视频教程-任务定时循环执行小工具
有时候,我们可能遇到这样的一种需求,就是需要有一个小工具,在服务器中不断的执行一些任务,那么这样的需求,我们可以做一个windows服务程序,或者做一个winform wpf等小程序,然后小程序里面开 ...
- Easyui入门视频教程 第04集---Easyui布局
目录 目录 ----------------------- Easyui入门视频教程 第09集---登录完善 图标自定义 Easyui入门视频教程 第08集---登录实现 ajax button的 ...
- java 下对字符串的格式化
1.对整数进行格式化:%[index$][标识][最小宽度]转换方式 我们可以看到,格式化字符串由4部分组成,其中%[index$]的含义我们上面已经讲过,[最小宽度]的含义也很好理解 ...
- LUA返回的是引用
,} function t1.Show() print("t1 show") end function GetT() return t1 end local t2 = GetT() ...
- 73条日常shell命令汇总,总有一条你需要!
1.检查远程端口是否对bash开放: echo >/dev/tcp/8.8.8.8/53 && echo "open" 2.让进程转入后台: Ctrl + z ...
- webdriver之py,driver启动chrome时加载profile
import os from selenium import webdriver from selenium.webdriver.chrome.options import Options execu ...
- 进阶之路(中级篇) - 015 串口控RGB三色灯
本文由博主原创,如有不对之处请指明,转载请说明出处. /********************************* 代码功能:串口控RGB三色灯 使用函数: Serial.flush(); / ...