AndyB
AndyB

Reputation: 513

Eagerly connect to topic as a Kafka Producer

I'm implementing a service which sends messages to a downstream service via a Kafka topic. This will only happen when my service's API is called, which is likely to be infrequently, at least at first.

I've found that the reactive Kafka producer API connects to Kafka lazily, which I'm sure is great for most use-cases, but I'd like to know that my Kafka connection configuration is correct at the point I deploy my service. I don't want to have to wait for the first API call only to find out that something somewhere is wrong.

The solution I've got for this at the moment is simply to send an initialisation message to the topic at start-up, but this feels clunky. Is there anything better I can be doing to force an initial connection to the topic, or at least to validate the connection configuration?

val response = kafkaSender.send(Mono.just(initialise))
        .next()
        .block();

if (response == null) {
    throw new RuntimeException("empty Mono from Kafka initialisation");
}

if (response.exception() != null) {
    throw propagate(response.exception());
}

Upvotes: 1

Views: 687

Answers (1)

OneCricketeer
OneCricketeer

Reputation: 191681

You can use AdminClient API to describe the cluster or the topic you're going to use if you prefer not to be sending heartbeats via a producer

Upvotes: 1

Related Questions