Exemplo n.º 1
0
        public void Subscribe(Type eventDataType, Type eventHandlerType)
        {
            if (!typeof(IntegrationEvent).IsAssignableFrom(eventDataType) || !typeof(IIntegrationEventHandler <>).MakeGenericType(eventDataType).IsAssignableFrom(eventHandlerType))
            {
                throw new Exception("eventDataType参数不继承IntegrationEvent!");
            }
            var topicAttribute = eventDataType.GetCustomAttributes().OfType <EventTopicAttribute>().FirstOrDefault();
            var topicName      = topicAttribute?.TopicName ?? this.GetType().FullName;
            var groupAttribute = eventHandlerType.GetCustomAttributes().OfType <EventGroupAttribute>().FirstOrDefault();
            var groupId        = groupAttribute?.GroupId ?? "default-group";

            lock (_lock)
            {
                var handler = new IntegrationHandlerDesc(eventHandlerType, eventDataType, groupId, typeof(IIntegrationEventHandler <>).MakeGenericType(eventDataType).GetMethod("Handle"));
                if (eventStore.ContainsKey(topicName))
                {
                    var handlers = eventStore[topicName];
                    if (!handlers.Any(o => o.GroupId == handler.GroupId && o.HandlerType == handler.HandlerType && o.IntegrationEventType == handler.IntegrationEventType))
                    {
                        handlers.Add(handler);
                    }
                }
                else
                {
                    eventStore.TryAdd(topicName, new List <IntegrationHandlerDesc> {
                        handler
                    });
                }
            }
        }
Exemplo n.º 2
0
        public void Subscribe <T, TH>()
            where T : IntegrationEvent
            where TH : IIntegrationEventHandler <T>
        {
            var topicAttribute = typeof(T).GetCustomAttributes().OfType <EventTopicAttribute>().FirstOrDefault();
            var topicName      = topicAttribute?.TopicName ?? this.GetType().FullName;
            var groupAttribute = typeof(TH).GetCustomAttributes().OfType <EventGroupAttribute>().FirstOrDefault();
            var groupId        = groupAttribute?.GroupId ?? "default-group";

            lock (_lock)
            {
                var handler = new IntegrationHandlerDesc(typeof(TH), typeof(T), groupId, typeof(IIntegrationEventHandler <>).MakeGenericType(typeof(TransportMessage).MakeGenericType(typeof(T))).GetMethod("Handle"));
                if (eventStore.ContainsKey(topicName))
                {
                    var handlers = eventStore[topicName];
                    if (!handlers.Any(o => o.GroupId == handler.GroupId && o.HandlerType == handler.HandlerType && o.IntegrationEventType == handler.IntegrationEventType))
                    {
                        handlers.Add(handler);
                    }
                }
                else
                {
                    eventStore.TryAdd(topicName, new List <IntegrationHandlerDesc> {
                        handler
                    });
                }
            }
        }