예제 #1
0
 private List <IEventHandlerFactory> GetOrCreateHandlerFactories(Type eventType)
 {
     return(HandlerFactories.GetOrAdd(
                eventType,
                type =>
     {
         var eventName = EventNameAttribute.GetName(type);
         EventTypes[eventName] = type;
         return new List <IEventHandlerFactory>();
     }
                ));
 }
예제 #2
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.GetName(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));
        }
예제 #3
0
        public override Task PublishAsync(Type eventType, object eventData)
        {
            var eventName = EventNameAttribute.GetName(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);
        }