【发布时间】:2019-10-17 21:01:59
【问题描述】:
我们正在使 Kafka Concurrent Consumer 的 kafka 自动提交为 false。然后基于使用 Rest Template 在异步中的另一个微服务调用,我们正在尝试确认响应。我们在 acknowledgement.acknowledge() 时收到 KafkaConsumer 对多线程访问不安全的错误;正在执行。
如果我们删除方法中的异步,它不会抛出异常并且工作正常。
@Async("******")
public CompletableFuture<String> process*****Service(****** ******, String serviceUrl, Acknowledgment acknowledgment){
ResponseEntity<String> serviceResponse = null;
int retryCount = 0;
String response = null;
try {
HttpHeaders headers = KafkaConsumerUtil.prepareHeaders(ApplicationConstant.****, apikey);
HttpEntity<******> entity = new HttpEntity<>(*****, headers);
retryCount++;
System.out.println("Attempting retry mechanism with counter "+retryCount);
serviceResponse = restTemplate.exchange(serviceUrl, HttpMethod.POST, entity, String.class);
if (null != serviceResponse) {
response = serviceResponse.getStatusCode().toString();
acknowledgment.acknowledge();
}
}catch (Exception e) {
log.error(ApplicationConstant.*****.concat("Exception caught at process*****Service "+e.getMessage()));
String producerUrl = appProperties.getKafkapropmap().get(ApplicationConstant.PUBLISHER_MS_SERVICE_URL);
// deadLetterTopic.invokeDeadLetterTopic(****Request, producerUrl, acknowledgment);
}
return CompletableFuture.completedFuture(response);
}
@Bean
public ConcurrentKafkaListenerContainerFactory<Long, String> kafkaListenerContainerFactory() {
ConcurrentKafkaListenerContainerFactory<Long, String> factory =
new ConcurrentKafkaListenerContainerFactory<>();
factory.setConsumerFactory(consumerFactory());
factory.getContainerProperties().setAckMode(AckMode.MANUAL_IMMEDIATE);
factory.setConcurrency(Integer.parseInt(concurrency));
return factory;
}
and
props.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, false);
错误
2019-10-17 11:59:03.612 [Pre-******-3] ERROR c.a.m.reg.service.******* -
********: Exception caught at ******Service KafkaConsumer is not safe for multi-threaded access
我们使用的是 spring-kafka 1.3.0.RELEASE
感谢任何提示!
【问题讨论】:
标签: apache-kafka kafka-consumer-api spring-kafka