예제 #1
0
        public IConsumerManager Create(IConsumerConfiguration configuration, IDependencyResolver resolver)
        {
            var logHandler = resolver.Resolve <ILogHandler>();

            var middlewares = configuration.MiddlewareConfiguration.Factories
                              .Select(factory => factory(resolver))
                              .ToList();

            var consumer = new Consumer(configuration, logHandler);

            var consumerWorkerPool = new ConsumerWorkerPool(
                consumer,
                resolver,
                logHandler,
                new MiddlewareExecutor(middlewares),
                configuration.DistributionStrategyFactory);

            var feeder = new WorkerPoolFeeder(
                consumer,
                consumerWorkerPool,
                logHandler);

            var consumerManager = new ConsumerManager(
                consumer,
                consumerWorkerPool,
                feeder,
                logHandler);

            return(consumerManager);
        }
예제 #2
0
        public IConsumerManager Create(IConsumerConfiguration configuration, IDependencyResolver resolver)
        {
            var logHandler = resolver.Resolve <ILogHandler>();

            var consumer = configuration.CustomFactory(new Consumer(configuration, resolver, logHandler), resolver);

            var consumerWorkerPool = new ConsumerWorkerPool(
                consumer,
                resolver,
                new MiddlewareExecutor(configuration.MiddlewaresConfigurations),
                configuration,
                logHandler);

            var feeder = new WorkerPoolFeeder(
                consumer,
                consumerWorkerPool,
                logHandler);

            var consumerManager = new ConsumerManager(
                consumer,
                consumerWorkerPool,
                feeder,
                logHandler);

            return(consumerManager);
        }
예제 #3
0
  public ExclusiveConsumer(
      IQueue queue,
      Func<byte[], MessageProperties, MessageReceivedInfo, Task> onMessage,
      IPersistentConnection connection,
      IConsumerConfiguration configuration,
      IInternalConsumerFactory internalConsumerFactory,
      IEventBus eventBus
      )
  {
      Preconditions.CheckNotNull(queue, "queue");
      Preconditions.CheckNotNull(onMessage, "onMessage");
      Preconditions.CheckNotNull(connection, "connection");
      Preconditions.CheckNotNull(internalConsumerFactory, "internalConsumerFactory");
      Preconditions.CheckNotNull(eventBus, "eventBus");
      Preconditions.CheckNotNull(configuration, "configuration");
 
      this.queue = queue;
      this.onMessage = onMessage;
      this.connection = connection;
      this.configuration = configuration;
      this.internalConsumerFactory = internalConsumerFactory;
      this.eventBus = eventBus;
      timer = new Timer(s =>
          {
              StartConsumer();
              ((Timer)s).Change(10000, -1);
          });
      timer.Change(10000, -1);
  }
        public void SetUp()
        {
            eventBus = new EventBus();
            internalConsumers = new List<IInternalConsumer>();

            createConsumerCalled = 0;
            mockBuilder = new MockBuilder();

            queue = new Queue(queueName, false);
            onMessage = (body, properties, info) => Task.Factory.StartNew(() => { });

            persistentConnection = MockRepository.GenerateStub<IPersistentConnection>();

            internalConsumerFactory = MockRepository.GenerateStub<IInternalConsumerFactory>();
            
            internalConsumerFactory.Stub(x => x.CreateConsumer()).WhenCalled(x =>
                {
                    var internalConsumer = MockRepository.GenerateStub<IInternalConsumer>();
                    internalConsumers.Add(internalConsumer);
                    createConsumerCalled++;
                    x.ReturnValue = internalConsumer;
                }).Repeat.Any();
            configuration = new ConsumerConfiguration(0);
            consumer = new PersistentConsumer(
                queue,
                onMessage,
                persistentConnection,
                configuration,
                internalConsumerFactory, 
                eventBus);

            AdditionalSetup();
        }
예제 #5
0
        private void WireUpConsumer(IRawConsumer consumer, IConsumerConfiguration cfg)
        {
            consumer.OnMessageAsync = (o, args) =>
            {
                ResponseCompletionSource responseTcs = null;
                return(_errorStrategy.ExecuteAsync(() =>
                {
                    if (_responseDictionary.TryRemove(args.BasicProperties.CorrelationId, out responseTcs))
                    {
                        _logger.LogDebug($"Recived response with correlationId {args.BasicProperties.CorrelationId}.");
                        responseTcs.RequestTimer.Dispose();

                        _errorStrategy.OnResponseRecievedAsync(args, responseTcs);
                        if (responseTcs.Task.IsFaulted)
                        {
                            return _completed;
                        }
                        var response = _serializer.Deserialize(args);
                        responseTcs.TrySetResult(response);
                        return _completed;
                    }
                    _logger.LogWarning($"Unable to find callback for {args.BasicProperties.CorrelationId}.");
                    return _completed;
                }, exception => _errorStrategy.OnResponseRecievedException(consumer, cfg, args, responseTcs, exception)));
            };
        }
예제 #6
0
        public void SetUp()
        {
            eventBus          = new EventBus();
            internalConsumers = new List <IInternalConsumer>();

            createConsumerCalled = 0;
            mockBuilder          = new MockBuilder();

            queue     = new Queue(queueName, false);
            onMessage = (body, properties, info) => Task.Factory.StartNew(() => { });

            persistentConnection = MockRepository.GenerateStub <IPersistentConnection>();

            internalConsumerFactory = MockRepository.GenerateStub <IInternalConsumerFactory>();

            internalConsumerFactory.Stub(x => x.CreateConsumer()).WhenCalled(x =>
            {
                var internalConsumer = MockRepository.GenerateStub <IInternalConsumer>();
                internalConsumers.Add(internalConsumer);
                createConsumerCalled++;
                x.ReturnValue = internalConsumer;
            }).Repeat.Any();
            configuration = new ConsumerConfiguration();
            consumer      = new PersistentConsumer(
                queue,
                onMessage,
                persistentConnection,
                configuration,
                internalConsumerFactory,
                eventBus);

            AdditionalSetup();
        }
예제 #7
0
        public QueueConsumer(IQueueConfiguration queueConfiguration,
                             IQueueConnectionFactory connectionFactory,
                             IJsonSerializer serializer,
                             IValidationHelper validationHelper,
                             string consumerName)
        {
            _queueConfiguration = queueConfiguration ?? throw new ArgumentNullException(nameof(queueConfiguration));
            _connectionFactory  = connectionFactory ?? throw new ArgumentNullException(nameof(connectionFactory));
            _serializer         = serializer ?? throw new ArgumentNullException(nameof(serializer));
            _validationHelper   = validationHelper ?? throw new ArgumentNullException(nameof(validationHelper));

            if (string.IsNullOrEmpty(consumerName))
            {
                throw new ArgumentNullException(nameof(consumerName));
            }

            _consumerName = consumerName;

            // verify that the queue configuration is valid
            if (!queueConfiguration.IsValid)
            {
                throw new ArgumentException("Queue Configuration is not valid", nameof(queueConfiguration));
            }

            // retrieve the specific queues configuration
            _consumerConfig = queueConfiguration.Consumers?.FirstOrDefault(c => c.Name == _consumerName);

            if (_consumerConfig == null)
            {
                throw new ArgumentNullException(nameof(_consumerConfig));
            }

            this._performanceLoggingMethodName = $"{consumerName}.Run";
        }
예제 #8
0
        public virtual Task OnResponseHandlerExceptionAsync(IRawConsumer rawConsumer, IConsumerConfiguration cfg, BasicDeliverEventArgs args, Exception exception)
        {
            _logger.LogError($"An unhandled exception was thrown in the response handler for message '{args.BasicProperties.MessageId}'.", exception);
            var innerException = UnwrapInnerException(exception);
            var rawException = new MessageHandlerException(
                message: $"An unhandled exception was thrown when consuming a message\n  MessageId: {args.BasicProperties.MessageId}\n  Queue: '{cfg.Queue.FullQueueName}'\n  Exchange: '{cfg.Exchange.ExchangeName}'\nSee inner exception for more details.",
                inner: innerException
            );

            _logger.LogInformation($"Sending MessageHandlerException with CorrelationId '{args.BasicProperties.CorrelationId}'");
            rawConsumer.Model.BasicPublish(
                exchange: string.Empty,
                routingKey: args.BasicProperties?.ReplyTo ?? string.Empty,
                basicProperties: _propertiesProvider.GetProperties<MessageHandlerException>(p =>
                {
                    p.CorrelationId = args.BasicProperties?.CorrelationId ?? string.Empty;
                    p.Headers.Add(PropertyHeaders.ExceptionHeader, _messageExceptionName);
                }),
                body: _serializer.Serialize(rawException)
            );

            if (!cfg.NoAck)
            {
                _logger.LogDebug($"Nack'ing message with delivery tag '{args.DeliveryTag}'.");
                rawConsumer.Model.BasicNack(args.DeliveryTag, false, false);
            }
            return Task.FromResult(true);
        }
예제 #9
0
 public ConsumerIntegrationTests()
 {
     _kafkaUri       = TestConfig.IntegrationUri;
     _config         = new ConnectionConfiguration(TimeSpan.FromSeconds(10));
     _options        = new KafkaOptions(TestConfig.IntegrationUri, _config, log: TestConfig.InfoLog);
     _consumerConfig = new ConsumerConfiguration(maxPartitionFetchBytes: DefaultMaxMessageSetSize);
 }
예제 #10
0
        public ExclusiveConsumer(
            IQueue queue,
            Func <byte[], MessageProperties, MessageReceivedInfo, Task> onMessage,
            IPersistentConnection connection,
            IConsumerConfiguration configuration,
            IInternalConsumerFactory internalConsumerFactory,
            IEventBus eventBus
            )
        {
            Preconditions.CheckNotNull(queue, "queue");
            Preconditions.CheckNotNull(onMessage, "onMessage");
            Preconditions.CheckNotNull(connection, "connection");
            Preconditions.CheckNotNull(internalConsumerFactory, "internalConsumerFactory");
            Preconditions.CheckNotNull(eventBus, "eventBus");
            Preconditions.CheckNotNull(configuration, "configuration");

            this.queue                   = queue;
            this.onMessage               = onMessage;
            this.connection              = connection;
            this.configuration           = configuration;
            this.internalConsumerFactory = internalConsumerFactory;
            this.eventBus                = eventBus;
            timer = new Timer(s =>
            {
                StartConsumer();
                ((Timer)s).Change(10000, -1);
            });
            timer.Change(10000, -1);
        }
예제 #11
0
        public Given_a_PersistentConsumer()
        {
            eventBus          = new EventBus();
            internalConsumers = new List <IInternalConsumer>();

            createConsumerCalled = 0;
            mockBuilder          = new MockBuilder();

            queue     = new Queue(queueName, false);
            onMessage = (body, properties, info) => Task.Factory.StartNew(() => { });

            persistentConnection    = Substitute.For <IPersistentConnection>();
            internalConsumerFactory = Substitute.For <IInternalConsumerFactory>();

            internalConsumerFactory.CreateConsumer().Returns(x =>
            {
                var internalConsumer = Substitute.For <IInternalConsumer>();
                internalConsumers.Add(internalConsumer);
                createConsumerCalled++;
                return(internalConsumer);
            });
            configuration = new ConsumerConfiguration(0);
            consumer      = new PersistentConsumer(
                queue,
                onMessage,
                persistentConnection,
                configuration,
                internalConsumerFactory,
                eventBus);

            AdditionalSetup();
        }
예제 #12
0
        public QueueConsumer(IQueueConfiguration queueConfiguration,
                             IQueueConnectionFactory connectionFactory,
                             IJsonSerializer serializer,
                             string consumerName,
                             CancellationToken cancellationToken)
        {
            _queueConfiguration = queueConfiguration ?? throw new ArgumentNullException(nameof(queueConfiguration));
            _connectionFactory  = connectionFactory ?? throw new ArgumentNullException(nameof(connectionFactory));
            _consumerName       = consumerName ?? throw new ArgumentNullException(nameof(consumerName));
            _serializer         = serializer ?? throw new ArgumentNullException(nameof(serializer));

            if (cancellationToken == null)
            {
                throw new ArgumentNullException(nameof(cancellationToken));
            }

            _cancellationToken = cancellationToken;

            // retrieve the specific queues configuration
            _consumerConfig = queueConfiguration.Consumers?.FirstOrDefault(c => c.Name == _consumerName);

            if (_consumerConfig == null)
            {
                throw new ArgumentNullException(nameof(_consumerConfig));
            }
        }
예제 #13
0
        public IRawConsumer CreateConsumer(IConsumerConfiguration cfg, IModel channel)
        {
            ConfigureQos(channel, cfg.PrefetchCount);
            var rawConsumer = new EventingRawConsumer(channel);

            rawConsumer.Received += (sender, args) =>
            {
                Task.Run(() =>
                {
                    if (_processedButNotAcked.Contains(args.BasicProperties.MessageId))
                    {
                        /*
                         *      This instance of the consumer has allready handled this message,
                         *      but something went wrong when 'ack'-ing the message, therefore
                         *      and the message was resent.
                         */
                        BasicAck(channel, args);
                        return;
                    }
                    _logger.LogInformation($"Message recived: MessageId: {args.BasicProperties.MessageId}");
                    rawConsumer
                    .OnMessageAsync(sender, args)
                    .ContinueWith(t =>
                    {
                        if (cfg.NoAck || rawConsumer.AcknowledgedTags.Contains(args.DeliveryTag))
                        {
                            return;
                        }
                        BasicAck(channel, args);
                    });
                });
            };

            return(rawConsumer);
        }
        public IRawConsumer CreateConsumer(IConsumerConfiguration cfg, IModel channel)
        {
            ConfigureQos(channel, cfg.PrefetchCount);
            var rawConsumer = new EventingRawConsumer(channel);

            rawConsumer.Received += (sender, args) =>
            {
                Task.Run(() =>
                {
                    if (_processedButNotAcked.Contains(args.BasicProperties.MessageId))
                    {
                        /*
                            This instance of the consumer has allready handled this message,
                            but something went wrong when 'ack'-ing the message, therefore
                            and the message was resent.
                        */
                        BasicAck(channel, args);
                        return;
                    }
                    _logger.LogInformation($"Message recived: MessageId: {args.BasicProperties.MessageId}");
                    rawConsumer
                        .OnMessageAsync(sender, args)
                        .ContinueWith(t =>
                        {
                            if (cfg.NoAck || rawConsumer.AcknowledgedTags.Contains(args.DeliveryTag))
                            {
                                return;
                            }
                            BasicAck(channel, args);
                        });
                });
            };

            return rawConsumer;
        }
 public EventsConsumer(ILogger <EventsConsumer> logger,
                       IConsumerConfiguration consumerConfiguration,
                       ILoggerFactory loggerFactory)
 {
     _logger = logger;
     _consumerConfiguration = consumerConfiguration;
     _loggerFactory         = loggerFactory;
 }
예제 #16
0
 public Consumer(IBrokerRouter brokerRouter, IConsumerConfiguration configuration = null, bool leaveRouterOpen = true)
 {
     _stopToken       = new CancellationTokenSource();
     _brokerRouter    = brokerRouter;
     _leaveRouterOpen = leaveRouterOpen;
     Configuration    = configuration ?? new ConsumerConfiguration();
     _localMessages   = ImmutableList <Message> .Empty;
 }
예제 #17
0
        public TopicSubscriberScope CreateTopicSubscriberScope(IConsumerConfiguration configuration)
        {
            var consumer = new ConsumerBuilder <string, string>(configuration).Build();

            consumer.Subscribe(configuration.SubscribedTopics);

            return(new KafkaConsumerScope(consumer));
        }
예제 #18
0
        public StartConsumingStatus StartConsuming(
            IPersistentConnection connection,
            IQueue queue,
            Func <byte[], MessageProperties, MessageReceivedInfo, Task> onMessage,
            IConsumerConfiguration configuration
            )
        {
            Preconditions.CheckNotNull(connection, "connection");
            Preconditions.CheckNotNull(queue, "queue");
            Preconditions.CheckNotNull(onMessage, "onMessage");
            Preconditions.CheckNotNull(configuration, "configuration");

            var consumerTag = conventions.ConsumerTagConvention();
            IDictionary <string, object> arguments = new Dictionary <string, object>
            {
                { "x-priority", configuration.Priority }
            };

            try
            {
                Model = connection.CreateModel();

                var basicConsumer = new BasicConsumer(SingleBasicConsumerCancelled, consumerDispatcher, queue, eventBus, handlerRunner, onMessage, Model);

                basicConsumers = new[] { basicConsumer };

                Model.BasicQos(0, configuration.PrefetchCount, false);

                Model.BasicConsume(
                    queue.Name,         // queue
                    false,              // noAck
                    consumerTag,        // consumerTag
                    true,
                    configuration.IsExclusive,
                    arguments,          // arguments
                    basicConsumer);     // consumer

                logger.InfoFormat(
                    "Declared consumer with consumerTag {consumerTag} on queue {queue} and configuration {configuration}",
                    consumerTag,
                    queue.Name,
                    configuration
                    );


                return(StartConsumingStatus.Succeed);
            }
            catch (Exception exception)
            {
                logger.Error(
                    exception,
                    "Consume with consumerTag {consumerTag} from queue {queue} has failed",
                    consumerTag,
                    queue.Name
                    );
                return(StartConsumingStatus.Failed);
            }
        }
예제 #19
0
 private PactBuilder(string description, IConsumerConfiguration config)
 {
     if (string.IsNullOrWhiteSpace(description))
     {
         throw new ArgumentException("Please provide a description", nameof(description));
     }
     this._description = description;
     _configuration    = MergedConfiguration.MergeConfigs(Context.Configuration, config);
 }
예제 #20
0
        public Consumer(IEnumerable <TopicPartition> partitions, IRouter router, IConsumerConfiguration configuration = null, bool?leaveRouterOpen = null, bool?autoConsume = null)
        {
            Router           = router;
            _topicPartitions = ImmutableList <TopicPartition> .Empty.AddNotNullRange(partitions);

            _leaveRouterOpen = leaveRouterOpen.GetValueOrDefault(true);
            AutoConsume      = autoConsume.GetValueOrDefault(true);
            Configuration    = configuration ?? ConsumerConfiguration.Default;
        }
        public IRawConsumer CreateConsumer(IConsumerConfiguration cfg, IModel channel)
        {
            ConfigureQos(channel, cfg.PrefetchCount);
            var rawConsumer = new EventingRawConsumer(channel);

            _consumers.Add(rawConsumer);

            rawConsumer.Received += (sender, args) =>
            {
                if (_processedButNotAcked.Contains(args.BasicProperties.MessageId))
                {
                    /*
                        This instance of the consumer has allready handled this message,
                        but something went wrong when 'ack'-ing the message, therefore
                        and the message was resent.
                    */
                    BasicAck(channel, args, cfg);
                    return;
                }

                Task onMessageTask;
                try
                {
                    _logger.LogInformation($"Message recived: MessageId: {args.BasicProperties.MessageId}");
                    onMessageTask = rawConsumer.OnMessageAsync(sender, args);
                }
                catch (Exception)
                {
                    /*
                        The message handler threw an exception. It is time to hand over the
                        message handling to an error strategy instead.
                    */
                    if (!cfg.NoAck || rawConsumer.NackedDeliveryTags.Contains(args.DeliveryTag))
                    {
                        BasicAck(channel, args, cfg); // TODO: employ error handling strategy instead
                    }
                    return;
                }
                onMessageTask
                    .ContinueWith(t =>
                    {
                        if (cfg.NoAck || rawConsumer.NackedDeliveryTags.Contains(args.DeliveryTag))
                        {
                            /*
                                The consumer has stated that 'ack'-ing is not required, so
                                now that the message is handled, the consumer is done.
                            */
                            return;
                        }

                        BasicAck(channel, args, cfg);
                    });
            };

            return rawConsumer;
        }
        public IRawConsumer CreateConsumer(IConsumerConfiguration cfg, IModel channel)
        {
            ConfigureQos(channel, cfg.PrefetchCount);
            var rawConsumer = new EventingRawConsumer(channel);

            _consumers.Add(rawConsumer);

            rawConsumer.Received += (sender, args) =>
            {
                Task.Run(() =>
                {
                    if (_processedButNotAcked.Contains(args.BasicProperties.MessageId))
                    {
                        /*
                            This instance of the consumer has allready handled this message,
                            but something went wrong when 'ack'-ing the message, therefore
                            and the message was resent.
                        */
                        BasicAck(channel, args, cfg);
                        return;
                    }
                    _logger.LogInformation($"Message recived: MessageId: {args.BasicProperties.MessageId}");
                    try
                    {
                        rawConsumer
                            .OnMessageAsync(sender, args)
                            .ContinueWith(t =>
                            {
                                if (t.IsFaulted)
                                {
                                    _logger.LogError($"An unhandled exception was caught for message {args.BasicProperties.MessageId}.\n", t.Exception);
                                    _strategy.OnRequestHandlerExceptionAsync(rawConsumer, cfg, args, t.Exception);
                                    return;
                                }
                                if (cfg.NoAck || rawConsumer.NackedDeliveryTags.Contains(args.DeliveryTag))
                                {
                                /*
                                    The consumer has stated that 'ack'-ing is not required, so
                                    now that the message is handled, the consumer is done.
                                */
                                    return;
                                }

                                BasicAck(channel, args, cfg);
                        });
                    }
                    catch (Exception e)
                    {
                        _logger.LogError($"An unhandled exception was caught for message {args.BasicProperties.MessageId}.\n", e);
                        _strategy.OnRequestHandlerExceptionAsync(rawConsumer, cfg, args, e);
                    }
                });
            };

            return rawConsumer;
        }
예제 #23
0
 public KafkaOptions(Uri kafkaServerUri = null,
                     IConnectionConfiguration connectionConfiguration = null,
                     IRouterConfiguration routerConfiguration         = null,
                     IConnectionFactory connectionFactory             = null,
                     IProducerConfiguration producerConfiguration     = null,
                     IConsumerConfiguration consumerConfiguration     = null,
                     ILog log = null)
     : this(ImmutableList <Uri> .Empty.AddNotNull(kafkaServerUri), connectionConfiguration, routerConfiguration, connectionFactory, producerConfiguration, consumerConfiguration, log)
 {
 }
예제 #24
0
 public PactHandler(
     IPactInteractionDefinition pact,
     IConsumerConfiguration config,
     Action <PactHandler> unregister)
 {
     this.matcher    = new PactComparer(pact, config);
     this.pact       = pact;
     Configuration   = config;
     this.unregister = () => unregister(this);
 }
예제 #25
0
 /// <summary>
 /// Merges two configurations, letting <paramref name="right"/> override <paramref name="left"/>.
 /// </summary>
 /// <param name="left">The original config. May be <c>null</c>.</param>
 /// <param name="right">The overriding config. May be <c>null</c>.</param>
 /// <returns>If any parameter is null, the method may return the other parameter. Otherwise a merged configuration object.</returns>
 public static IConsumerConfiguration MergeConfigs(IConsumerConfiguration left, IConsumerConfiguration right)
 {
     if (left == null)
     {
         return(right);
     }
     else
     {
         return(right == null ? left : new MergedConfiguration(left, right));
     }
 }
예제 #26
0
        public IConsumer CreateConsumer(
            IQueue queue,
            Func <byte[], MessageProperties, MessageReceivedInfo, Task> onMessage,
            IPersistentConnection connection,
            IConsumerConfiguration configuration)
        {
            var consumer = CreateConsumerInstance(queue, onMessage, connection, configuration);

            consumers.TryAdd(consumer, null);
            return(consumer);
        }
예제 #27
0
 private void InitIConsumerConfiguration(IConsumerConfiguration consumerConfigure)
 {
     consumerConfigure
     .WithPriority(subConfigure.Priority)
     .WithCancelOnHaFailover(subConfigure.CancelOnHaFailover)
     .WithPrefetchCount(subConfigure.PrefetchCount);
     if (subConfigure.IsExclusive)
     {
         consumerConfigure.AsExclusive();
     }
 }
예제 #28
0
 public KafkaOptions(
     Uri kafkaServerUri = null,
     IConnectionConfiguration connectionConfiguration = null,
     ICacheConfiguration cacheConfiguration           = null,
     IConnectionFactory connectionFactory             = null,
     IPartitionSelector partitionSelector             = null,
     IProducerConfiguration producerConfiguration     = null,
     IConsumerConfiguration consumerConfiguration     = null,
     ILog log = null)
     : this(ImmutableList <Uri> .Empty.AddNotNull(kafkaServerUri), connectionConfiguration, cacheConfiguration, connectionFactory, partitionSelector, producerConfiguration, consumerConfiguration, log)
 {
 }
예제 #29
0
        public IConsumer CreateConsumer(
            ICollection <Tuple <IQueue, Func <byte[], MessageProperties, MessageReceivedInfo, Task> > > queueConsumerPairs,
            IPersistentConnection connection,
            IConsumerConfiguration configuration)
        {
            if (configuration.IsExclusive || queueConsumerPairs.Any(x => x.Item1.IsExclusive))
            {
                throw new NotSupportedException("Exclusive multiple consuming is not supported.");
            }

            return(new PersistentMultipleConsumer(queueConsumerPairs, connection, configuration, internalConsumerFactory, eventBus));
        }
예제 #30
0
        public StartConsumingStatus StartConsuming(
            IPersistentConnection connection,
            IQueue queue,
            Func <byte[], MessageProperties, MessageReceivedInfo, Task> onMessage,
            IConsumerConfiguration configuration
            )
        {
            Preconditions.CheckNotNull(connection, "connection");
            Preconditions.CheckNotNull(queue, "queue");
            Preconditions.CheckNotNull(onMessage, "onMessage");
            Preconditions.CheckNotNull(configuration, "configuration");

            var consumerTag = conventions.ConsumerTagConvention();
            IDictionary <string, object> arguments = new Dictionary <string, object>
            {
                { "x-priority", configuration.Priority },
                { "x-cancel-on-ha-failover", configuration.CancelOnHaFailover || connectionConfiguration.CancelOnHaFailover }
            };

            try
            {
                Model = connection.CreateModel();

                var basicConsumers = new BasicConsumer(SingleBasicConsumerCancelled, consumerDispatcher, queue, eventBus, handlerRunner, onMessage, logger, Model);

                this.basicConsumers = new[] { new BasicConsumer(SingleBasicConsumerCancelled, consumerDispatcher, queue, eventBus, handlerRunner, onMessage, logger, Model) };

                Model.BasicQos(0, configuration.PrefetchCount, false);

                //// if configured, re-establish the consumer on a ConsumerCancelled event.
                //if (queue.IsConsumerRepairable)
                //    ConsumerCancelled += (sender, args) => ConsumerReConnect(sender, consumerTag, configuration.IsExclusive, arguments,queue);

                Model.BasicConsume(
                    queue.Name,         // queue
                    false,              // noAck
                    consumerTag,        // consumerTag
                    true,
                    configuration.IsExclusive,
                    arguments,          // arguments
                    basicConsumers);    // consumer

                logger.InfoWrite("Declared Consumer. queue='{0}', consumer tag='{1}' prefetchcount={2} priority={3} x-cancel-on-ha-failover={4}",
                                 queue.Name, consumerTag, configuration.PrefetchCount, configuration.Priority, configuration.CancelOnHaFailover);
            }
            catch (Exception exception)
            {
                logger.ErrorWrite("Consume failed. queue='{0}', consumer tag='{1}', message='{2}'",
                                  queue.Name, consumerTag, exception.Message);
                return(StartConsumingStatus.Failed);
            }
            return(StartConsumingStatus.Succeed);
        }
예제 #31
0
        /// <summary>
        /// Create the correct implementation of IConsumer based on queue properties
        /// </summary>
        /// <param name="queue"></param>
        /// <param name="onMessage"></param>
        /// <param name="connection"></param>
        /// <param name="configuration"></param>
        /// <returns></returns>
        private IConsumer CreateConsumerInstance(
            IQueue queue, 
            Func<byte[], MessageProperties, MessageReceivedInfo, Task> onMessage, 
            IPersistentConnection connection,
            IConsumerConfiguration configuration)
        {
            if (queue.IsExclusive)
            {
                return new TransientConsumer(queue, onMessage, connection, configuration, internalConsumerFactory, eventBus);
            }

            return new PersistentConsumer(queue, onMessage, connection, configuration, internalConsumerFactory, eventBus);
        }
예제 #32
0
        /// <summary>
        /// Create the correct implementation of IConsumer based on queue properties
        /// </summary>
        /// <param name="queue"></param>
        /// <param name="onMessage"></param>
        /// <param name="connection"></param>
        /// <param name="configuration"></param>
        /// <returns></returns>
        private IConsumer CreateConsumerInstance(
            IQueue queue,
            Func <byte[], MessageProperties, MessageReceivedInfo, Task> onMessage,
            IPersistentConnection connection,
            IConsumerConfiguration configuration)
        {
            if (queue.IsExclusive)
            {
                return(new TransientConsumer(queue, onMessage, connection, configuration, internalConsumerFactory, eventBus));
            }

            return(new PersistentConsumer(queue, onMessage, connection, configuration, internalConsumerFactory, eventBus));
        }
		public IRawConsumer CreateConsumer(IConsumerConfiguration cfg, IModel channel)
		{
			ConfigureQos(channel, cfg.PrefetchCount);
			var consumer = new QueueingRawConsumer(channel);
			_consumers.Add(consumer);

			//TODO: Add exception handling etc.

			Task
				.Run(() => consumer.Queue.Dequeue())
				.ContinueWith(argsTask => consumer.OnMessageAsync(this, argsTask.Result));

			return consumer;
		}
예제 #34
0
        public StartConsumingStatus StartConsuming(
            IPersistentConnection connection,
            IQueue queue,
            Func <byte[], MessageProperties, MessageReceivedInfo, Task> onMessage,
            IConsumerConfiguration configuration
            )
        {
            Preconditions.CheckNotNull(connection, "connection");
            Preconditions.CheckNotNull(queue, "queue");
            Preconditions.CheckNotNull(onMessage, "onMessage");
            Preconditions.CheckNotNull(configuration, "configuration");

            this.queue         = queue;
            this.onMessage     = onMessage;
            this.configuration = configuration;

            var consumerTag = conventions.ConsumerTagConvention();
            IDictionary <string, object> arguments = new Dictionary <string, object>
            {
                { "x-priority", configuration.Priority },
                { "x-cancel-on-ha-failover", configuration.CancelOnHaFailover || connectionConfiguration.CancelOnHaFailover }
            };

            try
            {
                Model = connection.CreateModel();

                Model.BasicQos(0, configuration.PrefetchCount, false);

                Model.BasicConsume(
                    queue.Name,         // queue
                    false,              // noAck
                    consumerTag,        // consumerTag
                    true,
                    configuration.IsExclusive,
                    arguments,          // arguments
                    this);              // consumer

                logger.InfoWrite("Declared Consumer. queue='{0}', consumer tag='{1}' prefetchcount={2} priority={3} x-cancel-on-ha-failover={4}",
                                 queue.Name, consumerTag, configuration.PrefetchCount, configuration.Priority, configuration.CancelOnHaFailover);
            }
            catch (Exception exception)
            {
                logger.ErrorWrite("Consume failed. queue='{0}', consumer tag='{1}', message='{2}'",
                                  queue.Name, consumerTag, exception.Message);
                return(StartConsumingStatus.Failed);
            }
            return(StartConsumingStatus.Succeed);
        }
예제 #35
0
        public IConsumer CreateConsumer(
            IQueue queue, 
            Func<byte[], MessageProperties, MessageReceivedInfo, Task> onMessage, 
            IPersistentConnection connection, 
            IConsumerConfiguration configuration
            )
        {
            Preconditions.CheckNotNull(queue, "queue");
            Preconditions.CheckNotNull(onMessage, "onMessage");
            Preconditions.CheckNotNull(connection, "connection");

            var consumer = CreateConsumerInstance(queue, onMessage, connection, configuration);
            consumers.TryAdd(consumer, null);
            return consumer;
        }
예제 #36
0
 public TransientConsumer(
     IQueue queue,
     Func <byte[], MessageProperties, MessageReceivedInfo, Task> onMessage,
     IPersistentConnection connection,
     IConsumerConfiguration configuration,
     IInternalConsumerFactory internalConsumerFactory,
     IEventBus eventBus)
 {
     this.queue                   = queue;
     this.onMessage               = onMessage;
     this.connection              = connection;
     this.configuration           = configuration;
     this.internalConsumerFactory = internalConsumerFactory;
     this.eventBus                = eventBus;
 }
예제 #37
0
 /// <summary>
 /// Create a new instance of the shared context.
 /// </summary>
 /// <param name="configuration">A configuration that may be overridden by the individual interactions. This may be null.</param>
 public Context(IConsumerConfiguration configuration)
 {
     if (_instance != null)
     {
         throw new InvalidOperationException("Dispose the old context before creating a new.");
     }
     _instance      = this;
     _configuration = configuration;
     lock (_lockToken)
     {
         if (_servers == null)
         {
             _servers = new WebServerContainer(configuration, false);
         }
     }
 }
예제 #38
0
        public StartConsumingStatus StartConsuming(
            IPersistentConnection connection,
            IQueue queue,
            Func<byte[], MessageProperties, MessageReceivedInfo, Task> onMessage,
            IConsumerConfiguration configuration
            )
        {
            Preconditions.CheckNotNull(connection, "connection");
            Preconditions.CheckNotNull(queue, "queue");
            Preconditions.CheckNotNull(onMessage, "onMessage");
            Preconditions.CheckNotNull(configuration, "configuration");

            this.queue = queue;
            this.onMessage = onMessage;
            var consumerTag = conventions.ConsumerTagConvention();
            IDictionary<string, object> arguments = new Dictionary<string, object>
                {
                    {"x-priority", configuration.Priority},
                    {"x-cancel-on-ha-failover", configuration.CancelOnHaFailover || connectionConfiguration.CancelOnHaFailover}
                };
            try
            {
                Model = connection.CreateModel();

                Model.BasicQos(0, configuration.PrefetchCount, false);

                Model.BasicConsume(
                    queue.Name,         // queue
                    false,              // noAck
                    consumerTag,        // consumerTag
                    true,
                    configuration.IsExclusive,
                    arguments,          // arguments
                    this);              // consumer

                logger.InfoWrite("Declared Consumer. queue='{0}', consumer tag='{1}' prefetchcount={2} priority={3} x-cancel-on-ha-failover={4}",
                                  queue.Name, consumerTag, configuration.PrefetchCount, configuration.Priority, configuration.CancelOnHaFailover);
            }
            catch (Exception exception)
            {
                logger.ErrorWrite("Consume failed. queue='{0}', consumer tag='{1}', message='{2}'",
                                 queue.Name, consumerTag, exception.Message);
                return StartConsumingStatus.Failed;
            }
            return StartConsumingStatus.Succeed;
        }
예제 #39
0
        public KafkaOptions(IEnumerable <Uri> kafkaServerUris = null,
                            IConnectionConfiguration connectionConfiguration = null,
                            IRouterConfiguration routerConfiguration         = null,
                            IConnectionFactory connectionFactory             = null,
                            IProducerConfiguration producerConfiguration     = null,
                            IConsumerConfiguration consumerConfiguration     = null,
                            ILog log = null)
        {
            ServerUris = ImmutableList <Uri> .Empty.AddNotNullRange(kafkaServerUris);

            RouterConfiguration     = routerConfiguration ?? KafkaClient.RouterConfiguration.Default;
            ConnectionConfiguration = connectionConfiguration ?? Connections.ConnectionConfiguration.Default;
            ConnectionFactory       = connectionFactory ?? new ConnectionFactory();
            ProducerConfiguration   = producerConfiguration ?? KafkaClient.ProducerConfiguration.Default;
            ConsumerConfiguration   = consumerConfiguration ?? KafkaClient.ConsumerConfiguration.Default;
            Log = log ?? TraceLog.Log;
        }
예제 #40
0
        public TransientConsumer(
            IQueue queue, 
            Func<byte[], MessageProperties, MessageReceivedInfo, Task> onMessage, 
            IPersistentConnection connection, 
            IConsumerConfiguration configuration,
            IInternalConsumerFactory internalConsumerFactory, 
            IEventBus eventBus)
        {
            Preconditions.CheckNotNull(queue, "queue");
            Preconditions.CheckNotNull(onMessage, "onMessage");
            Preconditions.CheckNotNull(connection, "connection");
            Preconditions.CheckNotNull(internalConsumerFactory, "internalConsumerFactory");
            Preconditions.CheckNotNull(eventBus, "eventBus");
            Preconditions.CheckNotNull(configuration, "configuration");

            this.queue = queue;
            this.onMessage = onMessage;
            this.connection = connection;
            this.configuration = configuration;
            this.internalConsumerFactory = internalConsumerFactory;
            this.eventBus = eventBus;
        }
 protected void BasicAck(IModel channel, BasicDeliverEventArgs args, IConsumerConfiguration cfg)
 {
     try
     {
         _logger.LogDebug($"Ack:ing message with id {args.DeliveryTag}.");
         channel.BasicAck(
             deliveryTag: args.DeliveryTag,
             multiple: false
         );
     }
     catch (AlreadyClosedException)
     {
         _logger.LogWarning("Unable to ack message, channel is allready closed.");
         _processedButNotAcked.Add(args.BasicProperties.MessageId);
     }
 }
		public IRawConsumer CreateConsumer(IConsumerConfiguration cfg)
		{
			return CreateConsumer(cfg, _channelFactory.GetChannel());
		}
예제 #43
0
        public void StartConsuming(
            IPersistentConnection connection,
            IQueue queue,
            Func<byte[], MessageProperties, MessageReceivedInfo, Task> onMessage,
            IConsumerConfiguration configuration
            )
        {
            Preconditions.CheckNotNull(connection, "connection");
            Preconditions.CheckNotNull(queue, "queue");
            Preconditions.CheckNotNull(onMessage, "onMessage");
            Preconditions.CheckNotNull(configuration, "configuration");

            this.queue = queue;
            this.onMessage = onMessage;
            var consumerTag = conventions.ConsumerTagConvention();
            IDictionary<string, object> arguments = new Dictionary<string, object>
                {
                    {"x-priority", configuration.Priority}
                };
            try
            {
                Model = connection.CreateModel();

                Model.BasicQos(0, connectionConfiguration.PrefetchCount, false);

                Model.BasicConsume(
                    queue.Name,         // queue
                    false,              // noAck
                    consumerTag,        // consumerTag
                    arguments,          // arguments
                    this);              // consumer

                logger.InfoWrite("Declared Consumer. queue='{0}', consumer tag='{1}' prefetchcount={2} priority={3}",
                                  queue.Name, consumerTag, connectionConfiguration.PrefetchCount, configuration.Priority);
            }
            catch (Exception exception)
            {
                logger.InfoWrite("Consume failed. queue='{0}', consumer tag='{1}', message='{2}'",
                                 queue.Name, consumerTag, exception.Message);
            }
        }
예제 #44
0
 public virtual Task OnResponseRecievedException(IRawConsumer rawConsumer, IConsumerConfiguration cfg, BasicDeliverEventArgs args, TaskCompletionSource<object> responseTcs, Exception exception)
 {
     _logger.LogError($"An exception was thrown when recieving response to messaeg '{args.BasicProperties.MessageId}' with CorrelationId '{args.BasicProperties.CorrelationId}'.", exception);
     responseTcs.TrySetException(exception);
     return Task.FromResult(true);
 }