Exemplo n.º 1
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());
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// 生成消费者实例
        /// </summary>
        /// <typeparam name="T">消费者服务基类类型</typeparam>
        /// <param name="assembly">消费者服务类所在程序集</param>
        /// <param name="type">消费者服务类的类型</param>
        /// <param name="messageType">消息类型BASE,ORDER,TRAN</param>
        /// <param name="func">生成消费者实例的委托代码</param>
        internal static void CreateConsumer <T>(Assembly assembly, Type type, ONSMessageType messageType, Func <ONSFactoryProperty, string, string, Type, IONSConsumer> func)
        {
            if (type.BaseType.FullName.IndexOf(typeof(T).Name) >= 0)
            {
                //添加到消费者服务类实例列表
                //ONSConsumerServiceList.Add(type);
                object service = assembly.CreateInstance(type.FullName);
                ONSConsumerServiceList.Add(service);
                //获取服务接口
                IAbstractConsumerService iservice = (IAbstractConsumerService)service;
                //获取枚举数组对象
                Enum[] topicTagList = iservice.TopicTagList;

                if (topicTagList != null && topicTagList.Length > 0)
                {
                    foreach (Enum topicTag in topicTagList)
                    {
                        string serviceTopic = topicTag.GetType().Name;
                        string serviceTag   = topicTag.ToString();
                        string className    = type.Name;

                        string topic      = (_Environment + "_" + serviceTopic).ToUpper();
                        string consumerId = ("CID_" + topic + "_" + _ApplicationAlias + "_" + className).ToUpper();

                        DebugUtil.Debug("consumerId:" + consumerId);

                        IONSConsumer consumer = ONSConsumerList.Where(c => c.Type == messageType.ToString() && c.ConsumerId == consumerId).FirstOrDefault();
                        if (consumer == null)
                        {
                            //实例化ONSFactoryProperty
                            ONSFactoryProperty onsConsumerFactoryProperty = new ONSFactoryProperty();
                            onsConsumerFactoryProperty.setFactoryProperty(ONSFactoryProperty.AccessKey, _AliyunOnsAccessKey);
                            onsConsumerFactoryProperty.setFactoryProperty(ONSFactoryProperty.SecretKey, _AliyunOnsSecretKey);
                            onsConsumerFactoryProperty.setFactoryProperty(ONSFactoryProperty.ConsumerId, consumerId);
                            //onsConsumerFactoryProperty.setFactoryProperty(ONSFactoryProperty.PublishTopics, _ONSTopic);

                            if (_AliyunOnsConsumerLogPath != "")
                            {
                                if (Directory.Exists(_AliyunOnsConsumerLogPath))
                                {
                                    onsConsumerFactoryProperty.setFactoryProperty(ONSFactoryProperty.LogPath, _AliyunOnsConsumerLogPath);
                                }
                            }

                            //获取消费者IONSConsumer
                            consumer = func(onsConsumerFactoryProperty, topic, consumerId, type);

                            //记录消费者初始化信息
                            DebugUtil.Debug("Topic:" + topic + ",ConsumerId(" + consumer.Type + "):" + consumer.ConsumerId + "消费者.new()");

                            //新增代理类ONSConsumer实例到ONSConsumerList中
                            ONSConsumerList.Add(consumer);
                        }

                        if (!consumer.TagList.Contains(serviceTag))
                        {
                            consumer.TagList.Add(serviceTag);
                        }
                    }
                }
            }
        }