How can I catch the ProvisioningException that can occur when Spring kafka cannot connect to Producer endpoint?

Viewed 261

How can I catch the ProvisioningException that can occur when Spring kafka cannot connect to Producer endpoint during Spring-Boot initialization phase?

I am using the lib called spring-cloud-stream, or in my case, more specifically: spring-cloud-starter-stream-kafka .

I assume there must be a way to define a config-Bean, or AOP, that might be able to catch the null error thrown due to connection problems? The existing error is not informative enough when connectivity is gone:

Caused by: java.util.concurrent.TimeoutException: null
    at org.apache.kafka.common.internals.KafkaFutureImpl$SingleWaiter.await(KafkaFutureImpl.java:108)
    at org.apache.kafka.common.internals.KafkaFutureImpl.get(KafkaFutureImpl.java:272)
    at org.springframework.cloud.stream.binder.kafka.provisioning.KafkaTopicProvisioner.createTopicAndPartitions(KafkaTopicProvisioner.java:355)
    at org.springframework.cloud.stream.binder.kafka.provisioning.KafkaTopicProvisioner.createTopicIfNecessary(KafkaTopicProvisioner.java:329)
    at org.springframework.cloud.stream.binder.kafka.provisioning.KafkaTopicProvisioner.createTopic(KafkaTopicProvisioner.java:306)

I'm talking about the "Bean Creation Phases" documented here: https://reflectoring.io/spring-bean-lifecycle/ which describes how to hook your own code to initialization phases but I want to hook 3rd party code.

NOTE: The bounty is expired. An answer outside of the context of Spring Kafka is ok the answer is still relevant to my question.

NOTE: I found another question that also does not provide a good answer: Spring - catch bean creation exception

1 Answers

you can for example Capture that in ExcepcionHander

@ControllerAdvice
@RestController
public class ExceptionHandler {

  @ExceptionHandler(value = {ProvisioningException.class})
  public void handleException(ProvisioningExceptione) {
    ....code....
  }

I mean the main paoint is to know when do you get this error.... when invoking a method the method should throw it but instead if it is on the initializacion or something like that, it is diferent. I can see such information in your question, please be more specific

Related