|
@@ -1,7 +1,6 @@ |
|
|
using System; |
|
|
using System; |
|
|
using System.Text; |
|
|
using System.Text; |
|
|
using System.Threading.Tasks; |
|
|
using System.Threading.Tasks; |
|
|
using Confluent.Kafka; |
|
|
|
|
|
using DotNetCore.CAP.Processor.States; |
|
|
using DotNetCore.CAP.Processor.States; |
|
|
using Microsoft.Extensions.Logging; |
|
|
using Microsoft.Extensions.Logging; |
|
|
|
|
|
|
|
@@ -9,49 +8,52 @@ namespace DotNetCore.CAP.Kafka |
|
|
{ |
|
|
{ |
|
|
internal class PublishQueueExecutor : BasePublishQueueExecutor |
|
|
internal class PublishQueueExecutor : BasePublishQueueExecutor |
|
|
{ |
|
|
{ |
|
|
private readonly KafkaOptions _kafkaOptions; |
|
|
|
|
|
|
|
|
private readonly ConnectionPool _connectionPool; |
|
|
private readonly ILogger _logger; |
|
|
private readonly ILogger _logger; |
|
|
|
|
|
|
|
|
public PublishQueueExecutor( |
|
|
public PublishQueueExecutor( |
|
|
CapOptions options, |
|
|
CapOptions options, |
|
|
IStateChanger stateChanger, |
|
|
IStateChanger stateChanger, |
|
|
KafkaOptions kafkaOptions, |
|
|
|
|
|
|
|
|
ConnectionPool connectionPool, |
|
|
ILogger<PublishQueueExecutor> logger) |
|
|
ILogger<PublishQueueExecutor> logger) |
|
|
: base(options, stateChanger, logger) |
|
|
: base(options, stateChanger, logger) |
|
|
{ |
|
|
{ |
|
|
_logger = logger; |
|
|
_logger = logger; |
|
|
_kafkaOptions = kafkaOptions; |
|
|
|
|
|
|
|
|
_connectionPool = connectionPool; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public override Task<OperateResult> PublishAsync(string keyName, string content) |
|
|
|
|
|
|
|
|
public override async Task<OperateResult> PublishAsync(string keyName, string content) |
|
|
{ |
|
|
{ |
|
|
|
|
|
var producer = _connectionPool.Rent(); |
|
|
try |
|
|
try |
|
|
{ |
|
|
{ |
|
|
var config = _kafkaOptions.AsKafkaConfig(); |
|
|
|
|
|
var contentBytes = Encoding.UTF8.GetBytes(content); |
|
|
var contentBytes = Encoding.UTF8.GetBytes(content); |
|
|
using (var producer = new Producer(config)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var message = await producer.ProduceAsync(keyName, null, contentBytes); |
|
|
|
|
|
|
|
|
|
|
|
if (!message.Error.HasError) |
|
|
{ |
|
|
{ |
|
|
var message = producer.ProduceAsync(keyName, null, contentBytes).Result; |
|
|
|
|
|
|
|
|
|
|
|
if (!message.Error.HasError) |
|
|
|
|
|
{ |
|
|
|
|
|
_logger.LogDebug($"kafka topic message [{keyName}] has been published."); |
|
|
|
|
|
|
|
|
|
|
|
return Task.FromResult(OperateResult.Success); |
|
|
|
|
|
} |
|
|
|
|
|
return Task.FromResult(OperateResult.Failed(new OperateError |
|
|
|
|
|
{ |
|
|
|
|
|
Code = message.Error.Code.ToString(), |
|
|
|
|
|
Description = message.Error.Reason |
|
|
|
|
|
})); |
|
|
|
|
|
|
|
|
_logger.LogDebug($"kafka topic message [{keyName}] has been published."); |
|
|
|
|
|
|
|
|
|
|
|
return OperateResult.Success; |
|
|
} |
|
|
} |
|
|
|
|
|
return OperateResult.Failed(new OperateError |
|
|
|
|
|
{ |
|
|
|
|
|
Code = message.Error.Code.ToString(), |
|
|
|
|
|
Description = message.Error.Reason |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
catch (Exception ex) |
|
|
catch (Exception ex) |
|
|
{ |
|
|
{ |
|
|
_logger.LogError( |
|
|
|
|
|
|
|
|
_logger.LogError(ex, |
|
|
$"An error occurred during sending the topic message to kafka. Topic:[{keyName}], Exception: {ex.Message}"); |
|
|
$"An error occurred during sending the topic message to kafka. Topic:[{keyName}], Exception: {ex.Message}"); |
|
|
|
|
|
|
|
|
return Task.FromResult(OperateResult.Failed(ex)); |
|
|
|
|
|
|
|
|
return OperateResult.Failed(ex); |
|
|
|
|
|
} |
|
|
|
|
|
finally |
|
|
|
|
|
{ |
|
|
|
|
|
_connectionPool.Return(producer); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |