示例#1
0
        protected override async Task ExecuteAsync(CancellationToken cancellationToken)
        {
            var topics = options.Topics.Distinct();

            consumerFactory.GetConsumer().Subscribe(topics);
            await Consume(cancellationTokenSource.Token);
        }
示例#2
0
        GetConsumer <TMessage>(IConsumeContext <TMessage> context,
                               InstanceHandlerSelector <TConsumer, TMessage> selector)
            where TMessage : class
        {
            IEnumerable <Action <IConsumeContext <TMessage> > > consumers = _consumerFactory.GetConsumer(context, selector);

            foreach (var action in consumers)
            {
                Action <IConsumeContext <TMessage> > consumer = action;

                yield return(message =>
                {
                    var received = new ReceivedMessageImpl <TMessage>(message);

                    try
                    {
                        consumer(message);
                    }
                    catch (Exception ex)
                    {
                        received.SetException(ex);
                    }
                    finally
                    {
                        _received.Add(received);
                    }
                });
            }
        }
示例#3
0
        public IEnumerable <Action <IConsumeContext <TMessage> > > GetConsumer <TMessage>(IConsumeContext <TMessage> context,
                                                                                          InstanceHandlerSelector <TConsumer, TMessage> selector)
            where TMessage : class
        {
            foreach (var action in _consumerFactory.GetConsumer(context, selector))
            {
                Action <IConsumeContext <TMessage> > consumer = action;
                yield return(x =>
                {
                    DateTime startTime = DateTime.UtcNow;
                    Stopwatch timer = Stopwatch.StartNew();
                    try
                    {
                        consumer(x);

                        timer.Stop();

                        x.NotifyResourceUsageCompleted(x.Bus.Endpoint.Address.Uri, startTime, timer.Elapsed);
                    }
                    catch (Exception)
                    {
                        throw;
                    }
                });
            }
        }
示例#4
0
        /// <summary>
        /// Entry Point of Application Business Logic
        /// </summary>
        /// <returns></returns>
        public async Task <bool> Execute()
        {
            try
            {
                bool hasItems = false;

                //get consumer
                var consumer = _consumerFactory.GetConsumer();

                //get items from queue
                await consumer.Consume <IEnumerable <Payload> >(async (IEnumerable <Payload> items) =>
                {
                    if (items != null && items.Any())
                    {
                        hasItems = true;

                        var graph = await GetGraph();

                        await _processor.Run(items, graph);
                    }
                });

                return(hasItems);
            }
            catch (Exception error)
            {
                _logger.LogError(error, nameof(Execute));
                throw;
            }
        }
 public IEnumerable <Action <IConsumeContext <TMessage> > > GetConsumer <TMessage>(IConsumeContext <TMessage> context,
                                                                                   InstanceHandlerSelector <TConsumer, TMessage> selector)
     where TMessage : class
 {
     yield return(x =>
     {
         _interceptor(() =>
         {
             foreach (var consumer in _consumerFactory.GetConsumer(context, selector))
             {
                 consumer(x);
             }
         });
     });
 }
 public IEnumerable <Action <IConsumeContext <TMessage> > > Enumerate(IConsumeContext <TMessage> context)
 {
     return(_consumerFactory.GetConsumer(context, Selector));
 }
示例#7
0
 public IEnumerable <Action <IConsumeContext <TMessage> > > GetConsumer <TMessage>(
     IConsumeContext <TMessage> context, InstanceHandlerSelector <TConsumer, TMessage> selector)
     where TMessage : class
 {
     return(_delegate.GetConsumer(context, selector));
 }
        private void StartSchedule(IProducerFactory producerFactory, IConsumerFactory consumerFactory, CancellationToken token)
        {
            var producers     = Enumerable.Range(0, this.Configuration.ProducerCount).Select(_ => producerFactory.GetProducer()).ToList();
            var consumers     = Enumerable.Range(0, this.Configuration.ConsumerCount).Select(_ => consumerFactory.GetConsumer()).ToList();
            var alsoConsumers = producers.Where(x => x is IConsumer).Select(x => x as IConsumer).ToList();
            var alsoProducers = consumers.Where(x => x is IProducer).Select(x => x as IProducer).ToList();

            if (this.logger != null)
            {
                this.logger.LogInformation($"{this.LogName}: {producers.Count} Producers has created!");
                this.logger.LogInformation($"{this.LogName}: {consumers.Count} Consumers has created!");
                if (alsoConsumers.Count > 0 || alsoProducers.Count > 0)
                {
                    var count = alsoConsumers.Count + alsoProducers.Count;
                    this.logger.LogInformation($"{this.LogName}: {count} detected as both and added!");
                }
            }

            this.producers = producers.Concat(alsoProducers);
            this.consumers = consumers.Concat(alsoConsumers);
            this.Schedule(token);
        }