JMS ExceptionListener

2020-07-26 03:54发布

问题:

I am using javax.jms.Connection to send and receive JMS messages to/from JBoss501. I am also using the Connection.setExceptionListener(). I would like to know if the exception listener needs to be set before the connection is started by Connection.start()? Any ideas to reproduce the JBoss connection exception at will to confirm if the exception listener is invoked.

回答1:

From the spec:

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.

Remember that there is place for vendor specific implementation here, about how exceptions are handled. Some vendors try to "fix" the situation if possible.

Now about start the connection before or after setting the exception listeneer... Always set the exception listener BEFORE starting the connection.

And About reproducing I think you could

  • Start a consumer, connection.start should be run. And waiting for a message.
  • Shutdown jboss immediately.
  • Restart jboss.

Also I know that using Eclipse or other dev tools will help you start in debug mode, and you can at any specific time as the debugger shows you the status just abort the jboss server and restart it again.



回答2:

With Jboss 5.0.1, setting the exception listener worked even after starting the connection. As mentioned by "MrSimpleMind" exception listener serves better before starting the connection - in fact - best as soon as the connection is created from ConnectionFactory.

The exception listener is effective even if the connection is not started - in case of Jboss 501.

 //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");
    }
    }

To reproduce the scenario where the ExceptionListener gets triggered/invoked, I used the JBoss Management console and stopped the ConnectionFactory using the mx bean exposed by Jboss mgmt console.



标签: java jms