0
votes

I am using IBM MQ-7.5. I am running a jms client which connects to the manager running on some other hosts.

I want to monitor the TCP connections with the manager. How do I get notified if my client connection is broken with the manager ? Is there any callback or listener provided in IBM MQ APIs to know any interruption on connection ?

Eg. Like ActiveMQ has http://activemq.apache.org/maven/apidocs/org/apache/activemq/transport/TransportListener.html

Thanks,
Anuj

3

3 Answers

2
votes

In terms of connections being dropped a connection broken exception will be sent via the exception listener.

The JMS Specification is written such that events such as connection broken are legitimately returned on synchronous calls only. I would also recommended setting an exception listener, and to catch exceptions from all messaging operations and taking appropriate action.

1
votes

Do you want to monitor client application connections at the queue manager end or in the client application?

To get notified of any connection issues, MQ JMS client has an ExceptionListener that can be attached to MQConnection. This exception listener will be invoked when there is an issue with connection to queue manager, for example connection to queue manager is broken. More details here: View details of setExceptionListener method. Call the setExceptionListener method on MQConnection to register a callback as shown below.

  MQQueueConnectionFactory cf = new MQQueueConnectionFactory();
  ExceptionListener exceptionListener = new ExceptionListener(){
                @Override
                public void onException(JMSException e) {
                    System.out.println(e);
                    if(e.getLinkedException() != null)
                        System.out.println(e.getLinkedException());
                }
            };
 MQQueueConnection connection = (MQQueueConnection) cf.createQueueConnection();
 connection.setExceptionListener(exceptionListener);
0
votes

To actively check the health of connection and session, i am thinking of using below approach.

/**
 * Method to check if connection is healthy or not.
 * It creates a session and close it. mqQueueConnection
 * is the connection for which we want to check the health. 
 */
protected boolean isConnectionHealthy()
{
    try {
        Session session = mqQueueConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        session.close();
    }
    catch(JMSException e) {
        LOG.warn("Exception occured while checking health of connection. Not able to create " + "new session" + e.getMessage(), e);
        return false;
    }
    return true;
}

/**
 * Method to check if session is healthy or not.
 * It creates a consumer and close it. mqQueueSession
 * is the session for which we want to check the health.
 */
protected boolean isSessionHealthy()
{
    try {
        MessageConsumer consumer = mqQueueSession.createConsumer(mqQueue);
        consumer.close();
    }
    catch(JMSException e) {
        LOG.warn("Exception occured while checking health of the session. Not able to create "
            + "new consumer" + e.getMessage(), e);
        return false;
    }
    return true;
}

Does it approach looks good ?

I just have one fear here: I am creating a test session in isConnectionhealthy() method and closing it. Will it not affect the already created session which is actually used for real communication? I mean will it do something like it closed already created session and started new one ?