/// <summary> /// 构造函数 /// </summary> /// <param name="messageType">消息类别,分为BASE,ORDER,TRAN</param> /// <param name="topicTag">主题标签,用{Topic}.{Tag}格式的枚举实现</param> public AbstractProducerService(ONSMessageType messageType, Enum topicTag) { this.MessageType = messageType; this.TopicTag = topicTag; this.Topic = (_Environment + "_" + TopicTag.GetType().Name).ToUpper(); this.Tag = TopicTag.ToString(); this.Pid = ("PID_" + this.Topic).ToUpper(); }
/// <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); } } } }
/// <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); } } } } }