我对Kafka
很新。我试图找出并理解错误方案如何适用于@Listener batch consumer factory
。
我正在使用topic
batch
进程中的记录并将其插入DB
,如下所示...
@KafkaListener( topics = "KAFKA.TEST")
public Boolean listen(List<ConsumerRecord<String, User>> list) throws Exception {
Boolean result = null;
List<User> userList = new ArrayList<>();
for (ConsumerRecord<String, User> record : list) {
User user = record.value();
userList.add(user);
}
if(userList.size()>0) {
result = dbService.insertBatchUser(userList);
LOGGER.info(" users inserted " + userList.size());
}
else
LOGGER.info(" No users found in the topic ");
countDownLatch.countDown();
return result;
}
Kafka Listener
和zookeeper
服务器后尝试了Kafka
但是没有错误或异常。 Kafka Producer
我的意思是Template
发送邮件在停止Kafka Server
后发送错误但在Listener
找不到错误@Bean
public ConsumerFactory consumerFactory(){
return new DefaultKafkaConsumerFactory(consumerConfigs(),stringKeyDeserializer(),jsonValueDeserializer());
}
@Bean
public RetryPolicy getRetryPolicy(){
SimpleRetryPolicy simpleRetryPolicy = new SimpleRetryPolicy();
simpleRetryPolicy.setMaxAttempts(getMaxRetryAttempts());
return simpleRetryPolicy;
}
@Bean
public FixedBackOffPolicy getBackOffPolicy() {
FixedBackOffPolicy backOffPolicy = new FixedBackOffPolicy();
backOffPolicy.setBackOffPeriod(getRetryInterval());
return backOffPolicy;
}
@Bean
public RetryTemplate getRetryTemplate(){
RetryTemplate retryTemplate = new RetryTemplate();
retryTemplate.setRetryPolicy(getRetryPolicy());
retryTemplate.setBackOffPolicy(getBackOffPolicy());
return retryTemplate;
}
@Bean
public ConcurrentKafkaListenerContainerFactory kafkaListenerContainerFactory(){
ConcurrentKafkaListenerContainerFactory factory = new ConcurrentKafkaListenerContainerFactory();
factory.setConcurrency(getConcurrency());
factory.getContainerProperties().setPollTimeout(getPollTimeout());
factory.setConsumerFactory(consumerFactory());
factory.setBatchListener(true);
factory.setRetryTemplate(getRetryTemplate());
return factory;
}
我在这里使用org.springframework.retry.support.RetryTemplate
java.lang.ClassCastException: org.springframework.kafka.listener.adapter.BatchMessagingMessageListenerAdapter cannot be cast to org.springframework.kafka.listener.MessageListener
at org.springframework.kafka.config.AbstractKafkaListenerEndpoint.setupMessageListener(AbstractKafkaListenerEndpoint.java:306) ~[spring-kafka-1.1.2.RELEASE.jar:na]
at org.springframework.kafka.config.AbstractKafkaListenerEndpoint.setupListenerContainer(AbstractKafkaListenerEndpoint.java:282) ~[spring-kafka-1.1.2.RELEASE.jar:na]
at org.springframework.kafka.config.AbstractKafkaListenerContainerFactory.createListenerContainer(AbstractKafkaListenerContainerFactory.java:211) ~[spring-kafka-1.1.2.RELEASE.jar:na]
答案 0 :(得分:2)
使用@KafkaListener时,在容器工厂上设置RetryTemplate(以及可选的recoveryCallback),并将侦听器包装在适当的重试适配器中。
新的Kafka客户端(0.9.x.x或0.10.x.x)不会直接与zookeeper通信kafka服务器本身。
客户端内部不断尝试重新连接;打开调试日志以查看活动。