/// <summary> /// Write Message to Local Queue /// </summary> /// <param name="strInputMsg">Text Message</param> /// <param name="strqueueName">Queue Name</param> /// <returns></returns> public MQMessageStatus WriteLocalQMsg(string strInputMsg, string strQueueName) { MQMessageStatus messageStatus = new MQMessageStatus(); string strReturn = string.Empty; SendQueueName = strQueueName; try { queue = queueManager.AccessQueue(SendQueueName, MQC.MQOO_OUTPUT + MQC.MQOO_FAIL_IF_QUIESCING); message = strInputMsg; queueMessage = new MQMessage(); queueMessage.WriteString(message); queueMessage.Format = MQC.MQFMT_STRING; queuePutMessageOptions = new MQPutMessageOptions(); queue.Put(queueMessage, queuePutMessageOptions); strReturn = "Message sent to the queue successfully"; 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); }
internal void InitiateForRecovery() { uint method = 0x520; this.TrEntry(method); try { this.Qmgr = new MQQueueManager(this.qmgrName_, this.properties); log.WriteLog("QMgr Connection Details: ", this.connname_ + ";" + this.channel_); if ((this.Qmgr != null) && this.Qmgr.IsConnected) { this.Queue = this.Qmgr.AccessQueue("SYSTEM.DOTNET.XARECOVERY.QUEUE", this.openOpts); this.RunRecovery(); if (this.reenter) { this.RunRecovery(); this.reenter = false; } this.Qmgr.Disconnect(); } } catch (MQException exception) { base.TrException(method, exception); throw exception; } finally { base.TrExit(method); } }
public void MaintainQueueInline() { try { using (MQQueueManager queueManager = new MQQueueManager(qMgr, connectionParams)) { int 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 }; while (queue.CurrentDepth > this.maxMessages) { GetMessage(); } queue.Close(); } } catch (Exception ex) { if (!isLogger) { logger.Error($"Unable to maintain queue: {queueName}: {ex.Message}"); } } }
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> /// /// </summary> /// <param name="mqManager"></param> /// <param name="channel"></param> /// <param name="ipAddress"></param> /// <param name="putQueue"></param> /// <param name="getQueue"></param> /// <param name="timeout"></param> /// <param name="charSet"></param> /// <param name="port"></param> private void InitializeAdapter(string mqManager, string channel, string ipAddress, string putQueue, string getQueue, int timeout, int charSet, int port) { try { MQEnvironment.Hostname = ipAddress; MQEnvironment.Channel = channel; MQEnvironment.Port = port; mqQueueManagerName = mqManager; mqRequestQueueName = putQueue; mqResponseQueueName = getQueue; characterSet = charSet; pollingTimeout = timeout; mqQueueManager = new MQQueueManager(mqManager);//,channel, ipAddress); // Open Queue for Inquiry, Put Message in, and fail if Queue Manager is stopping mqPutQueue = mqQueueManager.AccessQueue(putQueue, MQC.MQOO_INQUIRE | MQC.MQOO_OUTPUT | MQC.MQOO_FAIL_IF_QUIESCING | MQC.MQOO_SET_IDENTITY_CONTEXT); mqGetQueue = mqQueueManager.AccessQueue(getQueue, MQC.MQOO_INPUT_AS_Q_DEF + MQC.MQOO_FAIL_IF_QUIESCING); } catch (MQException mqe) { NBK.Common.Foundations.Logging.LoggerHelper.Error(mqe); throw new MQAdapterException("Error Code: " + MQAdapterErrorReasons.GetMQFailureReasonErrorCode(mqe.Reason)); } }
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 void Reconnect(ref MQQueueManager queueManager, ref MQQueue queue, string queueName) { try { LogMsg(string.Format("开始重连MQ:{0},queueName:{1}", host, queueName)); try { if (queueManager != null) { queueManager.Disconnect(); } if (queue != null) { queue.Close(); } } catch (Exception e) { LogMsg("释放连接失败" + e); } queueManager = new MQQueueManager(uri, properties); queue = queueManager.AccessQueue(queueName, MQC.MQOO_INPUT_AS_Q_DEF + MQC.MQOO_FAIL_IF_QUIESCING); LogMsg(string.Format("重连MQ成功:{0},{1}", host, queueName)); if (ReconnectEvent != null) { ReconnectEvent(this.Tag, null); } } catch (Exception ex) { LogMsg(string.Format("重连MQ失败:host:{0},exp:{1}", host, ex)); } }
public void SendMQ(string xm) { string messageXML = xm; bool sent = false; int tries = 1; lock (sendLock) { try { using (MQQueueManager queueManager = new MQQueueManager(qMgr, connectionParams)) { var openOptions = MQC.MQOO_OUTPUT + MQC.MQOO_FAIL_IF_QUIESCING; MQQueue queue = queueManager.AccessQueue(queueName, openOptions); var message = new MQMessage { CharacterSet = 1208 // UTF-8 }; message.WriteString(messageXML); message.Format = MQC.MQFMT_STRING; MQPutMessageOptions putOptions = new MQPutMessageOptions(); queue.Put(message, putOptions); queue.Close(); sent = true; logger.Trace($"===Message Sent to {queueName}"); } } catch (Exception ex) { tries++; logger.Info(ex.Message); logger.Info("Error send MQ Message"); } if (!sent) { logger.Trace($"===Message NOT Sent to {queueName}"); } } }
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); } }
string sendMessage(string message, string queueName) { try { queue = qMgr.AccessQueue(queueName, MQC.MQOO_OUTPUT | MQC.MQOO_INPUT_SHARED | MQC.MQOO_INQUIRE); } catch (MQException e) { return("打开队列失败:" + e.Message); } var mqMsg = new MQMessage(); mqMsg.WriteString(message); var putOptions = new MQPutMessageOptions(); try { queue.Put(mqMsg, putOptions); return("消息放置完毕"); } catch (MQException mqe) { return("发送异常终止:" + mqe.Message); } finally { try { qMgr.Disconnect(); } catch (MQException e) { } } }
/// <summary> /// Instantiates the Queue Manager /// </summary> /// <param name="mqManager">The Queue Manager controlling the Request and Response Queues</param> public MQAdapter(string mqManager,string channel, string ipAddress, string putQueue,int timeout, int charSet, int port) { try { MQEnvironment.Hostname = ipAddress; MQEnvironment.Channel = channel; MQEnvironment.Port = WSMQ_DEFAULT_PORT; mqQueueManagerName = mqManager; mqRequestQueueName = putQueue; characterSet = charSet; pollingTimeout = timeout; // Connect to an MQ Manager, and share the connection handle with other threads mqQueueManager = new MQQueueManager(mqManager, channel, ipAddress); // Open Queue for Inquiry, Put Message in, and fail if Queue Manager is stopping mqPutQueue = mqQueueManager.AccessQueue(putQueue, MQC.MQOO_INQUIRE | MQC.MQOO_OUTPUT | MQC.MQOO_FAIL_IF_QUIESCING | MQC.MQOO_SET_IDENTITY_CONTEXT); } catch (MQException mqe) { throw new MQAdapterException("Error Code: " + MQAdapterErrorReasons.GetMQFailureReasonErrorCode(mqe.Reason)); } }
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 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); } }
private void listen(MQQueue queue, MQQueueManager manager, string queueName, HandleMessage handler, OfacStatus status) { while (!pendingStop) { while (!pendingStop && currentMessagesToBeProcessed >= maxNoOfMessagesToProcess) { Thread.Sleep(5000); } if (pendingStop) { return; } try { MQMessage message = getMessage(queue); if (message == null) { continue; } handler(message, status); manager.Commit(); } catch (MQException e) { LogUtil.log("Error when getting message from the " + queueName + " queue", e); manager.Backout(); Thread.Sleep(30000); throw; } } }
private bool sendMessage(string queueName, int requestId, MQQueue queue, MQQueueManager manager, string messageId, string message, string correlationId, Object lockObject) { lock (lockObject) { bool sentMessage = false; try { MQPutMessageOptions pmo = new MQPutMessageOptions(); pmo.Options = MQC.MQOO_INQUIRE | MQC.MQPMO_FAIL_IF_QUIESCING | MQC.MQPMO_SYNCPOINT; MQMessage mqMessage = new MQMessage(); mqMessage.Write(System.Text.ASCIIEncoding.ASCII.GetBytes(message)); mqMessage.Format = MQC.MQFMT_STRING; mqMessage.MessageId = System.Text.ASCIIEncoding.ASCII.GetBytes(messageId); mqMessage.CorrelationId = System.Text.ASCIIEncoding.ASCII.GetBytes(correlationId); queue.Put(mqMessage, pmo); manager.Commit(); sentMessage = true; LogUtil.logInfo("Sent message " + requestId + " to " + queueName); LogUtil.logInfo("End SendMesage:"); } catch (MQException e) { sentMessage = false; manager.Backout(); LogUtil.log("Error sending the message to " + queue.Name + " queue", e); } return(sentMessage); } }
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 bool PutMessage(string queueName, string message) { bool success = false; MQQueue queue = null; try { _log.DebugFormat("Accessing queue {0}", queueName); queue = _queueManager.AccessQueue(queueName, MQC.MQOO_OUTPUT + MQC.MQOO_FAIL_IF_QUIESCING); MQMessage queueMessage = new MQMessage(); queueMessage.CharacterSet = 1208; // force to UTF-8 for InService queueMessage.WriteString(message); queueMessage.Format = MQC.MQFMT_STRING; MQPutMessageOptions queuePutMessageOptions = new MQPutMessageOptions(); queue.Put(queueMessage, queuePutMessageOptions); success = true; } catch (MQException mqe) { success = false; _log.ErrorFormat("MQHelper::MQException caught: {0} - {1}", mqe.ReasonCode, mqe.Message); } finally { if (queue != null) { _log.Debug("MQHelper::PutMessage() - Closing queue..."); queue.Close(); } } return(success); }
/// <summary> /// Instantiates the Queue Manager /// </summary> /// <param name="mqManager">The Queue Manager controlling the Request and Response Queues</param> public MQAdapter(string mqManager, string channel, string ipAddress, string putQueue, string getQueue, int timeout, int charSet, int port) { try { MQEnvironment.Hostname = ipAddress; MQEnvironment.Channel = channel; MQEnvironment.Port = 1000; mqQueueManagerName = mqManager; mqRequestQueueName = putQueue; mqResponseQueueName = getQueue; characterSet = charSet; pollingTimeout = timeout; // Connect to an MQ Manager, and share the connection handle with other threads mqQueueManager = new MQQueueManager(mqManager, channel, ipAddress); // Open Queue for Inquiry, Put Message in, and fail if Queue Manager is stopping mqPutQueue = mqQueueManager.AccessQueue(putQueue, MQC.MQOO_INQUIRE | MQC.MQOO_OUTPUT | MQC.MQOO_FAIL_IF_QUIESCING | MQC.MQOO_SET_IDENTITY_CONTEXT); mqGetQueue = mqQueueManager.AccessQueue(getQueue, MQC.MQOO_INPUT_AS_Q_DEF + MQC.MQOO_FAIL_IF_QUIESCING); } catch (MQException mqe) { throw new MQAdapterException("Error Code: " + MQAdapterErrorReasons.GetMQFailureReasonErrorCode(mqe.Reason)); } }
public bool PutMessage(string queueName, string message) { bool success = false; MQQueue queue = null; try { //eventLog1.WriteEntry("Accessing queue {0}", queueName); queue = _queueManager.AccessQueue(queueName, MQC.MQOO_OUTPUT + MQC.MQOO_FAIL_IF_QUIESCING); MQMessage queueMessage = new MQMessage(); queueMessage.CharacterSet = 1208; // force to UTF-8 for InService queueMessage.WriteString(message); queueMessage.Format = MQC.MQFMT_STRING; MQPutMessageOptions queuePutMessageOptions = new MQPutMessageOptions(); queue.Put(queueMessage, queuePutMessageOptions); success = true; } catch (MQException mqe) { success = false; //eventLog1.WriteEntry("MQException caught: {0} - {1}", mqe.ReasonCode, mqe.Message); //throw new Exception(string.Format("Error writing to {0} queue", queueName)); } finally { if (queue != null) { //eventLog1.WriteEntry("Closing queue"); queue.Close(); } } return(success); }
/// <summary> /// Disconnect from MQ server and release all resources used. /// </summary> /// <returns>True if success.</returns> public bool Disconnect() { bool success = false; try { MyQueueMessage.ClearMessage(); MyQueueMessage = null; MyQueue.Close(); MyQueue = null; MyQueueManager.Disconnect(); MyQueueManager = null; success = true; GC.Collect(); } catch (MQException exp) { throw exp; } catch (Exception exp) { throw exp; } return(success); }
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 string WriteMsg(string strInputMsg) { string strReturn = ""; try { queue = queueManager.AccessQueue(QueueName, MQC.MQOO_OUTPUT + MQC.MQOO_FAIL_IF_QUIESCING); message = strInputMsg; queueMessage = new MQMessage(); queueMessage.WriteString(message); queueMessage.Format = MQC.MQFMT_STRING; queuePutMessageOptions = new MQPutMessageOptions(); queue.Put(queueMessage, queuePutMessageOptions); strReturn = "Message sent to the queue successfully"; } catch (MQException MQexp) { strReturn = "Exception: " + MQexp.Message; } catch (Exception exp) { strReturn = "Exception: " + exp.Message; } return(strReturn); }
/// <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); }
/// <summary> /// Puts a message object in the queue. /// </summary> /// <param name="queueName"></param> /// <param name="message"></param> private void PutRequestMessage(string connectionKeyName, MQMessage message) { MQQueue queue = null; try { if (!IsConnected) { Connect(connectionKeyName); } MQPutMessageOptions queuePutMessageOptions = new MQPutMessageOptions(); queue = _queueManager.AccessQueue(_activeConnection.RequestQueue, MQC.MQOO_OUTPUT + MQC.MQOO_FAIL_IF_QUIESCING); queue.Put(message, queuePutMessageOptions); } catch (MQException mqe) { _log.ErrorFormat("Exception in PutRequestMessage(). Message: {0}", mqe.Message); } finally { if (queue != null) { queue.Close(); } } }
/// <summary> /// Get Message count from Local Queue /// </summary> /// <param name="strqueueName">Queue Name</param> /// <returns>Text Message</returns> public MQMessageStatus CheckLocalQMsg(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); strReturn = Convert.ToString(queue.CurrentDepth); 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); }
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(""); }
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 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); }
/// <summary> /// 发送 /// </summary> /// <param name="queueName">队列名称</param> /// <param name="sendMessages">发送内容</param> public void SendMessages(string queueName, string sendMessages) { MQQueueManager sQMgr = null; //发送消息的队列管理器 MQQueue remoteQ = null; //发送消息的队列 try { // 创建一个连接到队列管理器 sQMgr = new MQQueueManager(queueManagerName, ConnProperties); //现在指定要打开的队列, //和打开选项… remoteQ = sQMgr.AccessQueue(queueName, MQC.MQOO_OUTPUT | MQC.MQOO_FAIL_IF_QUIESCING | MQC.MQOO_INQUIRE); // 定义一个简单的WebSphere MQ消息 MQMessage putMessage = new MQMessage(); putMessage.WriteString(sendMessages); // 指定消息选项… MQPutMessageOptions pmo = new MQPutMessageOptions(); // 放入消息到队列 remoteQ.Put(putMessage, pmo); remoteQ.Close(); //关闭队列 sQMgr.Disconnect(); //关闭队列管理器连接 } catch { } }
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); }
public string WriteMsg(string strInputMsg) { string strReturn = ""; try { queue = queueManager.AccessQueue(QueueName, MQC.MQOO_OUTPUT + MQC.MQOO_FAIL_IF_QUIESCING); message = strInputMsg; queueMessage = new MQMessage(); queueMessage.WriteBytes(strInputMsg); queueMessage.ReplyToQueueName = "MBK.SESION3.SALIDA"; queueMessage.OriginalLength = 120; queueMessage.ReplyToQueueManagerName = "IB9QMGR"; queueMessage.DataOffset = 328; queueMessage.Format = MQC.MQFMT_STRING; queuePutMessageOptions = new MQPutMessageOptions(); try { queue.Put(queueMessage, queuePutMessageOptions); strReturn = "Message sent to the queue successfully"; } catch (MQException e) { strReturn = "Exception: " + "error al colocar(put) el mensaje en la cola " + QueueName + ". " + e.ToString(); } } catch (MQException MQexp) { strReturn = "Exception: " + MQexp.Message; } catch (Exception exp) { strReturn = "Exception: " + exp.Message; } return strReturn; }
/// <summary> /// Get Message count from Local Queue /// </summary> /// <param name="strqueueName">Queue Name</param> /// <returns>Text Message</returns> public MQMessageStatus CheckLocalQMsg(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); strReturn = Convert.ToString(queue.CurrentDepth); 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; }
public void Open() { try { // mq properties Hashtable properties; properties = new Hashtable(); properties.Add(MQC.TRANSPORT_PROPERTY, MQC.TRANSPORT_MQSERIES_MANAGED); properties.Add(MQC.HOST_NAME_PROPERTY, this.HostName); properties.Add(MQC.PORT_PROPERTY, this.Port); properties.Add(MQC.CHANNEL_PROPERTY, this.ChannelName); // create connection _queueManager = new MQQueueManager(this.QueueManager, properties); // accessing queue //_queue = _queueManager.AccessQueue(this.QueueName, MQC.MQOO_OUTPUT + MQC.MQOO_FAIL_IF_QUIESCING); _queue = _queueManager.AccessQueue(this.QueueName, MQC.MQOO_OUTPUT + MQC.MQOO_INPUT_AS_Q_DEF + MQC.MQOO_FAIL_IF_QUIESCING); } catch (MQException mqe) { throw new Exception(string.Format("MQ Error: {0}", mqe.Message)); } catch (Exception ex) { throw new Exception(string.Format("Error: {0}", ex.Message)); } }
/// <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; }
/// <summary> /// Write Message to Local Queue /// </summary> /// <param name="strInputMsg">Text Message</param> /// <param name="strqueueName">Queue Name</param> /// <returns></returns> public MQMessageStatus WriteLocalQMsg(string strInputMsg, string strQueueName) { MQMessageStatus messageStatus = new MQMessageStatus(); string strReturn = string.Empty; SendQueueName = strQueueName; try { queue = queueManager.AccessQueue(SendQueueName, MQC.MQOO_OUTPUT + MQC.MQOO_FAIL_IF_QUIESCING); message = strInputMsg; queueMessage = new MQMessage(); queueMessage.WriteString(message); queueMessage.Format = MQC.MQFMT_STRING; queuePutMessageOptions = new MQPutMessageOptions(); queue.Put(queueMessage, queuePutMessageOptions); strReturn = "Message sent to the queue successfully"; 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; }