/// <summary> /// Read Message from Local Queue /// </summary> /// <param name="strqueueName">Queue Name</param> /// <returns>Text Message</returns> public MQMessageStatus ReadLocalQMsg(string strQueueName) { MQMessageStatus messageStatus = new MQMessageStatus(); string strReturn = string.Empty; ReceiveQueueName = strQueueName; try { queue = queueManager.AccessQueue(ReceiveQueueName, MQC.MQOO_INPUT_AS_Q_DEF + MQC.MQOO_FAIL_IF_QUIESCING); queueMessage = new MQMessage(); queueMessage.Format = MQC.MQFMT_STRING; queueGetMessageOptions = new MQGetMessageOptions(); queue.Get(queueMessage, queueGetMessageOptions); strReturn = queueMessage.ReadString(queueMessage.MessageLength); messageStatus.Message = strReturn; messageStatus.Status = true; } catch (MQException MQexp) { strReturn = "Exception: " + MQexp.Message; messageStatus.Message = strReturn; messageStatus.Status = false; } catch (Exception exp) { strReturn = "Exception: " + exp.Message; messageStatus.Message = strReturn; messageStatus.Status = false; } return(messageStatus); }
public IEnumerable JustReadYieldAllMessageFromQueue(string queueName) { queue = queueManager.AccessQueue(queueName, MQC.MQOO_BROWSE + MQC.MQOO_FAIL_IF_QUIESCING); while (true) { try { queueMessage = new MQMessage(); queueMessage.Format = MQC.MQFMT_STRING; MQGetMessageOptions mQGetMessageOptions = new MQGetMessageOptions(); //mQGetMessageOptions.Options = MQC.MQGMO_BROWSE_FIRST; mQGetMessageOptions.Options = MQC.MQGMO_BROWSE_NEXT + MQC.MQGMO_NO_WAIT + MQC.MQGMO_FAIL_IF_QUIESCING; queue.Get(queueMessage, mQGetMessageOptions); } catch (MQException mqexp) { queue.Close(); yield break; } yield return(queueMessage.ReadString(queueMessage.MessageLength)); } }
/// <summary> /// Receive a reply message /// </summary> /// <returns></returns> public String ReceiveMessage(byte[] messageId) { String retMsg = ""; MQMessage mqMsg = null; try { mqMsg = new MQMessage(); mqMsg.CorrelationId = messageId; MQGetMessageOptions gmo = new MQGetMessageOptions(); gmo.MatchOptions |= MQC.MQMO_MATCH_CORREL_ID; gmo.WaitInterval = 3000; gmo.Options |= MQC.MQGMO_WAIT; replyQ.Get(mqMsg, gmo); retMsg = mqMsg.ReadUTF(); } catch (MQException ex) { if (ex.Reason == 2033) { retMsg = ""; } else { throw ex; } } return(retMsg); }
/// <summary> /// Get a message from Queue. /// </summary> /// <returns>message.</returns> public string Get() { var result = string.Empty; try { var mqMsg = new MQMessage(); mQQueue.Get(mqMsg); if (mqMsg.TotalMessageLength != 0) { result = mqMsg.ReadString(mqMsg.TotalMessageLength); } } catch (MQException ex) { // If the WaitInterval option is specified and you can't get it within the specified time, an exceptin is raised. // As for processing, it is processed as follows: no message = string.empty. If any other exceptions are raised, it is thrown to the caller. if (ex.Reason != MQC.MQRC_NO_MSG_AVAILABLE) { throw ex; } } return(result); }
public List <MQMessage> BrowseMessages(string queueName) { int totalMessages = MessageCount(queueName); MQQueue mqQueue = _mqQueueManager.AccessQueue(queueName, MQC.MQOO_BROWSE); MQGetMessageOptions messageOptions = new MQGetMessageOptions { Options = MQC.MQGMO_BROWSE_FIRST }; if (totalMessages > 0) { List <MQMessage> messages = new List <MQMessage>(); for (int i = 1; i <= totalMessages; i++) { // Gettin an IBM MQ message from the queue. MQMessage message = new MQMessage(); mqQueue.Get(message, messageOptions); messages.Add(message); // get next message messageOptions.Options = MQC.MQGMO_BROWSE_NEXT; } mqQueue.Close(); return(messages); } mqQueue.Close(); return(null); }
public string ReadOneMensage() { MQQueueManager qMgr = new MQQueueManager(IbmConection.DbConection[5], _queueManagerProperties); // Agora especifique a fila que queremos abrir e as opções de abertura MQQueue fila = qMgr.AccessQueue(IbmConection.DbConection[6], MQC.MQQA_GET_ALLOWED + MQC.MQQA_GET_INHIBITED + MQC.MQOO_INPUT_AS_Q_DEF + MQC.MQOO_OUTPUT + MQC.MQOO_FAIL_IF_QUIESCING + MQC.MQOO_INQUIRE + MQC.MQOO_BROWSE); MQMessage retrievedMessage = new MQMessage(); // Defina as opções de obtenção de mensagens MQGetMessageOptions gmo = new MQGetMessageOptions(); // aceita os padrões // mesmo que MQGMO_DEFAULT string msgText = ""; if (fila.CurrentDepth > 0) { // Tire a mensagem da fila fila.Get(retrievedMessage, gmo); msgText = retrievedMessage.ReadUTF(); return(msgText); } else { return(msgText); } }
private MQMessage getMessage(MQQueue queue) { MQGetMessageOptions gmo = new MQGetMessageOptions(); gmo.Options = MQC.MQGMO_FAIL_IF_QUIESCING | MQC.MQGMO_WAIT | MQC.MQGMO_SYNCPOINT; gmo.WaitInterval = 5000; MQMessage message = new MQMessage(); try { //wait for message queue.Get(message, gmo);; } catch (MQException ex) { message = null; if (ex.CompletionCode == MQC.MQCC_FAILED && ex.ReasonCode == MQC.MQRC_NO_MSG_AVAILABLE) { return(null); } else { throw ex; } } return(message); }
private MQMessage getMessagebyCorelationId(MQQueue queue, string correlationId) { MQGetMessageOptions gmo = new MQGetMessageOptions(); gmo.Options = MQC.MQGMO_FAIL_IF_QUIESCING | MQC.MQGMO_WAIT | MQC.MQGMO_SYNCPOINT | MQC.MQMO_MATCH_CORREL_ID; ; gmo.WaitInterval = 5000; MQMessage message = new MQMessage(); message.CorrelationId = System.Text.ASCIIEncoding.ASCII.GetBytes(correlationId); MQQueueManager manager = _mqManagerReviewQueue; try { //wait for message queue.Get(message, gmo);; manager.Commit(); } catch (MQException ex) { message = null; if (ex.CompletionCode == MQC.MQCC_FAILED && ex.ReasonCode == MQC.MQRC_NO_MSG_AVAILABLE) { return(null); } else { throw ex; } } return(message); }
public string ReadMsg() { String strReturn = ""; try { queue = queueManager.AccessQueue(QueueName, MQC.MQOO_INPUT_AS_Q_DEF + MQC.MQOO_FAIL_IF_QUIESCING); queueMessage = new MQMessage(); queueMessage.Format = MQC.MQFMT_STRING; queueGetMessageOptions = new MQGetMessageOptions(); queue.Get(queueMessage, queueGetMessageOptions); strReturn = queueMessage.ReadString(queueMessage.MessageLength); } catch (MQException MQexp) { strReturn = "Exception : " + MQexp.Message; } catch (Exception exp) { strReturn = "Exception: " + exp.Message; } return(strReturn); }
private void GetMessage() { try { using (MQQueueManager queueManager = new MQQueueManager(qMgr, connectionParams)) { var openOptions = MQC.MQOO_INPUT_AS_Q_DEF + MQC.MQOO_OUTPUT + MQC.MQOO_FAIL_IF_QUIESCING + MQC.MQOO_INQUIRE; MQQueue queue = queueManager.AccessQueue(queueName, openOptions); MQGetMessageOptions getOptions = new MQGetMessageOptions { WaitInterval = 5, Options = MQC.MQGMO_WAIT }; MQMessage msg = new MQMessage { Format = MQC.MQFMT_STRING }; queue.Get(msg, getOptions); queue.Close(); } } catch (Exception ex) { if (!isLogger) { logger.Error($"Unable to maintain queue: {queueName}: {ex.Message}"); } } }
private string GetMessage(MQGetMessageOptions getMessageOptions) { // transactions: add wait // transactions: add rollback wait check try { // if transaction exists, add transaction complete handler var message = new MQMessage(); _queue.Get(message, getMessageOptions); var result = message.ReadString(message.MessageLength); message.ClearMessage(); return(result); } catch (MQException mqe) { // queue empty if (mqe.ReasonCode == 2033) { return(null); } // wrap exception throw; } }
private bool SetBrowseCursorAtMessageId(MQQueue queue, byte[] msgId) { var getMsgOpts = new MQGetMessageOptions() { Options = MQC.MQGMO_FAIL_IF_QUIESCING | MQC.MQGMO_BROWSE_FIRST }; try { getMsgOpts.MatchOptions = MQC.MQMO_MATCH_MSG_ID; var msg = new MQMessage { MessageId = msgId }; queue.Get(msg, getMsgOpts); return(true); } catch (MQException ex) { if (ex.ReasonCode != MQC.MQRC_NO_MSG_AVAILABLE) { throw; } return(false); } }
string getMessage(string queueName) { try { queue = qMgr.AccessQueue(queueName, MQC.MQOO_OUTPUT | MQC.MQOO_INPUT_SHARED | MQC.MQOO_INQUIRE); } catch (MQException e) { return("打开队列失败:" + e.Message); } try { MQMessage message = new MQMessage(); queue.Get(message); string s = message.ReadString(message.MessageLength); return(s); } catch (MQException mqe) { // return ("获取异常终止:" + mqe.Message); } finally { try { qMgr.Disconnect(); } catch (MQException e) { } } return(""); }
private void testLoop(MQQueue inQ) { bool flag = true; MQGetMessageOptions gmo = new MQGetMessageOptions(); gmo.Options |= MQC.MQGMO_WAIT | MQC.MQGMO_FAIL_IF_QUIESCING; gmo.WaitInterval = 2500; // 2.5 seconds wait time or use MQC.MQEI_UNLIMITED to wait forever MQMessage msg = null; while (flag) { try { msg = new MQMessage(); inQ.Get(msg, gmo); System.Console.Out.WriteLine("Message Data: " + msg.ReadString(msg.MessageLength)); } catch (MQException mqex) { System.Console.Out.WriteLine("MQTest62 CC=" + mqex.CompletionCode + " : RC=" + mqex.ReasonCode); if (mqex.Reason == MQC.MQRC_NO_MSG_AVAILABLE) { // no meesage - life is good - loop again } else { flag = false; // severe error - time to exit } } catch (System.IO.IOException ioex) { System.Console.Out.WriteLine("MQTest62 ioex=" + ioex); } } }
public List <string> GetAllMessagesFromQueue(string queueName) { List <string> listOfMessages = new List <string>(); queue = queueManager.AccessQueue(queueName, MQC.MQOO_INPUT_AS_Q_DEF + MQC.MQOO_FAIL_IF_QUIESCING); while (true) { try { queueMessage = new MQMessage(); queueMessage.Format = MQC.MQFMT_STRING; queue.Get(queueMessage); string message = queueMessage.ReadString(queueMessage.MessageLength); queue.Close(); listOfMessages.Add(message); } catch (MQException mqexp) { break; log.LogInformation(string.Concat("MQQueue::Get ended with ", mqexp.Message)); } } return(listOfMessages); }
public void ReadMQMessage() { try { // get message options MQGetMessageOptions mqGMO = new MQGetMessageOptions(); mqGMO.Options = MQC.MQGMO_FAIL_IF_QUIESCING + MQC.MQGMO_NO_WAIT + MQC.MQGMO_BROWSE_NEXT; // browse with no wait mqGMO.MatchOptions = MQC.MQMO_NONE; queueManager = new MQQueueManager(queueManagerName, mqProperties); //queIn = queueManager.AccessQueue(queueName, MQC.MQOO_FAIL_IF_QUIESCING); queIn = queueManager.AccessQueue(queueName, MQC.MQOO_INPUT_AS_Q_DEF + MQC.MQOO_FAIL_IF_QUIESCING); while (true) { try { queueManager = new MQQueueManager(queueManagerName, mqProperties); queIn = queueManager.AccessQueue(queueName, MQC.MQOO_INPUT_AS_Q_DEF + MQC.MQOO_FAIL_IF_QUIESCING); while (true) { mqMessage = new MQMessage(); //queIn.Get(mqMessage, mqGMO); queIn.Get(mqMessage); string message = mqMessage.ReadString(mqMessage.MessageLength); mqMessage.ClearMessage(); bool isFileSaved = SaveFile(message); } } catch (MQException mqe) { if (queIn != null && queIn.IsOpen) { queIn.Close(); } if (queueManager != null && queueManager.IsConnected) { queueManager.Disconnect(); } if (mqe.ReasonCode == 2033) { Console.WriteLine("No Message"); Thread.Sleep(10000); } else { Console.WriteLine(mqe.ReasonCode + " : " + mqe.Reason); } } } } catch (MQException mqe) { Console.WriteLine("Conneciton Error: " + mqe.ReasonCode + " : " + mqe.Reason); } //Thread.Sleep(10000); }
protected MQMessage[] Send(int command, PCFParameter[] parameters) { if (parameters == null) { throw new Exception("Must specify parameters!"); } MQQueue queue = this.qMgr.AccessQueue("SYSTEM.DEFAULT.MODEL.QUEUE", 0x2021); MQMessage message = new MQMessage(); message.ReplyToQueueName = queue.Name; message.MessageType = 1; message.Feedback = 0; message.Format = "MQADMIN "; message.Report = 0; MQCFH.Write(message, command, parameters.Length); for (int i = 0; i < parameters.Length; i++) { parameters[i].Write(message); } MQQueue queue2 = this.qMgr.AccessQueue(this.qMgr.CommandInputQueueName, 0x2030); MQPutMessageOptions pmo = new MQPutMessageOptions(); pmo.Options = 0x40; queue2.Put(message, pmo); MQGetMessageOptions gmo = new MQGetMessageOptions(); gmo.Options = 0x2001; gmo.WaitInterval = this.waitInterval; gmo.MatchOptions = 2; ArrayList list = new ArrayList(); MQMessage message2 = null; int compCode = 0; int reason = 0; int num4 = 1; do { message2 = new MQMessage(); message2.CorrelationId = message.MessageId; queue.Get(message2, gmo); message2.SkipBytes(20); num4 = message2.ReadInt4(); compCode = message2.ReadInt4(); reason = message2.ReadInt4(); message2.Seek(0); if (compCode != 0) { throw new PCFException(compCode, reason); } list.Add(message2); }while (num4 == 0); queue2.Close(); queue.Close(); return((MQMessage[])list.ToArray(typeof(MQMessage))); }
public void getFileFromMQ() { if (!String.IsNullOrEmpty(RecvQueue) || !String.IsNullOrEmpty(RecvFolder)) { try { IList FileList = new ArrayList(); string fileName = ""; //初始化环境变量 MQEnvironment.Hostname = HostName; //服务器的IP MQEnvironment.Channel = SvrCHL; //服务器连接通道 MQEnvironment.Port = HostPort; Environment.SetEnvironmentVariable("MQCCSID", CCSID); string qManager = MQName; //队列管理器 string QLName = RecvQueue; //接收队列 MQQueueManager queueManager = new MQQueueManager(qManager); int OutOpenOptions = MQC.MQOO_INPUT_AS_Q_DEF | MQC.MQOO_INQUIRE; queueGetMessageOptions = new MQGetMessageOptions(); queueGetMessageOptions.Options = MQC.MQGMO_FAIL_IF_QUIESCING | MQC.MQGMO_WAIT; queueGetMessageOptions.MatchOptions = MQC.MQMO_MATCH_CORREL_ID; queueGetMessageOptions.WaitInterval = 500; queueMessage = new MQMessage(); queueMessage.Format = MQC.MQFMT_STRING; queue = queueManager.AccessQueue(QLName, OutOpenOptions); while (queue.CurrentDepth > 0) { queue.Get(queueMessage, queueGetMessageOptions); fileName = saveRecvFile(queueMessage, RecvFolder); //if (!FileList.Contains(fileName)) //{ // FileList.Add(fileName); //} Console.Write("[" + DateTime.Now.ToString("MM-dd HH:mm:ss") + "]提取成功:" + fileName + "\r\n"); } } catch (MQException MQExp) { Console.Write("[" + DateTime.Now.ToString("MM-dd HH:mm:ss") + "]提取失败:" + MQExp.Message + "\r\n"); } finally { if (queue != null) { queue.Close(); } } } else { Console.Write("[" + DateTime.Now.ToString("MM-dd HH:mm:ss") + "]提取失败:无RecvQueue配置\r\n"); } }
private void PutAndGetMqSeries() { Hashtable connectionProperties = init(connectionType); // Create a connection to the queue manager using the connection // properties just defined MQQueueManager qMgr = new MQQueueManager(qManager, connectionProperties); // Set up the options on the queue we want to open int openOptions = MQC.MQOO_INPUT_AS_Q_DEF | MQC.MQOO_OUTPUT; // Now specify the queue that we want to open,and the open options MQQueue system_default_local_queue = qMgr.AccessQueue("SYSTEM.DEFAULT.LOCAL.QUEUE", openOptions); // Define an IBM MQ message, writing some text in UTF format MQMessage hello_world = new MQMessage(); hello_world.WriteUTF("Hello World!"); // Specify the message options MQPutMessageOptions pmo = new MQPutMessageOptions(); // accept the defaults, // same as MQPMO_DEFAULT // Put the message on the queue system_default_local_queue.Put(hello_world, pmo); // Get the message back again // First define an IBM MQ message buffer to receive the message MQMessage retrievedMessage = new MQMessage(); retrievedMessage.MessageId = hello_world.MessageId; // Set the get message options MQGetMessageOptions gmo = new MQGetMessageOptions(); //accept the defaults //same as MQGMO_DEFAULT // Get the message off the queue system_default_local_queue.Get(retrievedMessage, gmo); // Prove we have the message by displaying the UTF message text String msgText = retrievedMessage.ReadUTF(); Console.WriteLine("The message is: {0}", msgText); // Close the queue system_default_local_queue.Close(); // Disconnect from the queue manager qMgr.Disconnect(); }
public override void Receive(Action <Message> onMessageReceived, bool processAsync, int maximumWaitMilliseconds = 0) { try { var inbound = new MQMessage { Format = MQC.MQFMT_STRING }; if (maximumWaitMilliseconds > 0) { var options = new MQGetMessageOptions { WaitInterval = maximumWaitMilliseconds, Options = MQC.MQGMO_WAIT }; _queue.Get(inbound, options); } else { _queue.Get(inbound); } var messageJson = inbound.ReadString(inbound.MessageLength); var message = Message.FromJson(messageJson); if (processAsync) { Task.Factory.StartNew(() => onMessageReceived(message)); } else { onMessageReceived(message); } _queueManager.Commit(); } catch (MQException mqEx) { if (mqEx.Reason != MQC.MQRC_NO_MSG_AVAILABLE) { throw; } } }
/// <summary> /// Get a message from an MQ Queue using a message id /// </summary> /// <param name="correlationId">correlation id</param> /// <returns>Response message</returns> public string GetMQResponseMessage(string correlationId) { string location = "NBK.EAI.Adapters.MQAdapter.GetMQResponseMessage"; MQMessage rsMsg = new MQMessage(); rsMsg.MessageId = NBK.Common.Foundations.Utilities. ConversionUtilities.HexToBin(correlationId); MQGetMessageOptions gmo = new MQGetMessageOptions(); gmo.Options = MQC.MQGMO_WAIT; gmo.MatchOptions = MQC.MQMO_MATCH_MSG_ID; gmo.WaitInterval = pollingTimeout; try { mqGetQueue.Get(rsMsg, gmo); NBK.Common.Foundations.Logging.LoggerHelper.Information (location, "Read MQ Message with CorrelationId: " + NBK.Common.Foundations.Utilities.ConversionUtilities.BinToHex(rsMsg.CorrelationId)); return(rsMsg.ReadString(rsMsg.DataLength)); } catch (MQException mqe) { // Close Reponse Queue if still opened if (mqGetQueue.OpenStatus) { mqGetQueue.Close(); } // Close Queue manager if still opened if (mqQueueManager.OpenStatus) { mqQueueManager.Close(); } NBK.Common.Foundations.Logging.LoggerHelper.Information (location, "Error Reading Message with Correlation ID: " + correlationId); // Check if it a timeout exception if (MQAdapterErrorReasons.GetMQFailureReasonErrorCode(mqe.Reason) == "MQRC_NO_MSG_AVAILABLE") { throw new MQAdapterTimeoutException("Message with correlation Id " + correlationId + " Timed out"); } // MQ Exception throw new MQAdapterException("Error Code: " + MQAdapterErrorReasons.GetMQFailureReasonErrorCode(mqe.Reason)); } }
public bool MessageWaiting(string queueName, int timeOutms) { bool hasMessage = false; MQQueue queue = null; MQMessage message = new MQMessage(); try { // MQOO_BROWSE option means the message is NOT removed from the queue when it is read // We just want to know if there is at least one message in the queue so the listener // can take the appropriate action queue = _queueManager.AccessQueue(queueName, MQC.MQOO_BROWSE + MQC.MQOO_FAIL_IF_QUIESCING); MQGetMessageOptions opt = new MQGetMessageOptions(); if (opt != null) { opt.Options = IBM.WMQ.MQC.MQGMO_BROWSE_FIRST; message.CorrelationId = IBM.WMQ.MQC.MQMI_NONE; message.MessageId = IBM.WMQ.MQC.MQMI_NONE; if (timeOutms > 0) { opt.Options += IBM.WMQ.MQC.MQGMO_WAIT; opt.WaitInterval = timeOutms; } } queue.Get(message, opt); hasMessage = true; } catch (MQException mqe) { if (mqe.ReasonCode == 2033) { hasMessage = false; } else { //eventLog1.WriteEntry("MQException caught: {0} - {1}", mqe.ReasonCode, mqe.Message); } } catch (Exception ex) { //eventLog1.WriteEntry(ex); } finally { if (queue != null) { queue.Close(); } } return(hasMessage); }
public string mtGetMessage(int timeOut = 0) { string msgString = string.Empty; //bool isContinue = true; try { fQueue = fMQueueManager.AccessQueue(fQueueOUT, MQC.MQOO_INPUT_AS_Q_DEF + IBM.WMQ.MQC.MQOO_FAIL_IF_QUIESCING); //, fQueueManagerName, System.Guid.NewGuid().ToString(), fUser); fMQMsg = new MQMessage(); fMQMsg.Format = MQC.MQFMT_STRING; fGetMsgOptions = new MQGetMessageOptions(); fGetMsgOptions.WaitInterval = timeOut | fTimeOut; fGetMsgOptions.MatchOptions = MQC.MQMO_MATCH_CORREL_ID; fMQMsg.MessageId = fCorrelationId; try { fGetMsgOptions.MatchOptions = MQC.MQMO_MATCH_CORREL_ID; fGetMsgOptions.Options = MQC.MQGMO_WAIT + MQC.MQGMO_ACCEPT_TRUNCATED_MSG; //fMQMsg.MessageId = MQC.MQMI_NONE; fMQMsg.CorrelationId = fCorrelationId; fQueue.Get(fMQMsg, fGetMsgOptions); msgString = fMQMsg.ReadString(fMQMsg.MessageLength); } catch (MQException ex) { if (ex.Message.Contains("MQRC_NO_MSG_AVAILABLE")) { throw new Exception($"Time Out Broker: { ex.ReasonCode.ToString()} Descripción: {ex.Message}"); } throw new Exception($"No message available. MQ Error Code: { ex.ReasonCode.ToString()} Descripción: {ex.Message}"); } } catch { throw; } finally { fQueue?.Close(); } return(msgString); }
/// <summary> /// Helper method to read a message from an MQ Series queue /// </summary> /// /// <param name="queueManagerName">The name of the MQ Series queue manager</param> /// <param name="queueName">The name of the MQ Series queue to read from</param> /// <param name="waitDelay">The time to wait for the message to be read from the queue</param> /// <param name="context">The BizUnit context object which holds state and is passed between test steps</param> /// <param name="msgID">[out] the MQ Series message ID</param> /// <returns>String containing the data from the MQ series message</returns> static public string ReadMessage(string queueManagerName, string queueName, int waitDelay, Context context, out byte[] msgID) { MQQueueManager queueManager = null; MQQueue receiveQueue = null; string message = null; try { context.LogInfo("Opening queue manager: \"{0}\"", queueManagerName); queueManager = new MQQueueManager(queueManagerName); context.LogInfo("Opening queue: \"{0}\"", queueName); receiveQueue = queueManager.AccessQueue(queueName, MQC.MQOO_INPUT_AS_Q_DEF + MQC.MQOO_FAIL_IF_QUIESCING); MQMessage mqMsg = new MQMessage(); MQGetMessageOptions mqMsgOpts = new MQGetMessageOptions(); mqMsgOpts.WaitInterval = waitDelay * 1000; mqMsgOpts.Options = MQC.MQGMO_WAIT; context.LogInfo("Reading message from queue '{0}'.", queueName); receiveQueue.InhibitGet = MQC.MQQA_GET_ALLOWED; receiveQueue.Get(mqMsg, mqMsgOpts); if (mqMsg.Format.CompareTo(MQC.MQFMT_STRING) == 0) { mqMsg.Seek(0); message = System.Text.UTF8Encoding.UTF8.GetString(mqMsg.ReadBytes(mqMsg.MessageLength)); msgID = mqMsg.MessageId; } else { throw new NotSupportedException(string.Format("Unsupported message format: '{0}' read from queue: {1}.", mqMsg.Format, queueName)); } } finally { if (receiveQueue != null) { receiveQueue.Close(); } if (queueManager != null) { queueManager.Close(); } } return(message); }
/// <summary> /// Export queue messages to text file /// </summary> private void exportQueueMessageToFile(string exportDirectoryPath) { using (var writer = new StreamWriter(Path.Combine(exportDirectoryPath, $"{MQQueue.Name}.txt"))) { BotConfig.Target.ForEach(queue => { var queueMessage = MQQueue.Get(); writer.WriteLine(queueMessage); writer.Flush(); }); } }
/// <summary> Connect, open queue, read a message, close queue and disconnect. /// /// </summary> /// <throws> MQException </throws> private void testReceive() { MQQueueManager qMgr = null; MQQueue queue = null; int openOptions = MQC.MQOO_INPUT_AS_Q_DEF + MQC.MQOO_FAIL_IF_QUIESCING; MQGetMessageOptions gmo = new MQGetMessageOptions(); MQMessage receiveMsg = null; try { qMgr = new MQQueueManager(qManager, qMgrProp); System.Console.Out.WriteLine("MQTest02 successfully connected to " + qManager); queue = qMgr.AccessQueue(outputQName, openOptions, null, null, null); // no alternate user id System.Console.Out.WriteLine("MQTest02 successfully opened " + outputQName); receiveMsg = new MQMessage(); queue.Get(receiveMsg, gmo); System.Console.Out.WriteLine("Message Data>>>" + receiveMsg.ReadString(receiveMsg.MessageLength)); } catch (MQException mqex) { System.Console.Out.WriteLine("MQTest02 cc=" + mqex.CompletionCode + " : rc=" + mqex.ReasonCode); } catch (System.IO.IOException ioex) { System.Console.Out.WriteLine("MQTest02 ioex=" + ioex); } finally { try { queue.Close(); System.Console.Out.WriteLine("MQTest02 closed: " + outputQName); } catch (MQException mqex) { System.Console.Out.WriteLine("MQTest02 cc=" + mqex.CompletionCode + " : rc=" + mqex.ReasonCode); } try { qMgr.Disconnect(); System.Console.Out.WriteLine("MQTest02 disconnected from " + qManager); } catch (MQException mqex) { System.Console.Out.WriteLine("MQTest02 cc=" + mqex.CompletionCode + " : rc=" + mqex.ReasonCode); } } }
/// <summary> /// Start the listen to the queue for incoming messages and /// notifiy the handlers as new messges arrive /// </summary> public void StartQueueListener() { //create a separate connection to the message queue queueListenerStarted = true; MQQueueManager listenermqm = (MQQueueManager)queueSession.AccessQueueManager(queueManager); MQQueue listenerqueue = (MQQueue)mqm.AccessQueue(QueueName, (int)MQ.MQOO_INPUT_AS_Q_DEF + (int)MQ.MQOO_OUTPUT, "", "", ""); listenerqueue.Open(); try { MQMessage message = (MQMessage)queueSession.AccessMessage(); MQGetMessageOptions messageOption = (MQGetMessageOptions)queueSession.AccessGetMessageOptions(); while (queueListenerStarted == true) { System.Threading.Thread.Sleep(sleepTime); if (handler.GetInvocationList().Length > 0) { try { //GET will raise an exception if no message is in the queue. //we want to keep listening despite of the exception, see exception block //for detail listenerqueue.Get(message, messageOption, System.Reflection.Missing.Value); SAF.MessageQueue.Message safMessage = new SAF.MessageQueue.Message(); safMessage.Label = message.MessageId; safMessage.Content = message.ReadString(message.MessageLength); //fire the event handler(safMessage, QueueName); } catch (System.Runtime.InteropServices.COMException ex) { //-2147467259 represents the error code for retrieving //message from an empty queue. do nothing if gotting this error code. if (ex.ErrorCode != -2147467259) { throw ex; } } } } } finally { //close the connetion listenerqueue.Close(); listenermqm.Disconnect(); } }
private void BrowseMessagesWorker(BlockingCollection <WsMessage> output, IBrowseFilter filter, CancellationToken ct, MQQueue queue, byte[] startingPointMessageId) { var browseOption = MQC.MQGMO_BROWSE_FIRST; if (startingPointMessageId != null) { if (SetBrowseCursorAtMessageId(queue, startingPointMessageId)) { browseOption = MQC.MQGMO_BROWSE_NEXT; } } var getMsgOpts = new MQGetMessageOptions() { Options = MQC.MQGMO_FAIL_IF_QUIESCING | browseOption }; try { while (!ct.IsCancellationRequested) { var msg = new MQMessage(); queue.Get(msg, getMsgOpts); var localMsg = new WsMessage(msg, this); if (filter == null || filter.IsMatch(localMsg)) { output.Add(localMsg, ct); } else { output.Add(null, ct); } getMsgOpts.Options = MQC.MQGMO_FAIL_IF_QUIESCING | MQC.MQGMO_BROWSE_NEXT; } } catch (OperationCanceledException) { } catch (MQException ex) { if (ex.ReasonCode != MQC.MQRC_NO_MSG_AVAILABLE) { throw; } } }
public void GetMessage(out string msg) { if (!isConnected) { Init(); } mqGetMsgOpts = new MQGetMessageOptions(); mqGetMsgOpts.Options = mqGetMsgOpts.Options + MQC.MQGMO_SYNCPOINT; //Get messages under sync point control(在同步点控制下获取消息) mqGetMsgOpts.Options = mqGetMsgOpts.Options + MQC.MQGMO_WAIT; // Wait if no messages on the Queue(如果在队列上没有消息则等待) mqGetMsgOpts.Options = mqGetMsgOpts.Options + MQC.MQGMO_FAIL_IF_QUIESCING; // Fail if Qeue Manager Quiescing(如果队列管理器停顿则失败) mqGetMsgOpts.WaitInterval = 1000; mqQueue = mqQMgr.AccessQueue(queuename, MQC.MQOO_INPUT_AS_Q_DEF | MQC.MQOO_OUTPUT); mqQueue.Get(mqMsg, mqGetMsgOpts); msg = mqMsg.ReadString(mqMsg.MessageLength); }
public MQMessage Get(string queueName) { MQQueue mqQueue = _mqQueueManager.AccessQueue(queueName, MQC.MQOO_INPUT_AS_Q_DEF + MQC.MQOO_FAIL_IF_QUIESCING); MQGetMessageOptions getMessageOptions = new MQGetMessageOptions(); getMessageOptions.Options += MQC.MQGMO_WAIT + MQC.MQGMO_SYNCPOINT; getMessageOptions.WaitInterval = 20000; //20 Seconds MQMessage mqMessage = new MQMessage(); mqQueue.Get(mqMessage, getMessageOptions); mqQueue.Close(); return(mqMessage); }
/// <summary> /// Read Message from Local Queue /// </summary> /// <param name="strqueueName">Queue Name</param> /// <returns>Text Message</returns> public MQMessageStatus ReadLocalQMsg(string strQueueName) { MQMessageStatus messageStatus = new MQMessageStatus(); string strReturn = string.Empty; ReceiveQueueName = strQueueName; try { queue = queueManager.AccessQueue(ReceiveQueueName, MQC.MQOO_INPUT_AS_Q_DEF + MQC.MQOO_FAIL_IF_QUIESCING); queueMessage = new MQMessage(); queueMessage.Format = MQC.MQFMT_STRING; queueGetMessageOptions = new MQGetMessageOptions(); queue.Get(queueMessage, queueGetMessageOptions); strReturn = queueMessage.ReadString(queueMessage.MessageLength); messageStatus.Message = strReturn; messageStatus.Status = true; } catch (MQException MQexp) { strReturn = "Exception: " + MQexp.Message; messageStatus.Message = strReturn; messageStatus.Status = false; } catch (Exception exp) { strReturn = "Exception: " + exp.Message; messageStatus.Message = strReturn; messageStatus.Status = false; } return messageStatus; }
public string ReadMsg() { String strReturn = ""; try { queue = queueManager.AccessQueue("MBK.SESION3.SALIDA", MQC.MQOO_INPUT_AS_Q_DEF + MQC.MQOO_FAIL_IF_QUIESCING); queueMessage = new MQMessage(); queueMessage.Format = MQC.MQFMT_STRING; queueGetMessageOptions = new MQGetMessageOptions(); queue.Get(queueMessage, queueGetMessageOptions); strReturn = queueMessage.ReadString(queueMessage.MessageLength); } catch (MQException MQexp) { strReturn = "Exception : " + MQexp.Message; } catch (Exception exp) { strReturn = "Exception: " + exp.Message; } return strReturn; }