Spring Kafka - How to Retry with @KafkaListener
spring-kafka SeekToCurrentErrorHandler
(如果设置了spring.kafka.listener.ack-mode=time
,可以重试吗?或以指定的确认模式重试工作,例如MANUAL MANUAL_IMMEDIATE。
@Value("${${sync.kafka.header.source.id}}")
private String headerSourceId;
@Value("${sync.kafka.from.id}")
private String syncKafkaFromId;
@Value("${sync.kafka.to.id}")
private String syncKafkaToId;
@Autowired
private KafkaTemplate kafkaTemplate;
@Autowired
private ConsumerFactory consumerFactory;
@KafkaListener(topics = "${sync.kafka.topics}")
public void listen(ConsumerRecord<?, ?> record,
@Header(KafkaHeaders.RECEIVED_TOPIC) String topic,
@Header(value = KafkaHeaders.RECEIVED_MESSAGE_KEY, required = false) String messageKey,
@Header(value = "${sync.kafka.header.source.id}", required = false) String customHeader,
@Headers MessageHeaders messageHeaders) {
log.info("- - - - - - - - - - - - - - -");
log.info("topic: {}", topic);
log.info("message key: {}", messageKey);
log.info("custom header: {}", customHeader);
log.info("messageHeaders: {}", messageHeaders);
ProducerRecord<Object, Object> ProducerRecord = new ProducerRecord(topic,record.key(), record.value());
ProducerRecord.headers().add(new RecordHeader(headerSourceId, syncKafkaFromId.getBytes()));
kafkaTemplate.send(ProducerRecord);
}
@Bean
public ConcurrentKafkaListenerContainerFactory<?, ?> kafkaListenerContainerFactory(
ConcurrentKafkaListenerContainerFactoryConfigurer configurer,
ConsumerFactory<Object, Object> kafkaConsumerFactory) {
ConcurrentKafkaListenerContainerFactory<Object, Object> factory = new ConcurrentKafkaListenerContainerFactory<>();
configurer.configure(factory, kafkaConsumerFactory);
factory.getContainerProperties().setErrorHandler(new SeekToCurrentErrorHandler());
// or factory.setRetryTemplate(aRetryTemplate);
// and factory.setRecoveryCallback(aRecoveryCallback);
return factory;
}
答案 0 :(得分:0)
使用SeekToCurrentErrorHandler
时,只能使用手动,记录或批处理确认模式。您还应该将ackOnError
容器属性设置为false
,并将enable.auto.commit
设置为false
。