1

我正在用下面的代码连接 MQ。我能够成功连接到 MQ。我的情况是我每 1 分钟将消息发送到 MQ 一次。断开电缆后,我收到 ResonCode 错误,但 IsConnected 属性仍然显示为 true。这是检查连接是否仍然连接的正确方法吗?或者有任何最佳实践。

我想在应用程序启动时打开连接,让它永远打开。

公共静态 MQQueueManager ConnectMQ() {

if ((queueManager == null) || (!queueManager.IsConnected)||(queueManager.ReasonCode == 2009)) { queueManager = new MQQueueManager(); } 返回队列管理器;}

4

1 回答 1

2

WMQ 客户端连接的行为是,当空闲时,它会显示为已连接,直到 API 调用失败或连接超时。因此 isConnected() 可能会报告 true,直到尝试获取、放置或查询调用并失败,此时 QMgr 将报告断开连接。

The other thing to consider here is that 2009 is not the only code you might get. It happens to be the one you get when the connection is severed but there are connection codes for QMgr shutting down, channel shutting down, and a variety of resource and other errors.

Typically for a requirement to maintain a constant connection you would want to wrap the connect and message processing loop inside a try/catch block nested inside a while statement. When you catch an exception other than an intentional exit, close the objects and QMgr, sleep at least 5 seconds, then loop around to the top of the while. The sleep is crucial because if you get caught in a tight reconnect loop and throw hundreds of connection attempts at the QMgr, you can bring even a mainframe QMgr to its knees.

另一种方法是使用 v7 WMQ 客户端和 QMgr。通过这种组合,自动重新连接可配置为通道配置。

于 2010-06-02T04:12:19.677 回答