Example #1
0
        // 发送心跳消息给 dp2mserver

        /*
         * <root>
         * <type>patronNotify</type>
         * <recipient>R0000001@LUID:62637a12-1965-4876-af3a-fc1d3009af8a</recipient>
         * <mime>xml</mime>
         * <body>...</body>
         * </root>
         *
         * */
        public bool SendHeartBeat()
        {
            if (this.MessageConnection == null || this.MessageConnection.ConnectState != Microsoft.AspNet.SignalR.Client.ConnectionState.Connected)
            {
                return(false);
            }

            try
            {
                XmlDocument dom = new XmlDocument();
                dom.LoadXml("<root><type>heartbeat</type></root>");
                MessageRecord record = new MessageRecord();
                record.groups = new string[1] {
                    "gn:_patronNotify"
                };                                                     // gn 表示 group name
                record.data   = dom.DocumentElement.OuterXml;
                record.format = "xml";
                List <MessageRecord> records = new List <MessageRecord> {
                    record
                };

                DigitalPlatform.Message.SetMessageRequest param =
                    new DigitalPlatform.Message.SetMessageRequest("create",
                                                                  "dontNotifyMe",
                                                                  records);
                SetMessageResult result = this.MessageConnection.SetMessageTaskAsync(param,
                                                                                     _cancel.Token).Result;
                if (result.Value == -1)
                {
                    this.WriteErrorLog("Instance.SendHeartBeat() 中 SetMessageAsync() [heartbeat] 出错: " + result.ErrorInfo);
                    if (result.String == "_connectionNotFound")
                    {
                        Task.Run(() => TryResetConnection(/*result.String*/));
                    }
                    return(false);
                }

                return(true);
            }
            catch (ThreadAbortException)
            {
                return(false);
            }
            catch (Exception ex)
            {
                this.WriteErrorLog("Instance.SendHeartBeat() 出现异常: " + ExceptionUtil.GetDebugText(ex));
                return(false);
            }
        }
Example #2
0
        public void Notify()
        {
            // TODO: 要增加判断,防止过分频繁地被调用
            this.MessageConnection.CleanWebDataTable();

            if (this.dp2library.DefaultQueue == "!api")
            {
                try
                {
                    if (this.MessageConnection.ConnectState != Microsoft.AspNet.SignalR.Client.ConnectionState.Connected)
                    {
                        return;
                    }

                    MessageData[] messages = null;
                    string        strError = "";
                    int           nRet     = this.MessageConnection.GetMsmqMessage(
                        out messages,
                        out strError);
                    if (nRet == -1)
                    {
                        this.WriteErrorLog("Instance.Notify() 中 GetMsmqMessage() 出错: " + strError);
                        return;
                    }
                    if (messages != null)
                    {
                        foreach (MessageData data in messages)
                        {
                            MessageRecord record = new MessageRecord();
                            record.groups = new string[1] {
                                "gn:_patronNotify"
                            };                                                     // gn 表示 group name
                            record.data   = (string)data.strBody;
                            record.format = "xml";
                            List <MessageRecord> records = new List <MessageRecord> {
                                record
                            };

                            DigitalPlatform.Message.SetMessageRequest param =
                                new DigitalPlatform.Message.SetMessageRequest("create",
                                                                              "dontNotifyMe",
                                                                              records);
                            SetMessageResult result = this.MessageConnection.SetMessageTaskAsync(param,
                                                                                                 _cancel.Token).Result;
                            if (result.Value == -1)
                            {
                                this.WriteErrorLog("Instance.Notify() 中 SetMessageAsync() 出错: " + result.ErrorInfo);
                                return;
                            }
                        }

                        nRet = this.MessageConnection.RemoveMsmqMessage(
                            messages.Length,
                            out strError);
                        if (nRet == -1)
                        {
                            this.WriteErrorLog("Instance.Notify() 中 RemoveMsmqMessage() 出错: " + strError);
                            return;
                        }
                    }

                    this._notifyThread.Activate();
                    return;
                }
                catch (ThreadAbortException)
                {
                    return;
                }
                catch (Exception ex)
                {
                    this.WriteErrorLog("Instance.Notify() 出现异常1: " + ExceptionUtil.GetDebugText(ex));
                }
            }

            // 如果第一次初始化 Queue 没有成功,这里再试探初始化
            InitialQueue(false);

            // 进行通知处理
            if (_queue != null &&
                this.MessageConnection.ConnectState == Microsoft.AspNet.SignalR.Client.ConnectionState.Connected)
            {
                try
                {
                    ServerInfo._recordLocks.LockForWrite(this._queue.Path);
                }
                catch (ApplicationException)
                {
                    // 超时了
                    return;
                }

                bool bSucceed = false;
                try
                {
                    MessageEnumerator iterator = _queue.GetMessageEnumerator2();
                    while (iterator.MoveNext())
                    {
                        Message message = iterator.Current;

                        MessageRecord record = new MessageRecord();
                        record.groups = new string[1] {
                            "gn:_patronNotify"
                        };                                                     // gn 表示 group name
                        record.data   = (string)message.Body;
                        record.format = "xml";
                        List <MessageRecord> records = new List <MessageRecord> {
                            record
                        };

                        int length = record.data.Length;

                        DigitalPlatform.Message.SetMessageRequest param =
                            new DigitalPlatform.Message.SetMessageRequest("create",
                                                                          "dontNotifyMe",
                                                                          records);
                        SetMessageResult result = this.MessageConnection.SetMessageTaskAsync(param,
                                                                                             _cancel.Token).Result;
                        if (result.Value == -1)
                        {
                            this.WriteErrorLog("Instance.Notify() 中 SetMessageAsync() 出错: " + result.ErrorInfo);
                            if (result.String == "_connectionNotFound")
                            {
                                Task.Run(() => TryResetConnection(/*result.String*/));
                            }
                            return;
                        }

                        // http://stackoverflow.com/questions/21864043/with-messageenumerator-removecurrent-how-do-i-know-if-i-am-at-end-of-queue
                        try
                        {
                            iterator.RemoveCurrent();
                        }
                        finally
                        {
                            iterator.Reset();
                        }
                    }

                    bSucceed = true;
                }
                catch (ThreadAbortException)
                {
                    return;
                }
                catch (MessageQueueException ex)
                {
                    // 记入错误日志
                    // Program.WriteWindowsLog("Instance.Notify() 出现异常: " + ExceptionUtil.GetDebugText(ex));
                    this.WriteErrorLog("Instance.Notify() 出现异常: " + ExceptionUtil.GetDebugText(ex));
                    // Thread.Sleep(5 * 1000);   // 拖延 5 秒
                }
                catch (InvalidCastException ex)
                {
                    // 记入错误日志
                    // Program.WriteWindowsLog("Instance.Notify() 出现异常: " + ExceptionUtil.GetDebugText(ex));
                    this.WriteErrorLog("Instance.Notify() 出现异常: " + ExceptionUtil.GetDebugText(ex));
                    // Thread.Sleep(5 * 1000);   // 拖延 5 秒
                }
                catch (Exception ex)
                {
                    // 记入错误日志
                    // Program.WriteWindowsLog("Instance.Notify() 出现异常: " + ExceptionUtil.GetDebugText(ex));
                    this.WriteErrorLog("Instance.Notify() 出现异常: " + ExceptionUtil.GetDebugText(ex));
                    // Thread.Sleep(5 * 1000);   // 拖延 5 秒
                }
                finally
                {
                    ServerInfo._recordLocks.UnlockForWrite(this._queue.Path);

                    // 只有当发送到 dp2mserver 成功的情况下才立即重新监控最新 MQ
                    // 否则就等下一轮 Worker() 来处理
                    if (bSucceed == true)
                    {
                        // _queue.BeginPeek(new TimeSpan(0, 1, 0), null, OnMessageAdded);
                        BeginPeek();
                    }
                }
            }
        }