AMQ学习笔记 - 08. Spring-JmsTemplate之发送
概述
- 基本的发送
- 转换并发送
- 转换、后处理再发送
必需的资源
- javax.jms.ConnectionFactory
ConnectionFactory是客户端编程的开始,由它依次获取Connection、Session、Message、MessageProducer或MessageConsumer,从而做好了发送或接收的准备。 - javax.jms.Destination
Destination是发送的目的地,或者接收的源
1.ConnectionFactory资源
- public JmsTemplate(ConnectionFactory connectionFactory)
- public void setConnectionFactory(ConnectionFactory connectionFactory)
# 继承自org.springframework.jms.support.JmsAccessor
2.Destination资源
- public void setDefaultDestination(Destination destination)
设置默认的Destination - public void send(Destination destination, MessageCreator messageCreator)
将消息发送到指定的Destination
MessageCreator接口用来提供创建消息的回调方法,后面再讲。
// jt is instance of JmsTemplate
jt.setDestinationResolver(new DynamicDestinationResolver()); // set Destination解析器
jt.setPubSubDomain(false); // set Destinantion类型
- public void setDefaultDestinationName(String destinationName)
设置defaultDestination。这个方法和setDefaultDestination(Destination destination)做同样的事情,只是这个方法依赖于解析器和类型。 - public void send(String destinationName, MessageCreator messageCreator)
将消息发送到指定的Destination。这个方法和send(Destination destination, MessageCreator messageCreator)做同样的事情,只是这个方法依赖于解析器和类型。
- Queue createQueue(java.lang.String queueName)
根据name创建Queue类型的Destination - Topic createTopic(java.lang.String topicName)
根据name创建Topic类型的Destination
public class DynamicDestinationResolver implements DestinationResolver {
@Override
public Destination resolveDestinationName(Session session, String destinationName, boolean pubSubDomain)
throws JMSException {
Assert.notNull(session, "Session must not be null");
Assert.notNull(destinationName, "Destination name must not be null");
if (pubSubDomain) {
return resolveTopic(session, destinationName);
}
else {
return resolveQueue(session, destinationName);
}
}
protected Topic resolveTopic(Session session, String topicName) throws JMSException {
return session.createTopic(topicName);
}
protected Queue resolveQueue(Session session, String queueName) throws JMSException {
return session.createQueue(queueName);
}
}
根据这份源码,可以帮助理解pubSubDomain的机制,以及将JMS的api和Spring的Destination解析器这两个知识点连接起来。
1.基本的发送方法
- public void send(Destination destination, MessageCreator messageCreator)
将消息发送到指定的Destination - public void send(String destinationName, MessageCreator messageCreator)
将消息发送到指定的Destination。这个方法和send(Destination destination, MessageCreator messageCreator)做同样的事情,只是这个方法依赖于解析器和类型。 - public void send(MessageCreator messageCreator)
将消息发送到defaultDestination。
这个方法要求提前设置defaultDestination,你可以调用setDefaultDestination(Destination destination)或者setDefaultDestinationName(String destinationName)来满足这个前提。
使用MessageCreator创建消息
- Message createMessage(Session session)
jt.send(DESTINATION_NAME, new MessageCreator() { public Message createMessage(Session session) throws JMSException {
String text = "A test for MessageCreator.";
Message message = session.createTextMessage(text);
return message;
}
});
2.转换并发送的方法
- Message toMessage(Object object, Session session)
发送时用到 - Object fromMessage(Message message)
接收时用到
- public void setMessageConverter(MessageConverter messageConverter)
转换发送 | 基本发送 |
|
|
转换发送 |
String message = "a message for test convertAndSend."; |
基本发送 |
jt.send(DESTINATION_NAME, new MessageCreator() { public Message createMessage(Session session) throws JMSException { |
2比8,这纯粹是数学问题了。
3.转换、后处理再发送的方法
- Message postProcessMessage(Message message)
对消息进行处理,并返回处理后的消息
转换、后处理、发送 | 转换、发送 |
|
|
转换、后处理、发送 |
String message = "a message for test convertProcessAndSend."; |
转换、发送 |
String message = "a message for test convertAndSend."; |
在转换、后处理、发送中,我们为Message设置了一个属性:order=1.
AMQ学习笔记 - 08. Spring-JmsTemplate之发送的更多相关文章
- Spring实战第五章学习笔记————构建Spring Web应用程序
Spring实战第五章学习笔记----构建Spring Web应用程序 Spring MVC基于模型-视图-控制器(Model-View-Controller)模式实现,它能够构建像Spring框架那 ...
- 机器学习实战(Machine Learning in Action)学习笔记————08.使用FPgrowth算法来高效发现频繁项集
机器学习实战(Machine Learning in Action)学习笔记————08.使用FPgrowth算法来高效发现频繁项集 关键字:FPgrowth.频繁项集.条件FP树.非监督学习作者:米 ...
- Spring实战第八章学习笔记————使用Spring Web Flow
Spring实战第八章学习笔记----使用Spring Web Flow Spring Web Flow是一个Web框架,它适用于元素按规定流程运行的程序. 其实我们可以使用任何WEB框架写流程化的应 ...
- C++ GUI Qt4学习笔记08
C++ GUI Qt4学习笔记08 qtc++signal图形引擎文档 本章介绍Qt的二维图形引擎,Qt的二维图形引擎是基于QPainter类的.<span style="colo ...
- Spring 源码学习笔记10——Spring AOP
Spring 源码学习笔记10--Spring AOP 参考书籍<Spring技术内幕>Spring AOP的实现章节 书有点老,但是里面一些概念还是总结比较到位 源码基于Spring-a ...
- Spring 源码学习笔记11——Spring事务
Spring 源码学习笔记11--Spring事务 Spring事务是基于Spring Aop的扩展 AOP的知识参见<Spring 源码学习笔记10--Spring AOP> 图片参考了 ...
- AMQ学习笔记 - 13. Spring-jms的配置
概述 如何使用spring-jms来简化jms客户端的开发? 这篇文章主要记录如何配置以便以后复用,而非原理的讲解,有些内容我 没有掌握原理. producer端 producer端负责发送,这里使用 ...
- spring学习笔记(一) Spring概述
博主Spring学习笔记整理大部分内容来自Spring实战(第四版)这本书. 强烈建议新手购入或者需要电子书的留言. 在学习Spring之前,我们要了解这么几个问题:什么是Spring?Spring ...
- Java架构师之路 Spring学习笔记(一) Spring介绍
前言 这是一篇原创的Spring学习笔记.主要记录我学习Spring4.0的过程.本人有四年的Java Web开发经验,最近在面试中遇到面试官总会问一些简单但我不会的Java问题,让我觉得有必要重新审 ...
随机推荐
- C# 获取ORACLE SYS.XMLTYPE "遇到不支持的 Oracle 数据类型 USERDEFINED"
1.需要加函数 2.需要加表别名 select a.XML.getclobval() from TB1 a
- 洛谷 P2827 蚯蚓 解题报告
P2827 蚯蚓 题目描述 本题中,我们将用符号 \(\lfloor c \rfloor\) 表示对 \(c\) 向下取整,例如:\(\lfloor 3.0 \rfloor = \lfloor 3.1 ...
- taotao用户登录springMVC拦截器的实现
在springMVC中写拦截器,只需要两步: 一.写 java 拦截器类,实现 interceptor 拦截器接口. 二.在 springMVC 的xml配置文件中,配置我们创建的拦截器对象及其拦截目 ...
- Spring事务管理—aop:pointcut expression 常见切入点表达式及事务说明
Spring事务管理—aop:pointcut expression 常见切入点表达式及事物说明 例: <aop:config> <aop:pointcut expression= ...
- Small things are better
Yesterday I had fun time repairing 1.5Tb ext3 partition, containing many millions of files. Of cours ...
- Codeforces Round #478 C. Valhalla Siege
C. Valhalla Siege time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- nodejs 喜欢报cannot find module .....的简单解决方案
在安装nodejs后使用命令npm install <package_name>一直喜欢报cannot find module........ 因为我之前在我的电脑上安装过nodejs,当 ...
- Binding and styling text to a RichTextBox in WPF
http://www.codeproject.com/Articles/137209/Binding-and-styling-text-to-a-RichTextBox-in-WPF The Rich ...
- php windows rename 中文出错
php windows rename 中文出错 rename()函数可以重命名文件.目录等,但是要注意目的地和起始地址的编码. 比如:我的PHP文件编码是UTF-8,但是在WINDOW系统中中文默认编 ...
- 【比赛】百度之星2017 初赛Round B
第一题 题意:给定n*m网络,定义两个棋子在同行同列则相互攻击,同时要求两个棋子的行和列不能一小一大,求满足条件的最大摆放的方案数. 题解:ans=C(max(n,m),min(n,m)),就是在ma ...