将 JMS 侦听器重新连接到 JBossMQ [英] Reconnecting JMS listener to JBossMQ

查看:16
本文介绍了将 JMS 侦听器重新连接到 JBossMQ的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

We have a Java listener that reads text messages off of a queue in JBossMQ. If we have to reboot JBoss, the listener will not reconnect and start reading messages again. We just get messages in the listener's log file every 2 minutes saying it can't connect. Is there something we're not setting in our code or in JBossMQ? I'm new to JMS so any help will be greatly appreciated. Thanks.

推荐答案

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

You should implement in your client code javax.jms.ExceptionListener. You will need a method called onException. When the client's connection is lost, you should get a JMSException, and this method will be called automatically. The only thing you have to look out for is if you are intentionally disconnecting from JBossMQ-- that will also throw an exception.

某些代码可能如下所示:

Some code might look like this:

    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/等.例外.

In your "establishConnection" code, you would then implement a while(!initialized) construct that contains a try/catch inside of it. Until you are sure you have connected and subscribed properly, stay inside the while loop catching all JMS/Naming/etc. exceptions.

我们多年来一直在 JBossMQ 上使用这种方法,而且效果很好.我们的 JMS 客户端在弹跳 JBossMQ 或失去网络连接后没有重新连接的问题从未出现过.

We've used this method for years with JBossMQ and it works great. We have never had a problem with our JMS clients not reconnecting after bouncing JBossMQ or losing our network connection.

这篇关于将 JMS 侦听器重新连接到 JBossMQ的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆