javajbossjmsjbossmq

Reconnecting JMS listener to JBossMQ


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.


Solution

  • 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
            }
        }
    

    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.

    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.