kafka @Listener异常处理 - 无法配置批量重试

时间:2017-02-02 11:12:39

标签: java error-handling listener spring-kafka

我对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;
}

我的问题

  1. 如果由于数据库不可用而导致任何批处理未能插入数据库,则如何重试
  2. 如何测试Kafka服务器是否正在运行并且能够连接到特定主题 - 为什么我问这个问题是我在本地停止Kafka Listenerzookeeper服务器后尝试了Kafka但是没有错误或异常。 Kafka Producer我的意思是Template发送邮件在停止Kafka Server后发送错误但在Listener找不到错误
  3. ADDED

    我的配置

    @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]
    

1 个答案:

答案 0 :(得分:2)

请参阅Retrying Deliveries

  

使用@KafkaListener时,在容器工厂上设置RetryTemplate(以及可选的recoveryCallback),并将侦听器包装在适当的重试适配器中。

新的Kafka客户端(0.9.x.x或0.10.x.x)不会直接与zookeeper通信kafka服务器本身。

客户端内部不断尝试重新连接;打开调试日志以查看活动。