Exemple #1
0
        public void Subscribe <TPayload>(IMessage message, Func <IPayloadWrapper <TPayload>, Task <HandleResult> > handler,
                                         SubscriptionType type = SubscriptionType.SharedBetweenConsumers, SubscriptionConfig configuration = null)
            where TPayload : IPayload
        {
            var qName = InitializeQueue <TPayload>(message, type, configuration, out var channel, out _);

            var concurrencyLimit = configuration?.ConcurrencyLimit ?? _config.ConcurrencyLimit;

            var consumer = CreateConsumer(channel, handler);

            channel.BasicQos(0, (ushort)concurrencyLimit, false);
            channel.BasicConsume(queue: qName, autoAck: false, consumer: consumer);
        }
Exemple #2
0
        private string InitializeQueue <TPayload>(IMessage message, SubscriptionType type, SubscriptionConfig configuration,
                                                  out IModel channel, out QueueDeclareOk declaration) where TPayload : IPayload
        {
            if (!_isInitialized)
            {
                throw new InvalidOperationException("Not initialized yet, " +
                                                    "don't forget to call 'Init()' method at least once");
            }

            var consumerName = _consumerName;
            var autoDelete   = false;

            if (type == SubscriptionType.PerConsumer)
            {
                autoDelete   = true;
                consumerName = $"{consumerName}.{INSTANCE_IDENTIFICATION}";
            }

            var qName = RabbitConsts.GenerateQueueName(
                RabbitConsts.QueuePrefix,
                RabbitConsts.QueueConsumerPrefix,
                consumerName,
                message.RoutingKey);

            channel = GetChannel();

            var arguments        = configuration?.QueueArguments;
            var createDeadLetter = configuration?.CreateDeadLetterQueue ?? _config.CreateDeadLetterQueue ?? false;

            if (createDeadLetter)
            {
                arguments = ConfigureDeadLetter(channel, qName, arguments);
            }

            declaration = channel.QueueDeclare(queue: qName, durable: true, exclusive: false, autoDelete: autoDelete, arguments: arguments);
            channel.QueueBind(queue: qName, exchange: RabbitConsts.ExchangeName, routingKey: message.RoutingKey);
            return(qName);
        }
Exemple #3
0
 public void Subscribe <TPayload>(IMessage message, Func <IPayloadWrapper <TPayload>, Task <HandleResult> > handler,
                                  SubscriptionType type = SubscriptionType.SharedBetweenConsumers, SubscriptionConfig configuration = null) where TPayload : IPayload
 {
     LogMessage($"Subscribing to message '{message?.GetType().Name}', type: {type}");
 }