简体   繁体   English

如何让我的 JMS 消息侦听器无限期地收听

[英]How do I keep my JMS message listener listening indefinitely

My code has two different JMS queues on different places.我的代码在不同的地方有两个不同的 JMS 队列。 It takes a message from one queue, does some processing, builds a new message, and puts it on another queue to be picked up by a different process.它从一个队列中获取一条消息,进行一些处理,构建一条新消息,然后将其放在另一个队列中以由不同的进程提取。 Although it works what I want is for the listener to listen continuously so that whenever a new message is put on the first queue it is picked up.虽然它可以工作,但我想要的是让侦听器连续侦听,以便每当有新消息放在第一个队列上时,它就会被拾取。 Main method of my listener:我的听众的主要方法:

public static void main(String[] args) throws JMSException {
    parseArgs(args);
    AMessageListener aMessageListener = new AMessageListener();
    try {
        aMessageListener.startListener();
        Thread.sleep(100000);
        aMessageListener.destroy();
    } catch (JMSException e) {
        // TODO handle exception
        e.printStackTrace();
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    aMessageListener.destroy();
}

And the code I use to start the listener:我用来启动监听器的代码:

public void startListener() throws JMSException{
    ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(ACTIVE_MQ_FACTORY_URL);
    conn = connectionFactory.createConnection(jmsBrokerUser, jmsBrokerPass);
    conn.start();

    Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
    Queue queue = session.createQueue("some.in.queue");
    MessageConsumer consumer = session.createConsumer(queue);
    consumer.setMessageListener(this);
}

I have been looking online to try to find any best practices but there's little to find.我一直在网上寻找任何最佳实践,但几乎找不到。 An obvious way to get what I want is to put while(true) where I have the Thread.sleep(100000) .获得我想要的东西的一个明显方法是将while(true)放在我有Thread.sleep(100000)的地方。 I don't really feel like that's a clean way of doing it.我真的不觉得这是一种干净的方式。

Are there any different/better ways?有没有不同/更好的方法?

Since this is apparently a simple command-line application you could use System.in.read() so that the program exits when the user presses the enter key.由于这显然是一个简单的命令行应用程序,您可以使用System.in.read()以便在用户按下回车键时程序退出。 Or if you wanted to prevent accidental closures you could make it so the user had to type a specific keyword like "exit" or "quit," etc.或者,如果您想防止意外关闭,您可以这样做,以便用户必须输入特定的关键字,如“exit”或“quit”等。

Aside from that I don't see any problem with using while(true) although I'd use it in conjunction with Thread.sleep() , eg:除此之外,我认为使用while(true)没有任何问题,尽管我会将它与Thread.sleep()结合使用,例如:

while(true) {
   Thread.sleep(100000);
}

Also, I would recommend you put aMessageListener.destroy();另外,我建议你把aMessageListener.destroy(); in a finally block instead of having it your code twice.finally块中,而不是让您的代码两次。

Here's the most terse way to lock up forever这是永久锁定的最简洁方法

Thread.currentThread().join(); // Sleep forever, waiting for ourselves to finish

That said you should really have an application that either restarts failed connections or dies along with the connection so something higher up can restart your JVM.也就是说,您确实应该拥有一个应用程序,该应用程序要么重新启动失败的连接,要么与连接一起终止,以便更高层的东西可以重新启动您的 JVM。 To that end, I would turn main into a polling loop that checks Session or Connection every second.为此,我会将 main 变成一个轮询循环,每秒检查SessionConnection

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM