public RabbitMqDistributedEventBus( IConnectionPool connectionPool, IConsumerFactory consumerFactory, IRabbitMqSerializer serializer, IEventBusSubscriptionsManager subscriptionsManager, IServiceProvider serviceProvider, IOptions <EventBusOptions> eventBusOptions, IOptions <RabbitMqOptions> rabbitMOptions) : base(subscriptionsManager, serviceProvider, eventBusOptions) { RabbitMqOptions = rabbitMOptions.Value; ConnectionPool = connectionPool; ConsumerFactory = consumerFactory; Serializer = serializer; _subsManager.OnEventRemoved += SubsManager_OnEventRemoved; Consumer = ConsumerFactory.Create( new ExchangeDeclareConfiguration( RabbitMqOptions.Exchange, type: "direct", durable: true), new QueueDeclareConfiguration( RabbitMqOptions.QueueName, durable: true, exclusive: false, autoDelete: false), RabbitMqOptions.ConnectionName ); Consumer.OnMessageReceived(ProcessEventAsync); }
private void StartConsumerThread() { var consumer = consumerFactory.Create(); consumer.Consume(OnMessageReceived); consumers.Add(consumer); logger.LogInformation($"Starting consumer {consumers.Count}"); }
public bool Run(string topic, ConsumerConfig consumeConfiguration, IEnumerable <ProducerConfig> produceConfigurations) { _topic = topic; _logger.LogInformation($"Starting feed proxy for topic {_topic}"); try { _consumer = _consumerFactory.Create(_topic, consumeConfiguration); _consumer.Connect(); _logger.LogInformation($"Connect consumer {_consumer.Environment}, topic {_topic}"); } catch (Exception e) { _logger.LogError(e, $"Exception while connect consumer {_consumer.Environment}."); return(false); } foreach (var producerConfiguration in produceConfigurations) { var producer = _producerFactory.Create(_topic, producerConfiguration); try { producer.Connect(); _producers.Add(producer); _logger.LogInformation($"Connect producer {producer.Environment}, topic {_topic}"); } catch (Exception e) { _logger.LogError(e, $"Exception while connect producer {producer.Environment}."); } } if (!_producers.Any()) { _logger.LogWarning($"Doesn't have any connected producers for topic {_topic}, feed proxy not started!"); return(false); } _consumer.Subscribe(Handler); _logger.LogInformation($"Subscribe consumer {_consumer.Environment}, topic {_topic}"); return(true); }
public Task StartAsync(CancellationToken token) { if (Configuration.TopicCreationEnabled) { CreateTopics().Wait(token); } _consumer = _consumerFactory.Create <TKey, TMessage>(Configuration); _consumer.Subscribe(_topic); return(Task.Factory.StartNew(async() => { while (!token.IsCancellationRequested) { try { var consumeResult = _consumer.Consume(100); var consumeContext = new ConsumeContext <TKey, TMessage>(consumeResult); if (consumeResult != null) { if (_filterPredicate(consumeContext)) { if (_handler != null) { await _handler.Handle(consumeContext); } } } } catch (Exception e) { Console.WriteLine(e); } } }, token, TaskCreationOptions.LongRunning, TaskScheduler.Default)); }
public IConsumer NewInstance(Session session, IReadonlyContext context, byte?index) => Factory.Create(Clz, session, context, index);