return new AdderResults();
}
- @Bean
- public ApplicationRebalanceListener rebalanceListener(
- ApplicationRecordHandler recordHandler,
- AdderResults adderResults,
- StateRepository stateRepository,
- ApplicationProperties properties)
- {
- return new ApplicationRebalanceListener(
- recordHandler,
- adderResults,
- stateRepository,
- properties.getClientId());
- }
-
@Bean
public EndlessConsumer<String, String> endlessConsumer(
KafkaConsumer<String, String> kafkaConsumer,
ExecutorService executor,
- ApplicationRebalanceListener rebalanceListener,
ApplicationRecordHandler recordHandler,
ApplicationProperties properties)
{
properties.getClientId(),
properties.getTopic(),
kafkaConsumer,
- rebalanceListener,
recordHandler);
}
+++ /dev/null
-package de.juplo.kafka;
-
-import lombok.RequiredArgsConstructor;
-import lombok.extern.slf4j.Slf4j;
-import org.apache.kafka.clients.consumer.ConsumerRebalanceListener;
-import org.apache.kafka.common.TopicPartition;
-
-import java.util.*;
-
-
-@RequiredArgsConstructor
-@Slf4j
-public class ApplicationRebalanceListener implements ConsumerRebalanceListener
-{
- private final ApplicationRecordHandler recordHandler;
- private final AdderResults adderResults;
- private final StateRepository stateRepository;
- private final String id;
-
- private final Set<Integer> partitions = new HashSet<>();
-
- @Override
- public void onPartitionsAssigned(Collection<TopicPartition> partitions)
- {
- partitions.forEach(tp ->
- {
- Integer partition = tp.partition();
- log.info("{} - adding partition: {}", id, partition);
- });
- }
-
- @Override
- public void onPartitionsRevoked(Collection<TopicPartition> partitions)
- {
- partitions.forEach(tp ->
- {
- Integer partition = tp.partition();
- log.info("{} - removing partition: {}", id, partition);
- Map<String, AdderResult> state = recordHandler.getState(partition).getState();
- for (String user : state.keySet())
- {
- log.info(
- "{} - Saved state for partition={}|user={}: {}",
- id,
- partition,
- user,
- state.get(user));
- }
- });
- }
-}
private final String id;
private final String topic;
private final Consumer<K, V> consumer;
- private final ConsumerRebalanceListener rebalanceListener;
private final RecordHandler<K, V> recordHandler;
private final Lock lock = new ReentrantLock();
try
{
log.info("{} - Subscribing to topic {}", id, topic);
- consumer.subscribe(Arrays.asList(topic), rebalanceListener);
+ consumer.subscribe(Arrays.asList(topic));
while (true)
{
@Autowired
MongoProperties mongoProperties;
@Autowired
- ConsumerRebalanceListener rebalanceListener;
- @Autowired
RecordHandler<K, V> recordHandler;
KafkaProducer<Bytes, Bytes> testRecordProducer;
properties.getClientId(),
properties.getTopic(),
kafkaConsumer,
- rebalanceListener,
captureOffsetAndExecuteTestHandler);
endlessConsumer.start();