MQ放入消息C#

本文关键字:消息 MQ | 更新日期: 2023-09-27 18:19:29

我正忙于C# MQ,我想在Q上添加文本,但不确定我所做的是否是错误的。正在获取以下错误。

"System.NullReferenceException"类型的未处理异常发生在Data.Manager.dll中,发生在"Put1Queue.Put(myPutMessage,Put1PutMsgOptions);"

这是代码:

class Program
{
    static void Main(string[] args)
    {
        MQ mq = new MQ();
        Console.Read();
    }
}
public class MQ
{
    private MQQueue queue;
    private MQMessage queueMessage;
    private MQQueueManager queueManager;
    private string queueManagerName;
    private string host;
    private int port;
    private string channel;
    private string queueName;
    public String putMsg;
    public String markerMsg;
    private MQQueueManager Put1QueueManager;
    private MQPutMessageOptions Put1PutMsgOptions;
    private MQQueue Put1Queue;
    private MQQueueManager Put2QueueManager;
    private MQPutMessageOptions Put2PutMsgOptions;
    private MQQueue Put2Queue;
    private MQQueueManager myQueueManager;
    private MQPutMessageOptions myPutMsgOptions;
    private MQQueue myQueue;
    private Hashtable queueProperties;
    public MQ()
    {
        setupTestData();
    }
    public void setupTestData()
    {
        markerMsg = "<msglamdav5_xml><system message_id='"'" message_type='"'" correlation_id='"'" date_time_stamp='"'" originator='"'" destination='"'" queue_manager='"'" txn_req_number='"'" txn_req_id='"'"/><security user_name='"G9022O2 '" user_type='"'" user_identity='"G90226K     '" user_language='"'" environment='"PPEX    '" host_name='"'"/><errors/><data><txn_ctl bus_txn_phase='"3'" bus_txn_session_id='"'" /><data_key party_no='"0'"/><tasks><task txn_id='"MARK<uniqueNum> '" event_type='"2'" external_txn_id='"04255841252012-06-22-04.56.14.960000            '"><task_data><servicedata inputdata='"0124473471'"/></task_data><task_metadata><lists/></task_metadata></task></tasks></data></msglamdav5_xml>";
        try { 
        // Setup connection information
        MQEnvironment.Hostname = "SLHUBQ2.sanlam.co.za";
        MQEnvironment.properties.Add(MQC.TRANSPORT_PROPERTY, MQC.TRANSPORT_MQSERIES_CLIENT);
        MQEnvironment.properties.Add(MQC.CCSID_PROPERTY, 437);
        MQEnvironment.Channel = "CLIENTS";//"EFL.SRVCONN";
        MQEnvironment.Port = 1615;//lr.eval_int("<Port>");

        Put1QueueManager = new MQQueueManager("SLHUBQ2");
        int openOptions = MQC.MQOO_OUTPUT | MQC.MQOO_INQUIRE | MQC.MQOO_FAIL_IF_QUIESCING;
        Put1Queue = Put1QueueManager.AccessQueue("LAMDA.PERF.REQUEST", openOptions, null, null, null);
        Put1PutMsgOptions = new MQPutMessageOptions();
        Put1PutMsgOptions.Options = MQC.MQPMO_NEW_MSG_ID | MQC.MQPMO_NEW_CORREL_ID;
        }
        catch (Exception error)
        {
        }
        try {
                MQMessage myPutMessage = new MQMessage();
                myPutMessage.ClearMessage();
                myPutMessage.Persistence = MQC.MQPER_PERSISTENT;
                myPutMessage.Persistence = MQC.MQPER_NOT_PERSISTENT;
                myPutMessage.CorrelationId = MQC.MQCI_NONE;
                myPutMessage.MessageId  = MQC.MQMI_NONE;
                myPutMessage.Format = MQC.MQFMT_STRING; 
                myPutMessage.ReplyToQueueName= "LAMDA.PERF.RESPONSE";
                myPutMessage.WriteString(markerMsg);

              Put1Queue.Put(myPutMessage, Put1PutMsgOptions);


        }
        catch (MQException error)
        {

        }

        try
        {
            Put1Queue.Close();
            Put1QueueManager.Disconnect();
        }
        catch (Exception error)
        {
        }

    }

    }
}

MQ放入消息C#

问题可能出现在您的代码行中

Put1Queue = Put1QueueManager.AccessQueue("LAMDA.PERF.REQUEST", openOptions, null, null, null);

"AccessQueue"方法可能返回"null"结果。所以你可以尝试检查:

  1. 您的队列名称是否正确
  2. 选项正确吗。您可以尝试传递MQC.MQOO_OUTPUT + MQC.MQOO_FAIL_IF_QUIESCING选项

不如在EVERY catch语句中打印出异常,因为我打赌你在AccessQueue上得到的是RC 2035,因此Put方法的Put1Queue为null。即

catch (MQException mqex)
{
   System.Console.Out.WriteLine("MQException cc=" + mqex.CompletionCode + " : rc=" + mqex.ReasonCode);
}
catch (System.IO.IOException ioex)
{
   System.Console.Out.WriteLine("IOException ioex=" + ioex);
}
catch (Exception ex)
{
   System.Console.Out.WriteLine("Exception ex=" + ex);
}