Browse Source

Merge pull request #7630 from abpframework/liangshiwei/kafka

Add re-queue feature to kafka
pull/7631/head
Halil İbrahim Kalkan 6 years ago
committed by GitHub
parent
commit
9b5a75fc49
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 4
      framework/src/Volo.Abp.Kafka/Volo/Abp/Kafka/AbpKafkaOptions.cs
  2. 26
      framework/src/Volo.Abp.Kafka/Volo/Abp/Kafka/KafkaMessageConsumer.cs

4
framework/src/Volo.Abp.Kafka/Volo/Abp/Kafka/AbpKafkaOptions.cs

@ -9,11 +9,13 @@ namespace Volo.Abp.Kafka
public KafkaConnections Connections { get; } public KafkaConnections Connections { get; }
public Action<ProducerConfig> ConfigureProducer { get; set; } public Action<ProducerConfig> ConfigureProducer { get; set; }
public Action<ConsumerConfig> ConfigureConsumer { get; set; } public Action<ConsumerConfig> ConfigureConsumer { get; set; }
public Action<TopicSpecification> ConfigureTopic { get; set; } public Action<TopicSpecification> ConfigureTopic { get; set; }
public bool ReQueue { get; set; } = true;
public AbpKafkaOptions() public AbpKafkaOptions()
{ {
Connections = new KafkaConnections(); Connections = new KafkaConnections();

26
framework/src/Volo.Abp.Kafka/Volo/Abp/Kafka/KafkaMessageConsumer.cs

@ -1,6 +1,5 @@
using System; using System;
using System.Collections.Concurrent; using System.Collections.Concurrent;
using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using Confluent.Kafka; using Confluent.Kafka;
using Confluent.Kafka.Admin; using Confluent.Kafka.Admin;
@ -20,6 +19,8 @@ namespace Volo.Abp.Kafka
protected IConsumerPool ConsumerPool { get; } protected IConsumerPool ConsumerPool { get; }
protected IProducerPool ProducerPool { get; }
protected IExceptionNotifier ExceptionNotifier { get; } protected IExceptionNotifier ExceptionNotifier { get; }
protected AbpKafkaOptions Options { get; } protected AbpKafkaOptions Options { get; }
@ -37,10 +38,12 @@ namespace Volo.Abp.Kafka
public KafkaMessageConsumer( public KafkaMessageConsumer(
IConsumerPool consumerPool, IConsumerPool consumerPool,
IExceptionNotifier exceptionNotifier, IExceptionNotifier exceptionNotifier,
IOptions<AbpKafkaOptions> options) IOptions<AbpKafkaOptions> options,
IProducerPool producerPool)
{ {
ConsumerPool = consumerPool; ConsumerPool = consumerPool;
ExceptionNotifier = exceptionNotifier; ExceptionNotifier = exceptionNotifier;
ProducerPool = producerPool;
Options = options.Value; Options = options.Value;
Logger = NullLogger<KafkaMessageConsumer>.Instance; Logger = NullLogger<KafkaMessageConsumer>.Instance;
@ -132,14 +135,29 @@ namespace Volo.Abp.Kafka
{ {
await callback(consumeResult.Message); await callback(consumeResult.Message);
} }
Consumer.Commit(consumeResult);
} }
catch (Exception ex) catch (Exception ex)
{ {
await RequeueAsync(consumeResult);
Logger.LogException(ex); Logger.LogException(ex);
await ExceptionNotifier.NotifyAsync(ex); await ExceptionNotifier.NotifyAsync(ex);
} }
finally
{
Consumer.Commit(consumeResult);
}
}
protected virtual async Task RequeueAsync(ConsumeResult<string, byte[]> consumeResult)
{
if (!Options.ReQueue)
{
return;
}
var producer = ProducerPool.Get(ConnectionName);
await producer.ProduceAsync(consumeResult.Topic, consumeResult.Message);
} }
public virtual void Dispose() public virtual void Dispose()

Loading…
Cancel
Save