卡夫卡消费者重试并恢复

时间:2017-10-05 21:04:34

标签: java spring apache-kafka kafka-consumer-api spring-kafka

每当遇到DataAccessResourceFailureException时,我想重试三次。但是,当遇到以下代码时,它会通过我发送的最大尝试次数立即恢复到3。

监听器配置

public void listen(@Payload Map<String, Object> conciseMap,
                   @Header(KafkaHeaders.RECEIVED_PARTITION_ID) int partition,
                   Acknowledgment ack) {

        try {
            //logic                 
        } catch (DataAccessResourceFailureException ex){
            logger.error(" error while parsing response {}", ExceptionUtils.getStackTrace(ex));
            throw ex;
        } catch (Exception ex) {
        }
}

重试配置

    static {
        retryableExceptions = new HashMap<>();
        retryableExceptions.put(DataAccessResourceFailureException.class, true);
    }

    @Bean
    public ConsumerFactory<String, Map<String, Object>> consumerFactory() {
        Map<String, Object> props = new HashMap<>();
        props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapAddress);
        props.put(ConsumerConfig.GROUP_ID_CONFIG, groupId);
        props.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, false);
        return new DefaultKafkaConsumerFactory<>(props, new StringDeserializer(), new ConciseMessageDeserializer());
    }

    @Bean
    public ConcurrentKafkaListenerContainerFactory<String, Map<String, Object>> kafkaListenerContainerFactory() {
        ConcurrentKafkaListenerContainerFactory<String, Map<String, Object>> factory = new ConcurrentKafkaListenerContainerFactory<>();
        factory.setConcurrency(conncurrency);
        factory.setConsumerFactory(consumerFactory());
        factory.setRetryTemplate(retryTemplate());
        factory.setRecoveryCallback(new RecoveryCallback<Object>() {
            @Override
            public Object recover(RetryContext context) throws Exception {
                logger.debug(" In recovery callback method !!");
                ConsumerRecord<?, ?> record = (ConsumerRecord) context.getAttribute(RetryingMessageListenerAdapter.CONTEXT_RECORD);
                Map conciseMap = (Map)record.value();
                publisher.sendMessage((String)conciseMap.get("messagePayload"));
                ((Acknowledgment)context.getAttribute(RetryingMessageListenerAdapter.CONTEXT_ACKNOWLEDGMENT)).acknowledge();
                return null;
            }
        });
        factory.getContainerProperties().setAckMode(AckMode.MANUAL);
        return factory;
    }

/*
 * Retry template.
 */

protected RetryPolicy retryPolicy() {
    SimpleRetryPolicy policy = new SimpleRetryPolicy(maxRetryAttempts, retryableExceptions);
    return policy;
}

protected BackOffPolicy backOffPolicy() {
    ExponentialBackOffPolicy policy = new ExponentialBackOffPolicy();
    policy.setInitialInterval(initialRetryInterval);
    policy.setMultiplier(retryMultiplier);
    return policy;
}

protected RetryTemplate retryTemplate() {
   RetryTemplate template = new RetryTemplate();
   template.setRetryPolicy(retryPolicy());
   template.setBackOffPolicy(backOffPolicy());
   return template;
}

DEBUG记录

  

09:24:26.856   [org.springframework.kafka.KafkaListenerEndpointContainer#1-0-L-1]   DEBUG o.s.retry.support.RetryTemplate - 检查rethrow:count = 1   09:24:26.856   [org.springframework.kafka.KafkaListenerEndpointContainer#1-0-L-1]   DEBUG o.s.retry.support.RetryTemplate - 上次尝试重试失败:   count = 1 09:24:26.856   [org.springframework.kafka.KafkaListenerEndpointContainer#1-0-L-1]   DEBUG c.c.c.tas.config.KafkaConsumerConfig - 在恢复回调中   方法!!

0 个答案:

没有答案