转自:http://topmanopensource.iteye.com/blog/1111321

ActiveMQ和Tomcat的整合应用

博客分类:

 

在ActiveMQ的资源让容器Tomcat管理时候,可以在Tomcat的配置文件中添加相关的配置信息。

说明:Tomcat示例版本6.0.14,其它版本在配置上可能有一些差异

1、准备jar包:

将ActiveMQ lib目录下的5个jar包复制到Tomcat lib目录下:

activemq-core-5.1.0.jar

activemq-web-5.1.0.jar

geronimo-j2ee-management_1.0_spec-1.0.jar

geronimo-jms_1.1_spec-1.1.1.jar

geronimo-jta_1.0.1B_spec-1.0.1.jar

修改配置文件:

修改Tomcat的conf/context.xml文件:

在<context></context>节点中添加以下内容:

  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. <Resource name="jms/Queue"
  9. auth="Container"
  10. type="org.apache.activemq.command.ActiveMQQueue"
  11. description="My Queue"
  12. factory="org.apache.activemq.jndi.JNDIReferenceFactory"
  13. physicalName="TomcatQueue" />

创建一个Java Web项目:

备注:必须是web项目,目前ActiveMQ依赖Tomcat,Tomcat是web容器,必须创建一个web容器。

消息接收者:

  1. package easyway.activemq.app.demo2;
  2. import javax.jms.JMSException;
  3. import javax.jms.Message;
  4. import javax.jms.MessageListener;
  5. import javax.jms.TextMessage;
  6. import org.springframework.jms.core.JmsTemplate;
  7. /**
  8. * 消息接收者
  9. * @author longgangbai
  10. *
  11. */
  12. public class MessageReceiver implements  MessageListener {
  13. private JmsTemplate jmsTemplate;
  14. public JmsTemplate getJmsTemplate() {
  15. return jmsTemplate;
  16. }
  17. public void setJmsTemplate(JmsTemplate jmsTemplate) {
  18. this.jmsTemplate = jmsTemplate;
  19. }
  20. public void receive() throws JMSException{
  21. TextMessage text=(TextMessage)this.jmsTemplate.receive();
  22. System.out.println("receive="+text.getText());
  23. }
  24. public void onMessage(Message message) {
  25. if(message instanceof TextMessage){
  26. TextMessage text=(TextMessage)message;
  27. try {
  28. System.out.println(text.getText());
  29. } catch (Exception e) {
  30. }
  31. }
  32. }
  33. }

消息发送者:

  1. package easyway.activemq.app.demo2;
  2. import javax.jms.JMSException;
  3. import javax.jms.Message;
  4. import javax.jms.Session;
  5. import org.springframework.jms.core.JmsTemplate;
  6. import org.springframework.jms.core.MessageCreator;
  7. /**
  8. * tomcat和activemq整合
  9. * 消息发送者
  10. * @author longgangbai
  11. *
  12. */
  13. public class MessageSender {
  14. private JmsTemplate jmsTemplate;
  15. public void send(final String text){
  16. jmsTemplate.send(new MessageCreator(){
  17. public Message createMessage(Session session) throws JMSException {
  18. // TODO Auto-generated method stub
  19. return session.createTextMessage(text);
  20. }
  21. });
  22. }
  23. public JmsTemplate getJmsTemplate() {
  24. return jmsTemplate;
  25. }
  26. public void setJmsTemplate(JmsTemplate jmsTemplate) {
  27. this.jmsTemplate = jmsTemplate;
  28. }
  29. }

业务类:

  1. package easyway.activemq.app.demo2;
  2. import javax.jms.JMSException;
  3. import org.apache.xbean.spring.context.ClassPathXmlApplicationContext;
  4. /**
  5. * 测试类
  6. * @author longgangbai
  7. *
  8. */
  9. public class MessageTest {
  10. public void test() throws JMSException {
  11. ClassPathXmlApplicationContext ctx=new ClassPathXmlApplicationContext("app-activemq-tomcat.xml");
  12. MessageSender sender=(MessageSender)ctx.getBean("sender");
  13. MessageReceiver receive=(MessageReceiver)ctx.getBean("receiver");
  14. sender.send("helloworld");
  15. receive.receive();
  16. }
  17. }

配置文件:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!--
  3. Licensed to the Apache Software Foundation (ASF) under one or more
  4. contributor license agreements.  See the NOTICE file distributed with
  5. this work for additional information regarding copyright ownership.
  6. The ASF licenses this file to You under the Apache License, Version 2.0
  7. (the "License"); you may not use this file except in compliance with
  8. the License.  You may obtain a copy of the License at
  9. http://www.apache.org/licenses/LICENSE-2.0
  10. Unless required by applicable law or agreed to in writing, software
  11. distributed under the License is distributed on an "AS IS" BASIS,
  12. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. See the License for the specific language governing permissions and
  14. limitations under the License.
  15. -->
  16. <!-- START SNIPPET: xbean -->
  17. <beans
  18. xmlns="http://www.springframework.org/schema/beans"
  19. xmlns:amq="http://activemq.apache.org/schema/core"
  20. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  21. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
  22. http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core.xsd">
  23. <!-- 连接连接工厂 -->
  24. <bean  id="jmsConnectionFactory" class="org.springframework.jndi.JndiObjectFactoryBean">
  25. <property name="jndiName" value="java:comp/env/jms/ConnectionFactory"></property>
  26. </bean>
  27. <bean  id="tomcatQueue" class="org.springframework.jndi.JndiObjectFactoryBean">
  28. <property name="jndiName" value="java:comp/env/jms/Queue"></property>
  29. </bean>
  30. <!-- 配置JMS的模板 -->
  31. <bean id="jmsTemplate"  class="org.springframework.jms.core.JmsTemplate">
  32. <property name="connectionFactory" >
  33. <ref  bean="jmsConnectionFactory"/>
  34. </property>
  35. <property name="defaultDestination">
  36. <ref bean="tomcatQueue"/>
  37. </property>
  38. </bean>
  39. <!-- 发送消息队列到目的地 -->
  40. <bean id="sender"  class="easyway.activemq.app.demo2.MessageSender">
  41. <property name="jmsTemplate">
  42. <ref bean="jmsTemplate"/>
  43. </property>
  44. </bean>
  45. <!-- 接收消息 -->
  46. <bean id="receiver" class="easyway.activemq.app.demo2.MessageReceiver">
  47. <property name="jmsTemplate">
  48. <ref bean="jmsTemplate"/>
  49. </property>
  50. </bean>
  51. <bean id="listenerContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
  52. <property name="connectionFactory">
  53. <ref bean="jmsConnectionFactory"/>
  54. </property>
  55. <property name="destination">
  56. <ref bean="tomcatQueue"/>
  57. </property>
  58. <property name="messageListener">
  59. <ref bean="receiver"/>
  60. </property>
  61. </bean>
  62. </beans>

创建一个jsp页面:

  1. <%@ page language="java" import="easyway.activemq.app.demo2.*" pageEncoding="UTF-8"%>
  2. <%
  3. String path = request.getContextPath();
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  5. %>
  6. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  7. <html>
  8. <head>
  9. <base href="<%=basePath%>">
  10. <title>My JSP 'index.jsp' starting page</title>
  11. <meta http-equiv="pragma" content="no-cache">
  12. <meta http-equiv="cache-control" content="no-cache">
  13. <meta http-equiv="expires" content="0">
  14. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  15. <meta http-equiv="description" content="This is my page">
  16. <!--
  17. <link rel="stylesheet" type="text/css" href="styles.css">
  18. -->
  19. </head>
  20. <body>
  21. <%
  22. MessageTest message=new MessageTest();
  23. message.test();
  24. %>
  25. <br>
  26. </body>
  27. </html>

ActiveMQ和Tomcat的整合应用(转)的更多相关文章

  1. Apache与Tomcat的整合

    一 Apache与Tomcat比较联系 apache支持静态页,tomcat支持动态的,比如servlet等. 一般使用apache+tomcat的话,apache只是作为一个转发,对jsp的处理是由 ...

  2. Solr系列一:Solr与Tomcat的整合

    第一次尝试着去写一个系列的教程,希望自己能坚持下去,也希望自己能够通过博客的编写来加深自己对solr搜索的理解. Solr与Tomcat的整合网上有很多教程,我就以我的整合为例来讲述一下我的整合过程, ...

  3. 性能测试二十六:环境部署之Mysql+Redis+Tomcat环境整合

    系统中使用了缓存+数据库,通用读取数据规则1.先从缓存读数据,如果有,直接返回数据:2.如果没有,去数据库中读,然后再插入到缓存中,再返回数据 Mysql+Redis+Tomcat环境整合 1.修改P ...

  4. Centos6.7配置Nginx+Tomcat简单整合

    系统环境:Centos 6.7 软件环境:JDK-1.8.0_65.Nginx-1.10.3.Tomcat-8.5.8 文档环境:/opt/app/ 存放软件目录,至于mkdir创建文件就不用再说了 ...

  5. solr + tomcat + mysql整合

    上一次分享了solr+tomcat的整合 学习就是要一步一步的进行才有趣 所以这次给大家分享solr+tomcat+mysql 一.准备工作 1.一张带数据的数据库表(我用的是这张叫merchant的 ...

  6. nginx于tomcat项目整合(拆分静态文件)

    1.在很多时候我们在网站上应用的时候都会用到nginx,由于我们是java开发者,不可避免的是我们需要在我们的tomcat的工程中应用到nginx,这里的应用可以是请求转发,负载均衡,反向代理,配置虚 ...

  7. Apache和Tomcat的整合过程(转载)

    一 Apache与Tomcat比较联系 apache支持静态页,tomcat支持动态的,比如servlet等. 一般使用apache+tomcat的话,apache只是作为一个转发,对jsp的处理是由 ...

  8. ActiveMQ学习笔记(21)----ActiveMQ集成Tomcat

    1. 监控和管理Broker Web Console 方式:直接访问ActiveMQ的管理页面:http://localhost:8161/admin,默认的用户名和密码是admin/admin.具体 ...

  9. ActiveMQ学习总结------Spring整合ActiveMQ 04

    通过前几篇的学习,相信大家已经对我们的ActiveMQ的原生操作已经有了个深刻的概念, 那么这篇文章就来带领大家一步一步学习下ActiveMQ结合Spring的实战操作 注:本文将省略一部分与Acti ...

随机推荐

  1. P1231: [Usaco2008 Nov]mixup2 混乱的奶牛

    这是一道状压DP,首先这道题让我意识到状态是从 1 to (1<<n)-1 的,所以当前加入的某头牛编号是从 0 to n-1 的,所以存储的时候习惯要改一下,这样子做状压DP才会顺一点吧 ...

  2. Factorial Trailing Zeroes

    Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in log ...

  3. 时间日期设置--ctime头文件

    简述:ctime头文件中的4中与时间相关的数据类型 <ctime>头文件中定义了4种与时间有关的数据类型,如下:clock_tsize_ttime_tstruct tm clock_tCl ...

  4. cacti install on ubuntu

    安装cacti需要的软件需要 nginx + php + mysql + rrdtool + cacti + snmp 1.nginx 安装配置 首先按照如下命令安装,明显是马虎不细心./config ...

  5. VS2013 IIS Express 添加MIME映射

    VS2013,则可以直接在IIS Express中添加MIME映射.操作如下: 1.在DOS窗口下进入IIS Express安装目录,默认是“C:\Program Files\IIS Express” ...

  6. android开发 获取父控件的高宽

    @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(wi ...

  7. android开发 两张bitmap图片合成一张图片

    场景:对android4.4解码gif(解码文章见前面一篇)后的图片进行每帧处理,android4.3 解码出来的每帧都很完整,但是到android4.4版本就不完整了,每帧都是在第一帧的基础上把被改 ...

  8. WebSphere数据源配置

    WebSphere data source Configuration login http://localhost:9061/ibm/console/login.do(According to yo ...

  9. 自己学习编程时间比较短,现在把一下自己以前刚刚接触C++时的程序上传一下,有空可以看看

    键盘输入十个数,找出最大值和最小值. #include<iostream.h>void main (){int a[10];int i,t,max,min;cout<<&quo ...

  10. Matlab设置网格线密度(坐标精度)

    1.不精确 set(gca,'XMinorTick','on') 这样的话知识x轴显示了细的密度,网格线并没有变. 2.精确 set(gca,'xtick',-1:0.1:1); set(gca,'y ...