Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

111
Vistas
Cannot catch Kafka TopicExistsException

I have written a program which creates Kafka topics upon startup with AdminClient. If a topic exists, a TopicExistsException is thrown. However I am not able to catch it. Related code:

try {
    CreateTopicsResult result = adminClient.createTopics(Collections.singleton(new NewTopic(topic, 2, (short) 1)));
    result.all().get(5, TimeUnit.SECONDS);
} catch (TopicExistsException e){
    log.info("topic exists: {}", topic);
} catch ( InterruptedException | TimeoutException | ExecutionException e){
    log.error("Topic create failed with: {}", e.getMessage());
}

The expected behavior would be that the first catch is executed, but based on the logs this is not what happens:

2020-12-01 13:18:56,462 ERROR [restartedMain] com.mypackage.KafkaService@lambda$configureTopics$0:75 - Topic create failed with: org.apache.kafka.common.errors.TopicExistsException: Topic 'MEASUREMENT_TEST' already exists.

My imports are for the exceptions:

import org.apache.kafka.common.errors.TopicExistsException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeoutException;
import java.lang.InterruptedException;

Any suggestion? Is there anything I am missing?

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

This is because the topic exists exception is getting wrapped in the ExecutionException that gets thrown when the KafkaFuture completes.

You can see this by logging the full stack trace, not just the getMessage() part:

java.util.concurrent.ExecutionException: org.apache.kafka.common.errors.TopicExistsException: Topic 'hello' already exists.
    at org.apache.kafka.common.internals.KafkaFutureImpl.wrapAndThrow(KafkaFutureImpl.java:45) ~[kafka-clients-2.0.0.jar:?]
    at org.apache.kafka.common.internals.KafkaFutureImpl.access$000(KafkaFutureImpl.java:32) ~[kafka-clients-2.0.0.jar:?]
    at org.apache.kafka.common.internals.KafkaFutureImpl$SingleWaiter.await(KafkaFutureImpl.java:89) ~[kafka-clients-2.0.0.jar:?]
    at org.apache.kafka.common.internals.KafkaFutureImpl.get(KafkaFutureImpl.java:262) ~[kafka-clients-2.0.0.jar:?]
Caused by: org.apache.kafka.common.errors.TopicExistsException: Topic 'hello' already exists.

So if you want to catch the TopicExistsException, you've got to do something like:

try {
    CreateTopicsResult result = adminClient.createTopics(Collections.singleton(new NewTopic(topic, 2, (short) 1)));
    result.all().get(5, TimeUnit.SECONDS);
} catch (ExecutionException e){
    if(e.getCause() != null && e.getCause() instanceof TopicExistsException) {
        log.info("topic exists: {}", topic);
    } else {
        log.error("Topic create failed with: {}", e);
    }
} catch ( InterruptedException | TimeoutException){
    log.error("Topic create failed with: {}", e.getMessage());
} 
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda