JmsTemplate 发送方式
---恢复内容开始---
背景:
原来我准备是setDefaultDestinationName 设置队列的名称 发现 系统运行后 创建 的并不是队列 ,而是Topic ,
自己看下源码,发现在创建时候,需要观察session的类型
- 基本的发送
- 转换并发送
- 转换、后处理再发送
必需的资源
- 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接口用来提供创建消息的回调方法,后面再讲。
1 // jt is instance of JmsTemplate
2 jt.setDestinationResolver(new DynamicDestinationResolver()); // set Destination解析器
3 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)

1 jt.send(DESTINATION_NAME, new MessageCreator() {
2
3 public Message createMessage(Session session) throws JMSException {
4 String text = "A test for MessageCreator.";
5 Message message = session.createTextMessage(text);
6 return message;
7 }
8 });

2.转换并发送的方法
- Message toMessage(Object object, Session session)
发送时用到 - Object fromMessage(Message message)
接收时用到
- public void setMessageConverter(MessageConverter messageConverter)
| 转换发送 | 基本发送 |
|
|
| 转换发送 |
1 String message = "a message for test convertAndSend."; |
| 基本发送 |
![]() 1 jt.send(DESTINATION_NAME, new MessageCreator() {
![]() |
2比8,这纯粹是数学问题了。
3.转换、后处理再发送的方法
- Message postProcessMessage(Message message)
对消息进行处理,并返回处理后的消息
| 转换、后处理、发送 | 转换、发送 |
|
|
| 转换、后处理、发送 |
![]() 1 String message = "a message for test convertProcessAndSend."; ![]() |
| 转换、发送 |
1 String message = "a message for test convertAndSend."; |
在转换、后处理、发送中,我们为Message设置了一个属性:order=1.
---恢复内容结束---
JmsTemplate 发送方式的更多相关文章
- spring-jms,spring-boot-starter-activemq JmsTemplate 发送方式
spring-jms,spring-boot-starter-activemq JmsTemplate 发送方式 背景: 原来我准备是setDefaultDestinationName 设置队列的名称 ...
- HTML中发送方式(method)中get和post的比较
get的方式是将表单控件的控件名name和取值value信息经过编码后,通过URL发送(可以在地址栏里看到).而post则将表单的内容通过http发送.一个 get通过URL传送变量,能传送的数据总量 ...
- post 发送方式
/** * post 方式 解码 */ public static String getWebContentByPost(String urlString, String data, final St ...
- android handler ,message消息发送方式
1.Message msg = Message.obtain(mainHandler) msg.obj=obj;//添加你需要附加上去的内容 msg.what = what;//what消息处理的类 ...
- python实现邮件发送完整代码(带附件发送方式)
实例一:利用SMTP与EMAIL实现邮件发送,带附件(完整代码) __author__ = 'Administrator'#coding=gb2312 from email.Header import ...
- Android为TV端助力 handler ,message消息发送方式
1.Message msg = Message.obtain(mainHandler) msg.obj=obj;//添加你需要附加上去的内容 msg.what = what;//what消息处理的类 ...
- activemq jmsTemplate 发送消息速度太慢
http://activemq.apache.org/async-sends.html http://bbs.csdn.net/topics/370062485
- ActiveMQ学习总结(10)——ActiveMQ采用Spring注解方式发送和监听
对于ActiveMQ消息的发送,原声的api操作繁琐,而且如果不进行二次封装,打开关闭会话以及各种创建操作也是够够的了.那么,Spring提供了一个很方便的去收发消息的框架,spring jms.整合 ...
- PHP通过XML报文格式的POST请求方式,与第三方接口交互(发送xml,获取XML,并解析xml步骤)
开发者端:发送请求,并接收结果 <?php // 下面的demo,实现的功能如下: // 1-开发者需要判断一个用户是否存在,去请求第三方接口. // 2-与第三方接口的通信,是以xml格式传送 ...
随机推荐
- Github——Git设置及GitHub的使用
把github上的help. First : 安装:ubuntu 下,终端输入命令: sudo apt-get install git-core git-gui git-doc Next : 设置SS ...
- php中的 file_get_contents(‘php://input’)用法
php中的 file_get_contents('php://input')用法: file_get_contents 获取php页面中input内容的值: eg: php: 页面提交了usernam ...
- VLOOKUP多条件查找不使用辅助列
问题描述: 可能很多人在使用VLOOKUP的时候,对于多条件时,则会使用辅助列进行查找,那么不使用辅助列是否也可以呢? 答案是:必须可以. 问题处理: 1.使用辅助列 2.不使用辅助列的操作如下: 公 ...
- POJ1088 动态规划
题意: id=1088">题目链接 解答: 这个题目和最长子序列什么的极为类似.只是之前都是一维,如今变成二维的了.仅此而已.因此我们能够想办法把它先变成一维的. 先用一个结构体存储这 ...
- Jmeter-安装及配置(一)
Apache JMeter是Apache组织开发的基于Java的压力测试工具.用于对软件做压力测试,它最初被设计用于Web应用测试,但后来扩展到其他测试领域. 它可以用于测试静态和动态资源,例如静态文 ...
- Centos下Subversion 服务器安装配置
1.安装 # yum install subversion 2. svn配置 建立svn版本库目录可建多个:2.1 新建文件夹: # mkdir -p /opt/svndata/repos 2.2 建 ...
- LVM详解笔记pv-vg-lv创建和扩展
LVM Logical Volume Manager(逻辑卷管理) 是Linux环境下对底层磁盘的一种管理机制(方式),处在物理磁盘和文件系统之间. 名词: PV (Physical Volume)物 ...
- Linux C 中 open close read write 使用实例
这里实现的是将文件cody.txt中的内容拷贝到to_cody.txt中去. 1 /* ======================================================== ...
- Ionic学习笔记1_基本布局
<body> <!-- 头部 --> bar里嵌入子元素:title,button,button-bar和 inpu ...
- cef
http://blog.csdn.net/hats8888/article/details/53886591 http://blog.csdn.net/gong_hui2000/article/det ...