简体   繁体   中英

How to programmatically retrieve reason code from a XMS XMSException

I have a XMS MQ Client app that is pulling off messages from a series of MQ endpoints. There are certain reason codes for which the process can continue, and for some that it should abort. For example a MQRC_Q_MGR_NOT_AVAILABLE 2059 for one endpoint shouldn't abort the whole process. Consequently I would like to check for this reason code.

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);
    }
}

Problem is that the only attributes available on the XMSException to examine are ex.ErrorCode and ex.Message , which are respectively:

Error Code: CWSMQ0006

and

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

I can see the Reason in the Message, but can't find a method or attribute to retrieve it.

There are probably 2 ways to do it

1) You can use the LinkedException

Something like the following

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

2) Reference amqmdnet.dll as well to the project and use MQException.Something like

    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);
    }

Solution by OP

Based on accepted answer, a 'working' code is:

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;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM