最后处置WebSphere MQ连接

时间:2013-09-12 22:29:36

标签: c# ibm-mq

我在.Net代码中连接到IBM Websphere MQ服务器,我想确保在使用“finally”时遵循最佳实践。

我目前有以下代码块,我认为可以修改为只包含finally子句中的close部分。那是对的吗? (我在应用程序的调用部分中捕获错误)。

    Hashtable properties = new Hashtable();
    properties.Add(MQC.TRANSPORT_PROPERTY, MQC.TRANSPORT_MQSERIES_CLIENT);
    properties.Add(MQC.CHANNEL_PROPERTY, channel);
    properties.Add(MQC.HOST_NAME_PROPERTY, host);
    properties.Add(MQC.PORT_PROPERTY, port);

    MQQueueManager qmgr = new MQQueueManager(queueManager, properties);

    try
    {
        var queueDepth = qmgr.AccessQueue(userQueue,
                                          MQC.MQOO_INPUT_AS_Q_DEF +
                                          MQC.MQOO_FAIL_IF_QUIESCING +
                                          MQC.MQOO_INQUIRE).CurrentDepth;
        if (qmgr.IsOpen)
            qmgr.Close();

        return queueDepth;
    }
    finally
    {
        if (qmgr.IsOpen)
            qmgr.Close();
    }

现在是这个

    Hashtable properties = new Hashtable();
    properties.Add(MQC.TRANSPORT_PROPERTY, MQC.TRANSPORT_MQSERIES_CLIENT);
    properties.Add(MQC.CHANNEL_PROPERTY, channel);
    properties.Add(MQC.HOST_NAME_PROPERTY, host);
    properties.Add(MQC.PORT_PROPERTY, port);

    MQQueueManager qmgr = new MQQueueManager(queueManager, properties);

    try
    {
        var queueDepth = qmgr.AccessQueue(userQueue,
                                          MQC.MQOO_INPUT_AS_Q_DEF +
                                          MQC.MQOO_FAIL_IF_QUIESCING +
                                          MQC.MQOO_INQUIRE).CurrentDepth;

        return queueDepth;
    }
    finally
    {
        if (qmgr.IsOpen)
            qmgr.Close();
    }

编辑: Renan提出了一个很好的建议。我认为MQQueueManger不是一次性的。听起来我可以做到这一点:

        using(MQQueueManager qmgr = new MQQueueManager(queueManager, properties))
    {
        var queueDepth = qmgr.AccessQueue(userQueue,
                              MQC.MQOO_INPUT_AS_Q_DEF +
                              MQC.MQOO_FAIL_IF_QUIESCING +
                              MQC.MQOO_INQUIRE).CurrentDepth;

        return queueDepth;
    }

编辑:我在阅读了Renan的建议后做了一些研究,并在下面找到了。听起来他们确实把它变成了一次性的。

MQ.Net

3 个答案:

答案 0 :(得分:3)

你是对的。即使try块中的代码返回异常,finally子句也会执行。

你也可以使用“using”结构进行连接(如果它实现了IDisposable,它应该这样做。)

using(qmgr){
    //do stuff
}

答案 1 :(得分:2)

没关系。

要调用CLR保证finally块(除非在一些非常罕见的边缘情况下,IIRC是内部CLR错误,例如调用FailFast或{{1 }})。通过从ExecutingEngineException中删除它,您将删除冗余代码。

答案 2 :(得分:2)

首先,没有正当理由认为应用程序需要知道队列的深度。应用程序应处理队列中的所有消息,直到它为空。

其次,不要使用IsOpen方法,因为它们不像您期望的那样工作。 IsOpen方法实际上不会检查队列句柄是否打开 - 它只检查内部标志。因此,不要使用它。

第三,不要关闭队列管理器对象,而是断开与队列管理器的连接。

第四,当连接到队列管理器时,该语句需要在try / catch中,因为如果连接失败,它将抛出MQException。

这是一个更好的代码布局,可以捕获并处理错误:

MQQueueManager qMgr = null;
MQQueue queue = null;
int openOptions = MQC.MQOO_INPUT_AS_Q_DEF + MQC.MQOO_FAIL_IF_QUIESCING + MQC.MQOO_INQUIRE;

try
{
   qMgr = new MQQueueManager(qMgrName);
   System.Console.Out.WriteLine("Successfully connected to " + qMgrName);

   queue = qMgr.AccessQueue(qName, openOptions, null, null, null);
   System.Console.Out.WriteLine("Successfully opened " + qName);

   System.Console.Out.WriteLine("Current queue depth is " + queue.CurrentDepth);
}
catch (MQException mqex)
{
   System.Console.Out.WriteLine("Exception CC=" + mqex.CompletionCode + " : RC=" + mqex.ReasonCode);
}
catch (System.IO.IOException ioex)
{
   System.Console.Out.WriteLine("Exception ioex=" + ioex);
}
finally
{
   try
   {
      if (queue !=null)
      {
         queue.Close();
         System.Console.Out.WriteLine("Successfully closed " + qName);
      }

   }
   catch (MQException mqex)
   {
      System.Console.Out.WriteLine("Exception on close CC=" + mqex.CompletionCode + " : RC=" + mqex.ReasonCode);
   }
   try
   {
      if (qMgr !=null)
      {
         qMgr.Disconnect();
         System.Console.Out.WriteLine("Disconnected from " + qMgrName);
      }
   }
   catch (MQException mqex)
   {
      System.Console.Out.WriteLine("Exception on disconnect CC=" + mqex.CompletionCode + " : RC=" + mqex.ReasonCode);
   }
}