13

我们有一个 Java 侦听器,它从 JBossMQ 的队列中读取文本消息。如果我们必须重新启动 JBoss,侦听器将不会重新连接并再次开始读取消息。我们只是每 2 分钟在侦听器的日志文件中收到一条消息,说明它无法连接。有什么我们没有在我们的代码或 JBossMQ 中设置的吗?我是 JMS 的新手,因此我们将不胜感激任何帮助。谢谢。

4

4 回答 4

10

您应该在您的客户端代码中实现 javax.jms.ExceptionListener。您将需要一个名为 onException 的方法。当客户端的连接丢失时,你应该得到一个 JMSException,并且这个方法会被自动调用。您唯一需要注意的是,如果您有意断开与 JBossMQ 的连接——这也会引发异常。

一些代码可能如下所示:

    public void onException (JMSException jsme)
    {
        if (!closeRequested)
        {
            this.disconnect();
            this.establishConnection(connectionProps, queueName, uname, pword, clientID, messageSelector);
        }        
        else
        {
            //Client requested close so do not try to reconnect
        }
    }

在您的“建立连接”代码中,您将实现一个while(!initialized)内部包含 try/catch 的构造。在您确定您已正确连接和订阅之前,请留在 while 循环中以捕获所有 JMS/Naming/等。例外。

我们多年来一直在 JBossMQ 中使用这种方法,并且效果很好。我们从未遇到过 JMS 客户端在弹回 JBossMQ 或失去网络连接后无法重新连接的问题。

于 2008-09-07T00:05:10.490 回答
9

我强烈建议您使用JMS 的 Spring 抽象,例如 MessageListenerContainer来为您处理重新连接、事务和池。您只需要提供一个 MessageListener 并使用 ConnectionFactory 配置 MessageListenerContainer ,其余的由容器完成。

于 2008-09-16T14:24:52.360 回答
6

If you're purely a listener and do no other JMS calls other than connection setup, then the "onException() handler" answer is correct.

If you do any JMS calls in your code, just using onException() callback isn't sufficient. Problems are relayed from the JMS provider to the app either via an exception on a JMS method call or through the onException() callback. Not both.

So if you call any JMS methods from your code, you'll also want to invoke that reconnection logic if you get any exceptions on those calls.

于 2008-09-22T20:43:01.253 回答
1

Piece of advice from personal experience. Upgrade to JBoss Messaging. I've seen it in production for 4 months without problems. It has fully transparent failover - amongst many other features.

Also, if you do go with Spring, be very careful with the JmsTemplate.

于 2008-09-23T00:47:41.803 回答