コード例 #1
0
    private Task PublishAsync(string topicName, Type eventType, object eventData, Headers headers, Dictionary <string, object> headersArguments)
    {
        var eventName = EventNameAttribute.GetNameOrDefault(eventType);
        var body      = Serializer.Serialize(eventData);

        return(PublishAsync(topicName, eventName, body, headers, headersArguments));
    }
コード例 #2
0
    public Task PublishAsync(Type eventType, object eventData, IBasicProperties properties, Dictionary <string, object> headersArguments = null)
    {
        var eventName = EventNameAttribute.GetNameOrDefault(eventType);
        var body      = Serializer.Serialize(eventData);

        return(PublishAsync(eventName, body, properties, headersArguments));
    }
コード例 #3
0
        protected virtual IEnumerable <ConsumerExecutorDescriptor> GetHandlerDescription(Type eventType, Type typeInfo)
        {
            var serviceTypeInfo = typeof(IDistributedEventHandler <>)
                                  .MakeGenericType(eventType);
            var method = typeInfo
                         .GetMethod(
                nameof(IDistributedEventHandler <object> .HandleEventAsync),
                new[] { eventType }
                );
            var eventName       = EventNameAttribute.GetNameOrDefault(eventType);
            var topicAttr       = method.GetCustomAttributes <TopicAttribute>(true);
            var topicAttributes = topicAttr.ToList();

            if (topicAttributes.Count == 0)
            {
                topicAttributes.Add(new CapSubscribeAttribute(eventName));
            }

            foreach (var attr in topicAttributes)
            {
                SetSubscribeAttribute(attr);

                var parameters = method.GetParameters()
                                 .Select(parameter => new ParameterDescriptor
                {
                    Name          = parameter.Name,
                    ParameterType = parameter.ParameterType,
                    IsFromCap     = parameter.GetCustomAttributes(typeof(FromCapAttribute)).Any()
                }).ToList();

                yield return(InitDescriptor(attr, method, typeInfo.GetTypeInfo(), serviceTypeInfo.GetTypeInfo(), parameters));
            }
        }
コード例 #4
0
        protected override void UnSubscribe(Type eventType, Type handlerType)
        {
            var eventName = EventNameAttribute.GetNameOrDefault(eventType);

            _logger.LogInformation("Unsubscribing from event {EventName}", eventName);
            _subsManager.RemoveSubscription(eventType, handlerType);
        }
コード例 #5
0
        public Task PublishAsync(Type eventType, object eventData, IBasicProperties properties, Dictionary <string, object> headersArguments = null)
        {
            var eventName = EventNameAttribute.GetNameOrDefault(eventType);
            var body      = Serializer.Serialize(eventData);

            using (var channel = ConnectionPool.Get(AbpRabbitMqEventBusOptions.ConnectionName).CreateModel())
            {
                channel.ExchangeDeclare(
                    AbpRabbitMqEventBusOptions.ExchangeName,
                    "direct",
                    durable: true
                    );

                if (properties == null)
                {
                    properties = channel.CreateBasicProperties();
                    properties.DeliveryMode = RabbitMqConsts.DeliveryModes.Persistent;
                    properties.MessageId    = Guid.NewGuid().ToString("N");
                }

                SetEventMessageHeaders(properties, headersArguments);

                channel.BasicPublish(
                    exchange: AbpRabbitMqEventBusOptions.ExchangeName,
                    routingKey: eventName,
                    mandatory: true,
                    basicProperties: properties,
                    body: body
                    );
            }

            return(Task.CompletedTask);
        }
コード例 #6
0
        public override Task PublishAsync(Type eventType, object eventData)
        {
            var eventName = EventNameAttribute.GetNameOrDefault(eventType);
            var body      = Serializer.Serialize(eventData);

            using (var channel = ConnectionPool.Get(AbpRabbitMqEventBusOptions.ConnectionName).CreateModel())
            {
                channel.ExchangeDeclare(
                    AbpRabbitMqEventBusOptions.ExchangeName,
                    "direct",
                    durable: true
                    );

                var properties = channel.CreateBasicProperties();
                properties.DeliveryMode = RabbitMqConsts.DeliveryModes.Persistent;

                channel.BasicPublish(
                    exchange: AbpRabbitMqEventBusOptions.ExchangeName,
                    routingKey: eventName,
                    mandatory: true,
                    basicProperties: properties,
                    body: body
                    );
            }

            return(Task.CompletedTask);
        }
コード例 #7
0
ファイル: DistributedEventBusBase.cs プロジェクト: xyfy/abp
    private async Task <bool> AddToOutboxAsync(Type eventType, object eventData)
    {
        var unitOfWork = UnitOfWorkManager.Current;

        if (unitOfWork == null)
        {
            return(false);
        }

        foreach (var outboxConfig in AbpDistributedEventBusOptions.Outboxes.Values)
        {
            if (outboxConfig.Selector == null || outboxConfig.Selector(eventType))
            {
                var eventOutbox = (IEventOutbox)unitOfWork.ServiceProvider.GetRequiredService(outboxConfig.ImplementationType);
                var eventName   = EventNameAttribute.GetNameOrDefault(eventType);
                await eventOutbox.EnqueueAsync(
                    new OutgoingEventInfo(
                        GuidGenerator.Create(),
                        eventName,
                        Serialize(eventData),
                        Clock.Now
                        )
                    );

                return(true);
            }
        }

        return(false);
    }
コード例 #8
0
    public virtual string GetName(Type eventType)
    {
        if (!eventType.IsGenericType)
        {
            throw new AbpException($"Given type is not generic: {eventType.AssemblyQualifiedName}");
        }

        var genericArguments = eventType.GetGenericArguments();

        if (genericArguments.Length > 1)
        {
            throw new AbpException($"Given type has more than one generic argument: {eventType.AssemblyQualifiedName}");
        }

        var eventName = EventNameAttribute.GetNameOrDefault(genericArguments[0]);

        if (!Prefix.IsNullOrEmpty())
        {
            eventName = Prefix + eventName;
        }

        if (!Postfix.IsNullOrEmpty())
        {
            eventName = eventName + Postfix;
        }

        return(eventName);
    }
コード例 #9
0
 private List <IEventHandlerFactory> GetOrCreateHandlerFactories(Type eventType)
 {
     return(HandlerFactories.GetOrAdd(
                eventType,
                type => {
         var eventName = EventNameAttribute.GetNameOrDefault(type);
         EventTypes[eventName] = type;
         return new List <IEventHandlerFactory> ();
     }
                ));
 }
コード例 #10
0
 private List <IEventHandlerFactory> GetOrCreateHandlerFactories(Type eventType)
 {
     return(HandlerFactories.GetOrAdd(
                eventType,
                type =>
     {
         var eventName = EventNameAttribute.GetNameOrDefault(type);
         EventStopingTokens[eventName] = new CancellationTokenSource();
         return new List <IEventHandlerFactory>();
     }
                ));
 }
コード例 #11
0
        protected override void Subscribe(Type eventType, Type handlerType)
        {
            var rabbitMqMessageConsumer = TeyGetOrSetMessageConsumer(eventType);
            var eventName = EventNameAttribute.GetNameOrDefault(eventType);

            _logger.LogInformation("Subscribing from event {EventName}", eventName);
            if (!_subsManager.IncludeSubscriptionsHandlesForEventName(eventName))
            {
                rabbitMqMessageConsumer?.BindAsync(eventName);
            }
            _subsManager.AddSubscription(eventType, handlerType);
        }
コード例 #12
0
    public async Task ProcessEventAsync(Type eventType, object eventData)
    {
        var messageId = MessageContext.Current.TransportMessage.GetMessageId();
        var eventName = EventNameAttribute.GetNameOrDefault(eventType);

        if (await AddToInboxAsync(messageId, eventName, eventType, MessageContext.Current.TransportMessage.Body))
        {
            return;
        }

        await TriggerHandlersAsync(eventType, eventData);
    }
コード例 #13
0
        protected override IEnumerable <EventTypeWithEventHandlerFactories> GetHandlerFactories(Type eventType)
        {
            var handlerFactoryList = new List <EventTypeWithEventHandlerFactories>();

            foreach (var handlerFactory in HandlerFactories.Where(hf => ShouldTriggerEventForHandler(eventType, hf.Key)))
            {
                handlerFactoryList.Add(new EventTypeWithEventHandlerFactories(handlerFactory.Key, handlerFactory.Value));
                var topic = EventNameAttribute.GetNameOrDefault(handlerFactory.Key);
                AddHandler(topic, handlerFactory.Value);
            }

            return(handlerFactoryList.ToArray());
        }
コード例 #14
0
        public override IDisposable Subscribe(Type eventType, IEventHandlerFactory factory)
        {
            var handlerFactories = GetOrCreateHandlerFactories(eventType);

            handlerFactories.Add(factory);

            if (handlerFactories.Count == 1) //TODO: Multi-threading!
            {
                Consumer.BindAsync(EventNameAttribute.GetNameOrDefault(eventType));
            }

            return(new EventHandlerFactoryUnregistrar(this, eventType, factory));
        }
コード例 #15
0
        public override async Task PublishAsync(Type eventType, object eventData)
        {
            if (eventData is null)
            {
                throw new ArgumentNullException(nameof(eventData));
            }
            var topic = EventNameAttribute.GetNameOrDefault(eventType);

            // We need to make sure that we pass the concrete type to PublishEventAsync,
            // which can be accomplished by casting the event to dynamic. This ensures
            // that all event fields are properly serialized.
            await _dapr.PublishEventAsync(_options.Value.PubSubName, topic, (dynamic)eventData);
        }
コード例 #16
0
        public override async Task PublishAsync(Type eventType, object eventData)
        {
            var eventName = EventNameAttribute.GetNameOrDefault(eventType);
            var body      = Serializer.Serialize(eventData);

            var producer = ProducerPool.Get(AbpKafkaEventBusOptions.ConnectionName);

            await producer.ProduceAsync(
                AbpKafkaEventBusOptions.TopicName,
                new Message <string, byte[]>
            {
                Key = eventName, Value = body
            });
        }
コード例 #17
0
 /// <summary>
 /// 发布事件
 /// </summary>
 /// <param name="eventType">事件类型</param>
 /// <param name="eventData">事件数据对象</param>
 /// <returns></returns>
 public override async Task PublishAsync(Type eventType, object eventData)
 {
     var eventName = EventNameAttribute.GetNameOrDefault(eventType);
     await CapPublisher
     .PublishAsync(
         eventName, eventData,
         new Dictionary <string, string>
     {
         { AbpCAPHeaders.UserId, CurrentUser.Id?.ToString() ?? "" },
         { AbpCAPHeaders.ClientId, CurrentClient.Id ?? "" },
         { AbpCAPHeaders.TenantId, CurrentTenant.Id?.ToString() ?? "" },
     },
         CancellationTokenProvider.FallbackToProvider());
 }
コード例 #18
0
        private async Task PublishAsync(string topicName, Type eventType, object eventData, Headers headers, Dictionary <string, object> headersArguments)
        {
            var eventName = EventNameAttribute.GetNameOrDefault(eventType);
            var body      = Serializer.Serialize(eventData);

            var producer = ProducerPool.Get(AbpKafkaEventBusOptions.ConnectionName);

            SetEventMessageHeaders(headers, headersArguments);

            await producer.ProduceAsync(
                topicName,
                new Message <string, byte[]>
            {
                Key = eventName, Value = body, Headers = headers
            });
        }
コード例 #19
0
        public override IDisposable Subscribe(Type eventType, IEventHandlerFactory factory)
        {
            var handlerFactories = GetOrCreateHandlerFactories(eventType);

            if (factory.IsInFactories(handlerFactories))
            {
                return(NullDisposable.Instance);
            }

            handlerFactories.Add(factory);

            if (handlerFactories.Count == 1) //TODO: Multi-threading!
            {
                var topic = EventNameAttribute.GetNameOrDefault(eventType);
                AddHandler(topic, handlerFactories);
            }

            return(new EventHandlerFactoryUnregistrar(this, eventType, factory));
        }
コード例 #20
0
        protected override Task PublishAsync(Type eventType, IntegrationEvent eventDate)
        {
            if (!_persistentConnection.IsConnected)
            {
                _persistentConnection.TryConnect();
            }
            var policy = Policy.Handle <BrokerUnreachableException>()
                         .Or <SocketException>()
                         .WaitAndRetry(_retryCount, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)), (ex, time) =>
            {
                _logger.LogWarning(ex, "Could not publish event: {EventId} after {Timeout}s ({ExceptionMessage})", eventDate.Id, $"{time.TotalSeconds:n1}", ex.Message);
            });

            var eventName = EventNameAttribute.GetNameOrDefault(eventType);

            _logger.LogTrace("Creating RabbitMQ channel to publish event: {EventId} ({EventName})", eventDate.Id, eventName);

            using (var channel = _persistentConnection.CreateModel())
            {
                _logger.LogTrace("Declaring RabbitMQ exchange to publish event: {EventId}", eventDate.Id);
                var message = JsonConvert.SerializeObject(eventDate);
                var body    = Encoding.UTF8.GetBytes(message);

                var model        = channel;
                var exchangeName = GetPublishConfigure();
                model.ExchangeDeclare(exchange: exchangeName, type: "direct", durable: true);
                policy.Execute(() =>
                {
                    var properties          = model.CreateBasicProperties();
                    properties.DeliveryMode = 2; // persistent

                    _logger.LogTrace("Publishing event to RabbitMQ: {EventId}", eventDate.Id);

                    model.BasicPublish(
                        exchange: exchangeName,
                        routingKey: eventName,
                        mandatory: true,
                        basicProperties: properties,
                        body: body);
                });
            }
            return(Task.CompletedTask);
        }
コード例 #21
0
        private void SubsManager_OnEventRemoved(object sender, Type eventType)
        {
            var eventName = EventNameAttribute.GetNameOrDefault(eventType);

            var(exchangeName, queueName) = GetExchangeNameAndQueueName(eventType);
            var key = $"{exchangeName}_{queueName}";

            if (!RabbitMqMessageConsumerDic.ContainsKey(key))
            {
                return;
            }
            var rabbitMqMessageConsumer = RabbitMqMessageConsumerDic[key];

            rabbitMqMessageConsumer.UnbindAsync(eventName);
            if (rabbitMqMessageConsumer.HasRoutingKeyBindingQueue())
            {
                return;
            }
            rabbitMqMessageConsumer.Dispose();
            RabbitMqMessageConsumerDic.TryRemove(key, out _);
        }
コード例 #22
0
        protected override void Publish(Type eventType, IntegrationEvent eventDate)
        {
            var exceptions = new List <Exception>();
            var eventName  = EventNameAttribute.GetNameOrDefault(eventType);

            if (_subsManager.IncludeEventTypeForEventName(eventName))
            {
                var eventHandleTypes = _subsManager.TryGetEventHandlerTypes(eventName);
                foreach (var eventHandleType in eventHandleTypes)
                {
                    try
                    {
                        var handlerInstance = _eventHandlerFactory.GetHandler(eventHandleType);
                        var concreteType    = typeof(IIntegrationEventHandler <>).MakeGenericType(eventType);
                        var method          = concreteType.GetMethod("Handle");
                        if (method != null)
                        {
                            ((Task)method.Invoke(handlerInstance, new object[] { eventDate })).GetAwaiter().GetResult();
                        }
                    }
                    catch (TargetInvocationException ex)
                    {
                        exceptions.Add(ex.InnerException);
                    }
                    catch (Exception ex)
                    {
                        exceptions.Add(ex);
                    }
                }
            }
            else
            {
                _logger.LogWarning("No subscription for local memory event: {eventName}", eventName);
            }
            if (exceptions.Any())
            {
                throw new AggregateException(
                          "More than one error has occurred while triggering the event: " + eventType, exceptions);
            }
        }
コード例 #23
0
        public override IDisposable Subscribe(Type eventType, IEventHandlerFactory factory)
        {
            var handlerFactories = GetOrCreateHandlerFactories(eventType);

            handlerFactories.Add(factory);

            if (handlerFactories.Count == 1) //TODO: Multi-threading!
            {
                var eventName = EventNameAttribute.GetNameOrDefault(eventType);

                using (var channel = ConnectionPool.Get().CreateModel()) //TODO: Connection name per event!
                {
                    channel.QueueBind(
                        queue: RabbitMqDistributedEventBusOptions.ClientName,
                        exchange: RabbitMqDistributedEventBusOptions.ExchangeName,
                        routingKey: eventName
                        );
                }
            }

            return(new EventHandlerFactoryUnregistrar(this, eventType, factory));
        }
コード例 #24
0
        private void TeyCreateMessageConsumer(Type eventType)
        {
            var(exchangeName, queueName) = GetExchangeNameAndQueueName(eventType);
            var key = $"{exchangeName}_{queueName}";

            if (RabbitMqMessageConsumerDic.ContainsKey(key))
            {
                return;
            }
            lock (_lock)
            {
                if (RabbitMqMessageConsumerDic.ContainsKey(key))
                {
                    return;
                }
                var rabbitMqMessageConsumer = _rabbitMqMessageConsumerFactory.Create(
                    new RabbitMqExchangeDeclareConfigure(exchangeName, "direct", true),
                    new RabbitMqQueueDeclareConfigure(queueName));
                var eventName = EventNameAttribute.GetNameOrDefault(eventType);
                rabbitMqMessageConsumer.BindAsync(eventName);
                rabbitMqMessageConsumer.OnMessageReceived(Consumer_Received);
                RabbitMqMessageConsumerDic.TryAdd(key, rabbitMqMessageConsumer);
            }
        }
コード例 #25
0
        public override Task PublishAsync(Type eventType, object eventData)
        {
            var eventName = EventNameAttribute.GetNameOrDefault(eventType);
            var body      = Serializer.Serialize(eventData);

            using (var channel = ConnectionPool.Get().CreateModel()) //TODO: Connection name per event!
            {
                //TODO: Other properties like durable?
                channel.ExchangeDeclare(RabbitMqDistributedEventBusOptions.ExchangeName, "");

                var properties = channel.CreateBasicProperties();
                properties.DeliveryMode = 2; //persistent

                channel.BasicPublish(
                    exchange: RabbitMqDistributedEventBusOptions.ExchangeName,
                    routingKey: eventName,
                    mandatory: true,
                    basicProperties: properties,
                    body: body
                    );
            }

            return(Task.CompletedTask);
        }
コード例 #26
0
 protected async override Task PublishToEventBusAsync(Type eventType, object eventData)
 {
     await PublishAsync(EventNameAttribute.GetNameOrDefault(eventType), eventData);
 }
 /// <summary>
 /// 发布事件
 /// </summary>
 /// <param name="eventType">事件类型</param>
 /// <param name="eventData">事件数据对象</param>
 /// <returns></returns>
 public override async Task PublishAsync(Type eventType, object eventData)
 {
     var eventName = EventNameAttribute.GetNameOrDefault(eventType);
     await CapPublisher.PublishAsync(eventName, eventData);
 }
コード例 #28
0
        public static void ConfigDaprServiceBus(this EndpointRouteBuilderContext endpointContext, IServiceProvider serviceProvider)
        {
            // Get services
            var loggerFactory                 = serviceProvider.GetRequiredService <ILoggerFactory>();
            var logger                        = loggerFactory.CreateLogger("StartUp");
            var serviceBus                    = serviceProvider.GetRequiredService <IDaprServiceBus>() as DaprServiceBus;
            var serviceScopeFactory           = serviceProvider.GetRequiredService <IServiceScopeFactory>();
            var serializerOptions             = serviceProvider.GetRequiredService <JsonSerializerOptions>();
            var serviceBusOptions             = serviceProvider.GetRequiredService <IOptions <DaprServiceBusOptions> >();
            var abpDistributedEventBusOptions = serviceProvider.GetRequiredService <IOptions <AbpDistributedEventBusOptions> >();

            //handlers
            var handlers = abpDistributedEventBusOptions.Value.Handlers;

            // Map endpoints
            endpointContext.Endpoints.MapSubscribeHandler();

            foreach (var handler in handlers)
            {
                var interfaces = handler.GetInterfaces();

                foreach (var @interface in interfaces)
                {
                    if (!typeof(IEventHandler).GetTypeInfo().IsAssignableFrom(@interface))
                    {
                        continue;
                    }
                    var genericArgs = @interface.GetGenericArguments();

                    if (genericArgs.Length != 1)
                    {
                        continue;
                    }

                    var typeInfo  = handler;
                    var eventType = genericArgs[0];

                    var serviceTypeInfo = typeof(IDistributedEventHandler <>).MakeGenericType(genericArgs[0]);
                    var method          = typeInfo
                                          .GetMethod(
                        nameof(IDistributedEventHandler <object> .HandleEventAsync),
                        new[] { eventType }
                        );
                    var eventName       = EventNameAttribute.GetNameOrDefault(eventType);
                    var topicAttr       = method.GetCustomAttributes <TopicAttribute>(true);
                    var topicAttributes = topicAttr.ToList();

                    if (topicAttributes.Count == 0)
                    {
                        topicAttributes.Add(new TopicAttribute(serviceBusOptions.Value.PubSubName, eventName));
                    }

                    foreach (var attr in topicAttributes)
                    {
                        logger.LogInformation($"pubsubname: {attr.PubsubName}{ attr.Name}");
                        endpointContext.Endpoints.MapPost(attr.Name, HandleMessage)
                        .WithTopic(attr.PubsubName, attr.Name);

                        serviceBus.Subscribe(genericArgs[0], new IocEventHandlerFactory(serviceScopeFactory, handler));
                    }
                }
            }

            async Task HandleMessage(HttpContext context)
            {
                var handlers = GetHandlersForRequest(context.Request.Path, out string topic);

                logger.LogInformation($"Request handlers count: {handlers.Count}");

                if (handlers != null)
                {
                    foreach (var handler in handlers)
                    {
                        var @event = await GetEventFromRequestAsync(context, topic, handler, serializerOptions);

                        logger.LogInformation($"Handling event: {@event}");

                        if (serviceBus.EventTypes.TryGetValue(topic, out Type eventType))
                        {
                            await serviceBus.TriggerHandlersAsync(eventType, @event);

                            // await (handler.GetHandler().EventHandler as IDistributedEventHandler<dynamic>).HandleEventAsync(@event);
                        }
                    }
                }
            }

            List <IEventHandlerFactory> GetHandlersForRequest(string path, out string topic)
            {
                topic = path.Substring(path.IndexOf("/") + 1);
                logger.LogInformation($"Topic for request: {topic}");
                if (serviceBus.Topics.TryGetValue(topic, out List <IEventHandlerFactory> handlers))
                {
                    return(handlers);
                }
                return(null);
            }

            async Task <dynamic> GetEventFromRequestAsync(HttpContext context, string topic,
                                                          IEventHandlerFactory handler, JsonSerializerOptions serializerOptions)
            {
                if (serviceBus.EventTypes.TryGetValue(topic, out Type eventType))
                {
                    var value = await JsonSerializer.DeserializeAsync(context.Request.Body, eventType, serializerOptions);

                    return(value);
                }
                return(null);
            }
        }