Ejemplo n.º 1
0
        public bool PublishDistributed <T>(string topic, T eventMessage, DistributedConsumerType distributedConsumerType, string lable = "")
        {
            if (!SubscriptionManager.DistributedEventService.IsRunning)
            {
                throw new EventException("分布式事件处理服务未启动");
            }
            WriteTrackLog("发布分布式事件通知", topic, lable, eventMessage);

            //WriteTrackLog("发布分布式事件通知,本地通知开始", topic, lable, eventMessage);
            var subscriptions = _subscriptionService.GetSubscriptions <T>();

            subscriptions.ToList().ForEach(x => PublishToConsumer(x, lable, eventMessage));
            //WriteTrackLog("发布分布式事件通知,本地通知完成", topic, lable, eventMessage);

            var consumerType = typeof(T);
            var objectName   = consumerType.FullName;
            DistributedObjectNameAttribute objectNameAttr = consumerType.GetCustomAttribute <DistributedObjectNameAttribute>();

            if (objectNameAttr != null && !string.IsNullOrEmpty(objectNameAttr.Name))
            {
                objectName = objectNameAttr.Name;
            }
            var dEvent = new DistributedEvent(eventMessage, topic, lable, objectName);

            //WriteTrackLog(string.Format("发布分布式事件通知,请求远程通信开始,对象名:{0}", objectName), topic, lable, eventMessage);
            return(SubscriptionManager.EventTransport.SendEventMessage(dEvent, distributedConsumerType));
        }
Ejemplo n.º 2
0
        private static void InternalRegisterConsumerType(Type consumer)
        {
            logger.Info(string.Format("开始注册订阅对象,类型:{0}", consumer.FullName));
            DistributedEventAttribute dEvent = consumer.GetCustomAttribute <DistributedEventAttribute>();
            var interfaces = consumer.FindInterfaces((type, criteria) =>
            {
                var isMatch = type.IsGenericType && ((Type)criteria).IsAssignableFrom(type.GetGenericTypeDefinition());
                return(isMatch);
            }, typeof(IConsumer <>));
            object consumerInstance = null;

            try
            {
                consumerInstance = Activator.CreateInstance(consumer);
            }
            catch (Exception ex)
            {
                throw new EventException(string.Format("订阅对象创建失败,对象类型:{0}", consumer.FullName), ex);
            }
            foreach (var item in interfaces)
            {
                if (subList.ContainsKey(item))
                {
                    subList[item].Add(consumerInstance);
                }
                else
                {
                    subList.Add(item, new List <object> {
                        consumerInstance
                    });
                }
                logger.Info(string.Format("解析订阅类型完成,订阅对象类型:{0},订阅类型:{1}", consumer.FullName, item.FullName));

                if (dEvent != null)
                {
                    var    genericArgs  = item.GetGenericArguments();
                    var    consumerType = genericArgs[0];
                    string objectName   = consumerType.FullName;
                    DistributedObjectNameAttribute objectNameAttr = consumerType.GetCustomAttribute <DistributedObjectNameAttribute>();
                    if (objectNameAttr != null && !string.IsNullOrEmpty(objectNameAttr.Name))
                    {
                        objectName = objectNameAttr.Name;
                    }
                    if (distribSubObjectNameList.ContainsKey(dEvent.Topic))
                    {
                        if (!distribSubObjectNameList[dEvent.Topic].ContainsKey(objectName))
                        {
                            distribSubObjectNameList[dEvent.Topic].Add(objectName, consumerType);
                        }
                        else
                        {
                            if (distribSubObjectNameList[dEvent.Topic][objectName] != consumerType)
                            {
                                throw new EventException("该类型对象名重复");
                            }
                        }
                    }
                    else
                    {
                        var objectNameDic = new Dictionary <string, Type>();
                        objectNameDic.Add(objectName, consumerType);
                        distribSubObjectNameList.Add(dEvent.Topic, objectNameDic);
                    }
                    if (distribSubList.ContainsKey(dEvent.Topic))
                    {
                        if (distribSubList[dEvent.Topic].ContainsKey(objectName))
                        {
                            distribSubList[dEvent.Topic][objectName].Add(Tuple.Create(dEvent.HandleType, consumerInstance));
                        }
                        else
                        {
                            distribSubList[dEvent.Topic].Add(objectName, new List <Tuple <HandleType, object> > {
                                Tuple.Create(dEvent.HandleType, consumerInstance)
                            });
                        }
                    }
                    else
                    {
                        Dictionary <string, List <Tuple <HandleType, object> > > dict = new Dictionary <string, List <Tuple <HandleType, object> > >();
                        dict.Add(objectName, new List <Tuple <HandleType, object> > {
                            Tuple.Create(dEvent.HandleType, consumerInstance)
                        });
                        distribSubList.Add(dEvent.Topic, dict);
                    }
                    logger.Info(string.Format("解析分布式订阅类型完成,订阅对象类型:{0},订阅类型:{1},订阅主题:{2},对象名:{3}", consumer.FullName, item.FullName, dEvent.Topic, objectName));
                }
            }
        }