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学习记录的更多相关文章

  1. 在Spring下集成ActiveMQ

    1.参考文献 Spring集成ActiveMQ配置 Spring JMS异步发收消息 ActiveMQ 2.环境 在前面的一篇ActiveMQ入门实例中我们实现了消息的异步传送,这篇博文将如何在spr ...

  2. Spring boot 集成ActiveMQ(包含双向队列实现)

    集百家之长,成一家之言.  1. 下载ActiveMQ https://mirrors.tuna.tsinghua.edu.cn/apache/activemq/5.15.9/apache-activ ...

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

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

  4. spring boot集成activemq

    spring boot集成activemq 转自:https://blog.csdn.net/maiyikai/article/details/77199300

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

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

  6. Spring下集成ActiveMQ推送

    本文是将ActiveMQ消息制造者集成进spring,通过spring后台推送消息的实现. 首先是spring的applicationContext的配置,如下 <?xml version=&q ...

  7. 86. Spring Boot集成ActiveMQ【从零开始学Spring Boot】

    在Spring Boot中集成ActiveMQ相对还是比较简单的,都不需要安装什么服务,默认使用内存的activeMQ,当然配合ActiveMQ Server会更好.在这里我们简单介绍怎么使用,本节主 ...

  8. ActiveMQ学习笔记(5)——使用Spring JMS收发消息

      摘要 ActiveMQ学习笔记(四)http://my.oschina.net/xiaoxishan/blog/380446 中记录了如何使用原生的方式从ActiveMQ中收发消息.可以看出,每次 ...

  9. 【ActiveMQ入门-9】ActiveMQ学习-与Spring集成2

    概述: 下面将介绍如何在Spring下集成ActiveMQ. 消费者:同步接收: 目的地:Queue 环境: 共5个文件 Receiver.java ReceiverTest.java Sender. ...

随机推荐

  1. 【PHP采集】php采集、[\s\S]的使用、正则获取 换行字符串或html块

    1.如图,我想要获取 红框框中的html内容,但是普通的正则一直获取不到: 2.原因剖析:因为html换行了,所以直接 /<h3 class=\"s_name\"(.+?)& ...

  2. CentOS vps下,安装xfce/KDE/GNOME桌面+vncserver进行远程管理

    CentOS vps下,安装xfce/KDE/GNOME桌面+vncserver进行远程管理 首先安装桌面环境,我选择的是xfce,轻量级桌面,小巧实用不占太多内存,(占用内存方面,xfce少于kde ...

  3. Category在项目中的实际运用

    先看两行代码:1. label2.textColor = [UIColor colorWithHexString:@"707070"]; 2. _table.header = [M ...

  4. Nginx 和 PHP 的两种部署方式比较

    2种部署方式简介 第一种 前置1台nginx服务器做HTTP反向代理和负载均衡 后面多态服务器部署Nginx Web服务和php-fpm提供的fast cgi服务 第二种 前置1台nginx服务器做W ...

  5. ROS学习(八)—— 理解ROS服务和参数

    一.ROS 服务 服务(services)是节点之间通讯的另一种方式.服务允许节点发送请求(request) 并获得一个响应(response) 二.rosservice 1.用途 rosservic ...

  6. 【Android】Android如何对APK签名

    在eclipse项目,生成的apk是自动签名的,因此无需关心.接下来笔者介绍通过DOS窗口对APK进行签名,以及签名的过程中需要注意的问题. 1.为什么需要对APK签名 所有的Android应用程序都 ...

  7. 【C#】浅析C#中的日期处理

    1.字符串转化为日期 1.1第一种方式 使用 Convert.toDateTime 方法,该方法有很多重载方法,这里笔者就介绍两个常用的重载方法. 第一种: 使用: Convert.ToDateTim ...

  8. win7怎么快速截取图片

    点击开始--运行或者winkey + r 键直接进入运行. 2 在输入框输入snippingtool,点击确定. 3 这就找到截图工具,如图. END 方法/步骤2   进入c盘--Windows-- ...

  9. 一步一步掌握线程机制(三)---synchronized和volatile的使用

    现在开始进入线程编程中最重要的话题---数据同步,它是线程编程的核心,也是难点,就算我们理解了数据同步的基本原理,但是我们也无法保证能够写出正确的同步代码,但基本原理是必须掌握的. 要想理解数据同步的 ...

  10. Asp.net2.0之自定义控件ImageButton

    控件模仿winform中的button,可以支持图片和文字.可以选择执行服务器端程序还是客户端程序,还有一些简单的设置. 不足的是不支持样式,下次希望可以写一个工具条. 以下就是代码 以下为引用的内容 ...