0

Actually I am working in a Kafka streams application using Spring Boot.

So here I am trying to handle exceptions globally using @ControllerAdvice but it is not working for me.

Is it possible to use @ControllerAdvice in my application.

Is this controller advice is only works when the error is coming from controller.

Note: I am not having any controller / rest controller endpoints in my application.

Can we achieve the same in some other ways?

Please share your valuable thoughts!

Main Stream:

@Autowired
private XyzTransformer xyztransformer;

@Bean
public KStream<String, ABC> processMember(@Qualifier("defaultKafkaStreamsBuilder") StreamsBuilder streamsBuilder) {
    try {
        LOGGER.info("streaming started...");
        KStream<String, Xyz> xyStream = streamsBuilder.stream(appProperty.kafkaStreamsTopicInput)                   
                .transformValues(() -> xyztransformer)
        xyStream.to(appProperty.kafkaStreamsTopicOutput);
        return memberStream;
    } catch (Exception ex) {
        LOGGER.error("Exception occurred in Streams " + Arrays.toString(ex.getStackTrace()));
        throw ex;
    }
}

TransformerClass:

@Component
public class XyzTransformer implements ValueTransformer<Xyz, Abc> {

    @Override
    public void close() {
    }

    @Override
    public void init(ProcessorContext processorContext) {
    }

    @SneakyThrows
    @Override
    public Abc transform(Xyz data) {
        String[] dataSourceTables = new String[]{"abc"};
        try {
            return Abc.builder()
                    .name(data.getName())
                    .build();
        } catch (Exception ex) {
        System.out.println("catched and throwing");  
        throw new CustomTesException("test 1");
        }
    }
}

ControllerAdvice:

@ControllerAdvice
public class Advice extends ResponseEntityExceptionHandler {

    @ExceptionHandler(NullPointerException.class)
    public final void handleAllExceptions(NullPointerException ex) {
        System.out.println("it is in the handler");
        System.out.println(ex.getMessage());
    }

    @ExceptionHandler(Exception.class)
    public final void handleAllException(Exception ex) {
        System.out.println("it is in the exception handler");
        System.out.println(ex.getMessage());
    }

    @ExceptionHandler(CustomTesException.class)
    public final void handleAllExceptio(CustomTesException ex) {
        System.out.println("it is in the exception handler");
        System.out.println(ex.getMessage());
    }   
}
İsmail Y.
  • 3,579
  • 5
  • 21
  • 29
RandomUser
  • 31
  • 1
  • 8
  • The name `@ControllerAdvice` (as well as the package it resides in )implies it use, it is for `@Controllers`, the web. So it isn't usable for anything else. – M. Deinum Feb 22 '21 at 15:59
  • ControllerAdvise is used only to handle exception and respond to an API, It works as RequestMapping. – Arun Prasat Feb 22 '21 at 16:11
  • Possible duplicate of: https://stackoverflow.com/questions/52311512/controller-kafka-listener-exception-not-catched-by-exception-handler-controlle – Majid Ali Khan Dec 10 '21 at 13:08

0 Answers0