博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
spring整合JMS - 基于ActiveMQ实现
阅读量:4558 次
发布时间:2019-06-08

本文共 2982 字,大约阅读时间需要 9 分钟。

一. 开篇语

继上一篇后, 由于近期一直在复习spring的东西, 所以本文就使用spring整合下JMS.

二. 环境准备

1. ActiveMQ5.2.0 (activemq-all-5.2.0.jar)

2. spring2.5 (spring.jar)

3. JavaEE5

4. JDK1.6

注意: 測试前请先启动ActiveMQserver

三. 代码測试(P2P)

1. MsgSender: 消息生产者

/** * message sender */public class MsgSender {	public static void main(String[] args) throws Exception {		// load xml and create bean factory		ApplicationContext ctx = new ClassPathXmlApplicationContext("/applicationContext.xml");				// get JmsTemplate object from spring container		JmsTemplate jmsTemplate = (JmsTemplate) ctx.getBean("jmsTemplate");				// get Destination object from spring container		Destination destination = (Destination) ctx.getBean("destination");		// send msg to activeMQ server		jmsTemplate.send(destination, new MessageCreator() {			TextMessage message = null;			public Message createMessage(Session session) {				try {					String str = "hello activeMQ!";					message = session.createTextMessage(str);					System.out.println("send: " + str);				} catch (Exception e) {					throw new RuntimeException("error happens...", e);				}				return message;			}		});	}}

2. MsgReceiver: 消息消费者

/** * message receiver */public class MsgReceiver {	public static void main(String[] args) throws Exception {		// load xml and create bean factory		ApplicationContext ctx = new ClassPathXmlApplicationContext("/applicationContext.xml");				// get JmsTemplate object from spring container		JmsTemplate jmsTemplate = (JmsTemplate) ctx.getBean("jmsTemplate");				// get Destination object from spring container		Destination destination = (Destination) ctx.getBean("destination");				while (true) {			// receive msg from activeMQ server			TextMessage txtmsg = (TextMessage) jmsTemplate.receive(destination);			if (null != txtmsg){				System.out.println("receive: " + txtmsg.getText());			}else{				break;			}		}	}}
3. 配置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" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <!-- config JMS connection factory --> <bean id="connectionFactory" class="org.apache.activemq.spring.ActiveMQConnectionFactory"> <property name="brokerURL" value="tcp://localhost:61616" /> </bean> <!-- config JMS template --> <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"> <property name="connectionFactory" ref="connectionFactory" /> </bean> <!-- config message send destination(queue) --> <bean id="destination" class="org.apache.activemq.command.ActiveMQQueue"> <!-- set the name of message queue --> <constructor-arg index="0" value="myQueue" /> </bean> </beans>

4. 源代码下载地址: 

转载于:https://www.cnblogs.com/mengfanrong/p/5069410.html

你可能感兴趣的文章
2、文件夹
查看>>
11、求二进制中1的个数
查看>>
【nodejs】让nodejs像后端mvc框架(asp.net mvc)一样处理请求--请求处理结果适配篇(7/8)...
查看>>
CodeForces 731A Night at the Museum
查看>>
MySQL 删除数据库
查看>>
JavaScript 字符串(String) 对象
查看>>
How to use VisualSVN Server and TortoiseSVN to host your codes and control your codes' version
查看>>
微信小程序picker组件 - 省市二级联动
查看>>
Dynamics CRM 给视图配置安全角色
查看>>
Eclipse修改已存在的SVN地址
查看>>
(转)使用 python Matplotlib 库绘图
查看>>
进程/线程切换原则
查看>>
正则表达式语法
查看>>
20165301 2017-2018-2 《Java程序设计》第四周学习总结
查看>>
Vue的简单入门
查看>>
urllib 中的异常处理
查看>>
通过SQL Server的扩展事件来跟踪SQL语句在运行时,时间都消耗到哪儿了?
查看>>
比较:I/O成员函数getline() 与 get()(第二种用法)的用法异同
查看>>
7.内部类(一)之详解内部类
查看>>
1.messager消息提示框
查看>>