概要


JmsTemplate提供了4组*3,共计12个接收用的方法。
 
JmsTemplate接收所需要的资源ConnectionFactory和Destination,和发送是一致的。
 
接收的方法有4组:
  1. 基本的接收
  2. 接收并转换
  3. 带有选择器的接收
  4. 选择接收并转换

1.基本的接收方法


JmsTemplate提供了3个基本的接收方法,与发送的方法进行对比:
接收 发送
  • public Message receive(Destination destination)
    从指定的Destination接收消息并返回
  • public Message receive(String destinationName)
    从指定的Destination接收消息并返回。
  • public Message receive()
    从默认的Destination接收消息并返回。
  • public void send(Destination destination, MessageCreator messageCreator)
    将消息发送到指定的Destination
  • public void send(String destinationName, MessageCreator messageCreator)
    将消息发送到指定的Destination。
  • public void send(MessageCreator messageCreator)
    将消息发送到defaultDestination。
接收到消息之后,需要对消息进行转换,再提取消息中的数据:
 Message message = jt.receive(DESTINATION_NAME);

 // convert
if (message != null && message instanceof TextMessage) {
String text = ( (TextMessage) message ).getText();
System.out.println(text);
}

2.接收并转换的方法


与转换并发送的方法相对应,接收的时候也有接收并转换的方法:
  • public Object receiveAndConvert()
    参考public Message receive(),在此基础上做了转换。
  • public Object receiveAndConvert(Destination destination)
    参考public Message receive(Destination destination),在此基础上做了转换。
  • public Object receiveAndConvert(String destinationName)
    参考public Message receive(String destinationName),在此基础上做了转换。
下面给出使用的示例,并把上面的示例也拿过来,做一下比较:
convert
 Object data = jt.receiveAndConvert(DESTINATION_NAME);

 if (data != null && data instanceof String) {
System.out.println(data);
usual
 Message message = jt.receive(DESTINATION_NAME);

 // convert
if (message != null && message instanceof TextMessage) {
String text = ( (TextMessage) message ).getText();
System.out.println(text);
至少,我们不用再考虑收到的Message具体是哪一种类型的。

3.带有选择器的接收方法


到目前为止,我们介绍的接收会无差别接收指定的Destination中的消息。如果我们只想接收其中的一部分——基于一个筛选的条件,满足条件的消息快到碗里来,不满足的就留在锅里吧。这种“挑食”的行为就是通过选择器来实现的。
04. 消息选择器中,我们已经介绍了选择器是什么一回事,并给出了基于JMS API的demo;这里我们直接列出要说的三个方法吧:
  • public Message receiveSelected(String messageSelector)
    参考public Message receive(),只是加入了筛选条件。
  • public Message receiveSelected(Destination destination, String messageSelector)
    参考public Message receive(Destination destination),只是加入了筛选条件
  • public Message receiveSelected(String destinationName, String messageSelector)
    参考public Message receive(String destinationName),只是加入了筛选条件
 
然后,再给一个demo:
发送
 String message = "a message for test convertProcessAndSend.";
jt.convertAndSend(DESTINATION_NAME, message,
new MessagePostProcessor() { public Message postProcessMessage(Message message)
throws JMSException {
message.setIntProperty("order", 1);
return message;
}
}); 
接收
 jt.setReceiveTimeout(3*1000); // in milliseconds

 String messageSelector = "order = 1";
Message message = jt.receiveSelected(DESTINATION_NAME, messageSelector); if (message != null && message instanceof TextMessage) {
String text = ((TextMessage) message).getText();
System.out.println(text);
消息选择器的语法类似于sql。你可能注意到在接收的时候,有调用jt.setReceiveTimeout(3*1000);这是因为JmsTemplate所有的接收,包括前面讲的和后面要讲的,都是阻塞式的——阻塞线程,等待接收,直到超过了预先设定的receiveTimeout,单位是毫秒。而它的初始值是0,表示无限等待。

4.选择接收并转换的方法


将选择器和转换进行叠加,我们得到了另外的三个方法:
  • public Object receiveSelectedAndConvert(String messageSelector)
  • public Object receiveSelectedAndConvert(Destination destination, String messageSelector)
  • public Object receiveSelectedAndConvert(String destinationName, String messageSelector)
 
如果你理解了选择器和转换的接收,应该可以轻松了解上面的方法。这里给出一个demo:
 jt.setReceiveTimeout(3 * 1000); // in milliseconds

 String messageSelector = "order = 1";
Object data = jt.receiveSelectedAndConvert(DESTINATION_NAME, messageSelector); if (data != null && data instanceof String) {
System.out.println(data);
}

AMQ学习笔记 - 09. Spring-JmsTemplate之接收的更多相关文章

  1. Spring实战第八章学习笔记————使用Spring Web Flow

    Spring实战第八章学习笔记----使用Spring Web Flow Spring Web Flow是一个Web框架,它适用于元素按规定流程运行的程序. 其实我们可以使用任何WEB框架写流程化的应 ...

  2. 机器学习实战(Machine Learning in Action)学习笔记————09.利用PCA简化数据

    机器学习实战(Machine Learning in Action)学习笔记————09.利用PCA简化数据 关键字:PCA.主成分分析.降维作者:米仓山下时间:2018-11-15机器学习实战(Ma ...

  3. Spring实战第五章学习笔记————构建Spring Web应用程序

    Spring实战第五章学习笔记----构建Spring Web应用程序 Spring MVC基于模型-视图-控制器(Model-View-Controller)模式实现,它能够构建像Spring框架那 ...

  4. C++ GUI Qt4学习笔记09

    C++ GUI Qt4学习笔记09   qtc++ 本章介绍Qt中的拖放 拖放是一个应用程序内或者多个应用程序之间传递信息的一种直观的现代操作方式.除了剪贴板提供支持外,通常它还提供数据移动和复制的功 ...

  5. Spring 源码学习笔记10——Spring AOP

    Spring 源码学习笔记10--Spring AOP 参考书籍<Spring技术内幕>Spring AOP的实现章节 书有点老,但是里面一些概念还是总结比较到位 源码基于Spring-a ...

  6. Spring 源码学习笔记11——Spring事务

    Spring 源码学习笔记11--Spring事务 Spring事务是基于Spring Aop的扩展 AOP的知识参见<Spring 源码学习笔记10--Spring AOP> 图片参考了 ...

  7. AMQ学习笔记 - 12. Spring-JmsTemplate特性设置

    概述 这是关于JmsTemplate的最后一篇总结,且只会介绍几个比较重要的特性. 消息的递送模式 在发送消息给时,可以告知这是持久化的消息,还是非持久化的消息.如果是非持久化的消息,broker会将 ...

  8. spring学习笔记(一) Spring概述

    博主Spring学习笔记整理大部分内容来自Spring实战(第四版)这本书.  强烈建议新手购入或者需要电子书的留言. 在学习Spring之前,我们要了解这么几个问题:什么是Spring?Spring ...

  9. Java架构师之路 Spring学习笔记(一) Spring介绍

    前言 这是一篇原创的Spring学习笔记.主要记录我学习Spring4.0的过程.本人有四年的Java Web开发经验,最近在面试中遇到面试官总会问一些简单但我不会的Java问题,让我觉得有必要重新审 ...

随机推荐

  1. iOS开发——UI篇Swift篇&UIImageView

    UIImageView override func viewDidLoad() { super.viewDidLoad() titleLabel.text = titleString //通过坐标和大 ...

  2. iOS开发——UI篇Swift篇&UISwitch/UIStepper

    UISwitch/UIStepper override func viewDidLoad() { super.viewDidLoad() titleLabel.text = titleString / ...

  3. IPC——匿名管道

    Linux进程间通信——使用匿名管道 在前面,介绍了一种进程间的通信方式:使用信号,我们创建通知事件,并通过它引起响应,但传递的信息只是一个信号值.这里将介绍另一种进程间通信的方式——匿名管道,通过它 ...

  4. SSMTP—让Linux系统从Office 365发送邮件

    SSMTP-让Linux系统从Office 365发送邮件 导读 SSMTP 是一个非常简单实用的小工具,它可以将 Linux 系统的电子邮件中继到 Office 365.Google 或其它第三方 ...

  5. 认识C中的结构体

    C中结构体是另外一种表示数据形式的方式,结构体中可以表示C中的基本数据形式,如int,double....结构体可以让我们更好的表示数据.下面来看看结构体. 说到结构体首先要了解的是它的申明形式,要申 ...

  6. nodejs的mysql模块学习(八)关闭连接池

    关闭连接池 可以用pool.end()关闭连接池 pool.end(function (err) { // 所有的连接都已经被关闭 }); 当关闭之后pool将不可以getconnection()

  7. BeginInvoke、ThreadPool、Task三类异步方法的区别和速度比较

      速度(最快为1) 返回值 多参数 等待在时限内完成 超时后结束 ThreadPool.UnsafeQueueUserWorkItem() 1 非原生支持1 非原生支持 非原生支持3 不支持 Thr ...

  8. 重构5-Pull Up Field(字段上移)

    我们来看看一个和上移方法十分类似的重构.我们处理的不是方法,而是字段. public abstract class Account{} public class CheckingAccount ext ...

  9. 程序员新人怎样在复杂代码中找 bug?

    分享下我的debug的经验 1. 优先解决那些可重现的,可重现的bug特别好找,反复调试测试就好了,先把好解决的干掉,这样最节约时间. 2. 对于某些bug没有头绪或者现象古怪不知道从哪里下手,找有经 ...

  10. Java IO 技术之基本流类

    流式IO 流(Stream)是字节的源或目的.         两种基本的流是:输入流(Input Stream)和输出流(Output Stream).可从中读出一系列字节的对象称为输入流.而能向其 ...