Exemplo n.º 1
0
        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);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Handle OnStop event
        /// </summary>
        public override void OnStop()
        {
            Trace.TraceInformation("OnStop called.");

            // Close and disconnect queue manager resources
            if (_qmConnected)
            {
                try
                {
                    _mqRequestQueue.Close();
                    _mqReplyQueue.Close();
                    _mqConnection.Disconnect();
                }
                catch (Exception ex)
                {
                    Trace.TraceInformation("Failed to dispose MQ connection. " + ex.ToString());
                }
            }

            // Close SQL connection and dispose
            if (_dbConnected)
            {
                try
                {
                    _sqlConnection.Close();
                    _sqlConnection.Dispose();
                }
                catch (Exception ex)
                {
                    Trace.TraceInformation("Failed to dispose DB connection. " + ex.ToString());
                }
            }
            bConnInitialized = false;
        }
Exemplo n.º 3
0
        /// <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
            {
            }
        }
Exemplo n.º 4
0
        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);
        }
Exemplo n.º 5
0
 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}");
         }
     }
 }
        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}");
                }
            }
        }
Exemplo n.º 7
0
        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);
        }
Exemplo n.º 8
0
        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));
            }
        }
Exemplo n.º 9
0
        /// <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();
                }
            }
        }
Exemplo n.º 10
0
 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));
     }
 }
Exemplo n.º 11
0
        /// <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);
        }
Exemplo n.º 12
0
        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);
        }
Exemplo n.º 13
0
        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}");
                }
            }
        }
Exemplo n.º 14
0
        public void Dispose()
        {
            //timerGetMessagesFromQueue.Dispose();
            queueManager.Disconnect();
            queueManager.Close();

            queue.Close();
        }
Exemplo n.º 15
0
        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);
        }
Exemplo n.º 16
0
        public async Task <ExchangeMessage> SendMqStuff(ExchangeMessage xm)
        {
            await Task.Run(() => { });

            string messageXML = xm.payload;
            bool   sent       = false;
            int    tries      = 1;

            lock (sendLock)
            {
                do
                {
                    if (tries > maxRetry && maxRetry > 0)
                    {
                        xm.sent   = false;
                        xm.status = $"Exceeded Connection retries on MQ {queueName}";
                        return(xm);
                    }

                    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 {xm.uuid} to {queueName}");
                        }
                    }
                    catch (Exception ex)
                    {
                        if (!isLogger)
                        {
                            logger.Info("Unable to send message to : " + queueName + " : " + ex.Message);
                        }
                        tries++;
                    }
                    if (!sent)
                    {
                        logger.Trace($"===Message NOT Sent {xm.uuid} to  {queueName}");
                    }
                } while (!sent);
            }
            xm.sent   = true;
            xm.status = $"Sent to  MQ {queueName}";

            return(xm);
        }
Exemplo n.º 17
0
 public void OpenQueue_Ok()
 {
     using (var broker = MQQueueManager.Connect(QueueManagerName, 0, Channel, ConnectionInfo))
     {
         var q = new MQQueue(broker, QueueName, MQC.MQOO_INPUT_AS_Q_DEF);
         Assert.AreNotEqual(MQC.MQHC_UNUSABLE_HCONN, q.Handle);
         Assert.AreNotEqual(MQC.MQHC_DEF_HCONN, q.Handle);
         q.Close();
     }
 }
Exemplo n.º 18
0
        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");
            }
        }
Exemplo n.º 19
0
        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)));
        }
Exemplo n.º 20
0
        public void Put(string queueName, MQMessage message)
        {
            MQQueue             mqQueue           = _mqQueueManager.AccessQueue(queueName, MQC.MQOO_OUTPUT + MQC.MQOO_FAIL_IF_QUIESCING);
            MQPutMessageOptions putMessageOptions = new MQPutMessageOptions();

            putMessageOptions.Options += MQC.MQGMO_SYNCPOINT;

            mqQueue.Put(message, putMessageOptions);

            mqQueue.Close();
        }
Exemplo n.º 21
0
 /// <summary>
 /// close the connection to the message queue
 /// </summary>
 public void Close()
 {
     if (queue != null)
     {
         queue.Close();
     }
     if (mqm != null)
     {
         mqm.Disconnect();
     }
 }
Exemplo n.º 22
0
        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();
        }
Exemplo n.º 23
0
        /// <summary> Connect, open queue, read (browse) a message, close queue and disconnect. </summary>
        ///
        private void testReceive()
        {
            MQQueueManager qMgr        = null;
            MQQueue        inQ         = null;
            int            openOptions = MQC.MQOO_INPUT_AS_Q_DEF + MQC.MQOO_FAIL_IF_QUIESCING;

            try
            {
                qMgr = new MQQueueManager(qManager, qMgrProp);
                System.Console.Out.WriteLine("MQTest62 successfully connected to " + qManager);

                inQ = qMgr.AccessQueue(inputQName, openOptions);
                System.Console.Out.WriteLine("MQTest62 successfully opened " + inputQName);

                testLoop(inQ);
            }
            catch (MQException mqex)
            {
                System.Console.Out.WriteLine("MQTest62 cc=" + mqex.CompletionCode + " : rc=" + mqex.ReasonCode);
            }
            catch (System.IO.IOException ioex)
            {
                System.Console.Out.WriteLine("MQTest62 ioex=" + ioex);
            }
            finally
            {
                try
                {
                    if (inQ != null)
                    {
                        inQ.Close();
                    }
                    System.Console.Out.WriteLine("MQTest62 closed: " + inputQName);
                }
                catch (MQException mqex)
                {
                    System.Console.Out.WriteLine("MQTest62 cc=" + mqex.CompletionCode + " : rc=" + mqex.ReasonCode);
                }

                try
                {
                    if (qMgr != null)
                    {
                        qMgr.Disconnect();
                    }
                    System.Console.Out.WriteLine("MQTest62 disconnected from " + qManager);
                }
                catch (MQException mqex)
                {
                    System.Console.Out.WriteLine("MQTest62 cc=" + mqex.CompletionCode + " : rc=" + mqex.ReasonCode);
                }
            }
        }
Exemplo n.º 24
0
        /// <summary>
        /// Destroy the resource.
        /// </summary>
        /// <param name="disposing">disposing</param>
        protected void Dispose(bool disposing)
        {
            if (!disposed)
            {
                if (disposing)
                {
                }

                mQQueue.Close();
                disposed = true;
            }
        }
Exemplo n.º 25
0
        /// <summary> Connect, open queue, retrieve all messages, close queue and disconnect.</summary>
        /// <throws>  MQException </throws>
        private void handleIt()
        {
            MQQueueManager qMgr        = null;
            MQQueue        inQ         = null;
            int            openOptions = MQC.MQOO_INPUT_AS_Q_DEF + MQC.MQOO_FAIL_IF_QUIESCING;

            try
            {
                qMgr = new MQQueueManager(qManager, qMgrProp);
                logger("MQTest72 successfully connected to " + qManager);

                inQ = qMgr.AccessQueue(inputQName, openOptions);
                logger("MQTest72 successfully opened " + inputQName);

                retrieveAll(inQ);
            }
            catch (MQException mqex)
            {
                logger("MQTest72 CC=" + mqex.CompletionCode + " : RC=" + mqex.ReasonCode);
            }
            catch (System.IO.IOException ioex)
            {
                logger("MQTest72 ioex=" + ioex);
            }
            finally
            {
                try
                {
                    if (inQ != null)
                    {
                        inQ.Close();
                        logger("MQTest72 closed: " + inputQName);
                    }
                }
                catch (MQException mqex)
                {
                    logger("MQTest72 CC=" + mqex.CompletionCode + " : RC=" + mqex.ReasonCode);
                }

                try
                {
                    if (qMgr != null)
                    {
                        qMgr.Disconnect();
                        logger("MQTest72 disconnected from " + qManager);
                    }
                }
                catch (MQException mqex)
                {
                    logger("MQTest72 CC=" + mqex.CompletionCode + " : RC=" + mqex.ReasonCode);
                }
            }
        }
Exemplo n.º 26
0
        /// <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));
            }
        }
Exemplo n.º 27
0
        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);
        }
Exemplo n.º 28
0
        /// <summary>
        /// Puts a message in an MQ Queue using the user Id provided
        /// <param name="message">The message to be put in the queue</param>
        /// <returns>Response message</returns>
        /// </summary>
        public string PushMQRequestMessage(string message)
        {
            string location = "NBK.EAI.Adapters.MQAdapter.PushMQRequestMessage";

            try
            {
                MQMessage requestMessage = new MQMessage();

                requestMessage.Persistence = 0;

                requestMessage.ReplyToQueueName        = mqResponseQueueName;
                requestMessage.ReplyToQueueManagerName = mqQueueManagerName;

                requestMessage.Format       = MQC.MQFMT_STRING;
                requestMessage.CharacterSet = characterSet;
                requestMessage.MessageType  = MQC.MQMT_REQUEST;
                requestMessage.MessageId    = NBK.Common.Foundations.Utilities.ConversionUtilities
                                              .HexToBin(GenerateMQMsgId());


                MQPutMessageOptions pmo = new MQPutMessageOptions();
                pmo.Options = MQC.MQPMO_SET_IDENTITY_CONTEXT;

                requestMessage.WriteString(message);

                mqPutQueue.Put(requestMessage, pmo);

                string _msgId = NBK.Common.Foundations.Utilities.
                                ConversionUtilities.BinToHex(requestMessage.MessageId);

                NBK.Common.Foundations.Logging.LoggerHelper.Information
                    (location, "Sent MQ Meesage with MessageId : "
                    + _msgId);

                return(_msgId);
            }
            catch (MQException mqe)
            {
                // Close request Queue if still opened
                if (mqPutQueue.OpenStatus)
                {
                    mqPutQueue.Close();
                }
                // Close Queue manager if still opened
                if (mqQueueManager.OpenStatus)
                {
                    mqQueueManager.Close();
                }

                throw new MQAdapterException("Error Code: " +
                                             MQAdapterErrorReasons.GetMQFailureReasonErrorCode(mqe.Reason));
            }
        }
Exemplo n.º 29
0
        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);
        }
Exemplo n.º 30
0
 //斷線
 public void disconnect()
 {
     try
     {
         sendqueue.Close();
         sendqueue        = null;
         connectionStatus = "NotReady";                                                                      //設定連線狀態為NotReady
     }
     catch (Exception e) {
         e.ToString();
         listener.systemMessage("sender disconnect fail");                                                   //sender 斷線失敗
     }
 }