Example #1
0
        /// <summary>
        /// 销毁ONS事务消息相关对象,一般在Application_End中调用
        /// </summary>
        public static void Destroy()
        {
            if (_AliyunOnsIsEnabled == "1")
            {
                try
                {
                    if (ONSProducerList != null && ONSProducerList.Count > 0)
                    {
                        ONSProducerList.ForEach(producer =>
                        {
                            //关闭上游消息生产者
                            DebugUtil.Debug("ProducerId(" + producer.Type.ToString() + @"):" + producer.ProducerId + @"生产者.准备shutdown()");
                            producer.shutdown();
                            DebugUtil.Debug("ProducerId(" + producer.Type.ToString() + @"):" + producer.ProducerId + @"生产者.shutdown()");
                        });
                    }

                    if (ONSConsumerList != null && ONSConsumerList.Count > 0)
                    {
                        ONSConsumerList.ForEach(consumer =>
                        {
                            //关闭下游消息消费者
                            DebugUtil.Debug("ConsumerId(" + consumer.Type.ToString() + @"):" + consumer.ConsumerId + @"消费者.准备shutdown()");
                            consumer.shutdown();
                            DebugUtil.Debug("ConsumerId(" + consumer.Type.ToString() + @"):" + consumer.ConsumerId + @"消费者.shutdown()");
                        });
                    }

                    //延时若干毫秒
                    Thread.Sleep(1000);
                    DebugUtil.Debug(_Environment + "." + _ApplicationAlias + ".ONSHelper.Destroy最后延迟1000毫秒");
                }
                catch (Exception e)
                {
                    //记录本地错误日志
                    DebugUtil.Debug(_Environment + "." + _ApplicationAlias + ".ONSHelper.Destroy出错:" + e.ToString());

                    //记录FATAL日志,发送FATAL目前出错
                    //SaveLog(LogTypeEnum.FATAL, "ONSHelper", "Destroy", _Environment + "." + _ApplicationAlias + ".ONSHelper.Destroy出错:" + e.ToString());

                    //发送邮件
                    SendDebugMail(_Environment + "." + _ApplicationAlias + ".ONSHelper.Destroy出错", ":" + e.ToString());
                }
            }
        }
Example #2
0
        /// <summary>
        /// 初始化ONS事务消息相关对象,一般在Application_Start中调用
        /// </summary>
        public static void Initialize()
        {
            if (_AliyunOnsIsEnabled == "1")
            {
                try
                {
                    //初始化服务类列表和服务类标签列表
                    InitializeProperties();

                    //判断生产者的服务类是否存在
                    if (ONSProducerServiceList.Count > 0)
                    {
                        //输出生产服务类类名
                        ONSProducerServiceList.ForEach(service => DebugUtil.Debug("生产者的服务类:" + service.GetType().FullName));

                        //判断生产者实例列表是否为空
                        if (ONSProducerList != null && ONSProducerList.Count > 0)
                        {
                            ONSProducerList.ForEach(producer =>
                            {
                                //启动上游事务生产者
                                producer.start();
                                DebugUtil.Debug("Topic:" + producer.Topic + ",ProducerId(" + producer.Type.ToString() + @"):" + producer.ProducerId + @"生产者.start()");
                            });
                        }
                    }

                    //判断消费者的服务类是否存在
                    if (ONSConsumerServiceList.Count > 0)
                    {
                        //输出消费服务类类名
                        ONSConsumerServiceList.ForEach(service => DebugUtil.Debug("消费者的服务类:" + service.GetType().FullName));

                        //判断消费者实例列表是否为空
                        if (ONSConsumerList != null && ONSConsumerList.Count > 0)
                        {
                            ONSConsumerList.ForEach(consumer =>
                            {
                                string tags = GetConsumerServiceTags(consumer.TagList);
                                consumer.subscribe(consumer.Topic, tags);
                                //启动上游事务生产者
                                consumer.start();
                                DebugUtil.Debug("Topic:" + consumer.Topic + ",ConsumerId(" + consumer.Type.ToString() + @"):" + consumer.ConsumerId + @"消费者.start(),topic:" + consumer.Topic + ",tags:" + tags);
                            });
                        }
                    }

                    //延时若干毫秒
                    Thread.Sleep(1000);
                    DebugUtil.Debug(_Environment + "." + _ApplicationAlias + ".ONSHelper.Initialize最后延迟1000毫秒");
                }
                catch (Exception e)
                {
                    //记录本地错误日志
                    DebugUtil.Debug(_Environment + "." + _ApplicationAlias + ".ONSHelper.Initialize出错:" + e.ToString());

                    //记录FATAL日志,发送FATAL目前出错
                    //SaveLog(LogTypeEnum.FATAL, "ONSHelper", "Initialize", _Environment + "." + _ApplicationAlias + ".ONSHelper.Initialize出错:" + e.ToString());

                    //发送邮件
                    SendDebugMail(_Environment + "." + _ApplicationAlias + ".ONSHelper.Initialize出错", ":" + e.ToString());
                }
            }
        }