2.3、使用 JmsTemplate 向 ActiveMQ 获取 Queue 类型消息

本文详细介绍如何使用JmsTemplate从ActiveMQ读取Queue类型消息,包括两种主要方法:使用receive直接获取消息和使用receiveAndConvert处理Java对象消息。并通过测试方法验证Queue消息的三大特性。

前言

在上一篇文章中(https://blog.youkuaiyun.com/bestcxx/article/details/83211928),笔者介绍了 JmsTemplate 如何向 ActiveMQ 发送 Queue 类型的消息,本文将介绍如何使用 JmsTemplate 从 ActiveMQ 中读取 Queue 类型的消息。

为什么不将发送和接收写到同一篇文章中?

笔者在学习 ActiveMQ 的过程中发现,同一类型的概念有所区分时做好分类在表述时更加方便,同时在之后的阅读体验也会更好,换句话说,分开写发送和接收是为了更好的进行所思所想进行分类阐述,散而不乱,不会让人觉得一篇文章读起来太过繁重。

发送和接收的联系和区别-本质上是对某一个 ActiveMQQueue 的操作

经过前几篇文章的阐述,笔者希望大家可以建立一种抽象的概念,即我们操作的本质是 ActiveMQQueue 对象,只要我们获取到这个对象,就可以向它发送消息,或者读取消息。
所以本质上,我们通过 JmsTemplate 向ActiveMQ 发送消息,就是向某一个特定的 ActiveMQQueue 对象发送消息,由这个对象和 ActiveMQ 进行交互,进行消息的存储和读取。(当然,如果我们讨论的是 Topic 类型的消息,则就是 ActiveMQTopic 对象了)

读取 Queue 中的消息
类的声明
/**
 * 使用 JmsTemplate 获取 Active 中 队列 消息
 * @author jie.wu
 */
@Component(value="jmsTemplateQueueConsumer")
public class JmsTemplateQueueConsumer {
private SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	
	//用于额几首 String (字符串)类型的消息
	@Resource(name="JmsTemplateQueue")
	private JmsTemplate jmsTemplate;
	
	//用于发送 Java 对象 类型的消息
	@Resource(name="JmsTemplateQueueForBean")
	private JmsTemplate JmsTemplateQueueForBean;
}
方法一:receive
//方式一:JmsTemplate.receive
	 public void jmsTemplateQueueConsumerString(){
	 
	 		//输出信息,便于了解程序触发时间
			System.out.println(sdf.format(new Date())+":"+this.getClass().getName()+";jmsTemplateQueueConsumerString 从ActiveMQ获取消息:");
			ObjectMessage receiveMessage=(ObjectMessage) jmsTemplate.receive();

			try {
				System.out.println(receiveMessage.getObject().toString());
			} catch (JMSException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	 }
方法二:
//方式二:JmsTemplate.receiveAndConvert()
	 public void jmsTemplateQueueConsumerStringConvertAndSend(){
		//输出信息,便于了解程序触发时间
		System.out.println(sdf.format(new Date())+":"+this.getClass().getName()+";jmsTemplateQueueConsumerStringConvertAndSend 从ActiveMQ获取消息:");
		System.out.println((String) jmsTemplate.receiveAndConvert());
	 }
使用 receiveAndConvert 处理 Java 对象消息
//JmsTemplate.receiveAndConvert 处理 Java 对象
	public void jmsTemplateQueueConsumerForBeanReceiveAndConvert(){
		//输出信息,便于了解程序触发时间
		System.out.println(sdf.format(new Date())+":"+this.getClass().getName()+";jmsTemplateQueueProductForBeanReceiveAndConvert ;从 ActiveMQ 获取消息");
		System.out.println(JmsTemplateQueueForBean.receiveAndConvert());
	}
测试方法
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.bestcxx.stu.jms.consumer.JmsTemplateQueueConsumer;

@DirtiesContext  
@RunWith(SpringJUnit4ClassRunner.class)  
@ContextConfiguration(locations={"classpath:applicationContext.xml"})  
public class TestJmsTemplateQueueConsumer {
	@Autowired
	private JmsTemplateQueueConsumer jmsTemplateQueueConsumer;
	@Test
	public void testJmsTemplateQueueConsumerString(){
		jmsTemplateQueueConsumer.jmsTemplateQueueConsumerString();
	}
	@Test
	public void testJmsTemplateQueueConsumerStringConvertAndSend(){
		jmsTemplateQueueConsumer.jmsTemplateQueueConsumerStringConvertAndSend();
	}
	@Test
	public void testJmsTemplateQueueConsumerForBeanReceiveAndConvert(){
		jmsTemplateQueueConsumer.jmsTemplateQueueConsumerForBeanReceiveAndConvert();
	}	
}
验证 Queue 消息的三个特性

建议读者验证 ActiveMQ 中 Queue 类型消息的三个特性
1、队列之间互不干扰,即向那个队列发送从那个队列获取互不干扰
2、如果一个消费者在请求队列时没有消息,则会等待,反之,如果一个消息到达队列而没有消费者消费时,这个消息会被存储
3、一个消息只能被一个消费者消费

参考资料

[1].《Spring 实战 第4版》
[2].https://blog.youkuaiyun.com/winter_chen001/article/details/78409125
[3].https://blog.youkuaiyun.com/u013123635/article/details/78362360
[4].https://www.cnblogs.com/Peter2014/p/8080192.html
[5].https://blog.youkuaiyun.com/wowwilliam0/article/details/81110943
[6].https://blog.youkuaiyun.com/lsj960922/article/details/79926947
[7].https://blog.youkuaiyun.com/wangfengwf/article/details/78966704
[8].https://blog.youkuaiyun.com/hpttlook/article/details/23391967
[9].https://blog.youkuaiyun.com/super_scan/article/details/39837591

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值