예제 #1
0
        public static void SubscribeToExchange <TEvent>(this IActor actor,
                                                        string exchange,
                                                        Expression <Func <TEvent, bool> >?routingStrategy = null,
                                                        string queueName                        = "",
                                                        string exchangeType                     = "topic",
                                                        bool isExchangeDurable                  = true,
                                                        bool isExchangeAutoDelete               = true,
                                                        bool createExchangeIfNotExist           = true,
                                                        bool createDeadLetterExchangeIfNotExist = true,
                                                        bool isQueueDurable                     = false,
                                                        bool isQueueAutoAck                     = false,
                                                        bool isQueueAutoDelete                  = true,
                                                        bool isQueueExclusive                   = true)
            where TEvent : class, IRabbitMqEvent
        {
            var rabbitMqBus = actor.GetConnectedBus <IRabbitMqBus>();

            var rabbitMqQueueConfiguration = new RabbitMqQueueConfiguration <TEvent>(routingStrategy,
                                                                                     queueName,
                                                                                     isQueueAutoAck,
                                                                                     isQueueDurable,
                                                                                     isQueueAutoDelete,
                                                                                     isQueueExclusive);

            var rabbitMqExchangeConfiguration = new RabbitMqExchangeConfiguration(exchange,
                                                                                  exchangeType,
                                                                                  createExchangeIfNotExist,
                                                                                  createDeadLetterExchangeIfNotExist,
                                                                                  isExchangeAutoDelete,
                                                                                  isExchangeDurable);

            var onMessage = new Func <IRabbitMqQueueMessage, Task>((@event) =>
            {
                actor.OnMessageReceived(@event);

                return(Task.CompletedTask);
            });

            var rabbitMqSubscription = new RabbitMqEventSubscription <TEvent>(onMessage,
                                                                              rabbitMqQueueConfiguration,
                                                                              rabbitMqExchangeConfiguration);

            rabbitMqBus.SubscribeToExchange(rabbitMqSubscription);

            var disposable = Disposable.Create(() => rabbitMqBus.UnsubscribeFromExchange(rabbitMqSubscription));

            actor.AddDisposable(disposable);
        }
예제 #2
0
        public static IObservable <TEvent> SubscribeToExchange <TEvent>(this IRabbitMqBus rabbitMqBus,
                                                                        string exchange,
                                                                        string queueName          = "",
                                                                        string exchangeType       = "topic",
                                                                        bool isExchangeDurable    = true,
                                                                        bool isExchangeAutoDelete = true,
                                                                        bool isQueueDurable       = false,
                                                                        bool isQueueAutoAck       = false,
                                                                        bool isQueueAutoDelete    = true,
                                                                        bool isQueueExclusive     = true,
                                                                        Expression <Func <TEvent, bool> >?routingStrategy = null)
            where TEvent : class, IRabbitMqEvent
        {
            var rabbitMqQueueConfiguration = new RabbitMqQueueConfiguration <TEvent>(routingStrategy,
                                                                                     queueName,
                                                                                     isQueueAutoAck,
                                                                                     isQueueDurable,
                                                                                     isQueueAutoDelete,
                                                                                     isQueueExclusive);

            var rabbitMqExchangeConfiguration = new RabbitMqExchangeConfiguration(exchange,
                                                                                  exchangeType,
                                                                                  true,
                                                                                  true,
                                                                                  isExchangeAutoDelete,
                                                                                  isExchangeDurable);

            var observable = Observable.Create <TEvent>((observer) =>
            {
                var rabbitMqSubscription = new RabbitMqEventSubscription <TEvent>((@event) =>
                {
                    observer.OnNext((TEvent)@event.Content);

                    return(Task.CompletedTask);
                }, rabbitMqQueueConfiguration, rabbitMqExchangeConfiguration);

                rabbitMqBus.SubscribeToExchange(rabbitMqSubscription);

                return(Disposable.Create(() => rabbitMqBus.UnsubscribeFromExchange(rabbitMqSubscription)));
            });

            return(observable);
        }