Kafka手动提交offset

1 监听代码


import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.kafka.listener.AcknowledgingMessageListener;
import org.springframework.kafka.support.Acknowledgment;

public class KafkaLisstener implements AcknowledgingMessageListener {
    private static Logger logger = LoggerFactory.getLogger(KafkaLisstener.class);

    private IGateInfoService gateInfoService;

    private static final Logger log = LoggerFactory.getLogger(KafkaLisstener.class);

    @Override
    public void onMessage(ConsumerRecord data, final Acknowledgment acknowledgment) {
        String message = data.value();
        log.info("offset={},partition={},timestamp={}", data.offset(), data.partition(), data.timestamp());
        log.info("message={}", message);
        acknowledgment.acknowledge();//提交offset
    }

}

2 kafka配置



    
    
        
            
                
                
                
                
                
                
            
        
    

    
    
        
            
        
    

    
    
        
    

    
    
        
        
        
    
    
    
        
        
        
    


    
        
            
        
    

enable.auto.commit=false

注:配置消费者容器配置信息时,配置ackMode(忘记配置会出现Acknowledgment acknowledgment=null的情况)

  • RECORD
    每处理一条commit一次
  • BATCH(默认)
    每次poll的时候批量提交一次,频率取决于每次poll的调用频率
  • TIME
    每次间隔ackTime的时间去commit
  • COUNT
    累积达到ackCount次的ack去commit
  • COUNT_TIME
    ackTime或ackCount哪个条件先满足,就commit
  • MANUAL
    listener负责ack,但是背后也是批量上去
  • MANUAL_IMMEDIATE
    listner负责ack,每调用一次,就立即commit

 

你可能感兴趣的:(kafka)