com.mageddo.kafka.client.RecordConsumer Maven / Gradle / Ivy
package com.mageddo.kafka.client;
import java.util.concurrent.atomic.AtomicBoolean;
import org.apache.kafka.clients.consumer.Consumer;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
@Slf4j
@RequiredArgsConstructor
public class RecordConsumer extends DefaultConsumer {
private final Consumer consumer;
private final Consumers consumers;
@Override
protected void consume(ConsumerRecords records) {
for (final ConsumerRecord record : records) {
final AtomicBoolean recovered = new AtomicBoolean();
Retrier
.builder()
.retryPolicy(this.consumers.retryPolicy())
.onExhausted((lastFailure) -> {
if(log.isDebugEnabled()){
log.debug("exhausted tries");
}
this.doRecoverWhenAvailable(
DefaultRecoverContext
.builder()
.consumer(this.consumer)
.lastFailure(lastFailure)
.record(record)
.build()
,
this.consumers.recoverCallback()
);
recovered.set(true);
})
.onRetry(() -> {
log.info("failed to consume");
commitSyncRecord(this.consumer, record);
})
.build()
.run(() -> {
if (log.isTraceEnabled()) {
log.info("status=consuming, record={}", record);
}
try {
this.consumers
.callback()
.accept(
DefaultCallbackContext
.builder()
.consumer(this.consumer)
.records(records)
.record(record)
.build(),
record
);
} catch (Exception e) {
Exceptions.throwException(e);
}
});
if (recovered.get()) {
// pare o consumo para fazer poll imediatamente
// e não chegar no timeout por não ter chamado poll
// por causa das retentativas dessa mensagem
return;
}
}
this.consumer.commitSync();
}
@Override
protected Consumer consumer() {
return this.consumer;
}
@Override
protected Consumers consumerConfig() {
return this.consumers;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy