2 Replies Latest reply on Jun 9, 2016 5:16 PM by jfisherdev

    HornetQ Shutdown Issue

    jfisherdev

      I am currently migrating some applications from JBoss AS 4.2.2/JBossMQ to WildFly 9.0.2/HornetQ. These applications will be deployed to a standalone WildFly server.

       

      I have an MBean service that connects/subscribes to a JMS destination when started and disconnects/unsubscribes from the destination when stopped. This is accomplished via a helper class that is responsible for connection tasks and acts as a MessageListener and ExceptionListener.

       

      This essentially looks like this:

       

      public class MessagingService implements MessagingServiceMBean {
      
           private final MessagingServiceHelper helper = new MessagingServiceHelper();
      
           public void start() throws Exception {
                helper.connect();
           }
      
           public void stop() throws Exception {
                helper.disconnect();
           }
      
      }
      
      public class MessagingServiceHelper implements MessageListener, ExceptionListener {
      
           public void subscribe() throws Exception {
                //subscribe to destination, create JMS resource references
                //set this instance as a connection exception listener and a destination message listener
                //wait/retry mechanism is in place in case subscription is not immediately successful
           }
      
           public void unsubscribe() throws Exception {
                //unsubscribe from destination, destroy JMS resource [connections, sessions] references
           }
      
           @Override
           public void onMessage(Message message) {
                //handle message
           }
      
           @Override
           public void onException(JMSException jmsException) {
                //Try to reconnect
                boolean retryConnect = true;
                while(retryConnect) {
                     try {
                         disconnect();
                         connect();
                         retryConnect = false;
                     } catch(Exception e) {
                          //wait and retry until retry limit is reached                    
                          if(...) {
                               retryConnect = false;
                          }
                     }
                }
           }
      }
      

       

      This does work on WildFly; however, sometimes when shutting down the server, the onException() method in the helper class is called, resulting in repeated attempts to reconnect, which will never succeed.

       

      I suspect that the HornetQ server is sometimes, though not always, being shut down before the MBean service is stopped. Because the helper method to close/destroy the connection has not been called, this results in the JMSException being thrown, causing the onException() method to be called.

       

      There are two ways of dealing with this that I can think of; however, I am not completely sure how to go about implementing them:

       

      1. Have the MBean or the deployment declare dependencies that express a dependency on the JMS components [connection factory, destination] or the HornetQ server/messaging subsystem. Based on this discussion HornetQ Server Deployment Timing Issue, I am not sure that this can be done with MBeans.

       

      2. Have the onException() method recognize that the exception is being thrown due to server shut down and handle this accordingly. My question here is how would one properly detect if the exception is due to server shutdown? It looks like the HornetQConnection.JMSFailureListener class, which calls ExceptionListener.onException(), throws an exception with an error code of "DISCONNECT" and a message that reads "HQ119015: The connection was disconnected because of server shutdown." Would it be sufficient to check if the error code is "DISCONNECT" and possibly the error message content if necessary, or is there a better way to do this?

       

      Any information about this would be appreciated.