简体   繁体   中英

When offset is commited in Spring Kafka

I read that since version 2.3 of Spring Kafka enable.auto.commit is false by default because Kafka's native implementation of autocommiting mechanism is based on time scheduling, which is quite inconvenient. Instead of it Spring Kafka offers a commiting mechanism, bounded to record processing, ie you can choose BATCH, RECORD, MANUAL, etc. for AckMode. So, the question is: for example, default AckMode is BATCH. When is the offset commited? Before passing a batch to a @KafkaListener handler method or after?

See JavaDocs of the ContainerProperties :

/**
 * The ack mode to use when auto ack (in the configuration properties) is false.
 * <ul>
 * <li>RECORD: Commit the offset after each record has been processed by the
 * listener.</li>
 * <li>BATCH: Commit the offsets for each batch of records received from the consumer
 * when they all have been processed by the listener</li>
 * <li>TIME: Commit pending offsets after {@link #setAckTime(long) ackTime} number of
 * milliseconds; (should be greater than
 * {@code #setPollTimeout(long) pollTimeout}.</li>
 * <li>COUNT: Commit pending offsets after at least {@link #setAckCount(int) ackCount}
 * number of records have been processed</li>
 * <li>COUNT_TIME: Commit pending offsets after {@link #setAckTime(long) ackTime}
 * number of milliseconds or at least {@link #setAckCount(int) ackCount} number of
 * records have been processed</li>
 * <li>MANUAL: Listener is responsible for acking - use a
 * {@link org.springframework.kafka.listener.AcknowledgingMessageListener}. Acks will
 * be queued and offsets will be committed when all the records returned by the
 * previous poll have been processed by the listener.</li>
 * <li>MANUAL_IMMDEDIATE: Listener is responsible for acking - use a
 * {@link org.springframework.kafka.listener.AcknowledgingMessageListener}. The commit
 * will be performed immediately if the {@code Acknowledgment} is acknowledged on the
 * calling consumer thread. Otherwise, the acks will be queued and offsets will be
 * committed when all the records returned by the previous poll have been processed by
 * the listener. Results will be indeterminate if you sometimes acknowledge on the
 * calling thread and sometimes not.</li>
 * </ul>
 */
private AckMode ackMode = AckMode.BATCH;

And also official Reference Manual: https://docs.spring.io/spring-kafka/reference/html/#committing-offsets

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM