Exemple #1
0
        public void SubscribeAsync <T>(string subscriptionId, Func <T, Task> onMessage, Action <ISubscriptionConfiguration <T> > configure)
        {
            if (subscriptionId == null)
            {
                throw new ArgumentNullException("subscriptionId");
            }

            if (onMessage == null)
            {
                throw new ArgumentNullException("onMessage");
            }

            var configuration = new SubscriptionConfiguration <T>();

            configure(configuration);

            var queueName    = GetQueueName <T>(subscriptionId);
            var exchangeName = GetExchangeName <T>();

            var queue    = Queue.DeclareDurable(queueName, configuration.Arguments);
            var exchange = Exchange.DeclareTopic(exchangeName);

            queue.BindTo(exchange, configuration.Topics.ToArray());

            advancedBus.Subscribe <T>(queue, (message, messageRecievedInfo) => onMessage(message.Body));
        }
Exemple #2
0
        public virtual void SubscribeAsync <T>(string subscriptionId, Func <T, Task> onMessage, Action <ISubscriptionConfiguration <T> > configure)
        {
            Preconditions.CheckNotNull(subscriptionId, "subscriptionId");
            Preconditions.CheckNotNull(onMessage, "onMessage");

            var configuration = new SubscriptionConfiguration <T>();

            configure(configuration);

            var queueName    = GetQueueName <T>(subscriptionId);
            var exchangeName = GetExchangeName <T>();

            var queue    = advancedBus.QueueDeclare(queueName);
            var exchange = advancedBus.ExchangeDeclare(exchangeName, ExchangeType.Topic);

            if (configuration.Topics.Count == 0)
            {
                advancedBus.Bind(exchange, queue, "#");
            }
            else
            {
                foreach (var topic in configuration.Topics)
                {
                    advancedBus.Bind(exchange, queue, topic);
                }
            }

            advancedBus.Consume <T>(queue, (message, messageRecievedInfo) => onMessage(message.Body));
        }
Exemple #3
0
        public virtual IDisposable SubscribeAsync <T>(string subscriptionId, Func <T, Task> onMessage, Action <ISubscriptionConfiguration> configure) where T : class
        {
            Preconditions.CheckNotNull(subscriptionId, "subscriptionId");
            Preconditions.CheckNotNull(onMessage, "onMessage");
            Preconditions.CheckNotNull(configure, "configure");

            var configuration = new SubscriptionConfiguration(connectionConfiguration.PrefetchCount);

            configure(configuration);

            var queueName    = conventions.QueueNamingConvention(typeof(T), subscriptionId);
            var exchangeName = conventions.ExchangeNamingConvention(typeof(T));

            var queue    = advancedBus.QueueDeclare(queueName, autoDelete: configuration.AutoDelete, expires: configuration.Expires);
            var exchange = advancedBus.ExchangeDeclare(exchangeName, ExchangeType.Topic);

            foreach (var topic in configuration.Topics.AtLeastOneWithDefault("#"))
            {
                advancedBus.Bind(exchange, queue, topic);
            }

            return(advancedBus.Consume <T>(
                       queue,
                       (message, messageReceivedInfo) => onMessage(message.Body),
                       x =>
            {
                x.WithPriority(configuration.Priority)
                .WithCancelOnHaFailover(configuration.CancelOnHaFailover)
                .WithPrefetchCount(configuration.PrefetchCount);
                if (configuration.IsExclusive)
                {
                    x.AsExclusive();
                }
            }));
        }
Exemple #4
0
        public virtual void SubscribeAsync <T>(string subscriptionId, Func <T, Task> onMessage, Action <ISubscriptionConfiguration <T> > configure)
        {
            Preconditions.CheckNotNull(subscriptionId, "subscriptionId");
            Preconditions.CheckNotNull(onMessage, "onMessage");

            var configuration = new SubscriptionConfiguration <T>();

            configure(configuration);

            var queueName    = GetQueueName <T>(subscriptionId);
            var exchangeName = GetExchangeName <T>();

            var queue    = Queue.DeclareDurable(queueName, configuration.Arguments);
            var exchange = Exchange.DeclareTopic(exchangeName);

            var topics = configuration.Topics.ToArray();

            if (topics.Length == 0)
            {
                topics = new[] { "#" };
            }

            queue.BindTo(exchange, topics);

            advancedBus.Subscribe <T>(queue, (message, messageRecievedInfo) => onMessage(message.Body));
        }
Exemple #5
0
        public virtual ISubscriptionResult SubscribeAsync <T>(string subscriptionId, Func <T, Task> onMessage, Action <ISubscriptionConfiguration> configure) where T : class
        {
            Preconditions.CheckNotNull(subscriptionId, "subscriptionId");
            Preconditions.CheckNotNull(onMessage, "onMessage");
            Preconditions.CheckNotNull(configure, "configure");

            var configuration = new SubscriptionConfiguration(connectionConfiguration.PrefetchCount);

            configure(configuration);

            var queueName    = configuration.QueueName ?? conventions.QueueNamingConvention(typeof(T), subscriptionId);
            var exchangeName = conventions.ExchangeNamingConvention(typeof(T));

            var queue = advancedBus.QueueDeclare(
                queueName,
                autoDelete: configuration.AutoDelete,
                durable: configuration.Durable,
                expires: configuration.Expires,
                maxPriority: configuration.MaxPriority,
                maxLength: configuration.MaxLength,
                maxLengthBytes: configuration.MaxLengthBytes,
                consumerRepariable: true);
            var exchange = advancedBus.ExchangeDeclare(exchangeName, ExchangeType.Topic);

            foreach (var topic in configuration.Topics.DefaultIfEmpty("#"))
            {
                advancedBus.Bind(exchange, queue, topic);
            }

            var consumerCancellation = advancedBus.Consume <T>(
                queue,
                (message, messageReceivedInfo) => onMessage(message.Body),
                x =>
            {
                x.WithPriority(configuration.Priority)
                .WithCancelOnHaFailover(configuration.CancelOnHaFailover)
                .WithPrefetchCount(configuration.PrefetchCount);
                if (configuration.IsExclusive)
                {
                    x.AsExclusive();
                }
            });

            return(new SubscriptionResult(exchange, queue, consumerCancellation));
        }
Exemple #6
0
        public virtual IDisposable SubscribeAsync <T>(string subscriptionId, Func <T, Task> onMessage, Action <ISubscriptionConfiguration> configure) where T : class
        {
            Preconditions.CheckNotNull(subscriptionId, "subscriptionId");
            Preconditions.CheckNotNull(onMessage, "onMessage");
            Preconditions.CheckNotNull(configure, "configure");

            var configuration = new SubscriptionConfiguration();

            configure(configuration);

            var queueName    = conventions.QueueNamingConvention(typeof(T), subscriptionId);
            var exchangeName = conventions.ExchangeNamingConvention(typeof(T));

            var queue    = advancedBus.QueueDeclare(queueName, autoDelete: configuration.AutoDelete);
            var exchange = advancedBus.ExchangeDeclare(exchangeName, ExchangeType.Topic);

            foreach (var topic in configuration.Topics.AtLeastOneWithDefault("#"))
            {
                advancedBus.Bind(exchange, queue, topic);
            }

            return(advancedBus.Consume <T>(queue, (message, messageRecievedInfo) => onMessage(message.Body)));
        }
Exemple #7
0
        private async Task <ISubscriptionResult> SubscribeAsyncInternal <T>(
            string subscriptionId,
            Func <T, CancellationToken, Task> onMessage,
            Action <ISubscriptionConfiguration> configure,
            CancellationToken cancellationToken
            )
        {
            using var cts = cancellationToken.WithTimeout(configuration.Timeout);

            var subscriptionConfiguration = new SubscriptionConfiguration(configuration.PrefetchCount);

            configure(subscriptionConfiguration);

            var queueName    = subscriptionConfiguration.QueueName ?? conventions.QueueNamingConvention(typeof(T), subscriptionId);
            var exchangeName = conventions.ExchangeNamingConvention(typeof(T));

            var queue = await advancedBus.QueueDeclareAsync(
                queueName,
                c =>
            {
                c.AsDurable(subscriptionConfiguration.Durable);
                c.AsAutoDelete(subscriptionConfiguration.AutoDelete);
                if (subscriptionConfiguration.Expires.HasValue)
                {
                    c.WithExpires(TimeSpan.FromMilliseconds(subscriptionConfiguration.Expires.Value));
                }
                if (subscriptionConfiguration.MaxPriority.HasValue)
                {
                    c.WithMaxPriority(subscriptionConfiguration.MaxPriority.Value);
                }
                if (subscriptionConfiguration.MaxLength.HasValue)
                {
                    c.WithMaxLength(subscriptionConfiguration.MaxLength.Value);
                }
                if (subscriptionConfiguration.MaxLengthBytes.HasValue)
                {
                    c.WithMaxLengthBytes(subscriptionConfiguration.MaxLengthBytes.Value);
                }
                if (!string.IsNullOrEmpty(subscriptionConfiguration.QueueMode))
                {
                    c.WithQueueMode(subscriptionConfiguration.QueueMode);
                }
            },
                cts.Token
                ).ConfigureAwait(false);

            var exchange = await advancedBus.ExchangeDeclareAsync(exchangeName, ExchangeType.Topic, cancellationToken : cts.Token).ConfigureAwait(false);

            foreach (var topic in subscriptionConfiguration.Topics.DefaultIfEmpty("#"))
            {
                await advancedBus.BindAsync(exchange, queue, topic, cts.Token).ConfigureAwait(false);
            }

            var consumerCancellation = advancedBus.Consume <T>(
                queue,
                (message, messageReceivedInfo) => onMessage(message.Body, default),
                x => x.WithPriority(subscriptionConfiguration.Priority)
                .WithPrefetchCount(subscriptionConfiguration.PrefetchCount)
                .WithExclusive(subscriptionConfiguration.IsExclusive)
                );

            return(new SubscriptionResult(exchange, queue, consumerCancellation));
        }