码迷,mamicode.com
首页 > 其他好文 > 详细

tomcat监听activemq jms配置

时间:2014-07-25 14:23:41      阅读:280      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   java   color   os   io   

当从webservice接收到信息的时候,消息生产者producer立刻把收到的消息放入到jms里面,消费者cusomer这时要设置一个监听,当生产者发送消息时,只要消息被发出来,消费者就会接收到消息,然后进行相应的操作。

在tomcat里面,要进行配置。

首先在tomcat安装目录里面,对conf/context.xml进行配置,加入以下代码:

<Resource

        name="jms/FailoverConnectionFactory"

        auth="Container"

        type="org.apache.activemq.ActiveMQConnectionFactory"

        description="JMS Connection Factory"

        factory="org.apache.activemq.jndi.JNDIReferenceFactory"

    brokerURL="failover:(tcp://localhost:61616)?initialReconnectDelay=100&amp;maxReconnectAttempts=5"

        brokerName="localhost"

        useEmbeddedBroker="false"/>

    <Resource

        name="jms/NormalConnectionFactory"

        auth="Container"

        type="org.apache.activemq.ActiveMQConnectionFactory"

        description="JMS Connection Factory"

        factory="org.apache.activemq.jndi.JNDIReferenceFactory"

        brokerURL="tcp://localhost:61616"

        brokerName="localhost"

        useEmbeddedBroker="false"/>

    <Resource name="jms/topic/MyTopic"

        auth="Container"

        type="org.apache.activemq.command.ActiveMQTopic"

        factory="org.apache.activemq.jndi.JNDIReferenceFactory"

        physicalName="MY.TEST.FOO"/>

    <Resource name="jms/queue/MyQueue"

        auth="Container"

        type="org.apache.activemq.command.ActiveMQQueue"

        factory="org.apache.activemq.jndi.JNDIReferenceFactory"

        physicalName="MY.TEST.FOO.QUEUE"/> 

具体也不晓得这个是做什么的,看上去貌似是配置jndi的,我没用jndi的方式,加上这个,也正常运行。所以还是加上吧。

然后在web.xml里面,加入监听的类,这个类是自己写的。

  <!-- 添加jms监听 -->
  <servlet>
    <servlet-name>jms-listener</servlet-name>
    <servlet-class>
        com.message.JMSListener
    </servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>

然后就建立这个类:

package com.message;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.ObjectMessage;
import javax.jms.Session;
import javax.servlet.http.HttpServlet;

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.log4j.Logger;public class JMSListener extends HttpServlet implements MessageListener {
    /**
     * 
     */
    private static final long serialVersionUID = -8455543162505698971L;
    private Logger logger=Logger.getLogger(JMSListener.class);
    
    ConnectionFactory connectionFactory;
    Connection connection=null;
    Session session;
    Destination destination;
    MessageConsumer consumer;
    int i;
    
    public JMSListener(){
        String url = "tcp://localhost:61616";
        String queue ="NewNocPacket";
        connectionFactory = new ActiveMQConnectionFactory(
                ActiveMQConnection.DEFAULT_USER,
                ActiveMQConnection.DEFAULT_PASSWORD, url);
                try {
                    connection = connectionFactory.createConnection();
                    session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);
                    destination = session.createQueue(queue);
                    consumer = session.createConsumer(destination);
                    consumer.setMessageListener(this);
                    connection.start();
                } catch (JMSException e) {
                    // TODO Auto-generated catch block
                    logger.error("===========cusumer create error");
                    e.printStackTrace();
                }
    }
            
    @Override
    public void onMessage(Message message) {
        // TODO Auto-generated method stub
        ObjectMessage msg=(ObjectMessage) message;try {
                logger.info("=========hello ===========");
                i++;
            } catch (JMSException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    }

}

原理大概是这样,上面代码不要照抄的。

然后就ok了,部署一下项目,建立一个消息的发送者,消息到达后,监听就会自动执行,打印出onmessage里面的话。

tomcat监听activemq jms配置,布布扣,bubuko.com

tomcat监听activemq jms配置

标签:des   style   blog   http   java   color   os   io   

原文地址:http://www.cnblogs.com/juepei/p/3867706.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!