recordHandler,
adderResults,
stateRepository,
- properties.getClientId(),
- Clock.systemDefaultZone(),
- properties.getCommitInterval());
+ properties.getClientId());
}
@Bean
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
+import org.apache.kafka.clients.consumer.ConsumerRebalanceListener;
import org.apache.kafka.common.TopicPartition;
import java.time.Clock;
@RequiredArgsConstructor
@Slf4j
-public class ApplicationRebalanceListener implements PollIntervalAwareConsumerRebalanceListener
+public class ApplicationRebalanceListener implements ConsumerRebalanceListener
{
private final ApplicationRecordHandler recordHandler;
private final AdderResults adderResults;
private final StateRepository stateRepository;
private final String id;
- private final Clock clock;
- private final Duration commitInterval;
private final Set<Integer> partitions = new HashSet<>();
- private Instant lastCommit = Instant.EPOCH;
-
@Override
public void onPartitionsAssigned(Collection<TopicPartition> partitions)
{
stateRepository.save(new StateDocument(partition, state, results));
});
}
-
-
- @Override
- public void beforeNextPoll()
- {
- if (lastCommit.plus(commitInterval).isBefore(clock.instant()))
- {
- log.debug("Storing data, last commit: {}", lastCommit);
- partitions.forEach(partition -> stateRepository.save(
- new StateDocument(
- partition,
- recordHandler.getState(partition).getState(),
- adderResults.getState(partition))));
- lastCommit = clock.instant();
- }
- }
}
private final String id;
private final String topic;
private final Consumer<K, V> consumer;
- private final PollIntervalAwareConsumerRebalanceListener rebalanceListener;
+ private final ConsumerRebalanceListener rebalanceListener;
private final RecordHandler<K, V> recordHandler;
private final Lock lock = new ReentrantLock();
consumed++;
}
-
- rebalanceListener.beforeNextPoll();
}
}
catch(WakeupException e)
+++ /dev/null
-package de.juplo.kafka;
-
-import org.apache.kafka.clients.consumer.ConsumerRebalanceListener;
-
-
-public interface PollIntervalAwareConsumerRebalanceListener extends ConsumerRebalanceListener
-{
- default void beforeNextPoll() {}
-}
import com.mongodb.client.MongoClient;
import lombok.extern.slf4j.Slf4j;
+import org.apache.kafka.clients.consumer.ConsumerRebalanceListener;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.clients.producer.KafkaProducer;
@Autowired
MongoProperties mongoProperties;
@Autowired
- PollIntervalAwareConsumerRebalanceListener rebalanceListener;
+ ConsumerRebalanceListener rebalanceListener;
@Autowired
RecordHandler<K, V> recordHandler;
/** Tests methods */
@Test
- void commitsCurrentOffsetsOnSuccess()
+ void commitsCurrentOffsetsOnSuccess() throws Exception
{
int numberOfGeneratedMessages =
recordGenerator.generate(false, false, messageSender);
.isThrownBy(() -> endlessConsumer.exitStatus())
.describedAs("Consumer should still be running");
+ endlessConsumer.stop();
recordGenerator.assertBusinessLogic();
}
{
try
{
- endlessConsumer.stop();
testRecordProducer.close();
offsetConsumer.close();
}