ActiveMQ和Tomcat的整合应用(转)
在ActiveMQ的资源让容器Tomcat管理时候,可以在Tomcat的配置文件中添加相关的配置信息。
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>节点中添加以下内容:
- <Resource name="jms/ConnectionFactory"
- auth="Container"
- type="org.apache.activemq.ActiveMQConnectionFactory"
- description="JMS Connection Factory"
- factory="org.apache.activemq.jndi.JNDIReferenceFactory"
- brokerURL="tcp://localhost:61616"
- brokerName="LocalActiveMQBroker" />
- <Resource name="jms/Queue"
- auth="Container"
- type="org.apache.activemq.command.ActiveMQQueue"
- description="My Queue"
- factory="org.apache.activemq.jndi.JNDIReferenceFactory"
- physicalName="TomcatQueue" />
创建一个Java Web项目:
备注:必须是web项目,目前ActiveMQ依赖Tomcat,Tomcat是web容器,必须创建一个web容器。
消息接收者:
- package easyway.activemq.app.demo2;
- import javax.jms.JMSException;
- import javax.jms.Message;
- import javax.jms.MessageListener;
- import javax.jms.TextMessage;
- import org.springframework.jms.core.JmsTemplate;
- /**
- * 消息接收者
- * @author longgangbai
- *
- */
- public class MessageReceiver implements MessageListener {
- private JmsTemplate jmsTemplate;
- public JmsTemplate getJmsTemplate() {
- return jmsTemplate;
- }
- public void setJmsTemplate(JmsTemplate jmsTemplate) {
- this.jmsTemplate = jmsTemplate;
- }
- public void receive() throws JMSException{
- TextMessage text=(TextMessage)this.jmsTemplate.receive();
- System.out.println("receive="+text.getText());
- }
- public void onMessage(Message message) {
- if(message instanceof TextMessage){
- TextMessage text=(TextMessage)message;
- try {
- System.out.println(text.getText());
- } catch (Exception e) {
- }
- }
- }
- }
消息发送者:
- package easyway.activemq.app.demo2;
- import javax.jms.JMSException;
- import javax.jms.Message;
- import javax.jms.Session;
- import org.springframework.jms.core.JmsTemplate;
- import org.springframework.jms.core.MessageCreator;
- /**
- * tomcat和activemq整合
- * 消息发送者
- * @author longgangbai
- *
- */
- public class MessageSender {
- private JmsTemplate jmsTemplate;
- public void send(final String text){
- jmsTemplate.send(new MessageCreator(){
- public Message createMessage(Session session) throws JMSException {
- // TODO Auto-generated method stub
- return session.createTextMessage(text);
- }
- });
- }
- public JmsTemplate getJmsTemplate() {
- return jmsTemplate;
- }
- public void setJmsTemplate(JmsTemplate jmsTemplate) {
- this.jmsTemplate = jmsTemplate;
- }
- }
业务类:
- package easyway.activemq.app.demo2;
- import javax.jms.JMSException;
- import org.apache.xbean.spring.context.ClassPathXmlApplicationContext;
- /**
- * 测试类
- * @author longgangbai
- *
- */
- public class MessageTest {
- public void test() throws JMSException {
- ClassPathXmlApplicationContext ctx=new ClassPathXmlApplicationContext("app-activemq-tomcat.xml");
- MessageSender sender=(MessageSender)ctx.getBean("sender");
- MessageReceiver receive=(MessageReceiver)ctx.getBean("receiver");
- sender.send("helloworld");
- receive.receive();
- }
- }
配置文件:
- <?xml version="1.0" encoding="UTF-8"?>
- <!--
- Licensed to the Apache Software Foundation (ASF) under one or more
- contributor license agreements. See the NOTICE file distributed with
- this work for additional information regarding copyright ownership.
- The ASF licenses this file to You under the Apache License, Version 2.0
- (the "License"); you may not use this file except in compliance with
- the License. You may obtain a copy of the License at
- http://www.apache.org/licenses/LICENSE-2.0
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
- -->
- <!-- START SNIPPET: xbean -->
- <beans
- xmlns="http://www.springframework.org/schema/beans"
- xmlns:amq="http://activemq.apache.org/schema/core"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
- http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core.xsd">
- <!-- 连接连接工厂 -->
- <bean id="jmsConnectionFactory" class="org.springframework.jndi.JndiObjectFactoryBean">
- <property name="jndiName" value="java:comp/env/jms/ConnectionFactory"></property>
- </bean>
- <bean id="tomcatQueue" class="org.springframework.jndi.JndiObjectFactoryBean">
- <property name="jndiName" value="java:comp/env/jms/Queue"></property>
- </bean>
- <!-- 配置JMS的模板 -->
- <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
- <property name="connectionFactory" >
- <ref bean="jmsConnectionFactory"/>
- </property>
- <property name="defaultDestination">
- <ref bean="tomcatQueue"/>
- </property>
- </bean>
- <!-- 发送消息队列到目的地 -->
- <bean id="sender" class="easyway.activemq.app.demo2.MessageSender">
- <property name="jmsTemplate">
- <ref bean="jmsTemplate"/>
- </property>
- </bean>
- <!-- 接收消息 -->
- <bean id="receiver" class="easyway.activemq.app.demo2.MessageReceiver">
- <property name="jmsTemplate">
- <ref bean="jmsTemplate"/>
- </property>
- </bean>
- <bean id="listenerContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
- <property name="connectionFactory">
- <ref bean="jmsConnectionFactory"/>
- </property>
- <property name="destination">
- <ref bean="tomcatQueue"/>
- </property>
- <property name="messageListener">
- <ref bean="receiver"/>
- </property>
- </bean>
- </beans>
创建一个jsp页面:
- <%@ page language="java" import="easyway.activemq.app.demo2.*" pageEncoding="UTF-8"%>
- <%
- String path = request.getContextPath();
- String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
- %>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <base href="<%=basePath%>">
- <title>My JSP 'index.jsp' starting page</title>
- <meta http-equiv="pragma" content="no-cache">
- <meta http-equiv="cache-control" content="no-cache">
- <meta http-equiv="expires" content="0">
- <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
- <meta http-equiv="description" content="This is my page">
- <!--
- <link rel="stylesheet" type="text/css" href="styles.css">
- -->
- </head>
- <body>
- <%
- MessageTest message=new MessageTest();
- message.test();
- %>
- <br>
- </body>
- </html>
ActiveMQ和Tomcat的整合应用(转)的更多相关文章
- Apache与Tomcat的整合
一 Apache与Tomcat比较联系 apache支持静态页,tomcat支持动态的,比如servlet等. 一般使用apache+tomcat的话,apache只是作为一个转发,对jsp的处理是由 ...
- Solr系列一:Solr与Tomcat的整合
第一次尝试着去写一个系列的教程,希望自己能坚持下去,也希望自己能够通过博客的编写来加深自己对solr搜索的理解. Solr与Tomcat的整合网上有很多教程,我就以我的整合为例来讲述一下我的整合过程, ...
- 性能测试二十六:环境部署之Mysql+Redis+Tomcat环境整合
系统中使用了缓存+数据库,通用读取数据规则1.先从缓存读数据,如果有,直接返回数据:2.如果没有,去数据库中读,然后再插入到缓存中,再返回数据 Mysql+Redis+Tomcat环境整合 1.修改P ...
- Centos6.7配置Nginx+Tomcat简单整合
系统环境:Centos 6.7 软件环境:JDK-1.8.0_65.Nginx-1.10.3.Tomcat-8.5.8 文档环境:/opt/app/ 存放软件目录,至于mkdir创建文件就不用再说了 ...
- solr + tomcat + mysql整合
上一次分享了solr+tomcat的整合 学习就是要一步一步的进行才有趣 所以这次给大家分享solr+tomcat+mysql 一.准备工作 1.一张带数据的数据库表(我用的是这张叫merchant的 ...
- nginx于tomcat项目整合(拆分静态文件)
1.在很多时候我们在网站上应用的时候都会用到nginx,由于我们是java开发者,不可避免的是我们需要在我们的tomcat的工程中应用到nginx,这里的应用可以是请求转发,负载均衡,反向代理,配置虚 ...
- Apache和Tomcat的整合过程(转载)
一 Apache与Tomcat比较联系 apache支持静态页,tomcat支持动态的,比如servlet等. 一般使用apache+tomcat的话,apache只是作为一个转发,对jsp的处理是由 ...
- ActiveMQ学习笔记(21)----ActiveMQ集成Tomcat
1. 监控和管理Broker Web Console 方式:直接访问ActiveMQ的管理页面:http://localhost:8161/admin,默认的用户名和密码是admin/admin.具体 ...
- ActiveMQ学习总结------Spring整合ActiveMQ 04
通过前几篇的学习,相信大家已经对我们的ActiveMQ的原生操作已经有了个深刻的概念, 那么这篇文章就来带领大家一步一步学习下ActiveMQ结合Spring的实战操作 注:本文将省略一部分与Acti ...
随机推荐
- P1231: [Usaco2008 Nov]mixup2 混乱的奶牛
这是一道状压DP,首先这道题让我意识到状态是从 1 to (1<<n)-1 的,所以当前加入的某头牛编号是从 0 to n-1 的,所以存储的时候习惯要改一下,这样子做状压DP才会顺一点吧 ...
- Factorial Trailing Zeroes
Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in log ...
- 时间日期设置--ctime头文件
简述:ctime头文件中的4中与时间相关的数据类型 <ctime>头文件中定义了4种与时间有关的数据类型,如下:clock_tsize_ttime_tstruct tm clock_tCl ...
- cacti install on ubuntu
安装cacti需要的软件需要 nginx + php + mysql + rrdtool + cacti + snmp 1.nginx 安装配置 首先按照如下命令安装,明显是马虎不细心./config ...
- VS2013 IIS Express 添加MIME映射
VS2013,则可以直接在IIS Express中添加MIME映射.操作如下: 1.在DOS窗口下进入IIS Express安装目录,默认是“C:\Program Files\IIS Express” ...
- android开发 获取父控件的高宽
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(wi ...
- android开发 两张bitmap图片合成一张图片
场景:对android4.4解码gif(解码文章见前面一篇)后的图片进行每帧处理,android4.3 解码出来的每帧都很完整,但是到android4.4版本就不完整了,每帧都是在第一帧的基础上把被改 ...
- WebSphere数据源配置
WebSphere data source Configuration login http://localhost:9061/ibm/console/login.do(According to yo ...
- 自己学习编程时间比较短,现在把一下自己以前刚刚接触C++时的程序上传一下,有空可以看看
键盘输入十个数,找出最大值和最小值. #include<iostream.h>void main (){int a[10];int i,t,max,min;cout<<&quo ...
- Matlab设置网格线密度(坐标精度)
1.不精确 set(gca,'XMinorTick','on') 这样的话知识x轴显示了细的密度,网格线并没有变. 2.精确 set(gca,'xtick',-1:0.1:1); set(gca,'y ...