Exemplo n.º 1
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 CreateProducer <T>(Assembly assembly, Type type, ONSMessageType messageType, Func <ONSFactoryProperty, string, string, IONSProducer> func)
        {
            if (type.BaseType.FullName.IndexOf(typeof(T).Name) >= 0)
            {
                //添加到生产者服务类实例列表
                //ONSProducerServiceList.Add(type);
                object service = assembly.CreateInstance(type.FullName);
                ONSProducerServiceList.Add(service);
                //获取服务接口
                IAbstractProducerService iservice = (IAbstractProducerService)service;
                //获取枚举对象
                Enum topicTag = iservice.TopicTag;

                if (topicTag != null)
                {
                    string serviceTopic = topicTag.GetType().Name;
                    string topic        = (_Environment + "_" + serviceTopic).ToUpper();
                    //string producerId = ("PID_" + topic).ToUpper();
                    string       producerId = ("GID_" + topic).ToUpper();
                    IONSProducer producer   = ONSProducerList.Where(p => (p.Type == messageType.ToString()) && (p.ProducerId == producerId)).FirstOrDefault();
                    if (producer == null)
                    {
                        //实例化ONSFactoryProperty
                        ONSFactoryProperty onsProducerFactoryProperty = new ONSFactoryProperty();
                        onsProducerFactoryProperty.setFactoryProperty(ONSFactoryProperty.AccessKey, _AliyunOnsAccessKey);
                        onsProducerFactoryProperty.setFactoryProperty(ONSFactoryProperty.SecretKey, _AliyunOnsSecretKey);
                        //用老的.net SDK按最新的RocketMQ架构,引入GroupId代替ProducerId和ConsumerId
                        onsProducerFactoryProperty.setFactoryProperty(ONSFactoryProperty.ProducerId, producerId);
                        onsProducerFactoryProperty.setFactoryProperty(ONSFactoryProperty.PublishTopics, topic);

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

                        //获取生产者IONSProducer
                        producer = func(onsProducerFactoryProperty, topic, producerId);

                        //记录生产者初始化信息
                        DebugUtil.Debug("Topic:" + topic + ",ProducerId(" + producer.Type.ToString() + "):" + producer.ProducerId + "生产者new()");

                        //新增代理类ONSProducer实例到ONSProducerList中
                        ONSProducerList.Add(producer);
                    }
                }
            }
        }
Exemplo n.º 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());
                }
            }
        }