示例#1
0
        private void GetMessage(string queueName, Func <Message, Task> messageHandler, CancellationToken ct = default(CancellationToken))
        {
            var mqGetMsgOpts = new MQGetMessageOptions
            {
                WaitInterval = 1000,
                Options      = MQC.MQGMO_WAIT | MQC.MQGMO_SYNCPOINT | MQC.MQGMO_FAIL_IF_QUIESCING
            };

            MQQueue mqQueue;

            try
            {
                mqQueue = _mqQmgr.AccessQueue(queueName, MQC.MQOO_OUTPUT | MQC.MQOO_INPUT_SHARED | MQC.MQOO_INQUIRE);
            }
            catch (MQException mqe)
            {
                Logger.LogMessage("MQQueueManager::AccessQueue ended with " + mqe);
                return;
            }

            try
            {
                bool error = false;
                while (!error && !ct.IsCancellationRequested)
                {
                    ct.ThrowIfCancellationRequested();

                    try
                    {
                        var mqMsg = new MQMessage();
                        mqQueue.Get(mqMsg, mqGetMsgOpts);

                        var msgBody   = mqMsg.ReadString(mqMsg.MessageLength);
                        var messageId = mqMsg.GetStringProperty("MessageId");
                        var msg       = new Message {
                            Body = msgBody, MessageId = messageId
                        };

                        messageHandler(msg);

                        _mqQmgr.Commit();
                    }
                    catch (MQException mqEx1)
                    {
                        if (mqEx1.Reason == 2033) // No message available
                        {
                            error = true;
                        }
                    }
                }
            }
            catch (MQException mqe)
            {
                // report reason, if any
                if (mqe.Reason == MQC.MQRC_NO_MSG_AVAILABLE)
                {
                    // special report for normal end
                    Logger.LogMessage("Wait timeout happened");
                }
                else
                {
                    // general report for other reasons
                    Logger.LogMessage("MQQueue::Get ended with " + mqe);

                    // treat truncated message as a failure for this sample
                    if (mqe.Reason == MQC.MQRC_TRUNCATED_MSG_FAILED)
                    {
                        // TODO Handle connection error here
                    }
                }
            }

            try
            {
                //Close the Queue
                mqQueue.Close();
            }
            catch (MQException mqe)
            {
                Logger.LogMessage("Completion code " + mqe.CompletionCode + "Reason code " + mqe.ReasonCode);
            }
        }
示例#2
0
        internal void RunRecovery()
        {
            uint method = 0x521;

            this.TrEntry(method);
            int  num2 = 0;
            bool flag = false;

            while ((num2 = this.Queue.CurrentDepth) > 0)
            {
                flag = true;
                log.WriteLog("SYSTEM.DOTNET.XARECOVERY.QUEUE Current Depth : ", num2.ToString());
                MQMessage message = new MQMessage();
                try
                {
                    this.Queue.Get(message, this.gmoB);
                }
                catch (MQException exception)
                {
                    base.TrException(method, exception, 1);
                    if (exception.Reason == 0x7f1)
                    {
                        break;
                    }
                }
                log.WriteLog("MsgID of current message(recovery message) being processed ", NmqiTools.ArrayToHexString(message.MessageId));
                if (message == null)
                {
                    break;
                }
                sbyte[] xid            = null;
                double  doubleProperty = 0.0;
                string  stringProperty = null;
                try
                {
                    xid            = message.GetBytesProperty("dnet.XARECOVERY_XID");
                    doubleProperty = message.GetDoubleProperty("dnet.XARECOVERY_TTIMEOUT");
                    stringProperty = message.GetStringProperty("dnet.XARECOVERY_HOSTANDUSER");
                }
                catch (MQException exception2)
                {
                    base.TrException(method, exception2, 2);
                    if ((((exception2.Reason != 0x9a7) && (exception2.Reason != 0x98a)) && ((exception2.Reason != 0x9a2) && (exception2.Reason != 0x9a3))) && ((exception2.Reason != 0x9a6) && (exception2.Reason != 0x9a5)))
                    {
                        throw exception2;
                    }
                    continue;
                }
                log.WriteLog("Host,UserId under which the Transaction Recovery message has been put : " + stringProperty);
                log.WriteLog("Current Xid from the indoubt message");
                log.WriteXid(xid);
                DateTime putDateTime = message.PutDateTime;
                DateTime time2       = putDateTime.AddMilliseconds(doubleProperty);
                if (time2 > DateTime.Now)
                {
                    log.WriteLog("TransactionTimeout - " + time2.ToLongTimeString());
                    log.WriteLog("Message arrival time - " + putDateTime.ToLongTimeString());
                    log.WriteLog("Monitor skiping the current messages as transaction did not timeout yet..");
                }
                else
                {
                    try
                    {
                        this.Queue.Get(message, this.gmoG);
                        log.WriteLog("Found one Inomplete Transaction from message,(ID):", NmqiTools.ArrayToHexString(message.MessageId));
                    }
                    catch (MQException exception3)
                    {
                        base.TrException(method, exception3, 2);
                        throw exception3;
                    }
                    try
                    {
                        int     intProperty   = message.GetIntProperty("dnet.XARECOVERY_RMID");
                        sbyte[] bytesProperty = message.GetBytesProperty("dnet.XARECOVERY_QMID");
                        sbyte[] recinfo       = message.GetBytesProperty("dnet.XARECOVERY_RECINFO");
                        sbyte[] numArray4     = message.GetBytesProperty("dnet.XARECOVERY_XID");
                        byte[]  dst           = new byte[bytesProperty.Length];
                        Buffer.BlockCopy(bytesProperty, 0, dst, 0, dst.Length);
                        this.QMId = new Guid(dst);
                        if (this.RecoverTransaction(this.QMId, recinfo, numArray4, intProperty))
                        {
                            this.Qmgr.Commit();
                            if (!CompletedQMId.Contains(this.QMId))
                            {
                                CompletedQMId.Add(this.QMId);
                            }
                        }
                        else
                        {
                            if (CompletedQMId.Contains(this.QMId))
                            {
                                CompletedQMId.Remove(this.QMId);
                            }
                            this.Qmgr.Backout();
                        }
                        continue;
                    }
                    catch (MQException exception4)
                    {
                        base.TrException(method, exception4, 3);
                        throw exception4;
                    }
                }
            }
            foreach (Guid guid in CompletedQMId)
            {
                TransactionManager.RecoveryComplete(guid);
            }
            if (!flag)
            {
                Thread.Sleep(0x1d4c0);
                this.reenter = true;
            }
            base.TrExit(method);
        }
示例#3
0
        public MqMessageGeneric GetNextMessage()
        {
            MqMessageGeneric res = null;

            String qname = this.queueName;

            if (String.IsNullOrWhiteSpace(qname))
            {
                throw new ArgumentNullException("Не определено имя очереди");
            }

            var getMessageOptions = new MQGetMessageOptions();

            getMessageOptions.Options      = MQC.MQGMO_WAIT + MQC.MQGMO_SYNCPOINT;
            getMessageOptions.WaitInterval = 100;  // 1 seconds wait​

            try
            {
                if (mqManager == null)
                {
                    mqManager = createManager();
                }

                message = new MQMessage();

                using (var queue = mqManager.AccessQueue(queueName, MQC.MQOO_INQUIRE))
                    if (queue.CurrentDepth == 0)
                    {
                        return(res);
                    }

                using (var q = mqManager.AccessQueue(qname, MQC.MQOO_INPUT_AS_Q_DEF + MQC.MQOO_FAIL_IF_QUIESCING))

                    q.Get(message, getMessageOptions);


                res             = new MqMessageGeneric();
                res.Body        = Encoding.UTF8.GetString(message.ReadBytes(message.MessageLength));
                res.MessageID   = message.MessageId;
                res.PutDateTime = message.PutDateTime;

                var inames = message.GetPropertyNames("%");
                if (inames != null)
                {
                    while (inames.MoveNext())
                    {
                        String name = inames.Current.ToString();
                        if (name.ToLower().Contains("jms") ||
                            name.ToLower().Contains("mcd"))
                        {
                            continue;
                        }
                        res.AddedProperties.Add(name, message.GetStringProperty(name));
                    }
                }
            }
            catch (MQException mqex)
            {
                RollbackGet();
                message = null;
                if (mqex.ReasonCode == 2033 && mqex.CompCode == 2) // В очереди нет сообщений
                {
                    return(null);
                }
                throw new InvalidOperationException(String.Format("MQ {0}", mqex.Message));
            }
            catch
            {
                RollbackGet();
                throw;
            }

            return(res);
        }