Spring与ActiveMQ(JMS)的整合说明 : 姓张的小子

来源:百度文库 编辑:神马文学网 时间:2024/04/29 19:41:09
Spring与ActiveMQ(JMS)的整合说明
ActiveMQ是完全支持JMS1.1和J2EE 1.4规范的 JMS Provider实现,也是Apache Geronimo默认的JMS provider。
ActiveMQ的网站:http://www.activemq.org
使用ActiveMQ后,可以在普通Web服务器使用JMS功能,不依赖于特定的应用服务器。
1. ActiveMQ 4.1-incubator-SNAPSHOT 与Spring 2.0 集成
ActiveMQ4.1 响应Spring 2.0号召,支持了引入XML Schema namespace的简单配置语法,简化了配置的语句。 1.1 引入ActiveMQ的XSD
在ApplicationContext.xml(Spring的配置文件)中引入ActiveMQ的XML Scheam 配置文件),如下:
(说明:由于ActiveMQ的那个XSD有部分错误,因此使用的是自行修改过的XSD,见"配置ClassPath中的scheam"小节)
1 2 xmlns:amq="http://activemq.org/config/1.0"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 xsi:schemaLocation="http://www.springframework.org/schema/beans
5 http://www.springframework.org/schema/beans/spring-beans.xsd
6 http://activemq.org/config/1.0
7 http://people.apache.org/repository/org.apache.activemq/xsds/activemq-core-4.1-incubator-SNAPSHOT.xsd">
view plain |print |?
1.2 配置ClassPath中的schema
在ClassPath 下面建立META-INF\spring.schemas 内容如下
1 http\://people.apache.org/repository/org.apache.activemq/xsds/activemq-core-4.1-incubator-SNAPSHOT.xsd=/activemq-core-4.1-incubator-SNAPSHOT.xsd
2 这个spring.schemas是spring自定义scheam的配置文件
3 请注意"http:\://"部分写法
view plain |print |?
1.3 配置ActiveMQ embedded ActiveMQ Broker
1
2
3     
4         
5     

6
view plain |print |?
当spring初始化时候,ActiveMQ embedded Broker 就会启动了 1.4 配置BookStore MDP
配置ConnectionFactory,由于是embedded 所以URL为:vm://localhost
1
2
view plain |print |?
配置Queue名字
1
2
view plain |print |?
配置JmsTemplate
1
2
3     
4         
5         
6             
7                 
8             
9         
10     
11
12     
13
view plain |print |?
配置MDP POJO
1
2
3     
4         
5     
6     
7         
8     
9
10
11
12
13     
14         
15             
16         
17     

18     
19     
20     
21     
22
23
24
25     
26     
27     
28
view plain |print |?
补充说明
采用了自定义的MessageConverter,Producer能够直接发送POJO。
使用了Spring的DefaultMessageListenerContainer,MessageListenerAdapter, Consumer 不用实现MessageListener 接口。