如何处理 WebSphere 队列订阅的连接失败?

How to handle connection failure for subscription on WebSphere queue?

当一个.net windows 服务连接到Websphere MQ队列进行订阅并不断读取消息时,我们如何处理网络断开或发生错误的问题,我们可以依靠MQQueueManager.IsConnected 属性一直?这篇文章让我感到困惑:IC75673: MQQueueManager.IsConnected property is "true" after the connection is broken in a .NET application.

下面是我必须从队列中读取消息的代码,我使用的是 MQ 版本 8.0

private MQQueueManager _queueManager;
private MQQueue _queue;
private MQTopic _topic;
public bool isSubscribed = false;

public void Subscribe()
{
    var queueManagerName = "myQueueManager";
    var properties = new Hashtable();
    //Set all the properties here
    _queueManager = new MQQueueManager(queueManagerName, properties);

    //Conect to Queue
    _queue = _queueManager.AccessQueue("devQueue", MQC.MQOO_INPUT_AS_Q_DEF);

    isSubscribed = true;
    while (isSubscribed)
    {
        if (cancellationToken.IsCancellationRequested)
        {
            isSubscribed = false;
            cancellationToken.ThrowIfCancellationRequested();
        }
        try
        {
            Receive(onMessageReceived);
        }
        catch (Exception ex)
        {
            Console.WriteLine("Exception: {0}", ex);
        }
    }   
}


public override void Receive<T>(Action<T> onMessageReceived)
{
    try
    {
        var dataReceived = new MQMessage();
        _queue.Get(dataReceived);

        T message;
        message = (T)(object)dataReceived;

        onMessageReceived(message);     
        _queueManager.Commit();
    }
    catch (Exception ex)
    {
        throw ex;
    }
}

您链接到的 APAR IC75673 已在发布 Aug 01 2011. MQ v8.0 was released June 13 2014 的 MQ 7.0.1.6 版本中修复。通常,在 MQ 的先前版本中修复的任何内容也将在新版本中修复。

您的应用程序是否连接到应用程序 运行 所在的同一 Windows 服务器上的队列管理器?在您的代码中,您没有指定主机名、端口、通道名称等表明您正在通过网络使用 MQ 客户端连接的内容。您可能正在使用 MQSERVER 或 MQCHLLIB/MQCHLTAB 环境变量提供连接详细信息。

如果您的应用程序与队列管理器建立本地连接,它使用的是所谓的 "binding mode" 并且不依赖于网络。 IsConnected 方法可以很好地指示您仍然处于连接状态。文档指出,如果您的连接处于空闲状态,则提供的状态是最后一次 activity 执行(Get、Put 等)时的最后已知状态。

如果您的应用程序通过网络建立 MQ 客户端连接,则客户端注意到连接断开所花费的时间取决于队列管理器和 MQ 客户端之间协商的 HBINT。对于 .NET,您似乎需要使用 MQ 通道 table 来设置此值。 @Morag Hughson 对 Whosebug 问题“WebSphere MQ - Changing channel definition structure using XMS.NET API" provides some more detail. You can also look at my answer to Whosebug question "”的回答,了解有关 HBINT 如何影响客户端通道超时的更多详细信息。