如何从 Web 应用程序监听消息队列?(Tomcat,ActiveMQ)

How to listen to a message queue from a web application? (Tomcat, ActiveMQ)(如何从 Web 应用程序监听消息队列?(Tomcat,ActiveMQ))

本文介绍了如何从 Web 应用程序监听消息队列?(Tomcat,ActiveMQ)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm happily improving my web application that runs on a Apache Tomcat. An ActiveMQ JMS server is added to send and receive messages.

我已经可以发送和接收消息,但需要接收方的帮助.

我的网络应用应该如何持续监听一个队列来接收消息?

新消息到达,服务器应该对它们采取行动.例如:将数据添加到数据库或发送回消息.

New messages arrive and the server should act on them. Ex: add data to the DB or or send an message back.

我已经可以发送消息了.这是代码.

ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory("tcp://localhost:61616");
Connection connection = factory.createConnection();
Session session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);
Queue queue = session.createQueue("clientQueue");
MessageProducer publisher = session.createProducer(queue);
connection.start();

Message message = null;
message = session.createTextMessage("Text Message");
publisher.send(message);

我已经可以在请求后收到一条消息(点击;-))

I can already receive a message after a request (a click ;-))

connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616");
connection = connectionFactory.createConnection();
connection.start();
session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);
destination = session.createQueue("serverQueue");
consumer = session.createConsumer(destination);

while (true) {
    Message message = consumer.receive(300000);
    //Do message stuff
}

我应该如何让网络应用不断地监听队列?建议的方式是什么?

热烈感谢所有帮助.谢谢.

编辑 - 解决方案

目前的工作解决方案来自 DaveH

我添加了一个 ServletContextListener 来持续收听我的消息.

I have added a ServletContextListener to listen to my message continuously.

web.xml

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

听众:

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
        }
    }
}

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

热烈感谢所有帮助.谢谢.

推荐答案

如果您的代码已经可以使用队列中的消息(看起来确实如此),那么我认为您的问题归结为您如何获得它要运行的一段代码.

If you have code that already can consume messages from the queue ( which it appears you do ), then I think your problem comes down to how do you get that piece of code to run.

看来您没有使用任何框架,所以我认为我将采用可以从队列中检索消息并在应用程序服务器的单独线程中运行的代码.让该线程在应用程序服务器启动时启动,并在应用程序服务器关闭时自行整理.

It appears you aren't using any frameworks so I think the approach that I would take would be to take that code that can retrieve messages from the queue and run it in a separate thread in the application server. Get that thread to start up at application server start time, and tidy up itself as the application server closes down.

在应用服务器启动时启动线程的最简单方法是引入一个 ServletContextListener(示例 这里.)在上下文监听器中,在一个单独的线程中启动你的队列监听代码.

Easiest way to start the thread on app server startup is to introduce a ServletContextListener ( an example here.) In the Context Listener start your queue listening code in a separate thread.

我使用了这个提议的解决方案并将上面的代码添加到问题中.

I used this proposed solution and added the code above to the question.

这篇关于如何从 Web 应用程序监听消息队列?(Tomcat,ActiveMQ)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何从 Web 应用程序监听消息队列?(Tomcat,ActiveMQ)

基础教程推荐