Example #1
0
        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;
            }
        }
Example #2
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);
        }
        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);
        }
Example #4
0
 private void SendMqMessage(String queueName)
 {
     using (var qm = createManager())
         using (var q = qm.AccessQueue(queueName, MQC.MQOO_OUTPUT + MQC.MQOO_FAIL_IF_QUIESCING))
         {
             q.Put(message);
             message.ClearMessage();
             q.Close();
         }
 }
Example #5
0
        public void SendFile(string filePath, string DesQueueName)
        {
            using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
            {
                byte[] buff     = new byte[FILE_SPLIT_LEN];
                var    fileName = System.IO.Path.GetFileName(filePath);
                var    queue    = queueManager.AccessQueue(DesQueueName,
                                                           MQC.MQOO_OUTPUT + MQC.MQOO_FAIL_IF_QUIESCING);

                MQPutMessageOptions op = new MQPutMessageOptions();
                op.Options = op.Options + MQC.MQPMO_LOGICAL_ORDER;
                op.Options = op.Options + MQC.MQPMRF_GROUP_ID;

                int       count = -1;
                MQMessage msg   = new MQMessage();
                msg.MessageFlags  = MQC.MQMF_MSG_IN_GROUP;
                msg.MQMD.CorrelId = System.Text.Encoding.Default.GetBytes("000|21");

                bool isContainFileName = true;
                while (true)
                {
                    if (isContainFileName)
                    {
                        byte[]      headBytes  = System.Text.Encoding.Default.GetBytes(fileName + "|");
                        List <byte> totalBytes = new List <byte>();
                        totalBytes.AddRange(headBytes);
                        byte[] b         = new byte[FILE_SPLIT_LEN - headBytes.Length];
                        var    tempCount = fs.Read(b, 0, FILE_SPLIT_LEN - headBytes.Length);
                        count = tempCount + headBytes.Length;
                        totalBytes.AddRange(b);
                        msg.Write(totalBytes.ToArray(), 0, count);
                        isContainFileName = false;
                    }
                    else
                    {
                        count = fs.Read(buff, 0, FILE_SPLIT_LEN);
                        msg.Write(buff, 0, count);
                    }
                    if (count == 0)
                    {
                        break;
                    }
                    if (count < FILE_SPLIT_LEN)
                    {
                        msg.MessageFlags = MQC.MQMF_LAST_MSG_IN_GROUP;
                    }
                    queue.Put(msg, op);
                    msg.ClearMessage();
                }
                queue.Close();
                queueManager.Disconnect();
                LogMsg(string.Format("【SEND】\t【FILE】\t【{0}】\t【{1}】", DesQueueName, fileName));
            }
        }
Example #6
0
        public override int Write(MQMessage message)
        {
            var num = 0;

            message.ClearMessage();
            message.MessageType   = 1;
            message.Expiry        = 0x3e8;
            message.Format        = MQC.MQFMT_ADMIN;
            message.Feedback      = 0;
            _mqCfh.ParameterCount = _list.Count;
            num += _mqCfh.Write(message);
            num += _list.Sum(parameter => parameter.Write(message));
            return(num);
        }
Example #7
0
        public void CreateDurableSubscriber(string topicName, IMessageListener listener)
        {
            this.topciName = topicName;
            var topic = queueManager.AccessTopic(topicName, null,
                                                 MQC.MQSO_CREATE | MQC.MQSO_FAIL_IF_QUIESCING | MQC.MQSO_RESUME,
                                                 null, DurableSubscriptionName);

            var message = new MQMessage();

            Task.Factory.StartNew(() =>
            {
                while (!listener.IsStop)
                {
                    try
                    {
                        message = new MQMessage();
                        topic.Get(message, new MQGetMessageOptions()
                        {
                            WaitInterval = -1,
                            Options      = MQC.MQGMO_WAIT | MQC.MQGMO_NO_SYNCPOINT
                        });
                        var qm = ConvertBack(message);
                        listener.OnMessage(qm);
                        message.ClearMessage();
                    }
                    catch (MQException mqe)
                    {
                        if (mqe.ReasonCode == 2033)
                        {
                            Thread.Sleep(500);
                        }
                        else
                        {
                            LogMsg("获取MQ(Topic)消息异常:" + host + mqe.ToString());
                            Thread.Sleep(5000);
                            Reconnect(ref queueManager, ref topic, topicName);
                        }
                    }
                    catch (Exception e)
                    {
                        LogMsg("获取MQ(Topic)消息异常:" + host + e.ToString());
                        Thread.Sleep(5000);
                        Reconnect(ref queueManager, ref topic, topicName);
                    }
                }
                topic.Close();
            });
        }
Example #8
0
        public override int Write(MQMessage message)
        {
            int num = 0;

            message.ClearMessage();
            message.MessageType       = 1;
            message.Expiry            = 0x3e8;
            message.Format            = "MQADMIN ";
            message.Feedback          = 0;
            this.mqCFH.parameterCount = this.list.Count;
            num += this.mqCFH.Write(message);
            for (int i = 0; i < this.list.Count; i++)
            {
                PCFParameter parameter = (PCFParameter)this.list[i];
                num += parameter.Write(message);
            }
            return(num);
        }
Example #9
0
        public string GetMessage(string queueName)
        {
            string  messageText = string.Empty;
            MQQueue queue       = null;

            try
            {
                //eventLog1.WriteEntry("Accessing queue {0}", queueName);
                queue = _queueManager.AccessQueue(queueName, MQC.MQOO_INPUT_AS_Q_DEF + MQC.MQOO_FAIL_IF_QUIESCING);

                //eventLog1.WriteEntry("Reading messages from queue");
                MQMessage message = new MQMessage();
                queue.Get(message);

                //eventLog1.WriteEntry("Message found in queue, reading content");
                messageText = message.ReadString(message.MessageLength);
                message.ClearMessage();
            }
            catch (MQException mqe)
            {
                if (mqe.ReasonCode == 2033)
                {
                    //eventLog1.WriteEntry("No messages in queue");
                    return("");
                }
                else
                {
                    //eventLog1.WriteEntry("MQException caught: {0} - {1}", mqe.ReasonCode, mqe.Message);
                    throw new Exception("Error reading message queue");
                }
            }
            finally
            {
                if (queue != null)
                {
                    //eventLog1.WriteEntry("Closing queue");
                    queue.Close();
                }
            }
            return(messageText);
        }
Example #10
0
        internal void PerformMsgProcessingAfterPut(ref MQMessage message, byte[] tempMsgBuffer, int bodyCcsid, int encoding, string format)
        {
            uint method = 0x80;

            this.TrEntry(method, new object[] { message, tempMsgBuffer, bodyCcsid, encoding, format });
            try
            {
                message.ClearMessage();
                message.CharacterSet = bodyCcsid;
                message.Format       = format;
                message.Encoding     = encoding;
                if (tempMsgBuffer.Length != 0)
                {
                    message.Write(tempMsgBuffer);
                }
            }
            finally
            {
                base.TrExit(method);
            }
        }
Example #11
0
        public void CreateSubscriber(string name, IMessageListener listener)
        {
            MQQueue queue = queueManager.AccessQueue(name, MQC.MQOO_INPUT_AS_Q_DEF + MQC.MQOO_FAIL_IF_QUIESCING);

            Task.Factory.StartNew(() =>
            {
                while (!listener.IsStop)
                {
                    try
                    {
                        var message = new MQMessage();
                        queue.Get(message);
                        var qm = ConvertBack(message);
                        listener.OnMessage(qm);
                        message.ClearMessage();
                    }
                    catch (MQException mqe)
                    {
                        if (mqe.ReasonCode == 2033)
                        {
                            LogMsg(string.Format("获取MQ【BUFF】消息异常,异常代码【2033】,队列:{0},MQ地址:{1},异常信息:{2}", name, host, mqe.ToString()));
                            Thread.Sleep(500);
                        }
                        else
                        {
                            LogMsg(string.Format("获取MQ【BUFF】消息异常,异常代码【2033】,队列:{0},MQ地址:{1},异常信息:{2}", name, host, mqe.ToString()));
                            Thread.Sleep(5000);
                            Reconnect(ref queueManager, ref queue, name);
                        }
                    }
                    catch (Exception e)
                    {
                        LogMsg(string.Format("获取MQ【BUFF】消息异常,异常代码【2033】,队列:{0},MQ地址:{1},异常信息:{2}", name, host, e.Message.ToString()));
                        Thread.Sleep(5000);
                        Reconnect(ref queueManager, ref queue, name);
                    }
                }
                queue.Close();
            });
        }
Example #12
0
        public string GetMessage(string queueName)
        {
            string  messageText = string.Empty;
            MQQueue queue       = null;

            try
            {
                queue = _queueManager.AccessQueue(queueName, MQC.MQOO_INPUT_AS_Q_DEF + MQC.MQOO_FAIL_IF_QUIESCING);

                MQMessage message = new MQMessage();
                queue.Get(message);

                messageText = message.ReadString(message.MessageLength);
                message.ClearMessage();
            }
            catch (MQException mqe)
            {
                if (mqe.ReasonCode == 2033)
                {
                    return("");
                }
                else
                {
                    _log.ErrorFormat("MQHelper::GetMessage() MQException caught: {0} - {1}", mqe.ReasonCode, mqe.Message);
                }
            }
            finally
            {
                if (queue != null)
                {
                    _log.Debug("MQHelper::GetMessage() - Closing queue...");
                    queue.Close();
                }
            }
            return(messageText);
        }
Example #13
0
        /// <summary>
        /// Permite obtener un mensaje directo de la Cola
        /// Creado por: Ludwing Ottoniel Cano fuentes - 05/03/2019
        ///</summary>
        public string GetMessages()
        {
            string response = string.Empty;

            try
            {
                // mq properties
                properties = new Hashtable
                {
                    { MQC.TRANSPORT_PROPERTY, MQC.TRANSPORT_MQSERIES_MANAGED },
                    { MQC.HOST_NAME_PROPERTY, Properties.Resources.hostName },
                    { MQC.PORT_PROPERTY, Properties.Resources.port },
                    { MQC.CHANNEL_PROPERTY, Properties.Resources.channelName }
                };

                // create connection
                queueManager = new MQQueueManager(Properties.Resources.queueManagerName, properties);
                // accessing queue
                queue = queueManager.AccessQueue(Properties.Resources.queueName_Get, MQC.MQOO_INPUT_AS_Q_DEF + MQC.MQOO_FAIL_IF_QUIESCING);

                // getting messages continuously
                //for (int i = 1; i <=  Int32.Parse(Properties.Resources.numberOfMsgs); i++)
                //{
                // creating a message object
                message = new MQMessage();


                try
                {
                    queue.Get(message);
                    response = message.ReadString(message.MessageLength);
                    //Console.WriteLine("Message " + i + " got = " + message.ReadString(message.MessageLength));
                    message.ClearMessage();
                }
                catch (MQException mqe)
                {
                    if (mqe.ReasonCode == 2033)
                    {
                        Console.WriteLine("No message available");
                    }
                    else
                    {
                        Console.WriteLine("MQException caught: {0} - {1}", mqe.ReasonCode, mqe.Message);
                    }

                    response = "-1";
                }
                //}

                // closing queue
                queue.Close();
                // disconnecting queue manager
                queueManager.Disconnect();
            }

            catch (MQException mqe)
            {
                Console.WriteLine("");
                Console.WriteLine("MQException caught: {0} - {1}", mqe.ReasonCode, mqe.Message);
                Console.WriteLine(mqe.StackTrace);
                response = "-2";
            }

            return(response);
        }
Example #14
0
        public void CreateFileSubscriber(string name, string path, IMessageListener listener)
        {
            MQQueue queue = queueManager.AccessQueue(name, MQC.MQOO_INPUT_AS_Q_DEF + MQC.MQOO_FAIL_IF_QUIESCING);

            MQGetMessageOptions gmo = new MQGetMessageOptions();

            gmo.Options      = MQC.MQGMO_FAIL_IF_QUIESCING;
            gmo.Options      = gmo.Options + MQC.MQGMO_SYNCPOINT;
            gmo.Options      = gmo.Options + MQC.MQGMO_WAIT;
            gmo.WaitInterval = 3000;
            gmo.Options      = gmo.Options + MQC.MQGMO_ALL_MSGS_AVAILABLE;
            gmo.Options      = gmo.Options + MQC.MQGMO_LOGICAL_ORDER;
            gmo.MatchOptions = MQC.MQMO_MATCH_GROUP_ID;
            gmo.Version      = MQC.MQMD_VERSION_2;
            Task.Factory.StartNew(() =>
            {
                while (!listener.IsStop)
                {
                    try
                    {
                        var message          = new MQMessage();
                        message.MQMD.Version = MQC.MQMD_VERSION_2;
                        List <byte> total    = new List <byte>();

                        while (true)
                        {
                            queue.Get(message, gmo);

                            int msgLength = message.MessageLength;
                            byte[] buff   = new byte[msgLength];
                            message.ReadFully(ref buff);
                            total.AddRange(buff);
                            if (gmo.GroupStatus == MQC.MQGS_LAST_MSG_IN_GROUP)
                            {
                                message.ClearMessage();
                                queueManager.Commit();
                                break;
                            }
                        }

                        byte[] findStr    = System.Text.Encoding.Default.GetBytes("|");
                        byte[] totalBytes = total.ToArray();
                        var ret           = Search(totalBytes, 0, findStr);
                        if (ret != -1)
                        {
                            byte[] fileNameHead = new byte[ret];
                            byte[] content      = new byte[totalBytes.Length - ret - 1];
                            Array.Copy(totalBytes, 0, fileNameHead, 0, ret);

                            Array.Copy(totalBytes, ret + 1, content, 0, totalBytes.Length - ret - 1);

                            var fileName     = System.Text.Encoding.Default.GetString(fileNameHead);
                            var generateFile = System.IO.Path.Combine(path, fileName);

                            using (FileStream fSteam = new FileStream(generateFile, FileMode.Create))
                            {
                                fSteam.Write(content, 0, content.Length);
                                fSteam.Flush();
                                fSteam.Close();
                                if (listener != null)
                                {
                                    QueueMessage qm = new QueueMessage();
                                    qm.Type         = MessageType.File;
                                    qm.Data         = fileName;
                                    listener.OnMessage(qm);
                                }
                            }
                        }
                    }
                    catch (MQException mqe)
                    {
                        if (mqe.ReasonCode == 2033)
                        {
                            LogMsg(string.Format("获取MQ【FILE】消息异常,异常代码【2033】,队列:{0},MQ地址:{1},异常信息:{2}", name, host, mqe.ToString()));
                            Thread.Sleep(500);
                        }
                        else
                        {
                            LogMsg(string.Format("获取MQ【FILE】消息异常,队列:{0},MQ地址:{1},异常信息:{2}", name, host, mqe.ToString()));
                            Thread.Sleep(5000);
                            Reconnect(ref queueManager, ref queue, name);
                        }
                    }
                    catch (Exception e)
                    {
                        LogMsg(string.Format("获取MQ【FILE】消息异常,队列:{0},MQ地址:{1},异常信息:{2}", name, host, e.Message.ToString()));
                        Thread.Sleep(5000);
                        Reconnect(ref queueManager, ref queue, name);
                    }
                }
                LogMsg(string.Format("队列{0},监控文件线程退出..."));
                queue.Close();
            });
        }
Example #15
0
        public void putFileToMQ()
        {
            if (!String.IsNullOrEmpty(SendQueue) || !String.IsNullOrEmpty(SendFolder))
            {
                try
                {
                    string _MessagePath    = SendFolder;
                    string _MessagePathBak = SendFolder + "\\Bak\\";

                    IList  sendList = scanDirectory(_MessagePath);
                    string fileName = null;
                    string filePath = null;
                    //初始化环境变量
                    MQEnvironment.Hostname = HostName; //服务器的IP
                    MQEnvironment.Channel  = SvrCHL;   //服务器连接通道
                    MQEnvironment.Port     = HostPort;
                    Environment.SetEnvironmentVariable("MQCCSID", CCSID);
                    string qManager = MQName;    //队列管理器
                    string QLName   = SendQueue; //接收队列

                    //***************注意需要在MQ的MCA添加MUSR_MQADMIN***************//
                    //构造队列管理器
                    //int openOptions = MQC.MQOO_INPUT_AS_Q_DEF | MQC.MQOO_OUTPUT;
                    int openOptions = MQC.MQOO_OUTPUT | MQC.MQOO_FAIL_IF_QUIESCING;
                    foreach (String _fileName in sendList)
                    {
                        if (sendList.Count <= 0)
                        {
                            //无新报文跳出循环
                            break;
                        }
                        MQQueueManager queueManager = new MQQueueManager(qManager);
                        queue                  = queueManager.AccessQueue(QLName, openOptions);
                        queueMessage           = new MQMessage();
                        queuePutMessageOptions = new MQPutMessageOptions();

                        fileName = _fileName;
                        filePath = Path.Combine(_MessagePath, fileName);

                        //放入消息
                        queueMessage.ClearMessage();
                        queueMessage.Format = MQC.MQFMT_STRING;
                        queueMessage.WriteString(handleFileName(fileName));
                        queueMessage.Write(readFileByte(filePath));
                        queue.Put(queueMessage, queuePutMessageOptions);
                        //发送成功后移动文件
                        moveFile(filePath, _MessagePathBak, 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") + "]放入失败:无SendQueue配置\r\n");
            }
        }
Example #16
0
        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);
            }
        }
Example #17
0
        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;
        }
Example #18
0
        public void Get(MQMessage message, MQGetMessageOptions gmo, int maxMsgSize)
        {
            uint method = 0x7a;

            this.TrEntry(method, new object[] { message, gmo, maxMsgSize });
            int dataLength = 0;

            byte[] buffer   = null;
            int    options  = -1;
            bool   flag     = true;
            int    compCode = 0;
            int    reason   = 0;

            try
            {
                if (message == null)
                {
                    base.throwNewMQException(2, 0x7ea);
                }
                if (gmo == null)
                {
                    base.throwNewMQException(2, 0x88a);
                }
                if (maxMsgSize < 0)
                {
                    base.throwNewMQException(2, 0x7d5);
                }
                options = gmo.Options;
                if ((gmo.Options & 0x1006) == 0)
                {
                    base.TrText(method, "Setting explicit NO_SYNCPOINT");
                    gmo.Options |= 4;
                }
                if (base.qMgr.CommandLevel >= 700)
                {
                    flag = (options & 0x1e000000) == 0;
                    if (((options & 0x8000000) != 0) || flag)
                    {
                        gmo.Options &= -134217729;
                        gmo.Options |= 0x2000000;
                    }
                }
                if ((Transaction.Current != null) && !base.qMgr.IsXAEnabled)
                {
                    base.qMgr.IsXAEnabled = true;
                }
                int characterSet = message.CharacterSet;
                if (!base.qMgr.IsHconnValid)
                {
                    base.throwNewMQException(2, 0x7e2);
                }
                message.ClearMessage();
                if (maxMsgSize == 0)
                {
                    int         num7       = gmo.Options;
                    MQBase.MQMD targetMqmd = new MQBase.MQMD();
                    this.CopyMQMD(message.md.StructMQMD, ref targetMqmd);
                    maxMsgSize   = defaultMaxMsgSize;
                    buffer       = new byte[maxMsgSize];
                    gmo.Options &= -65;
                    if ((num7 & 0x4000) == 0x4000)
                    {
                        MQLPIGetOpts lpiGetOpts = new MQLPIGetOpts();
                        lpiGetOpts.SetOptions(lpiGetOpts.GetOptions() | MQLPIGetOpts.lpiGETOPT_FULL_MESSAGE);
                        ((NmqiSP)base.qMgr.nmqiConnector).zstMQGET(base.qMgr.hConn, base.objectHandle.HOBJ, ref message.md, ref gmo, maxMsgSize, buffer, out dataLength, lpiGetOpts, out compCode, out reason);
                    }
                    else
                    {
                        base.qMgr.nmqiConnector.MQGET(base.qMgr.hConn, base.hObj, message.md, gmo, maxMsgSize, buffer, out dataLength, out compCode, out reason);
                    }
                    if (0x7da == reason)
                    {
                        maxMsgSize = dataLength;
                        buffer     = new byte[maxMsgSize];
                        base.qMgr.nmqiConnector.MQGET(base.qMgr.hConn, base.hObj, message.md, gmo, maxMsgSize, buffer, out dataLength, out compCode, out reason);
                    }
                    while ((compCode != 0) && (0x820 == reason))
                    {
                        gmo.Options = num7;
                        MQBase.MQMD structMQMD = message.md.StructMQMD;
                        this.CopyMQMD(targetMqmd, ref structMQMD);
                        message.md.StructMQMD = structMQMD;
                        maxMsgSize            = dataLength;
                        buffer       = new byte[maxMsgSize];
                        gmo.Options &= -65;
                        base.qMgr.nmqiConnector.MQGET(base.qMgr.hConn, base.hObj, message.md, gmo, maxMsgSize, buffer, out dataLength, out compCode, out reason);
                    }
                    if ((0x848 == reason) || (0x88e == reason))
                    {
                        string objectId       = "Server Binding convert message";
                        byte[] outString      = null;
                        int    outLength      = 0;
                        uint   bytesConverted = 0;
                        if (CommonServices.ConvertString(objectId, message.md.StructMQMD.CodedCharacterSetId, characterSet, buffer, dataLength, out outString, ref outLength, 0, out bytesConverted) == 0)
                        {
                            buffer     = outString;
                            maxMsgSize = outLength;
                            dataLength = outLength;
                            compCode   = 0;
                            reason     = 0;
                        }
                    }
                }
                else
                {
                    buffer = new byte[maxMsgSize];
                    base.qMgr.nmqiConnector.MQGET(base.qMgr.hConn, base.hObj, message.md, gmo, maxMsgSize, buffer, out dataLength, out compCode, out reason);
                }
                byte[] b = buffer;
                if (compCode == 0)
                {
                    bool flag2 = false;
                    if (base.qMgr.CommandLevel >= 700)
                    {
                        if (flag)
                        {
                            if (this.propControl != 3)
                            {
                                flag2 = true;
                            }
                        }
                        else if ((options & 0x8000000) != 0)
                        {
                            flag2 = true;
                        }
                        if (flag2 && (dataLength > 0x24))
                        {
                            b          = this.PerformMsgProcessingAfterGet(ref message, buffer, (dataLength > buffer.Length) ? buffer.Length : dataLength);
                            dataLength = b.Length;
                        }
                    }
                }
                message.totalMessageLength = dataLength;
                if (dataLength > 0)
                {
                    message.Write(b, 0, (dataLength < maxMsgSize) ? dataLength : maxMsgSize);
                    message.Seek(0);
                }
                if (compCode != 0)
                {
                    base.qMgr.CheckHConnHealth(reason);
                    base.throwNewMQException(compCode, reason);
                }
            }
            catch (MQException exception)
            {
                compCode = exception.CompCode;
                reason   = exception.Reason;
                throw exception;
            }
            catch (Exception exception2)
            {
                base.TrException(method, exception2);
                compCode = 2;
                reason   = 0x893;
                throw exception2;
            }
            finally
            {
                base.unsafe_compCode = compCode;
                base.unsafe_reason   = reason;
                gmo.Options          = options;
                base.TrExit(method);
            }
        }
Example #19
0
        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);
            }
        }
Example #20
0
        public void Get(MQMessage message, MQGetMessageOptions gmo, int maxMsgSize, int spigmo)
        {
            uint method = 550;

            this.TrEntry(method, new object[] { message, gmo, maxMsgSize, spigmo });
            int  num2       = 0;
            bool flag       = false;
            int  dataLength = 0;

            byte[] buffer   = null;
            int    compCode = 0;
            int    reason   = 0;

            try
            {
                NmqiSP nmqiConnector = (NmqiSP)base.qMgr.nmqiConnector;
                if (message == null)
                {
                    base.throwNewMQException(2, 0x7ea);
                }
                if (gmo == null)
                {
                    base.throwNewMQException(2, 0x88a);
                }
                if (maxMsgSize < 0)
                {
                    base.throwNewMQException(2, 0x7d5);
                }
                MQSPIGetOpts spigo        = new MQSPIGetOpts(spigmo);
                int          options      = gmo.Options;
                int          characterSet = message.CharacterSet;
                int          num7         = gmo.Options;
                if ((num7 & 0x8000000) != 0)
                {
                    gmo.Options &= -134217729;
                    gmo.Options |= 0x2000000;
                }
                else if ((((num7 & 0x10000000) == 0) && ((num7 & 0x2000000) == 0)) && ((num7 & 0x4000000) == 0))
                {
                    gmo.Options |= 0x2000000;
                }
                message.ClearMessage();
                num2 = gmo.Options;
                flag = true;
                if ((gmo.Options & 0x1006) == 0)
                {
                    gmo.Options |= 4;
                }
                if (maxMsgSize == 0)
                {
                    int         num8       = gmo.Options;
                    MQBase.MQMD targetMqmd = new MQBase.MQMD();
                    base.CopyMQMD(message.md.StructMQMD, ref targetMqmd);
                    maxMsgSize   = MQDestination.defaultMaxMsgSize;
                    buffer       = new byte[maxMsgSize];
                    gmo.Options &= -65;
                    nmqiConnector.SPIGet(base.qMgr.hConn, base.objectHandle.HOBJ, ref message.md, ref gmo, ref spigo, maxMsgSize, buffer, out dataLength, out compCode, out reason);
                    while ((1 == compCode) && (0x820 == reason))
                    {
                        gmo.Options = num8;
                        MQBase.MQMD structMQMD = message.md.StructMQMD;
                        base.CopyMQMD(targetMqmd, ref structMQMD);
                        message.md.StructMQMD = structMQMD;
                        maxMsgSize            = dataLength;
                        buffer       = new byte[maxMsgSize];
                        gmo.Options &= -65;
                        nmqiConnector.SPIGet(base.qMgr.hConn, base.objectHandle.HOBJ, ref message.md, ref gmo, ref spigo, maxMsgSize, buffer, out dataLength, out compCode, out reason);
                    }
                    gmo.Options = num8;
                }
                else
                {
                    buffer = new byte[maxMsgSize];
                    nmqiConnector.SPIGet(base.qMgr.hConn, base.objectHandle.HOBJ, ref message.md, ref gmo, ref spigo, maxMsgSize, buffer, out dataLength, out compCode, out reason);
                }
                if (compCode != 2)
                {
                    this.spiQueueEmpty   = spigo.QueueEmpty != 0;
                    message.spiInherited = spigo.Inherited != 0;
                    message.spiQTime     = spigo.QTime;
                }
                else
                {
                    this.spiQueueEmpty   = false;
                    message.spiInherited = false;
                    message.spiQTime     = 0L;
                }
                byte[] b = null;
                if (compCode != 2)
                {
                    if ((options & 0x2000000) == 0)
                    {
                        b = base.PerformMsgProcessingAfterGet(ref message, buffer, (dataLength > buffer.Length) ? buffer.Length : dataLength);
                    }
                    else
                    {
                        b = buffer;
                    }
                    dataLength = b.Length;
                }
                gmo.Options = options;
                message.totalMessageLength = dataLength;
                if (dataLength > 0)
                {
                    message.Write(b, 0, (dataLength < maxMsgSize) ? dataLength : maxMsgSize);
                    message.Seek(0);
                }
                if (compCode != 0)
                {
                    base.qMgr.CheckHConnHealth(reason);
                    base.throwNewMQException(compCode, reason);
                }
            }
            catch (MQException exception)
            {
                compCode = exception.CompCode;
                reason   = exception.Reason;
                throw exception;
            }
            finally
            {
                if (flag)
                {
                    gmo.Options = num2;
                }
                base.unsafe_compCode = compCode;
                base.unsafe_reason   = reason;
                base.TrExit(method);
            }
        }