ActiveMQ结合Spring开发

----------------------------------------------------------------------------

Spring结合ActiveMQ开发 : 发送和接收queue

1.首先在pom.xml文件中添加activemq的依赖包

 1      <!-- Spring对JMS的支持 -->
 2       <dependency>
 3           <groupId>org.springframework</groupId>
 4           <artifactId>spring-jms</artifactId>
 5           <version>4.0.0.RELEASE</version>
 6       </dependency>
 7       <!-- 添加ActiveMQ的pool包 -->
 8       <dependency>
 9           <groupId>org.apache.activemq</groupId>
10           <artifactId>activemq-pool</artifactId>
11           <version>5.9.0</version>
12       </dependency>

2.在Spring的配置文件applicationContext.xml中配置jmsTemplate

 1 <bean id="jmsFactory" class="org.apache.activemq.pool.PooledConnectionFactory"
 2         destroy-method="stop">
 3         <property name="connectionFactory">
 4             <bean class="org.apache.activemq.ActiveMQConnectionFactory">
 5                 <property name="brokerURL">
 6                     <value>tcp://192.168.1.81:61616</value>
 7                 </property>
 8             </bean>
 9         </property>
10         <property name="maxConnections" value="100"></property>
11     </bean>
12
13     <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
14         <property name="connectionFactory" ref="jmsFactory"></property>
15         <property name="defaultDestination" ref="destination"></property>
16         <property name="messageConverter">
17             <bean class="org.springframework.jms.support.converter.SimpleMessageConverter"/>
18         </property>
19     </bean>
20
21     <bean id="destination" class="org.apache.activemq.command.ActiveMQQueue">
22         <constructor-arg index="0" value="spring-queue"/>
23     </bean>

3.创建一个queue的消息发送者程序

 1 import javax.jms.JMSException;
 2 import javax.jms.Message;
 3 import javax.jms.Session;
 4 import javax.jms.TextMessage;
 5
 6 import org.springframework.beans.factory.annotation.Autowired;
 7 import org.springframework.context.ApplicationContext;
 8 import org.springframework.context.support.ClassPathXmlApplicationContext;
 9 import org.springframework.jms.core.JmsTemplate;
10 import org.springframework.jms.core.MessageCreator;
11 import org.springframework.stereotype.Service;
12
13 @Service
14 public class SpringQueueSender {
15     @Autowired
16     private JmsTemplate jt = null;
17
18     public static void main(String[] args) {
19         ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
20         SpringQueueSender sqs = (SpringQueueSender)ctx.getBean("springQueueSender");
21         sqs.jt.send(new MessageCreator(){
22             public Message createMessage(Session s) throws JMSException {
23                 TextMessage msg = s.createTextMessage("Spring send msg ----> Hello activeMQ");
24                 return msg;
25             }
26         });
27     }
28 }

4.创建一个queue的消息接收者程序

 1 import org.springframework.beans.factory.annotation.Autowired;
 2 import org.springframework.context.ApplicationContext;
 3 import org.springframework.context.support.ClassPathXmlApplicationContext;
 4 import org.springframework.jms.core.JmsTemplate;
 5 import org.springframework.stereotype.Service;
 6
 7 @Service
 8 public class SpringQueueReceiver {
 9     @Autowired
10     private JmsTemplate jt = null;
11
12     public static void main(String[] args) {
13         ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
14         SpringQueueReceiver sqr = (SpringQueueReceiver)ctx.getBean("springQueueReceiver");
15         String msg = (String)sqr.jt.receiveAndConvert();
16         System.out.println("receive msg = " + msg);
17     }
18 }

运行结果:

Spring结合ActiveMQ开发 : 发送和接收topic

1.在Spring的配置文件applicationContext.xml中配置topic

 1 <bean id="jmsFactory" class="org.apache.activemq.pool.PooledConnectionFactory"
 2         destroy-method="stop">
 3         <property name="connectionFactory">
 4             <bean class="org.apache.activemq.ActiveMQConnectionFactory">
 5                 <property name="brokerURL">
 6                     <value>tcp://120.76.123.81:61616</value>
 7                 </property>
 8             </bean>
 9         </property>
10         <property name="maxConnections" value="100"></property>
11     </bean>
12
13     <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
14         <property name="connectionFactory" ref="jmsFactory"></property>
15         <property name="defaultDestination" ref="destinationTopic"></property>
16         <property name="messageConverter">
17             <bean class="org.springframework.jms.support.converter.SimpleMessageConverter"/>
18         </property>
19     </bean>
20
21     <bean id="destinationTopic" class="org.apache.activemq.command.ActiveMQTopic">
22         <constructor-arg index="0" value="spring-topic"/>
23     </bean>

topic消息发送者和接收者 程序和上面一样。

Spring结合ActiveMQ开发 : 在Spring中配置消费者

Spring的applicationContext.xml配置

 1     <bean id="jmsFactory" class="org.apache.activemq.pool.PooledConnectionFactory"
 2         destroy-method="stop">
 3         <property name="connectionFactory">
 4             <bean class="org.apache.activemq.ActiveMQConnectionFactory">
 5                 <property name="brokerURL">
 6                     <value>tcp://192.168.1.81:61616</value>
 7                 </property>
 8             </bean>
 9         </property>
10         <property name="maxConnections" value="100"></property>
11     </bean>
12
13     <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
14         <property name="connectionFactory" ref="jmsFactory"></property>
15         <property name="defaultDestination" ref="destination"></property>
16         <property name="messageConverter">
17             <bean class="org.springframework.jms.support.converter.SimpleMessageConverter"/>
18         </property>
19     </bean>
20
21     <bean id="destination" class="org.apache.activemq.command.ActiveMQQueue">
22         <constructor-arg index="0" value="spring-queue"/>
23     </bean>
24
25     <bean id="jmsContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
26         <property name="connectionFactory" ref="jmsFactory"/>
27         <property name="destination" ref="destination"/>
28         <property name="messageListener" ref="messageListener"/>
29     </bean>
30
31     <bean id="messageListener" class="com.test.spring.MyMessageListener">
32     </bean>

消费发送者程序

 1 import javax.jms.JMSException;
 2 import javax.jms.Message;
 3 import javax.jms.Session;
 4 import javax.jms.TextMessage;
 5
 6 import org.springframework.beans.factory.annotation.Autowired;
 7 import org.springframework.context.ApplicationContext;
 8 import org.springframework.context.support.ClassPathXmlApplicationContext;
 9 import org.springframework.jms.core.JmsTemplate;
10 import org.springframework.jms.core.MessageCreator;
11 import org.springframework.stereotype.Service;
12
13 @Service
14 public class SpringQueueSender {
15     @Autowired
16     private JmsTemplate jt = null;
17
18     public static void main(String[] args) {
19         ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
20         SpringQueueSender sqs = (SpringQueueSender)ctx.getBean("springQueueSender");
21         sqs.jt.send(new MessageCreator(){
22             public Message createMessage(Session s) throws JMSException {
23                 TextMessage msg = s.createTextMessage("在Spring中配置消息接收者 ----> Hello activeMQ");
24                 return msg;
25             }
26         });
27     }
28 }

消息接受者程序

 1 import javax.jms.Message;
 2 import javax.jms.MessageListener;
 3 import javax.jms.TextMessage;
 4
 5 public class MyMessageListener implements MessageListener{
 6
 7     public void onMessage(Message msg) {
 8         TextMessage txtMsg = (TextMessage)msg;
 9         try{
10             System.out.println("reveive txt msg = " + txtMsg);
11         }catch(Exception e){
12             e.printStackTrace();
13         }
14     }
15 }
时间: 2024-08-16 18:50:17

ActiveMQ结合Spring开发的相关文章

ActiveMQ(06):ActiveMQ结合Spring开发

一.pom.xml与mq.properties Spring提供了对JMS的支持,需要添加Spring支持jms的包,如下: <dependency>     <groupId>org.springframework</groupId>     <artifactId>spring-jms</artifactId>     <version>4.1.7.RELEASE</version> </dependency&g

ActiveMQ(07):ActiveMQ结合Spring开发--建议

1.Camel框架支持大量的企业集成模式,可以大大简化集成组件间的大量服务和复杂的消息流.而Spring框架更注重简单性,仅仅支持基本的最佳实践. 2.Spring消息发送的核心架构是JmsTemplate,隔离了像打开.关闭Session和Producer的繁琐操作,因此应用开发人员仅仅需要关注实际的业务逻辑. 但是JmsTemplate损害了ActiveMQ的PooledConnectionFactory对session和消息producer的缓存机制而带来的性能提升. 3.新的Spring

ActiveMQ(06):ActiveMQ结合Spring开发--第二种方式

一.pom.xml与mq.properties Spring提供了对JMS的支持,需要添加Spring支持jms的包,如下: <dependency>     <groupId>org.springframework</groupId>     <artifactId>spring-jms</artifactId>     <version>4.1.7.RELEASE</version> </dependency&g

分布式-信息方式-ActiveMQ结合Spring

ActiveMQ结合 Spring开发■ Spring提供了对JMS的支持,需要添加 Spring支持jms的包,如下: <dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-pool</artifactId> </dependency> <dependency> <groupId>org.springframework

利用Maven搭建Spring开发环境 【转】

一.   概要说明 最近几天在测试Spring3.0的AOP功能,在测试功能之前,首先是要搭建出Spring3.0的开发功能.开始去官网下载Spring的相关jar包,但是这些jar包中还是会需要其他的一些jar包,于是又手动的去下载其他的相关jar包.这样也可以搭建出开发环境,但是需要频繁的去下载缺少的jar包,很麻烦.这里,我们可以还有一个更好的办法,采用maven来管理我们的工程,让maven来自动为我们去下载相关版本的jar包,具体的配置如下. 二.   下载并安装maven 去网上下载

利用Maven搭建Spring开发环境

一.   概要说明 最近几天在测试Spring3.0的AOP功能,在测试功能之前,首先是要搭建出Spring3.0的开发功能.开始去官网下载Spring的相关jar包,但是这些jar包中还是会需要其他的一些jar包,于是又手动的去下载其他的相关jar包.这样也可以搭建出开发环境,但是需要频繁的去下载缺少的jar包,很麻烦.这里,我们可以还有一个更好的办法,采用maven来管理我们的工程,让maven来自动为我们去下载相关版本的jar包,具体的配置如下. 二.   下载并安装maven 去网上下载

基于Spring开发的DUBBO服务接口测试

基于Spring开发的DUBBO服务接口测试 知识共享主要内容: 1. Dubbo相关概念和架构,以及dubbo服务程序开发步骤. 2. 基于Spring开发框架的dubbo服务接口测试相关配置. 3. spring test+junit和spring test+TestNG两种测试框架脚本编写方法. 一.        DUBBO与DUBBO架构 1.          什么是dubbo?DUBBO是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,是阿里巴巴SOA服务化治

使用Spring开发第一个HelloWorld应用

http://www.importnew.com/13246.html 让我们用Spring来写第一个应用程序吧. 完成这一章要求: 熟悉Java语言 设置好Spring的环境 熟悉简单的Eclipse IDE的操作 如果你还没有设置好环境,请参考Spring开发环境的配置. 我们第一个程序是打印”Hello World”语句,这个语句通过Spring的配置文件来设置. 1 – 新建Java项目: 第一步用Eclipse IDE新建一个项目. 点击 > File > New > Java

学习spring2--跟我一起学Spring 3(3)–使用Spring开发第一个HelloWorld应用

http://www.importnew.com/13246.html 首页 所有文章 资讯 Web 架构 基础技术 书籍 教程 我要投稿 更多频道 » - 导航条 - 首页 所有文章 资讯 Web 架构 基础技术 书籍 教程 我要投稿 更多频道 » - iOS - Python - Android - Web前端 跟我一起学Spring 3(3)–使用Spring开发第一个HelloWorld应用 2014/10/10 | 分类: 教程 | 5 条评论 | 标签: SPRING, 教程 分享到