public void LoopThruMessages() { 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 (running) { try { msg = new MQMessage(); inQ.Get(msg, gmo); System.Console.Out.WriteLine("Message Data: " + msg.ReadString(msg.MessageLength)); } catch (MQException mqex) { if (mqex.Reason == MQC.MQRC_NO_MSG_AVAILABLE) { // no meesage - life is good - loop again } else { running = false; // severe error - time to exit System.Console.Out.WriteLine("MQException CC=" + mqex.CompletionCode + " : RC=" + mqex.ReasonCode); } } catch (System.IO.IOException ioex) { System.Console.Out.WriteLine("ioex=" + ioex); } } try { if (inQ != null) { inQ.Close(); System.Console.Out.WriteLine("Closed queue"); } } catch (MQException mqex) { System.Console.Out.WriteLine("MQException CC=" + mqex.CompletionCode + " : RC=" + mqex.ReasonCode); } try { if (qMgr != null) { qMgr.Disconnect(); System.Console.Out.WriteLine("disconnected from queue manager"); } } catch (MQException mqex) { System.Console.Out.WriteLine("MQException CC=" + mqex.CompletionCode + " : RC=" + mqex.ReasonCode); } }
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)); } }
private void tmrSubscribe_Tick(object sender, EventArgs e) { MQMessage message = new MQMessage(); try { if (subscriber != null) { subscriber.Get(message); } if (message != null) { txMessage.Text += message.ReadString(message.MessageLength) + Environment.NewLine; } } catch (MQException mqex) { if (mqex.ReasonCode == IBM.WMQ.MQC.MQRC_NO_MSG_AVAILABLE) { txMessage.Text += Environment.NewLine + "No new messages."; } else { MessageBox.Show(mqex.Message + Environment.NewLine + mqex.StackTrace + Environment.NewLine + mqex.Source + Environment.NewLine + mqex.GetBaseException()); } } }
/// <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(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); }
public static string ReadAll(this MQMessage @this) { var message = @this.ReadString(@this.DataLength); @this.Seek(0); return(message); }
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); } } }
private void handleInputMessage(MQMessage message, OfacStatus status) { Request request = new Request(); request.Name = System.Text.ASCIIEncoding.ASCII.GetString(message.MessageId); request.MessageBody = message.ReadString(message.DataLength); WindowsIdentity windowsIdentity = WindowsIdentity.GetCurrent(); if (windowsIdentity != null) { request.CreateOper = windowsIdentity.Name; } RequestHeader header = new RequestHeader(); header.correlationId = System.Text.ASCIIEncoding.ASCII.GetString(message.CorrelationId); request.Header = header.ToString(); request.InterfaceId = _interfaceId; _dbUtils.addRequest(request); //queue the work with the thread pool lock (syncLock) { currentMessagesToBeProcessed++; } ThreadPool.QueueUserWorkItem(new WaitCallback(startTranslation), request); }
/// <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 void Put1InQueue_Ok() { string message = "Put1InQueue"; using (var broker = MQQueueManager.Connect(QueueManagerName, 0, Channel, ConnectionInfo)) { var outgoing = new MQMessage() { CharacterSet = MQC.CODESET_UTF, Encoding = MQC.MQENC_NORMAL }; outgoing.WriteString(message); var od = new MQObjectDescriptor { ObjectType = MQC.MQOT_Q, ObjectName = QueueName }; broker.Put1(od, outgoing, new MQPutMessageOptions()); using (var q = broker.AccessQueue(QueueName, MQC.MQOO_INPUT_AS_Q_DEF + MQC.MQOO_OUTPUT + MQC.MQOO_FAIL_IF_QUIESCING)) { var incoming = new MQMessage(); MQGetMessageOptions gmo = new MQGetMessageOptions(); gmo.WaitInterval = (int)TimeSpan.FromSeconds(30).TotalMilliseconds; //MQC.MQWI_UNLIMITED; gmo.Options |= MQC.MQGMO_WAIT; gmo.Options |= MQC.MQGMO_SYNCPOINT; q.Get(incoming, gmo); Assert.AreEqual(message, incoming.ReadString(incoming.DataLength)); } } }
public void TopicPublish_Ok() { var message = "PublishInTopic"; using (var broker = MQQueueManager.Connect(QueueManagerName, 0, Channel, ConnectionInfo)) using (var topic = new MQTopic(broker, TopicName, string.Empty, MQC.MQTOPIC_OPEN_AS_PUBLICATION, MQC.MQOO_OUTPUT + MQC.MQOO_FAIL_IF_QUIESCING)) { var outgoing = new MQMessage() { CharacterSet = MQC.CODESET_UTF, Encoding = MQC.MQENC_NORMAL }; outgoing.WriteString(message); topic.Publish(outgoing, new MQPutMessageOptions()); } using (var broker = MQQueueManager.Connect(QueueManagerName, 0, Channel, ConnectionInfo)) using (var q = broker.AccessQueue("QL.R4.EMPLEADOSANDREANI.SUBSCRIBER", MQC.MQOO_INPUT_AS_Q_DEF + MQC.MQOO_FAIL_IF_QUIESCING)) { var incoming = new MQMessage(); MQGetMessageOptions gmo = new MQGetMessageOptions(); gmo.WaitInterval = (int)TimeSpan.FromSeconds(30).TotalMilliseconds; //MQC.MQWI_UNLIMITED; gmo.Options |= MQC.MQGMO_WAIT; gmo.Options |= MQC.MQGMO_SYNCPOINT; q.Get(incoming, gmo); Assert.AreEqual(message, incoming.ReadString(incoming.DataLength)); } }
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 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; } }
/// <summary> /// 获取队列消息 /// </summary> private string GetQueueMsg(string business) { string message = string.Empty; try { string queueName = string.Format(IBMWMQConfig.TOPIC_TEMPLATE, business); using (var mqmgr = MQQueueManager.Connect(IBMWMQConfig.QUEUE_MGR_NAME, MQC.MQCO_NONE, IBMWMQConfig.CHANNEL, IBMWMQConfig.CONNECTION_INFO)) using (var q = mqmgr.AccessQueue(queueName, MQC.MQOO_INPUT_AS_Q_DEF + MQC.MQOO_OUTPUT + MQC.MQOO_FAIL_IF_QUIESCING)) { var incoming = new MQMessage(); MQGetMessageOptions gmo = new MQGetMessageOptions(); gmo.WaitInterval = 10 * 1000; //MQC.MQWI_UNLIMITED; gmo.Options |= MQC.MQGMO_WAIT; gmo.Options |= MQC.MQGMO_SYNCPOINT; q.Get(incoming, gmo); message = incoming.ReadString(incoming.DataLength); } } catch (MQException e) { message = e.Reason.ToString(); } return(message); }
/// <summary> /// Extracts the messages from an WMQ <see cref="MQMessage"/>. /// </summary> /// <param name="message">The WMQ message to extract from.</param> /// <returns>An array of handleable messages.</returns> protected IMessage[] Extract(MQMessage message) { // return this.messageSerializer.Deserialize((MemoryStream)message.ReadObject()); MemoryStream stream = new MemoryStream(StringToByteArray(message.ReadString(message.MessageLength))); return(this.messageSerializer.Deserialize(stream)); }
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); }
private string saveRecvFile(MQMessage queueMessage, string path) { string filename = queueMessage.ReadString(200).Trim(); string fullName = Path.Combine(path, filename); FileStream fsNF = new FileStream(fullName, FileMode.Create, FileAccess.Write); BinaryWriter bw = new BinaryWriter(fsNF); bw.Write(queueMessage.ReadBytes(queueMessage.MessageLength - 200)); bw.Close(); return(filename); }
/// <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)); } }
internal override sealed void Initialize(MQMessage message) { Type = message.ReadInt4(); StrucLength = message.ReadInt4(); Parameter = message.ReadInt4(); CodedCharSetId = message.ReadInt4(); _stringLength = message.ReadInt4(); _stringval = message.ReadString(_stringLength); // Encoding.ASCII.GetString(message.ReadBytes(_stringLength), 0, _stringLength); var padding = (4 - ((Cmqcfc.MqcfstStrucLengthFixed + _stringLength) % 4)) % 4; message.SkipBytes(padding); }
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> 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(); } }
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 string ReadLocalQMsg(string strQueueName, int queueId, string ChannelInfo, string queueMgrName) { string strReturn = ""; ReceiveQueueName = strQueueName; MQQueueManager queueMgr; MQQueue queue; if (queueId == 1) { queueMgr = queueManager1; } else { queueMgr = queueManager2; } try { if (queueMgr == null || queueMgr.IsConnected == false) { ConnectMQ(queueMgrName, ChannelInfo, queueId); } queue = queueMgr.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); if (queue.IsOpen == false) { } strReturn = queueMessage.ReadString(queueMessage.MessageLength); queue.Close(); } catch (MQException MQexp) { strReturn = "Exception: " + MQexp.Message; } catch (Exception exp) { strReturn = "Exception: " + exp.Message; } return(strReturn); }
/// <summary> /// Gets one message from a queue. /// </summary> /// <param name="QueueName">Name of queue.</param> /// <param name="TimeoutMilliseconds">Timeout, in milliseconds.</param> /// <returns>Message read, if received within the given time, null otherwise.</returns> public string GetOne(string QueueName, int TimeoutMilliseconds) { string Result = null; try { MQQueue Queue; lock (this.inputQueues) { if (!this.inputQueues.TryGetValue(QueueName, out Queue)) { Queue = this.manager.AccessQueue(QueueName, MQC.MQOO_INPUT_AS_Q_DEF | MQC.MQOO_FAIL_IF_QUIESCING); this.inputQueues[QueueName] = Queue; } } MQMessage Message = new MQMessage(); MQGetMessageOptions Options = new MQGetMessageOptions() { Options = MQC.MQGMO_FAIL_IF_QUIESCING | MQC.MQGMO_WAIT, WaitInterval = TimeoutMilliseconds }; Queue.Get(Message, Options); Result = Message.ReadString(Message.MessageLength); this.ReceiveText(Result); } catch (MQException ex) { if (ex.Reason == 2033) { return(null); } this.Exception(ex); ExceptionDispatchInfo.Capture(ex).Throw(); } catch (Exception ex) { this.Exception(ex); ExceptionDispatchInfo.Capture(ex).Throw(); } return(Result); }
internal override sealed void Initialize(MQMessage message) { Type = message.ReadInt4(); StrucLength = message.ReadInt4(); Parameter = message.ReadInt4(); CodedCharSetId = message.ReadInt4(); _count = message.ReadInt4(); _stringLength = message.ReadInt4(); _strings = new string[_count]; for (var i = 0; i < _count; i++) { _strings[i] = message.ReadString(_stringLength); // Encoding.ASCII.GetString(message.ReadBytes(_stringLength), 0, _stringLength); } var padding = (4 - (StrucLength % 4)) % 4; message.SkipBytes(padding); }
/// <summary> Retrieve all messages from a queue or until a 'QUIT' message is received.</summary> /// <param name="inQ"> /// </param> private void retrieveAll(MQQueue inQ) { bool flag = true; MQGetMessageOptions gmo = new MQGetMessageOptions(); gmo.Options |= MQC.MQGMO_NO_WAIT | MQC.MQGMO_FAIL_IF_QUIESCING; MQMessage msg = null; while (flag) { try { msg = new MQMessage(); inQ.Get(msg, gmo); if (msg.Feedback == MQC.MQFB_QUIT) { flag = false; logger("received quit message - exiting loop"); } else { logger("Message Data: " + msg.ReadString(msg.MessageLength)); } } catch (MQException mqex) { logger("CC=" + mqex.CompletionCode + " : RC=" + mqex.ReasonCode); if (mqex.Reason == MQC.MQRC_NO_MSG_AVAILABLE) { // no meesage - life is good flag = false; logger("no more meesages - exiting loop"); } else { flag = false; // severe error - time to exit } } catch (System.IO.IOException ioex) { logger("ioex=" + ioex); } } }
/// <summary> /// Initializes the storage - doesn't make use of the given message types. /// </summary> /// <param name="messageTypes"></param> public void Init(IList <Type> messageTypes) { InitializeQueueManager(); InitializeInputQueue(); MQGetMessageOptions getMessageOptions = new MQGetMessageOptions(); getMessageOptions.Options |= MQC.MQGMO_NO_WAIT + MQC.MQGMO_BROWSE_NEXT; /// Reads subscription messages from the message queue, but does not remove them /// Loop until there is a failure, expecting an exception to indicate no more messages bool isContinue = true; while (isContinue) { MQMessage message = new MQMessage(); try { queue.Get(message, getMessageOptions); string subscriber = message.ReplyToQueueName; string messageType = message.ReadString(message.MessageLength); this.entries.Add(new Entry(messageType, subscriber)); this.AddToLookup(subscriber, messageType, message.MessageId); } catch (MQException mqe) { // report reason, if any if (mqe.Reason == MQC.MQRC_NO_MSG_AVAILABLE) { isContinue = false; } else { // general report for other reasons throw new ApplicationException("MQQueue::Get ended with " + mqe.Message); } } } CloseWMQConnections(); }
internal static void MQGet(IConfiguration config) { SetupMQEnvironment(config); var qmgrName = config.GetValue <string>("QueueManagerName"); Console.WriteLine($"Trying to connect to Queue Manager {qmgrName}..."); var qmgr = new MQQueueManager(qmgrName); Console.WriteLine($"Connected to Queue Manager {qmgrName}."); var qName = "DEV.QUEUE.1"; Console.WriteLine($"Reading message from queue {qName}."); int mqQueueOpenOptions = MQC.MQOO_INPUT_AS_Q_DEF | MQC.MQOO_OUTPUT; var retrievedMessage = new MQMessage(); qmgr.AccessQueue(qName, mqQueueOpenOptions).Get(retrievedMessage); var messageContents = retrievedMessage.ReadString(retrievedMessage.MessageLength); Console.WriteLine($"Message read from queue {qName}."); }
public string GetMessageOffQueue() { string message = ""; try { MQMessage queueMessage = new MQMessage(); MQQueue queue = this.queueManager.AccessQueue(this.queueName, MQC.MQOO_INPUT_AS_Q_DEF + MQC.MQOO_FAIL_IF_QUIESCING); queueMessage.Format = MQC.MQFMT_STRING; queue.Get(queueMessage); message = queueMessage.ReadString(queueMessage.MessageLength); queue.Close(); } catch (MQException mqexp) { } return message; }
public string GetMessage() { string retVal = null; MQMessage mqMessage = new MQMessage(); mqMessage.Format = MQC.MQFMT_STRING; //mqMessage.CharacterSet = 437; //mqMessage.CharacterSet = 1208; _queue.Get(mqMessage); retVal = mqMessage.ReadString(mqMessage.MessageLength); //retVal = mqMessage.ReadString(mqMessage.DataLength); mqMessage.ClearMessage(); return retVal; }
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; }
private void GetMessages() { try { // mq properties Hashtable properties = new Hashtable(); properties.Add(MQC.TRANSPORT_PROPERTY, MQC.TRANSPORT_MQSERIES_MANAGED); properties.Add(MQC.HOST_NAME_PROPERTY, HostName); properties.Add(MQC.PORT_PROPERTY, Port); properties.Add(MQC.CHANNEL_PROPERTY, ChannelName); // create connection Console.Write("Connecting to queue manager.. "); MQQueueManager queueManager = new MQQueueManager(QueueManagerName, properties); Console.WriteLine("done"); // accessing queue Console.Write("Accessing queue " + QueueName + ".. "); MQQueue queue = queueManager.AccessQueue(QueueName, MQC.MQOO_INPUT_AS_Q_DEF + MQC.MQOO_FAIL_IF_QUIESCING); Console.WriteLine("done"); // creating a message options object MQGetMessageOptions mqGetMsgOpts = new MQGetMessageOptions(); mqGetMsgOpts.WaitInterval = MessageWaitTimeout; mqGetMsgOpts.Options = MQC.MQGMO_FAIL_IF_QUIESCING | MQC.MQGMO_WAIT; // getting messages continuously bool done = false; while (!done && !ShuttingDown) { try { // creating a message object MQMessage message = new MQMessage(); queue.Get(message, mqGetMsgOpts); string messageString = message.ReadString(message.MessageLength); handler.HandleMessage(messageString); message.ClearMessage(); } catch (MQException mqe) { if (mqe.ReasonCode != 2033) { Console.WriteLine("MQException caught: {0} - {1}", mqe.ReasonCode, mqe.Message); done = true; } } } // closing queue Console.Write("Closing queue.. "); queue.Close(); Console.WriteLine("done"); // disconnecting queue manager Console.Write("Disconnecting queue manager.. "); queueManager.Disconnect(); Console.WriteLine("done"); } catch (MQException mqe) { Console.WriteLine(""); Console.WriteLine("MQException caught: {0} - {1}", mqe.ReasonCode, mqe.Message); Console.WriteLine(mqe.StackTrace); } }
/// <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; }