`

Spring JMS 整合Tomcat和ActiveMQ

 
阅读更多

1.Active MQ安装配置 

1.1.下载并解压Active MQ 
1.2.双击bin/activemq.bat启动Active MQ 
1.3.浏览器输入http://localhost:8161/admin/ ,可以图形化界面管理activemq 
1.4.点击Queue,新建一个队列名字叫TestQueue1 

 

2.在Eclipse里面配置Tomcat的context.xml,这样Spring就能以JNDI方式访问ActiveMQ了 

 

加入以下代码 

Xml代码  收藏代码
  1. <Resource name="jms/ConnectionFactory"  
  2.        auth="Container"  
  3.        type="org.apache.activemq.ActiveMQConnectionFactory"  
  4.        description="JMS Connection Factory"  
  5.        factory="org.apache.activemq.jndi.JNDIReferenceFactory"  
  6.        brokerURL="tcp://localhost:61616"  
  7.        brokerName="LocalActiveMQBroker" />  
  8.      
  9.    <Resource name="jms/Queue"  
  10.        auth="Container"  
  11.        type="org.apache.activemq.command.ActiveMQQueue"  
  12.        description="my Queue"  
  13.        factory="org.apache.activemq.jndi.JNDIReferenceFactory"  
  14.        physicalName="TestQueue1" />  


3.导入以下Jar包 
activemq-all-5.4.1.jar 
commons-logging-1.1.1.jar 
javax.jms_1.1.1.jar 
spring.asm-3.0.0.RELEASE.jar 
spring.beans-3.0.0.RELEASE.jar 
spring.context-3.0.0.RELEASE.jar 
spring.core-3.0.0.RELEASE.jar 
spring.expression-3.0.0.RELEASE.jar 
spring.jms-3.0.0.RELEASE.jar 
spring.transaction-3.0.0.RELEASE.jar 
spring.web-3.0.0.RELEASE.jar 

4.编写2个类,一个MessageQueueSender发消息,一个MessageQueueReceiver收消息,为了方便测试,我们还写了一个Servlet调用MessageQueueSender,通过JSP来调用Servlet 

5.写Spring的配置文件,假设存在这个文件里:jms-activemq.xml 

Xml代码  收藏代码
  1. <beans xmlns="http://www.springframework.org/schema/beans"  
  2.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  3.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">  
  4.   
  5.     <!-- Connection factory -->  
  6.     <bean id="jmsConnectionFactory" class="org.springframework.jndi.JndiObjectFactoryBean">  
  7.         <property name="jndiName" value="java:comp/env/jms/ConnectionFactory"></property>  
  8.     </bean>  
  9.   
  10.     <!-- Queue -->  
  11.     <bean id="jmsQueue" class="org.springframework.jndi.JndiObjectFactoryBean"  
  12.         lazy-init="true">  
  13.         <property name="jndiName" value="java:comp/env/jms/Queue"></property>  
  14.     </bean>  
  15.   
  16.     <!-- Spring JMS Template -->  
  17.     <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"  
  18.         lazy-init="true">  
  19.         <property name="connectionFactory" ref="jmsConnectionFactory"></property>  
  20.         <property name="defaultDestination" ref="jmsQueue"></property>  
  21.     </bean>  
  22.   
  23.     <!-- Sender -->  
  24.     <bean id="jmsSender" class="com.xp.MessageQueueSender"  
  25.         lazy-init="true">  
  26.         <property name="jmsTemplate" ref="jmsTemplate"></property>  
  27.     </bean>  
  28.   
  29.     <!-- Receiver -->  
  30.     <bean id="jmsReceiver" class="com.xp.MessageQueueReceiver">  
  31.     </bean>  
  32.   
  33.     <!-- Message Listener -->  
  34.     <bean id="listenerContainer"  
  35.         class="org.springframework.jms.listener.DefaultMessageListenerContainer">  
  36.         <property name="connectionFactory" ref="jmsConnectionFactory"></property>  
  37.         <property name="destination" ref="jmsQueue"></property>  
  38.         <property name="messageListener" ref="jmsReceiver"></property>  
  39.         <property name="autoStartup" value="true" />  
  40.     </bean>  
  41.   
  42. </beans>  


6.启动tomcat测试看效果 

6.1.浏览器输入http://localhost:8080/JMSTest/,随便输入消息比如"Hello",点击submit 

 

6.2.可在控制台看到Hello的信息,表明发送和接受者都已经OK 



6.3.还可以在ActiveMQ管理台创建一条消息,点击"Send To" 

 

随便打入一点消息 

 

6.4.可在控制台看到接受者能收到此消息 



附件是eclipse工程代码

分享到:
评论
发表评论

文章已被作者锁定,不允许评论。

相关推荐

    Spring+JMS+ActiveMQ+Tomcat实现消息服务的demo

    基于Spring+JMS+ActiveMQ+Tomcat,我使用的版本情况如下所示:Spring 3.2.0,ActiveMQ 5.4.3,Tomcat 6.0.43。本例通过详细的说明和注释,实现消息服务的基本功能:发送与接收。Spring对JMS提供了很好的支持,可以...

    Spring+JMS+ActiveMQ+Tomcat jar下载

    Spring+JMS+ActiveMQ+Tomcat jar下载,在博客主页有实例,欢迎换看

    基于Spring+JMS+ActiveMQ+Tomcat整合

    基于Spring+JMS+ActiveMQ+Tomcat,做一个Spring4.1.0和ActiveMQ5.11.1整合实例,实现了Point-To-Point的异步队列消息和PUB/SUB(发布/订阅)模型,简单实例,不包含任何业务。

    Spring+JMS+ActiveMQ+Tomcat DOME

    在Tomcat的conf/context.xml中的元素里面加上如下配置: auth="Container" type="org.apache.activemq.ActiveMQConnectionFactory" description="JMS Connection Factory" factory="org.apache.activemq....

    Spring和ActiveMQ的整合实例源码

    此实例基于Spring+JMS+ActiveMQ+Tomcat,注解的完整实例,包含jar包

    Spring+JMS+ActiveMQ+Tomcat实现消息服务_服务器应用

    基于Spring+JMS+ActiveMQ+Tomcat,我使用的版本情况如下所示: •Spring 2.5 •ActiveMQ 5.4.0 •Tomcat 6.0.30 下面通过学习与配置,实现消息服务的基本功能:发送与接收。Spring对JMS提供了很好的支持,可以...

    Spring和ActiveMQ的整合实例源码ActiveMQSpringDemo.zip

    Spring和ActiveMQ的整合实例源码ActiveMQSpringDemo,此实例基于Spring+JMS+ActiveMQ+Tomcat,注解的完整实例,包含jar包

    Spring整合ActiveMQ超级详细实例

    Spring整合ActiveMQ超级详细实例,基于Spring+JMS+ActiveMQ+Tomcat,注解的完整实例,包含jar包,可以参考学习

    spring-jms-with-activemq:使用Spring MVC Framework和Apache Maven用ActiveMQ实现Spring JMS

    使用Spring MVC Framework和Apache Maven通过ActiveMQ实现Spring JMS。 项目环境: Java JDK 1.8 Apache Maven 3.3.1 Spring Framework 4.1.6 Spring JMS API Apache ActiveMQ 5.11.1 Apache Tomcat 8 蚀月...

    spring2 activemq5 tomcat6构建jms

    NULL 博文链接:https://zw7534313.iteye.com/blog/478392

    Spring集成ActiveMQ配置

    Spring 集 成ActiveMQ 配置 异步RPC框架 Missian ActiveMq-JMS简单实例使用tomcat

    tomcat activemq

    基于tomcat和activeMq组件的jms服务。源码,spring 的配置,如果想要仔细了解,请去 http://www.cnblogs.com/phoebus0501/archive/2011/02/28/1966896.html

    spring3+ActiveMQ+blazeds+flex consumer

    整合spring3 blazeds activeMQ(jms) 通过flex consumer来订阅消息。时时更新消息内容

    ActiveMQ.rar

    一: ActiveMQ简介 包括:是什么、能干什么、特点;...包括:监控和管理Broker、集成ActiveMQ和Tomcat、什么时候使用ActiveMQ等 n 十四: ActiveMQ优化 包括:影响ActiveMQ性能的因素、常见的优化方式和配置等

    ActiveMQSpringDemo.zip

    此实例基于Spring+JMS+ActiveMQ+Tomcat,注解的完整实例,包含jar包

    spring-boot-reference.pdf

    32.1.1. ActiveMQ Support 32.1.2. Artemis Support 32.1.3. Using a JNDI ConnectionFactory 32.1.4. Sending a Message 32.1.5. Receiving a Message 32.2. AMQP 32.2.1. RabbitMQ support 32.2.2. Sending a ...

    camel-cxf-jms-demo:示例 Apache Camel 应用程序(CXF、REST、JMS、缓存)

    ActiveMQ - JMS 代理; Ehcache - Camel 缓存实现; Maven 的生命周期支持; 单元测试( jUnit启动嵌入式Jetty进行测试) 环境 应用程序是通过以下方式创建的: JDK 8u40 Tomcat 8.0.* 技术栈 阿帕奇骆驼 2.15...

    单点登录源码

    ActiveMQ | 消息队列 | [http://activemq.apache.org/](http://activemq.apache.org/) JStorm | 实时流式计算框架 | [http://jstorm.io/](http://jstorm.io/) FastDFS | 分布式文件系统 | ...

    java开源包1

    GWT Spring 使得在 Spring 框架下构造 GWT 应用变得很简单,提供一个易于理解的依赖注入和RPC机制。 Java扫雷游戏 JVMine JVMine用Applets开发的扫雷游戏,可在线玩。 public class JVMine extends java.applet....

Global site tag (gtag.js) - Google Analytics