+++ /dev/null
-#!/bin/bash
-
-IMAGE=juplo/spring-consumer:1.1-rebalance-listener-SNAPSHOT
-
-if [ "$1" = "cleanup" ]
-then
- docker compose -f docker/docker-compose.yml down -t0 -v --remove-orphans
- mvn clean
- exit
-fi
-
-docker compose -f docker/docker-compose.yml up -d --remove-orphans kafka-1 kafka-2 kafka-3
-docker compose -f docker/docker-compose.yml rm -svf consumer-1 consumer-2
-
-if [[
- $(docker image ls -q $IMAGE) == "" ||
- "$1" = "build"
-]]
-then
- mvn clean install || exit
-else
- echo "Using image existing images:"
- docker image ls $IMAGE
-fi
-
-docker compose -f docker/docker-compose.yml up --remove-orphans setup || exit 1
-
-
-docker compose -f docker/docker-compose.yml up -d producer
-docker compose -f docker/docker-compose.yml up -d consumer-1
-sleep 10
-docker compose -f docker/docker-compose.yml exec cli http -v consumer-1:8881/
-
-docker compose -f docker/docker-compose.yml up -d consumer-2
-sleep 10
-docker compose -f docker/docker-compose.yml exec cli http -v consumer-1:8881/
-docker compose -f docker/docker-compose.yml exec cli http -v consumer-2:8881/
-
-docker compose -f docker/docker-compose.yml stop producer consumer-1
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.Consumer;
-import org.apache.kafka.clients.consumer.ConsumerRebalanceListener;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.common.TopicPartition;
@Slf4j
-public class ExampleConsumer implements Runnable, ConsumerRebalanceListener
+public class ExampleConsumer implements Runnable
{
private final String id;
private final String topic;
counterState = new CounterState[numPartitions];
log.info("{} - Subscribing to topic {}", id, topic);
- consumer.subscribe(Arrays.asList(topic), this);
+ consumer.subscribe(Arrays.asList(topic));
running = true;
while (running)
}
- @Override
- public void onPartitionsAssigned(Collection<TopicPartition> partitions)
- {
- partitions
- .stream()
- .filter(partition -> partition.topic().equals(topic))
- .forEach(partition ->
- {
- assignedPartitions.add(partition);
- counterState[partition.partition()] = new CounterState(new HashMap<>());
- });
- }
-
- @Override
- public synchronized void onPartitionsRevoked(Collection<TopicPartition> partitions)
- {
- partitions
- .stream()
- .filter(partition -> partition.topic().equals(topic))
- .forEach(partition ->
- {
- assignedPartitions.remove(partition);
- counterState[partition.partition()] = null;
- });
- }
-
-
public void shutdown() throws InterruptedException
{
log.info("{} joining the worker-thread...", id);