1.异步消息Jms及其JmsTemplate的源代码分析,消息代理ActiveMQ
一. 介绍
借助Spring,有多种异步消息的可选方案,本章使用Jms。Jms的消息模型有两种,点对点消息模型(队列实现)和发布-订阅消息模型(主题)。

图1.点对点消息模型(一对一)

图2.发布-订阅消息模型(一对多)
二. 仅适用Jsm
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage; import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQQueue; public class Testjsm {
public static void main(String[] args) throws JMSException {
ConnectionFactory cf = new ActiveMQConnectionFactory("tcp://localhost:61616");
// 连接
Connection conn = cf.createConnection();
// 创建回话
Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
// 创建目的地,队列的名称
Destination destination = new ActiveMQQueue("SimperConteoller.topic");
// 创建发送者
MessageProducer producer = session.createProducer(destination);
// 创建发送内容
TextMessage message = session.createTextMessage();
message.setText("hello,大家好");
// 发送
producer.send(message);
} }
步骤为:
1.创建工厂 2.创建连接 3.创建会话 4.创建目的地 5.创建发送者 6.创建发送内容 7.发送者进行发送内容的发送
代码步骤多,且异常不好处理。
二. 使用模板
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import javax.jms.TextMessage; 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 Test {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring-jsm.xml");
JmsTemplate tem = (JmsTemplate) context.getBean("jmsTemplate");
Destination des = (Destination) context.getBean("topic");
tem.send(des, new MessageCreator() { public Message createMessage(Session arg0) throws JMSException { return arg0.createTextMessage("大家好!");
}
}); TextMessage ms = (TextMessage) tem.receive(des);
System.out.println("接受者是:" + ms); }
}
步骤:只需要发送。使用JmsTemplate,可以创建连接,获得会话以及发送和接受消息,使代码专注于构建要发送的消息和处理接受到的消息。
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-broker</artifactId>
<version>5.12.0</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>4.1.6.RELEASE</version>
<scope>compile</scope>
</dependency>

<?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:p="http://www.springframework.org/schema/p"
xmlns:amq="http://activemq.apache.org/schema/core"
xmlns:jms="http://www.springframework.org/schema/jms"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd"> <bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory" p:brokerURL="tcp://localhost:61616">
</bean> <bean id="topic" class="org.apache.activemq.command.ActiveMQTopic" c:_="SimperConteoller.topic"></bean> <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate" c:_-ref="connectionFactory">
</bean> </beans>

tem.send(des, new MessageCreator() {
public Message createMessage(Session arg0) throws JMSException {
return arg0.createTextMessage("大家好!");
}
});
源代码:
@Override
public void send(final Destination destination, final MessageCreator messageCreator) throws JmsException {
execute(new SessionCallback<Object>() {
@Override
public Object doInJms(Session session) throws JMSException {
doSend(session, destination, messageCreator);
return null;
}
}, false);
}
send函数:1. execute:创建连接与会话
2.doSend:创建发送者和发送内容,并发送
execute源代码:创建连接与会话
public <T> T execute(SessionCallback<T> action, boolean startConnection) throws JmsException {
Assert.notNull(action, "Callback object must not be null");
Connection conToClose = null;
Session sessionToClose = null;
try {
Session sessionToUse = ConnectionFactoryUtils.doGetTransactionalSession(
getConnectionFactory(), this.transactionalResourceFactory, startConnection);
if (sessionToUse == null) {
conToClose = createConnection();
sessionToClose = createSession(conToClose);
if (startConnection) {
conToClose.start();
}
sessionToUse = sessionToClose;
}
if (logger.isDebugEnabled()) {
logger.debug("Executing callback on JMS Session: " + sessionToUse);
}
return action.doInJms(sessionToUse);
}
catch (JMSException ex) {
throw convertJmsAccessException(ex);
}
finally {
JmsUtils.closeSession(sessionToClose);
ConnectionFactoryUtils.releaseConnection(conToClose, getConnectionFactory(), startConnection);
}
}
doSend源代码:创建发送者和发送内容,并发送
protected void doSend(Session session, Destination destination, MessageCreator messageCreator)
throws JMSException { Assert.notNull(messageCreator, "MessageCreator must not be null");
MessageProducer producer = createProducer(session, destination);
try {
Message message = messageCreator.createMessage(session);
if (logger.isDebugEnabled()) {
logger.debug("Sending created message: " + message);
}
doSend(producer, message);
// Check commit - avoid commit call within a JTA transaction.
if (session.getTransacted() && isSessionLocallyTransacted(session)) {
// Transacted session created by this template -> commit.
JmsUtils.commitIfNecessary(session);
}
}
finally {
JmsUtils.closeMessageProducer(producer);
}
}
protected void doSend(MessageProducer producer, Message message) throws JMSException {
if (this.deliveryDelay >= 0) {
if (setDeliveryDelayMethod == null) {
throw new IllegalStateException("setDeliveryDelay requires JMS 2.0");
}
ReflectionUtils.invokeMethod(setDeliveryDelayMethod, producer, this.deliveryDelay);
}
if (isExplicitQosEnabled()) {
producer.send(message, getDeliveryMode(), getPriority(), getTimeToLive());
}
else {
producer.send(message);
}
}
1.异步消息Jms及其JmsTemplate的源代码分析,消息代理ActiveMQ的更多相关文章
- Android应用Activity、Dialog、PopWindow、Toast窗体加入机制及源代码分析
[工匠若水 http://blog.csdn.net/yanbober 转载烦请注明出处.尊重劳动成果] 1 背景 之所以写这一篇博客的原因是由于之前有写过一篇<Android应用setCont ...
- Java设计模式-代理模式之动态代理(附源代码分析)
Java设计模式-代理模式之动态代理(附源代码分析) 动态代理概念及类图 上一篇中介绍了静态代理,动态代理跟静态代理一个最大的差别就是:动态代理是在执行时刻动态的创建出代理类及其对象. 上篇中的静态代 ...
- Oozie JMS通知消息实现--根据作业ID来过滤消息
一,介绍 本文使用Oozie的消息通知功能,并根据JMS规范中的消息选择器(Selector)实现 根据作业的ID来过滤消息. 首先搭建好JMS Provider(ActiveMQ) ,并进行相关配置 ...
- JAVA消息 JMS 很重要
首先大致讲一下,java 消息模块 消息,个人理解分为两种:1.同步消息(RPC调用) 2.异步消息(本篇讲解部分) 一.同步消息java提供了多种方案: 最新比较常用的方式就是spring Http ...
- Android异步任务处理框架AsyncTask源代码分析
[转载请注明出处:http://blog.csdn.net/feiduclear_up CSDN 废墟的树] 引言 在平时项目开发中难免会遇到异步耗时的任务(比方最常见的网络请求).遇到这样的问题.我 ...
- Raw-OS源代码分析之消息系统-Queue_Size
分析的内核版本号截止到2014-04-15.基于1.05正式版.blogs会及时跟进最新版本号的内核开发进度,若源代码凝视出现"???"字样,则是未深究理解部分. Raw-OS官方 ...
- JMS学习(二)- JMS Message Model 组成介绍及消息头详解
一.前言 从本文起依次详细介绍JMS中的一些重要的概念,主要参考了官方的JMS1.1的文档,该文档很老了,是02年的,那年,JAVA还没有被Oracle收购..本文主要介绍Message及其相关概念, ...
- Memcached源代码分析 - Memcached源代码分析之消息回应(3)
文章列表: <Memcached源代码分析 - Memcached源代码分析之基于Libevent的网络模型(1)> <Memcached源代码分析 - Memcached源代码分析 ...
- RTMPdump(libRTMP) 源代码分析 10: 处理各种消息(Message)
===================================================== RTMPdump(libRTMP) 源代码分析系列文章: RTMPdump 源代码分析 1: ...
随机推荐
- Java:过去、未来的互联网编程之王
Java对你而言是什么?一门你大学里学过的语言?一个IT行业的通用语言?你相信Java已经为下一次互联网爆炸做好了准备么?Java 一方面为嵌入式计算做了增强,而另一方面为实时应用做了精简,本文将介绍 ...
- vs报算术运算溢出的错误
是因为查询的数据量太大,把数据量减少点就不会报这个错了. 或者查询速度快点比如加索引也可能解决,待确定.
- [Flex] PopUpButton系列 —— 添加按钮图标
<?xml version="1.0" encoding="utf-8"?><!--为主按钮添加默认图标 PopUpButtonIcon.mx ...
- nginx优化之request_time 和upstream_response_time差别
笔者在根据nginx的accesslog中$request_time进行程序优化时,发现有个接口,直接返回数据,平均的$request_time也比较大.原来$request_time包含了(服务器) ...
- eclipse导入不到嵌套的项目
search for nested projects搜索不到嵌套项目
- DELPHI SOKET 编程(使用TServerSocket和TClientSocket) 转
http://www.cnblogs.com/findumars/p/5272658.html 本文采用delphi7+TServerSocket+TClientSocket; 笔者在工作中遇到对 ...
- font awesome的图标在WP8浏览器下无法显示的问题解决
font awesome无疑是bootstrap上面做的很赞第三方图标 笔者最近做的一个项目,图标在iphone和安卓上面的浏览器上显示都无问题,偏偏WP8上的浏览器显示有问题 通过chrome的开发 ...
- 在weblogic11g上发布项目遇到的一个错误(不支持web-app_3_0)
错误信息如下: 消息图标 - 错误 [HTTP:101064][WebAppModule(ssh1:ssh1.war)] Error parsing descriptor in Web appplic ...
- oracle分布式事务总结-转载
基本概念 Local Coordinator:在分布事务中,必须参考其它节点上的数据才能完成自己这部分操作的站点. Global Coordinator:分布事务的发起者,负责协调这个分布事务. Co ...
- 用imageNamed加载图片产生的问题
通常我们会用imageNamed:来加载图片,但是用这个API有个问题,就是它会缓存加载的image. 因此,对于那些被重用的图片,这个API很高效.但是对于那些使用很少的图片,用这个就很耗内存,那怎 ...