如何实现ServletContextListener来监听消息队列? (Tomcat,ActiveMQ)

时间:2015-07-28 08:43:34

标签: java tomcat jms activemq

我很高兴改进在Apache Tomcat 上运行的 Web应用程序。添加 ActiveMQ JMS服务器以发送和接收消息。

我已经添加了一个ServletContextListener来连续收听我的消息。 但是,我不确定这是否适合生产。欢迎任何建议。

的web.xml

<listener>
    <listener-class>com.test.JMSContextListener</listener-class>
</listener>

The Listeren:

public class JMSContextListener implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent arg0) {
        JMSConnector.startListening();
    }

    @Override
    public void contextDestroyed(ServletContextEvent arg0) {
        //Nothing
    }
}

连接:

public class JMSConnector {

    static Context context;
    static QueueConnectionFactory factory;

    static {
        try {
            context = new InitialContext();
            factory = (QueueConnectionFactory) context.lookup("java:comp/env/jms/ConnectionFactory");
        } catch (NamingException ex) {
            //TODO
        }
    }

    public static void startListening() {
        try {
            Connection connection = factory.createConnection();
            Queue queue = (javax.jms.Queue) context.lookup("java:comp/env/jms/Queue");
            Session session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);

            MessageConsumer consumer = session.createConsumer(queue);

            //This MessageListener will do stuff with the message
            MessageListenerImpl messageListener = new MessageListenerImpl();
            consumer.setMessageListener(messageListener);
            connection.start();

            // Start connection or nothing will happen!!!
            connection.start();
        } catch (JMSException ex) {
            //TODO
        } catch (NamingException ex) {
            //TODO
        }
    }
}

这是一种建议的方式还是应该改进?

热烈欢迎所有帮助。感谢。

1 个答案:

答案 0 :(得分:0)

我添加了一个改进的解决方案,包含一个Thread。

仍然欢迎额外的帮助: - )

的web.xml

<listener>
    <listener-class>com.test.JMSContextListener</listener-class>
</listener>

The Listeren:

public class JMSContextListener implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent arg0) {
        Thread thread = new Thread(new JMSConnector());
        thread.start();
    }

    @Override
    public void contextDestroyed(ServletContextEvent arg0) {
        //Nothing
    }
}

连接:

public class JMSConnector implements Runnable {
    public void run() {
        try {
            Context context = new InitialContext();
            QueueConnectionFactory factory = (QueueConnectionFactory) context.lookup("java:comp/env/jms/ConnectionFactory");            
            Connection connection = factory.createConnection();
            Queue queue = (javax.jms.Queue) context.lookup("java:comp/env/jms/serverQueue");
            Session session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);

            MessageConsumer consumer = session.createConsumer(queue);

            //This MessageListener will do stuff with the message
            MessageListenerImpl messageListener = new MessageListenerImpl();
            consumer.setMessageListener(messageListener);
            connection.start();

            // Start connection or nothing will happen!!!
            connection.start();
        } catch (JMSException ex) {
            //TODO
        } catch (NamingException ex) {
            //TODO
        }
    }
}

这是一种建议的方式还是应该改进?

热烈欢迎所有帮助。感谢。