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格式传送 ...
随机推荐
- 解决Linux下3T硬盘分区只有2T(2199G)可用
分区转换成GPT即可 sudo parted /dev/sdb 将MBR硬盘格式化为GPT mklabel gpt 之后可以看一下状态 print 整个硬盘空间只分一个区 mkpart primary ...
- ConfigurationManager.AppSettings 属性 appSettings
https://msdn.microsoft.com/zh-cn/library/system.configuration.configurationmanager.appsettings(v=vs. ...
- How to manually remove an infected file from your computer
http://blog.csdn.net/pipisorry/article/details/41258577 How to manually remove an infected file from ...
- centos下nginx启动脚本和chkconfig管理
在安装完nginx后,重新启动需要“kill -HUP nginx进程编号”来进行重新加载,显然十分不方便.如果能像apache一样,直接通过脚本进行管理就方便多了. nginx官方早就想好了,也提供 ...
- svn的外网设置访问方法
一.花生壳 1. 设置静态ip 选择 DHCP服务器 -> 静态地址分配,为内网内的机器分配静态ip 2.设置端口转发 选择 转发规则 -> 虚拟服务器,将外网对443端口的访问转发到安装 ...
- oracle的一些问题
好久时间没有用oracle,这次因为有个项目的需要,又重新温习了一下oracle. 我使用的oracle的版本是windows oracle 11g_R2. 首先先说一下安装.这个没有太大的问题,主要 ...
- java-ApiValueDemo
关于API相关正则表达式,各方法的使用 package com.example; import java.util.Arrays; import java.util.Scanner; /** * Ap ...
- git设置及常用命令
下载 下载地址:https://git-scm.com/downloads windows系统安装 默认安装即可. 其它系统安装方式请自行百度,网上教程很多. 设置 windows桌面右键,选择Git ...
- Python tkinter 用键盘移动没反映修改代码
from tkinter import * def movetriangle(event): if event.keysym == 'Up': canvas.move(a1 ...
- 以css为例谈设计模式
什么是设计模式? 曾有人调侃,设计模式是工程师用于跟别人显摆的,显得高大上:也曾有人这么说,不是设计模式没用,是你还没有到能懂它,会用它的时候. 先来看一下比较官方的解释:"设计模式(Des ...