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。通过这种组合,自动重新连接可配置为通道配置。