Exemple #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);
        }
Exemple #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);
        }
  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();
        }
Exemple #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)));
            };
        }
Exemple #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();
        }
Exemple #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";
        }
        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);
        }
 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);
 }
Exemple #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);
        }
Exemple #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();
        }
Exemple #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));
            }
        }
Exemple #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;
 }
Exemple #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;
 }
Exemple #17
0
        public TopicSubscriberScope CreateTopicSubscriberScope(IConsumerConfiguration configuration)
        {
            var consumer = new ConsumerBuilder <string, string>(configuration).Build();

            consumer.Subscribe(configuration.SubscribedTopics);

            return(new KafkaConsumerScope(consumer));
        }
        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);
            }
        }
 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);
 }
Exemple #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;
        }
 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)
 {
 }
Exemple #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);
 }
Exemple #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));
     }
 }
Exemple #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);
        }
Exemple #27
0
 private void InitIConsumerConfiguration(IConsumerConfiguration consumerConfigure)
 {
     consumerConfigure
     .WithPriority(subConfigure.Priority)
     .WithCancelOnHaFailover(subConfigure.CancelOnHaFailover)
     .WithPrefetchCount(subConfigure.PrefetchCount);
     if (subConfigure.IsExclusive)
     {
         consumerConfigure.AsExclusive();
     }
 }
Exemple #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)
 {
 }
Exemple #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));
        }
Exemple #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);
        }
        /// <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);
        }
        /// <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;
		}
        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);
        }
Exemple #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;
        }
Exemple #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;
 }
Exemple #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);
         }
     }
 }
Exemple #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;
        }
        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;
        }
        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());
		}
        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);
            }
        }
Exemple #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);
 }