]]
then
docker-compose rm -svf adder-1 adder-2
- mvn -D skipTests clean install || exit
+ mvn clean install || exit
else
echo "Using image existing images:"
docker image ls $IMAGE
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
+public class ApplicationRebalanceListener implements RebalanceListener
{
private final ApplicationRecordHandler recordHandler;
private final AdderResults adderResults;
state.getOffset()));
});
}
+
+ @Override
+ public void beforeNextPoll()
+ {
+ partitions
+ .stream()
+ .forEach(partition ->
+ {
+ log.info("{} - persisting state & offset for partition: {}", id, partition);
+ ApplicationState state = recordHandler.getState(partition);
+ log.info(
+ "{} - offset of next unseen message for partition {} is {}",
+ id,
+ partition,
+ state.getOffset());
+ for (String user : state.getAdderState().keySet())
+ {
+ log.info(
+ "{} - Saved state for partition={}|user={}: {}",
+ id,
+ partition,
+ user,
+ state.getAdderState().get(user));
+ }
+ Map<String, List<AdderResult>> results = adderResults.getState(partition);
+ stateRepository.save(
+ new StateDocument(
+ partition,
+ state.getAdderState(),
+ results,
+ state.getOffset()));
+ });
+ }
}
protected ApplicationState removePartition(Integer partition)
{
- ApplicationState state =
- new ApplicationState(
- this.next.get(partition),
- this.state.remove(partition).getState());
-
+ ApplicationState state = getState(partition);
this.next.remove(partition);
-
+ this.state.remove(partition);
return state;
}
return state;
}
- public AdderBusinessLogic getState(Integer partition)
+ public ApplicationState getState(Integer partition)
{
- return state.get(partition);
+ return
+ new ApplicationState(
+ this.next.get(partition),
+ this.state.get(partition).getState());
}
}
private final String id;
private final String topic;
private final Consumer<K, V> consumer;
- private final ConsumerRebalanceListener rebalanceListener;
+ private final RebalanceListener 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 RebalanceListener 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
- ConsumerRebalanceListener rebalanceListener;
+ RebalanceListener rebalanceListener;
@Autowired
RecordHandler<K, V> recordHandler;