MQQueueManager:在创建之后,在使用期间对isConnected状态有什么期望?

时间:2013-10-10 12:05:28

标签: ibm-mq

我在下面继承了这段可爱的代码。

我读它的方式开发人员做了三个假设:

  1. MQQueueManager实例不一定在isConnected()返回true的状态下创建
  2. 如果在isConnected()== false状态下创建,状态可能会“更晚”更改,因此超时代码
  3. 如果您尝试从断开连接的MQQueueManager创建访问队列,则不会抛出异常。
  4. 我期望的是MQQueueManager实例是在isConnected()== true状态下创建的,此状态可能稍后更改(网络故障等),并且此状态更改(isConnected()== false)将导致队列上的操作因MQException而失败。

    文档在这些方面非常安静,除非要注意在手动断开MQQueueManager之后重新连接到队列的唯一方法是创建MQQueueManager的新实例。

    谁可以直接在这里安排我?

    qMgr = new MQQueueManager( qManager );
    
    // Set up the options on the queue we wish to open...
    // Note. All WebSphere MQ Options are prefixed with MQC in Java.
    final int openOptions = MQC.MQOO_INPUT_AS_Q_DEF | MQC.MQOO_OUTPUT;
    
    // Now specify the queue that we wish to open,
    // and the open options...
    queue = qMgr.accessQueue( queueName, openOptions );
    
    // Set the get message options...
    final MQGetMessageOptions gmo = new MQGetMessageOptions(); // accept the
    // defaults
    gmo.options = MQC.MQGMO_WAIT;
    gmo.waitInterval = 1000;
    
    
    connectionStatus = CONNECTING;
    int timeOutCounter = 0;
    while(!qMgr.isConnected()) {
        InboundMsgTask.sleep(1000);
        timeOutCounter++;
        if(timeOutCounter > 4) {
            connectionStatus = TIME_OUT;
            return;
        }
    }
    connectionStatus = CONNECTED;
    

1 个答案:

答案 0 :(得分:0)

不要检查IsConnected==True,最好继续进行实际的MQ .NET方法调用(Get, Put等)。如果连接断开,这些调用将抛出连接断开的execption(MQRC 2009)。请记住,IsConnected在调用MQ方法之前可能是True,但在执行MQ方法时它可能会发生变化。您的代码需要处理连接中断的异常并调用MQQueueManager.Disconnect方法,然后重新建立连接。 Disconnect调用将释放所有已分配的资源,并优先关闭所有已打开的队列管理器对象。忽略Disconnect方法抛出的任何异常。

如果您使用的是MQ v7.1或v7.5,那么.NET客户端可以在检测到连接错误时自动重新连接到队列管理器。您需要启用自动重新连接选项。请参阅MQ InfoCenter。

修改 如果成功建立了与队列管理器的连接,则new MQQueueManager()将返回MQQueueManager类的实例。如果出现错误,将引发MQException。无需等待连接完成,因为MQQueueManager构造函数是阻塞调用。