Spring + ActiveMQ实现jms发送消息
1. 概述:Spring提供了一个用于简化JMS API使用的抽象框架,并且对用户屏蔽了JMS API中1.0.2和1.1版本的差异。
JMS的功能大致上分为两块,叫做消息制造和消息消耗。JmsTemplate 用于制造消息和同步消息接收。我们今天就用JmsTemplate实现同步的消息接受。
使用JMS发(接)消息的步骤:
1)创建连接工厂
2)使用连接工厂创建连接
3)使用连接创建会话
4)获取一个目的地
5)使用会话和目的地创建消息生产者(消息消费者)
6)使用连接创建一个需要发送的消息类型实例
7)使用连接的一个队列发送器或主题公布器,使用发送器或者主题器发送消息(接受消息)
spring中的JmsTemplate实现了对jms的一些封装,内部提供了很多的方法,我们只需要实现定义的回调接口即可。JmsTemplate继承自JmsAccessor,在JmsAccessor中有ConnectionFactory的定义,而JmsTemplate本身的构造方法也有对ConnectionFactory的封装:
public JmsTemplate(ConnectionFactory connectionFactory) { this(); setConnectionFactory(connectionFactory); afterPropertiesSet(); }
所以,我们有两种方式注入ConnectionFactory,本文我们采用构造方法的方式。
spring_jms.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <!-- 这里我们用构造方法注入 connectionFactory--> <bean id = "jmsTemplate" class = "org.springframework.jms.core.JmsTemplate"> <constructor-arg ref="connectionFactory"></constructor-arg> </bean> <!-- 使用activemq中的连接工厂,提供一个brokerUrl,这里表示本地 --> <bean id = "connectionFactory" class = "org.apache.activemq.ActiveMQConnectionFactory"> <property name="brokerURL" value="vm://localhost" /> </bean> <!-- 使用activemq中的点对点消息模型,随意指定一个地址 --> <bean id="destination" class="org.apache.activemq.command.ActiveMQQueue"> <constructor-arg value="test/queue"/> </bean> </beans>
MessageCreator 回调接口通过JmsTemplate中调用代码提供的Session来创建一条消息。
看一下MessageCreator接口:
public interface MessageCreator { Message createMessage(Session session) throws JMSException; }
那么,我们来实现发送和接受消息DummyJms类
public class DummyJms { public static void main(String[] args) throws Exception{ ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml"); JmsTemplate jmsTemplate = (JmsTemplate)context.getBean("jmsTemplate"); Destination destination = (Destination)context.getBean("destination"); jmsTemplate.send(destination, new MessageCreator(){ public Message createMessage(Session session) throws JMSException { return session.createTextMessage("send message "); } }); TextMessage msg = (TextMessage)jmsTemplate.receive(destination); System.out.println("receive message = " + msg.getText()); } }
输出结果:
receive message = send message
可是我们并没有看到的像前文描述的那那些创建消息生产者,消息消费者的一些东西。继续分析,我们可以看一下,
jmsTemplate.send(Destination destination,MessageCreator messageCreator)这里到底做了什么,可以让我们不费吹灰之力,就可以实现消息的发送。JmsTemplate源代码:
public void send(final Destination destination, final MessageCreator messageCreator) throws JmsException { execute(new SessionCallback<Object>() { public Object doInJms(Session session) throws JMSException { doSend(session, destination, messageCreator); return null; } }, false); }
JmsTemplate实现了JmsOperations接口,在JmsOperations里有
<T> T execute(SessionCallback<T> action) throws JmsException;
的定义。
那么这个SessionCallback接口是什么呢?它也为用户提供了JMS session。
public interface SessionCallback<T> { T doInJms(Session session) throws JMSException; }
继续往下看。doSend方法:
protected void doSend(Session session, Destination destination, MessageCreator messageCreator) throws JMSException { Assert.notNull(messageCreator, "MessageCreator must not be null"); MessageProducer producer = createProducer(session, destination); try { Message message = messageCreator.createMessage(session); if (logger.isDebugEnabled()) { logger.debug("Sending created message: " + message); } doSend(producer, message); // Check commit - avoid commit call within a JTA transaction. if (session.getTransacted() && isSessionLocallyTransacted(session)) { // Transacted session created by this template -> commit. JmsUtils.commitIfNecessary(session); } } finally { JmsUtils.closeMessageProducer(producer); } }
createProducer()方法又调用了doCreateProducer(),实际的消息生产者在这里。
protected MessageProducer doCreateProducer(Session session, Destination destination) throws JMSException { return session.createProducer(destination); }
在这里,我们看到了,spring创建了消息的发送者,关闭连接的一些操作。到这里,大家就明白了,spring内部处理Jms消息的过程了吧(消息的接受也是一样)。
注:本文使用spring3.0和activemq5.2版本。
相关推荐
基于Spring+JMS+ActiveMQ+Tomcat,我使用的版本情况如下所示:Spring 3.2.0,ActiveMQ 5.4.3,Tomcat 6.0.43。本例通过详细的说明和注释,实现消息服务的基本功能:发送与接收。Spring对JMS提供了很好的支持,可以...
spring+activemq必备jar包:activeio-core-3.1.4.jar,activemq-all-5.13.2.jar,activemq-pool-5.13.2.jar,commons-pool2-2.4.2.jar
SpringBoot+ActiveMq+MQTT实现消息的发送和接收 后台消费者、生产者、消息发送接口、发送消息业务类等相关配置
NULL 博文链接:https://ihenu.iteye.com/blog/2270078
spring+activemq,示例工程,演示JMS消息通信。。。。。。。
单片机部分采用MQTT协议将主题消息发布到队列中,java部分也采用MQTT协议进行处理,整合MQTT协议, 具体这个资源是干什么的,请查看博客: https://blog.csdn.net/qq_34178998/article/details/93158429
基于Spring+JMS+ActiveMQ+Tomcat,我使用的版本情况如下所示: •Spring 2.5 •ActiveMQ 5.4.0 •Tomcat 6.0.30 下面通过学习与配置,实现消息服务的基本功能:发送与接收。Spring对JMS提供了很好的支持,可以...
基于Spring+JMS+ActiveMQ+Tomcat的整合ActiveMQSpringDemo实例源码,此实例基于Spring+JMS+ActiveMQ+Tomcat,注解的完整实例,包含jar包,可供学习及设计参考。
Spring+ActiveMQ整合实例代码工程,朋友提供给我参考的,我备个份以便下次查阅,也分享给大家,看看对大家有没有帮助了
整合Spring + ActiveMQ 的朋友可以下载看一下 简单易懂
Spring+JMS+ActiveMQ+Tomcat jar下载,在博客主页有实例,欢迎换看
spring3.0整合了activemQ和jms可以发送信息和接收消息
基于Spring+JMS+ActiveMQ+Tomcat,做一个Spring4.1.0和ActiveMQ5.11.1整合实例,实现了Point-To-Point的异步队列消息和PUB/SUB(发布/订阅)模型,简单实例,不包含任何业务。
springboot +netty+activeMq在线客服系统springboot +netty+activeMq在线客服系统springboot +netty+activeMq在线客服系统springboot +netty+activeMq在线客服系统springboot +netty+activeMq在线客服系统springboot...
Windowss 下 Spring + ActiveMq + maven 集成配置分为两章,环境搭建之前参考了网上的博客以及同事的配置。
基于Maven的Spring+ActiveMQ,比较贴合实际生产,只实现了Topic,queue改点配置就行了
资源的内容是Spring+ActiveMQ的整合,包括所有的架包。
spring+activeMQ 嵌入式配置 完整demo(包含jar包)
spring +activemq topic消息持久化订阅实例,整个项目中有activemq和spring的整合的所有实例,topic的持久化配置是在ApplicationContext3C、ApplicationContext3C2以及ApplicationContext3P三个中,消息生产者:...
spring+springmvc+mybatis+mongodb+ActiveMQ+CXF