5

I'm processing messages using a JMS MessageConsumer with a MessageListener. If something happens that causes the MessageConsumer to stop receiving and processing messages -- for example, if the underlying connection closes -- how can I detect it? There doesn't seem to be any notification mechanism that I can find in the spec.

I think the question is clear as is, but if you'd like me to post code to clarify the question, just ask!

In case it's important, I'm using ActiveMQ 5.8, although obviously I'd like a scheme that's not implementation-specific.

sigpwned
  • 6,957
  • 5
  • 28
  • 48

1 Answers1

2

Use ExceptionListener

If the JMS system detects a problem, it calls the listener's onException method:

public class MyConsumer implements ExceptionListener, MessageListener {

    private void init(){
        Connection connection = ... //create connection
        connection.setExceptionListener(this);
        connection.start();
    }

    public void onException(JMSException e){
        String errorCode = e.getErrorCode();
        Exception ex = e.getLinkedException();
        //clean up resources, or, attempt to reconnect 
    }

    public void onMessage(Message m){
       ...
}

Not much to it, really, the above is standard practice for standalone consumers; it's not implementation-specific; actually, quite the contrary as it's part of the spec!, so all JMS-compliant providers will support it.

raffian
  • 31,267
  • 26
  • 103
  • 174
  • 1
    Do I know if the JMS broker is alive or not? How to tell if the JMS connection is still OK? Can this tell if JMS broker is online? Thanks. – tonga Jan 13 '14 at 14:44
  • You set the `ExceptionListener` on the `Connection` object, right? Given that the `ExceptionListener` is `Connection`-scoped, what `JMSException`s would I expect to get? I suspect I would get any exceptions that relates to my `MessageConsumer`, plus many more besides. How can I tell if any given exception is relevant to my `MessageConsumer` as opposed to something else on the `Connection`? – sigpwned Jan 13 '14 at 16:08
  • The exception is telling you the connection instance is no good and should be so be discarded; attempt to open a new one using a loop with time delay in between to give the server a time to recover, if successful, that's how you know – raffian Jan 13 '14 at 16:08
  • Thanks for the quick response! So any call to `onException()` indicates that the underlying `Connection` is broken, and I should tear down and rebuild. Do I have that straight? – sigpwned Jan 13 '14 at 16:09
  • 1
    Exactly, don't try and make sense of the error; updated my answer, sorry for typos, damn this tiny iPhone screen! – raffian Jan 13 '14 at 16:51
  • Also, we have seen odd connection failures where the server knows it can't see the client, but the client still thinks its connection is valid (`onException` was never called and if you look at the JMS connection/session/subscriber you can see that they still think they are connected). This is very easy to replicate in JBoss5 using default http remoting settings. – Shadow Man Mar 17 '14 at 18:18