How to manage a RestEndpoint with Spring Application Events?

Tags: , ,



I´m developing a spring boot microservice and I´m raising application events to execute my services and do my business stuff. It works well when I listen from Kafka, but also I want to implement an endpoint that returns a response. I raise my application event in the restController but what I don´t know is how to get the result of the service to return the value in my controller.

    public String publisRequestEvent(@RequestHeader HttpHeaders httpHeaders,
            @RequestBody MyDto myDTO) throws InterruptedException {
        
        MyRequestEvent event =
                new MyRequestEvent(myDTO);
        applicationEventPublisher.publishEvent(event);

        //TODO how I listen to the result once the event has been processed?
        return "result.";

    } ```

Answer

You can not get result of your event. Both sync or asyn ApplicationEventPublisher returns void. And your event startes in another thread.



Source: stackoverflow