【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. ...
随机推荐
- samba config
[global] netbios name = HARDY #设置服务器的netbios名字 server string = my server #对samba服务器的描述 workgroup ...
- StompClient的包装类
为了简化MQ调用,写了个StompClient的包装类,可以供需要的参考: unit FStompClient; interface uses SysUtils, Windows, Messages, ...
- mysql--SQL编程(关于mysql中的日期,关于重叠) 学习笔记2.2
1.日期中的重叠问题建表sessions: CREATE TABLE `sessions` ( `id` ) NOT NULL AUTO_INCREMENT, `app` ) NOT NULL, `u ...
- 树莓派进阶之路 (027) - 在Linux中增加swap空间
原贴地址:http://blog.csdn.net/chinalinuxzend/article/details/1759593 在Linux中增加swap空间 在安装Linux的时候,不知道swa ...
- 如何使用SetTimer
1.SetTimer定义在那里? SetTimer表示的是定义个定时器.根据定义指定的窗口,在指定的窗口(CWnd)中实现OnTimer事件,这样,就可以相应事件了. SetTimer有两个函数.一个 ...
- idea 不下载jar包
是因为用的gradle 然后没有设置gradle jvm
- java生成PDF,各种格式、样式、水印都有
代码中有两处需要图片,请自行替换. 一个是水印.一个是手指. 需要的JAR包链接:http://download.csdn.net/detail/justinytsoft/9688893 下面是预览: ...
- 【java】解析java类加载与反射机制
目录结构: contents structure [+] 类的加载.连接和初始化 类的加载 类的连接 类的初始化 类加载器 类加载器机制 自定义类加载器 URLClassLoader类 反射的常规操作 ...
- 删除wordpress评论表单中的网址文本框
原始效果如下 想要去掉这个链接表单,一般想到的方法就是找到 comments.php 文件中的对应表单代码删掉.但是现在只需要一段非常简单的代码就可以去除: 代码如下 复制代码 add_filter( ...
- linux下配置tomcat集群的负载均衡
linux下配置tomcat集群的负载均衡 一.首先了解下与集群相关的几个概念集群:集群是一组协同工作的服务实体,用以提供比单一服务实体更具扩展性与可用性的服务平台.在客户端看来,一个集群就象是一个服 ...