在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>节点中添加以下内容:
- <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>