IBM MQ:有什么方法可以获得连接中断通知?
IBM MQ : Any way to get connection interruption notification?
我正在使用 IBM MQ-7.5。我是 运行 一个 jms 客户端,它连接到一些其他主机上的管理器 运行。
我想监控与管理器的 TCP 连接。如果我的客户端与管理器的连接断开,我如何收到通知? IBM MQ API 中是否提供任何回调或侦听器以了解连接中断?
例如。就像 ActiveMQ 有 http://activemq.apache.org/maven/apidocs/org/apache/activemq/transport/TransportListener.html
谢谢,
阿努杰
就连接断开而言,连接断开异常将通过异常侦听器发送。
JMS 规范的编写使得连接中断等事件仅在同步调用时合法返回。我还建议设置一个异常侦听器,并从所有消息传递操作中捕获异常并采取适当的措施。
您想在队列管理器端还是在客户端应用程序中监控客户端应用程序连接?
为了得到任何连接问题的通知,MQ JMS 客户端有一个 ExceptionListener
可以附加到 MQConnection
。当与队列管理器的连接出现问题时(例如与队列管理器的连接断开),将调用此异常侦听器。更多详细信息 here:查看 setExceptionListener
方法的详细信息。在 MQConnection 上调用 setExceptionListener
方法以注册回调,如下所示。
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);
为了主动检查连接和会话的健康状况,我正在考虑使用以下方法。
/**
* 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;
}
它的方法看起来不错吗?
我只有一个恐惧:
我正在 isConnectionhealthy() 方法中创建一个测试会话并关闭它。它不会影响已经创建的实际用于实际通信的会话吗?我的意思是它会做一些事情,比如关闭已经创建的会话并开始新的会话吗?
我正在使用 IBM MQ-7.5。我是 运行 一个 jms 客户端,它连接到一些其他主机上的管理器 运行。
我想监控与管理器的 TCP 连接。如果我的客户端与管理器的连接断开,我如何收到通知? IBM MQ API 中是否提供任何回调或侦听器以了解连接中断?
例如。就像 ActiveMQ 有 http://activemq.apache.org/maven/apidocs/org/apache/activemq/transport/TransportListener.html
谢谢,
阿努杰
就连接断开而言,连接断开异常将通过异常侦听器发送。
JMS 规范的编写使得连接中断等事件仅在同步调用时合法返回。我还建议设置一个异常侦听器,并从所有消息传递操作中捕获异常并采取适当的措施。
您想在队列管理器端还是在客户端应用程序中监控客户端应用程序连接?
为了得到任何连接问题的通知,MQ JMS 客户端有一个 ExceptionListener
可以附加到 MQConnection
。当与队列管理器的连接出现问题时(例如与队列管理器的连接断开),将调用此异常侦听器。更多详细信息 here:查看 setExceptionListener
方法的详细信息。在 MQConnection 上调用 setExceptionListener
方法以注册回调,如下所示。
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);
为了主动检查连接和会话的健康状况,我正在考虑使用以下方法。
/**
* 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;
}
它的方法看起来不错吗?
我只有一个恐惧: 我正在 isConnectionhealthy() 方法中创建一个测试会话并关闭它。它不会影响已经创建的实际用于实际通信的会话吗?我的意思是它会做一些事情,比如关闭已经创建的会话并开始新的会话吗?