2013-04-25 514 views
0

我正在嘗試編寫2個方法來測試是否在另一個隊列中接收到一個隊列中的已發送消息。驗證IBM MQ中的導入和導出隊列中的消息

發送方法 - 發送消息,例如 - 「消息123」 - 導出具有唯一相關ID的隊列。

Get方法

此隊列將有很多消息,不過我想只是我從高根據我的相關ID的消息。

代碼基於相關ID

 properties = new Hashtable(); 
     properties.Add(MQC.CONNECTION_NAME_PROPERTY, "connection name"); 
     properties.Add(MQC.TRANSPORT_PROPERTY, "transport type"); 
     properties.Add(MQC.CHANNEL_PROPERTY, "channel name"); 
     properties.Add(MQC.CONNECT_OPTIONS_PROPERTY, MQC.MQCNO_HANDLE_SHARE_BLOCK); 



     mqGetMsgOpts = new MQGetMessageOptions(); 
     mqGetMsgOpts.Options = MQC.MQGMO_BROWSE_FIRST | MQC.MQGMO_WAIT | MQC.MQOO_INQUIRE; 
     mqGetMsgOpts.MatchOptions = MQC.MQMO_MATCH_CORREL_ID; 
     mqGetMsgOpts.WaitInterval = 3000; //3 secs wait time 

檢查消息的問題,現在面臨是,當我讀到的消息,我得到的所有從導入隊列中的消息。

如何獲取我發送的消息並驗證在導出隊列中接收到的消息是我的消息?

從理論上講,像進口的隊列此

message.correlationid匹配的出口隊列message.correaltionid。

回答

1

閱讀郵件時,您的代碼段不顯示設置correlId。我有這個示例代碼只獲取匹配correlId的消息。

像之前一樣,您的代碼段仍然有MQC.MQOO_INQUIRE代表MQGMOMQOO代表Open OptionsMQGMO代表Get message options

 try 
     { 
      importQ = qm.AccessQueue("Q2", MQC.MQOO_INPUT_SHARED | MQC.MQOO_OUTPUT | MQC.MQOO_FAIL_IF_QUIESCING); 

      // Put one message. MQ generates correlid 
      MQMessage mqPutMsg = new MQMessage(); 
      mqPutMsg.WriteString("This is the first message with no app specified correl id"); 
      importQ.Put(mqPutMsg); 

      // Put another messages but with application specified correlation id 
      mqPutMsg = new MQMessage(); 
      mqPutMsg.WriteString("This is the first message with application specified correl id"); 
      mqPutMsg.CorrelationId = System.Text.Encoding.UTF8.GetBytes(strCorrelId); 
      MQPutMessageOptions mqpmo = new MQPutMessageOptions(); 
      importQ.Put(mqPutMsg,mqpmo); 

      mqPutMsg = new MQMessage(); 

      // Put another message with MQ generating correlation id 
      mqPutMsg.WriteString("This is the second message with no app specified correl id"); 
      importQ.Put(mqPutMsg); 

      // Get only the message that matches the correl id 
      MQMessage respMsg = new MQMessage(); 
      respMsg.CorrelationId = System.Text.Encoding.UTF8.GetBytes(strCorrelId); 
      MQGetMessageOptions gmo = new MQGetMessageOptions(); 
      gmo.WaitInterval = 3000; 
      gmo.Options = MQC.MQGMO_WAIT; 
      gmo.MatchOptions = MQC.MQMO_MATCH_CORREL_ID; 

      importQ.Get(respMsg, gmo); 
      Console.WriteLine(respMsg.ReadString(respMsg.MessageLength)); 
     } 
+0

謝謝,我也做了同樣的方式,正如你所說,但我不斷收到此錯誤的任何一個。 IBM.WMQ.MQException:MQRC_NO_MSG_AVAILABLE System.IO.EndOfStreamException:無法讀取超出流尾的內容。 - >消息離開隊列,並且由於數據長度變爲0而引發該消息,有什麼想法? – Sharpeye500 2013-04-27 00:19:02

+1

MQRC_NO_MSG_AVAILABLE:檢查代碼中的匹配選項,並在獲取消息時進行校驗。它看起來像指定的correlid可能不匹配隊列中任何消息的correlid。 EndOfStreamException:似乎嘗試讀取空消息的數據。 – Shashi 2013-04-27 12:14:33