<p>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.</p>

[{'answer_id': 47963, 'author': 'Todd', 'author_id': 3803, 'author_profile': 'https://Stackoverflow.com/users/3803', 'pm_score': 4, 'selected': True, 'text': '<p>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.</p>\n\n<p>Some code might look like this:</p>\n\n<pre><code>    public void onException (JMSException jsme)\n    {\n        if (!closeRequested)\n        {\n            this.disconnect();\n            this.establishConnection(connectionProps, queueName, uname, pword, clientID, messageSelector);\n        }        \n        else\n        {\n            //Client requested close so do not try to reconnect\n        }\n    }\n</code></pre>\n\n<p>In your "establishConnection" code, you would then implement a <code>while(!initialized)</code> 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.</p>\n\n<p>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.</p>\n'}, {'answer_id': 72859, 'author': 'James Strachan', 'author_id': 2068211, 'author_profile': 'https://Stackoverflow.com/users/2068211', 'pm_score': 3, 'selected': False, 'text': '<p>I\'d highly recommend you use the <a href="http://static.springframework.org/spring/docs/2.5.x/reference/jms.html#jms-mdp" rel="noreferrer">Spring abstractions for JMS such as the MessageListenerContainer</a> to deal with reconnection, transactions and pooling for you. You just need to supply a MessageListener and configure the MessageListenerContainer with the ConnectionFactory and the container does the rest.</p>\n'}, {'answer_id': 117503, 'author': 'John M', 'author_id': 20734, 'author_profile': 'https://Stackoverflow.com/users/20734', 'pm_score': 3, 'selected': False, 'text': '<p>If you\'re purely a listener and do <em>no</em> other JMS calls other than connection setup, then the "onException() handler" answer is correct.</p>\n\n<p>If you do any JMS calls in your code, just using onException() callback isn\'t sufficient.  Problems are relayed from the JMS provider to the app <em>either</em> via an exception on a JMS method call <em>or</em> through the onException() callback.  Not both.</p>\n\n<p>So if you call any JMS methods from your code, you\'ll also want to invoke that reconnection logic if you get any exceptions on those calls.</p>\n'}, {'answer_id': 118477, 'author': 'user13229', 'author_id': 13229, 'author_profile': 'https://Stackoverflow.com/users/13229', 'pm_score': 1, 'selected': False, 'text': '<p>Piece of advice from personal experience.  <strong>Upgrade to <a href="http://www.jboss.org/jbossmessaging/" rel="nofollow noreferrer">JBoss Messaging</a>.</strong>  I\'ve seen it in production for 4 months without problems.  It has fully transparent failover - amongst many other features.</p>\n\n<p>Also, if you do go with Spring, be <a href="http://wiki.jboss.org/wiki/JBMSpringJMSTemplateNotes" rel="nofollow noreferrer">very</a> <a href="http://activemq.apache.org/jmstemplate-gotchas.html" rel="nofollow noreferrer">careful</a> with the <a href="http://static.springframework.org/spring/docs/2.5.x/reference/jms.html#jms-jmstemplate" rel="nofollow noreferrer">JmsTemplate</a>.  </p>\n'}]
['https://Stackoverflow.com/questions/47683', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3098/']