【问题标题】:How to programmatically retrieve reason code from a XMS XMSException如何以编程方式从 XMS XMSException 中检索原因代码
【发布时间】:2020-03-16 07:36:25
【问题描述】:

我有一个 XMS MQ 客户端应用程序,它从一系列 MQ 端点提取消息。对于某些原因代码,该过程可以继续,而对于某些原因代码,它应该中止。例如,一个端点的MQRC_Q_MGR_NOT_AVAILABLE2059 不应中止整个过程。因此,我想检查这个原因代码。

cf = factoryFactory.CreateConnectionFactory();
foreach (Endpoint e in env.GetEndpoints())
{
    Console.WriteLine("Consuming messages from endpoint {0}({1})", e.host, e.port);

    // Set the properties
    SetConnectionProperties(cf, e);

    try
    {
        ReceiveMessagesFromEndpoint(cf);
    }
    catch (XMSException ex)
    {
        Console.WriteLine("XMSException caught: {0}", ex);
        Console.WriteLine("Error Code: {0}", ex.ErrorCode);
        Console.WriteLine("Error Message: {0}", ex.Message);
    }
}

问题是 XMSException 上唯一可以检查的属性是 ex.ErrorCodeex.Message,它们分别是:

Error Code: CWSMQ0006

Error Message: CWSMQ0006E: An exception was received during the call to the method ConnectionFactory.CreateConnection: CompCode: 2, Reason: 2059.

我可以在消息中看到原因,但找不到检索它的方法或属性。

【问题讨论】:

    标签: c# ibm-mq xms


    【解决方案1】:

    大概有两种方法

    1) 你可以使用 LinkedException

    类似下面的东西

        try
        {
        }
        catch (XMSException e)
        {
          if(e.LinkedException!=null)
            Console.WriteLine(e.LinkedException.Message);
          else
            Console.WriteLine(e);
        }
    

    2) 将 amqmdnet.dll 也引用到项目中并使用 MQException.Something 之类的

        try
        {
        }
        catch (XMSException e)
        {
          if(e.LinkedException!=null)
          {
            IBM.WMQ.MQException inner = (IBM.WMQ.MQException)e.LinkedException;
                Console.WriteLine("Reason:"+ inner.ReasonCode);
          }
          else
            Console.WriteLine(e);
        }
    

    【讨论】:

    • ex.LinkedException.Message 是原因码,而不是针对2059 的硬编码字符串进行测试,例如。 if (! "2059".Equals(ex.LinkedException.Message)) 有可以比较的已定义常量吗?
    • 如果我正确理解您的问题,您是说您想使用“IBM.XMS.MQC.MQRC_Q_MGR_NOT_AVAILABLE”之类的东西而不是 2059
    • 是的,就是这样,虽然现在它比较一个 int 和一个 ToString 和一个字符串。
    【解决方案2】:

    OP 解决方案

    根据接受的答案,“工作”代码是:

    cf = factoryFactory.CreateConnectionFactory();
    foreach (Endpoint e in env.GetEndpoints())
    {
        Console.WriteLine("Consuming messages from endpoint {0}({1})", e.host, e.port);
    
        // Set the properties
        SetConnectionProperties(cf, e);
    
        try
        {
            ReceiveMessagesFromEndpoint(cf);
        }
        catch (XMSException ex)
        {
            Console.WriteLine("XMSException caught: {0}", ex);
            Console.WriteLine("Error Code: {0}", ex.ErrorCode);
            Console.WriteLine("Error Message: {0}", ex.Message);
    
            if (ex.LinkedException != null && 
                    IBM.XMS.MQC.MQRC_Q_MGR_NOT_AVAILABLE.ToString().Equals(ex.LinkedException.Message))
    
            {
                Console.WriteLine("Queue Manager on this endpoint is not available");
                Console.WriteLine("Moving onto next endpoint");
                continue;
            }
            Console.WriteLine("Unexpected Error - Aborting");
            throw;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-24
      • 2012-11-01
      • 1970-01-01
      • 2011-04-07
      • 2012-11-23
      相关资源
      最近更新 更多