Skip to content

Instantly share code, notes, and snippets.

@mchmielarz
Created December 15, 2020 11:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mchmielarz/6241538bf920609041c61d4c0551e442 to your computer and use it in GitHub Desktop.
Save mchmielarz/6241538bf920609041c61d4c0551e442 to your computer and use it in GitHub Desktop.
// create required number of vertices
IntStream.range(0, numberOfPartitions)
.forEach(partition -> {
vertx.deployVerticle(
() -> KafkaPartitionedVerticle.create(topic, partition, kafkaConfig),
deploymentOptions,
async -> log.info("Partitioned Kafka consumer deployed. DeploymentId: {}", async.result())
);
});
// inside KafkaPartitionedVerticle
class KafkaPartitionedVerticle extends AbstractVerticle {
// initialization
@Override
public void start() {
KafkaConsumer.create(vertx, kafkaConfig)
.assign(new TopicPartition(topic, partition), AsyncResult::result)
.handler(record ->
// processing logic comes here
)
.endHandler(v -> log.info("End of data. Topic: {}, partition: {}", this.topic, this.partition))
.exceptionHandler(e -> log.error("Partitioned Kafka consumer error", e));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment