Spring整合JMS(一)-基础篇
1.基础知识

图1 同步通信和异步通信通信过程示意图
RMI使用的是同步通信,JMS使用的是异步通信。从图1可以看出异步通信的好处就是减少了不必要的等待,提高了效率。
JMS中有两个主要的概念:消息代理(message broker)和目的地(destination)。
消息代理指的概念: 发送者将消息发送到消息代理服务器,代理服务器收到消息后提示接收端去接收消息,消息代理就起了控制消息流转的作用,Apache 的activemq就是一种消息代理服务器。
目的地指的概念: 消息发送的目标地址,也是消息接收者获取消息的目标地址,对应于queue和topic中的一种,并且要指定具体的名称。
JMS通信又分为点对点和点对多2种形式。
点对点通信形式如图12.3所示,采用javax.jms.Queue表示,点多多通信形式如图12.4所示,采用javax.jms.Topic表示。



同步通信的缺点,这也是使用JMS所能够解决的:
2.Spring对JMS的支持
为了消除重复冗余的JMS代码,如建立连接,异常处理等,我们应该使用Spring所提供的JmsTemplate来进行消息的发送与接收。
和JDBCTemplate类似,JmsTemplate将捕获JMS所抛出的检查时异常,进行封装并以非检查时异常的形式进行抛出,我们就不必须对异常进行捕获。

3.示例代码
JMS只是一种标准,Apache的activemq是其多种实现中的一种,下面将基于activemq来做示例。
示例代码将分为发送端代码,接收端代码,XML配置和测试类4部分。
首先要下载apache-activemq,运行apache-activemq-5.10.0-bin[1]\apache-activemq-5.10.0\bin\win32下面的activemq.bat启动JMS服务,确保已经正常启动,61616端口未被占用。
3.1 发送端代码
发送消息的代码,注意要使用JmsTemplate:
@Component("producerServiceImpl")
public class ProducerServiceImpl implements ProducerService {
private JmsTemplate jmsTemplate;
public void sendMessage(Destination destination, final String message) {
jmsTemplate.send(destination, new MessageCreator() {
public Message createMessage(Session session) throws JMSException {
return session.createTextMessage(message);
}
});
}
public JmsTemplate getJmsTemplate() {
return jmsTemplate;
}
@Resource
public void setJmsTemplate(JmsTemplate jmsTemplate) {
this.jmsTemplate = jmsTemplate;
}
}

3.2 接收端代码

使用EJB的消息驱动来处理消息的代码如下,这样onMessage方法不会阻塞,但是要绑定MessageListener接口:
使用Spring所支持JMS接收消息的配置如下,比使用JmsTemplate和集成MessageListener接口要好:

需要在配置文件中做如下的配置:

配置一个listener-container,将消息接收端的bean放在其中

3.3.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" xmlns:context="http://www.springframework.org/schema/context"
xmlns:jms="http://www.springframework.org/schema/jms"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-3.0.xsd"> <context:component-scan base-package="com.tiantian" /> <!-- Spring提供的JMS工具类,它可以进行消息发送、接收等 -->
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<!-- 这个connectionFactory对应的是我们定义的Spring提供的那个ConnectionFactory对象 -->
<property name="connectionFactory" ref="connectionFactory"/>
</bean> <!-- 真正可以产生Connection的ConnectionFactory,由对应的 JMS服务厂商提供-->
<bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL" value="tcp://localhost:61616"/>
</bean> <!-- Spring用于管理真正的ConnectionFactory的ConnectionFactory -->
<bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory">
<!-- 目标ConnectionFactory对应真实的可以产生JMS Connection的ConnectionFactory -->
<property name="targetConnectionFactory" ref="targetConnectionFactory"/>
</bean> <!--这个是队列目的地-->
<bean id="queueDestination" class="org.apache.activemq.command.ActiveMQQueue">
<constructor-arg>
<value>queue</value>
</constructor-arg>
</bean>
<!-- 消息监听器 -->
<bean id="consumerMessageListener" class="com.tiantian.springintejms.listener.ConsumerMessageListener"/>
<!-- 消息监听容器 -->
<bean id="jmsContainer"
class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="connectionFactory" />
<property name="destination" ref="queueDestination" />
<property name="messageListener" ref="consumerMessageListener" />
</bean>
</beans>
3.4.测试类代码
测试的代码如下所示:
import javax.jms.Destination;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.tiantian.springintejms.service.ProducerService; @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("/applicationContext.xml")
public class ProducerConsumerTest { @Autowired
private ProducerService producerService;
@Autowired
@Qualifier("queueDestination")
private Destination destination; @Test
public void testSend() {
for (int i=0; i<2; i++) {
producerService.sendMessage(destination, "你好,生产者!这是消息:" + (i+1));
}
} }
Spring整合JMS(一)-基础篇的更多相关文章
- Spring整合JMS(一)——基于ActiveMQ实现
1.1 JMS简介 JMS的全称是Java Message Service,即Java消息服务.它主要用于在生产者和消费者之间进行消息传递,生产者负责产生消息,而消费者负责接收消息.把它应用到 ...
- Spring整合JMS(四)——事务管理
原文链接:http://haohaoxuexi.iteye.com/blog/1983532 Spring提供了一个JmsTransactionManager用于对JMS ConnectionFact ...
- Spring整合JMS(二)——三种消息监听器
原文地址:http://haohaoxuexi.iteye.com/blog/1893676 1.3 消息监听器MessageListener 在Spring整合JMS的应用中我们在定义消息监 ...
- Spring Boot 入门之基础篇(一)
原文地址:Spring Boot 入门之基础篇(一) 博客地址:http://www.extlight.com 一.前言 Spring Boot 是由 Pivotal 团队提供的全新框架,其设计目的是 ...
- Spring整合JMS——事务管理
Spring提供了一个JmsTransactionManager用于对JMS ConnectionFactory做事务管理.这将允许JMS应用利用Spring的事务管理特性.JmsTransactio ...
- Spring整合JMS(四)——事务管理(转)
*注:别人那复制来的 Spring提供了一个JmsTransactionManager用于对JMS ConnectionFactory做事务管理.这将允许JMS应用利用Spring的事务管理特性.Jm ...
- Spring整合JMS(二)——三种消息监听器(转)
*注:别人那复制来的 1.3 消息监听器MessageListener 在Spring整合JMS的应用中我们在定义消息监听器的时候一共可以定义三种类型的消息监听器,分别是MessageList ...
- Spring整合JMS(一)——基于ActiveMQ实现 (转)
*注:别人那复制来的 1.1 JMS简介 JMS的全称是Java Message Service,即Java消 息服务.它主要用于在生产者和消费者之间进行消息传递,生产者负责产生消息,而消费者 ...
- ActiveMQ (三) Spring整合JMS入门
Spring整合JMS入门 前提:安装好了ActiveMQ ActiveMQ安装 Demo结构: 生产者项目springjms_producer: pom.xml <?xml versio ...
随机推荐
- CodeForces723-A. The New Year: Meeting Friends
A. The New Year: Meeting Friends time limit per test 1 second memory limit per test 256 megabytes in ...
- 解决vi编辑器不能使用方向键和退格键问题的两种方法
方法1.使用vi命令时,不能正常编辑文件,使用方向键时老是出现很多字母? 在Ubuntu中,进入vi命令的编辑模式,发现按方向键不能移动光标,而是会输出ABCD,以及退格键也不能正常删除字符.这是由于 ...
- Spring框架学习笔记(7)——代理对象实现AOP
AOP(面向切面编程) AOP(Aspect-Oriented Programming, 面向切面编程): 是一种新的方法论, 是对传统 OOP(Object-Oriented Programming ...
- UEP-时间
时间戳转化为Date(or String) SimpleDateFormat format = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" ...
- 在Ubuntu虚拟机搭建数据库系统
连接数据库: mysql -uroot -p 输入数据库密码即可登陆. 查看mysql版本信息: mysql> select version(); +---------------------- ...
- 转:绝对干货--WordPress自定义查询wp_query所有参数详细注释
<?php /** * WordPress 查询综合参考 * 编译:luetkemj - luetkemj.com * * 官方文档: http://codex.wordpress.org/Cl ...
- bat复制文件夹下所有文件到另一个目录
一个需求,网上了半天都是错了,所以记一下吧,方便你我. copy是文件拷贝,文件夹拷贝需要用到xcopy @echo off::当前盘符set curPath=%cd%set digPath =&qu ...
- 数据结构与算法(c++)——双缓存队列
"双缓存队列"是我在一次开发任务中针对特殊场景设计出来的结构.使用场景为:发送端持续向接收端发送数据包--并且不理会接收端是否完成业务逻辑.由于接收端在任何情况下停止响应即可能产生 ...
- Java ASM介绍
一.什么是ASM 首先看下官方中的说明 ASM a very small and fast Java bytecode manipulation framework. ASM是一个JAVA字节码分析. ...
- springmvc +mybatis 配置多数据源
1.数据源配置: jdbc_multiple.properties: # MySQL #======================================================== ...