Spring kafka has a message-driven consumer model; you provide a POJO message listener and the framework performs the poll and passes the message to the listener, either one-at-a-time or in a batch.
It has various modes for committing offsets (it prefers turning off enable.auto.commmit
in the client).
There are two modes for manual acks AckMode.MANUAL
and AckMode.MANUAL_IMMEDIATE
; with these modes, we pass an Acknowledgment
object to the listener bean and you call ack.acknowledge()
.
When the mode is MANUAL_IMMEDIATE
, as long as you call the acknowledge()
on the consumer thread the consumer is called directly.
When the mode is MANUAL
, the offset is added to an internal queue and the commits will be done at the end of processing the results of the poll.
Similarly, there are several "auto" ack modes; the main being RECORD
and BATCH
where the container commits the offsets when the listener exits normally. In record mode, the commit is sent after each record is processed, in batch mode, the commits are done after all the results of the poll are handled.
Committing offsets in batches is more efficient, but increases the risk of duplicate deliveries.
We also commit any pending offsets when a rebalance occurs.
So, why the two onPartitionsRevoked*
methods?
When using MANUAL, BATCH, or one of the other AckMode
s that might have pending offsets to commit, onPartitionsRevokedBeforeCommit()
is called before those pending offsets are committed and onPartitionsRevokedAfterCommit()
is called after those offsets are committed.
So, consumer.position()
may return different results in each method.
Most people will be interested in onPartitionsRevokedAfterCommit()
but we felt we should provide both options.
If you use AckMode.MANUAL_IMMEDIATE
or AckMode.RECORD
, there should be no difference since there will be no pending acks.
However, since the listener is called on the consuming thread, during a poll, there will really only be a difference when using one of the time-based or count-based AckMode
s. With the other ackmodes we will already have committed the offsets.
Hope that's clear.