JMS 异常侦听器

JMS ExceptionListener

我正在使用 javax.jms.Connection 发送和接收 JMS 消息 to/from JBoss501。我也在用Connection.setExceptionListener()。我想知道Connection.start()连接启动前是否需要设置异常监听器?随意重现 JBoss 连接异常以确认是否调用异常侦听器的任何想法。

来自规范:

If a JMS provider detects a serious problem with a Connection object, it informs the Connection object's ExceptionListener, if one has been registered. It does this by calling the listener's onException method, passing it a JMSException argument describing the problem.

An exception listener allows a client to be notified of a problem asynchronously. Some connections only consume messages, so they would have no other way to learn that their connection has failed.

请记住,这里有供应商特定实现的地方,关于如何处理异常。如果可能,一些供应商会尝试“修复”这种情况。

现在关于在设置异常侦听器之前或之后启动连接... 始终在启动连接之前设置异常侦听器。

关于复制我想你可以

  • 启动一个消费者,connection.start应该是运行。正在等待消息。
  • 立即关闭jboss。
  • 重启jboss.

另外我知道使用 Eclipse 或其他开发工具将帮助您在调试模式下启动,并且您可以在任何特定时间因为调试器显示状态只是中止 jboss 服务器并重新启动它.

使用 Jboss 5.0.1,即使在启动连接后设置异常侦听器也能正常工作。正如 "MrSimpleMind" 提到的,异常侦听器在开始连接之前服务得更好 - 事实上 - 一旦从 ConnectionFactory 创建连接就最好。

异常侦听器即使在连接没有启动的情况下也是有效的——在Jboss501的情况下。

 //Main       
 try {
        connection = getConnection();
        connection.setExceptionListener(new MyExceptionListener());
       //Exception listener is effective even before connection is started.
        //connection.start(); 
        while(true){
            try {
                Thread.sleep(1000 * 5);
                Log.l("Kill the JMS provider any time now. !! Observe if the JMS listener works.");
            } catch (InterruptedException e) {
                //do nothing.
            }
        }
    } catch (NamingException e) {
        e.printStackTrace();
    } catch (JMSException e) {
        e.printStackTrace();
    }

    //Exception Listener
    public class MyExceptionListener implements ExceptionListener {

    @Override
    public void onException(JMSException e) {
            Log.l("Exception listener invoked");
    }
    }

为了重现 ExceptionListener 获取 triggered/invoked 的场景,我使用了 JBoss 管理控制台并使用 Jboss 管理控制台公开的 mx bean 停止了 ConnectionFactory。