【ActiveMQ入门-8】ActiveMQ学习-与Spring集成

主要包括4个文件:
- HelloSender.java;
- JMSTest.java;
- ProxyJMSConsumer.java;
- applicationContext-jms.xml(配置文件);

HelloSender.java
package com.ll.springActiveMQ1;import javax.jms.Destination;import javax.jms.JMSException;import javax.jms.Message;import javax.jms.Session;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import org.springframework.jms.core.JmsTemplate;import org.springframework.jms.core.MessageCreator;public class HelloSender {/*** @param args* jmsTemplate和destination都是在spring配置文件中进行配制的* Sender只使用了配置文件中的jmsFactory,jmsTemplate,还有destination这三个属性*/public static void main(String[] args) {ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext-jms.xml");JmsTemplate template = (JmsTemplate) applicationContext.getBean("jmsTemplate");Destination destination = (Destination) applicationContext.getBean("destination");template.send(destination, new MessageCreator() {public Message createMessage(Session session) throws JMSException {return session.createTextMessage("发送消息:Hello ActiveMQ Text Message2!");}});System.out.println("成功发送了一条JMS消息");}}
ProxyJMSConsumer.java
package com.ll.springActiveMQ1;import javax.jms.Destination;import javax.jms.TextMessage;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import org.springframework.jms.core.JmsTemplate;/*** JMS消费者 消息题的内容定义 消息对象 接收消息对象后: 接收到的消息体** <p>*/public class ProxyJMSConsumer {/*** 构造函数*/public ProxyJMSConsumer() {}/****/private JmsTemplate jmsTemplate;public JmsTemplate getJmsTemplate() {return jmsTemplate;}public void setJmsTemplate(JmsTemplate jmsTemplate) {this.jmsTemplate = jmsTemplate;}/*** 监听到消息目的有消息后自动调用onMessage(Message message)方法*/public void recive() {ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext-jms.xml");Destination destination = (Destination) applicationContext.getBean("destination");while (true) {try {//同步接收TextMessage txtmsg = (TextMessage) jmsTemplate.receive(destination);if (null != txtmsg) {System.out.println("[DB Proxy] " + txtmsg);System.out.println("[DB Proxy] 收到消息内容为: "+ txtmsg.getText());} else {break;}} catch (Exception e) {e.printStackTrace();}}}}
JMSTest.java
package com.ll.springActiveMQ1;import org.apache.commons.logging.Log;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class JMSTest {/*** @param args*/public static void main(String[] args) {ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext-jms.xml");ProxyJMSConsumer proxyJMSConsumer = (ProxyJMSConsumer) applicationContext.getBean("messageReceiver");proxyJMSConsumer.recive();System.out.println("初始化消息消费者");}}
applicationContext-jms.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" xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-2.5.xsd"default-autowire="byName"><!-- 配置connectionFactory --><bean id="jmsFactory" class="org.apache.activemq.pool.PooledConnectionFactory"destroy-method="stop"><property name="connectionFactory"><bean class="org.apache.activemq.ActiveMQConnectionFactory"><property name="brokerURL"><value>tcp://127.0.0.1:61616</value></property></bean></property><property name="maxConnections" value="100"></property></bean><!-- Spring JMS Template --><bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"><property name="connectionFactory"><ref local="jmsFactory" /></property><property name="defaultDestinationName" value="subject" /><!-- 区别它采用的模式为false是p2p为true是订阅 --><property name="pubSubDomain" value="true" /></bean><!-- 发送消息的目的地(一个队列) --><bean id="destination" class="org.apache.activemq.command.ActiveMQTopic"><!-- 设置消息队列的名字 --><constructor-arg index="0" value="subject" /></bean><bean id="messageReceiver" class="com.ll.springActiveMQ1.ProxyJMSConsumer"><property name="jmsTemplate" ref="jmsTemplate"></property></bean></beans>
运行顺序:
首先运行JMSTest,然后运行HelloSender。


附件列表
【ActiveMQ入门-8】ActiveMQ学习-与Spring集成的更多相关文章
- 【spring源码学习】spring集成orm数据框架
[一]简易的数据源配置 (1)配置文件 <!--springJdbcTemplemate数据操作配置信息 --> <bean id="driver" class= ...
- 【ActiveMQ入门-9】ActiveMQ学习-与Spring集成2
概述: 下面将介绍如何在Spring下集成ActiveMQ. 消费者:同步接收: 目的地:Queue 环境: 共5个文件 Receiver.java ReceiverTest.java Sender. ...
- ActiveMQ入门之四--ActiveMQ持久化方式
消息持久性对于可靠消息传递来说应该是一种比较好的方法,有了消息持久化,即使发送者和接受者不是同时在线或者消息中心在发送者发送消息后宕机了,在消息中心重新启动后仍然可以将消息发送出去,如果把这种持久化和 ...
- MyBatis 学习-与 Spring 集成篇
根据官方的说法,在 ibatis3,也就是 Mybatis3 问世之前,Spring3 的开发工作就已经完成了,所以 Spring3 中还是没有对 Mybatis3 的支持.因此由 Mybatis 社 ...
- ActiveMQ 入门和与 Spring 整合
ActiveMQ 入门演示 activemq 依赖 <dependency> <groupId>org.apache.activemq</groupId> < ...
- Spring学习笔记--spring+mybatis集成
前言: 技术的发展, 真的是日新月异. 作为javaer, 都不约而同地抛弃裸写jdbc代码, 而用各种持久化框架. 从hibernate, Spring的JDBCTemplate, 到ibatis, ...
- Spring Boot学习笔记——Spring Boot与ActiveMQ的集成
Spring Boot对JMS(Java Message Service,Java消息服务)也提供了自动配置的支持,其主要支持的JMS实现有ActiveMQ.Artemis等.这里以ActiveMQ为 ...
- 从零开始学 Java - Spring 集成 ActiveMQ 配置(一)
你家小区下面有没有快递柜 近两年来,我们收取快递的方式好像变了,变得我们其实并不需要见到快递小哥也能拿到自己的快递了.对,我说的就是类似快递柜.菜鸟驿站这类的代收点的出现,把我们原来快递小哥必须拿着快 ...
- 从零开始学 Java - Spring 集成 ActiveMQ 配置(二)
从上一篇开始说起 上一篇从零开始学 Java - Spring 集成 ActiveMQ 配置(一)文章中讲了我关于消息队列的思考过程,现在这一篇会讲到 ActivMQ 与 Spring 框架的整合配置 ...
随机推荐
- SQL Server 调优系列玩转篇一(如何利用查询提示(Hint)引导语句运行)
前言 前面几篇我们分析了关于SQL Server关于性能调优的一系列内容,我把它分为两个模块. 第一个模块注重基础内容的掌握,共分7篇文章完成,内容涵盖一系列基础运算算法,详细分析了如何查看执行计划. ...
- Yii1.1测试环境配置(一)
一.安装wampserver wampserver集成了PHP.Apacha.MySql,可以省去分别安装的麻烦.wampserver的安装配置方法可以自行搜索. wampserver安装完成后需要手 ...
- Microsoft Jet 数据库引擎 SQL 和 ANSI SQL 的比较
http://westlife063.blog.163.com/blog/static/129942096201052591210384/ Microsoft Jet 数据库引擎 SQL 和 AN ...
- 经典T-SQL代码
1. N到M条记录(要有主索引ID)SelectTop M-N *From [Table] Where ID in (SelectTop M ID From [Table]) Orderby ID ...
- srtvlet filter
Filter,过滤器,顾名思义,即是对数据等的过滤,预处理过程.为什么要引入过滤器呢?在平常访问网站的时候,有时候发一些敏感的信息,发出后显示时 就会将敏感信息用*等字符替代,这就是用过滤器对信息进行 ...
- Kali Liunx 2.0震撼来袭(附下载地址、新特性和更新日志)
Kali 2.0昨天已经在BlackHat 2015 USA上正式发布了.无论是粉丝们还是Kali官方都对这个2.0版本抱有极大的期待和热情.这是第一个基于Debian Jessie的Kali版本,此 ...
- android源码追踪学习 RecipientsEditor
RecipientsEditor 新建短信时输入收接者的editor, public class RecipientsEditor extends MultiAutoCompleteTextView ...
- grafana查询中的变量templating
有时我们在管理成百上千台机器的时候,配置grafana无疑是明智的,因为你不需要一个一个的把每个机器的图形都配置一遍,利用templating就可以瞬间实现n台机器的状态显示了. templating ...
- K - Strange Country II 暴力dfs判断有向图是否连通//lxm
You want to visit a strange country. There are n cities in the country. Cities are numbered from 1 t ...
- HDU 1518 Square(DFS)
Problem Description Given a set of sticks of various lengths, is it possible to join them end-to-end ...