Maciavelli
Maciavelli

Reputation: 101

post processing in spring boot webflux

I have a method in a restcontroller. I need to convert input request to String and send to another destination after method execution is totally completed, ideally I should not affect in method processing execution, method should execute without additional delay. In non webflux application I can use afterCompletion in HandlerIntercepter. But due to use WebFlux I cannot use that. How can I make additional processing after controller method execution ends, and response returned to caller?

Upvotes: 1

Views: 880

Answers (1)

meberhard
meberhard

Reputation: 1845

If the logic you want to call is specific to this one controller, you could use doFinally. It is executed after your Stream (Mono/Flux) terminates. https://projectreactor.io/docs/core/release/api/reactor/core/publisher/Flux.html#doFinally-java.util.function.Consumer-

Example:

public class Playground {

    ConverterService converterService;
    PostProcessor postProcessor;

    public Playground(ConverterService converterService, PostProcessor postProcessor) {
        this.converterService = converterService;
        this.postProcessor = postProcessor;
    }

    public Mono<String> endpoint(Integer input) {
        return Mono.just(input)
                .flatMap(integer -> converterService.convert(integer))
                .doFinally(signalType -> postProcess(input));
    }

    private void postProcess(Integer input) {
        this.postProcessor.process(input);
    }

}

First the converterService is called. After the stream is terminated, the postProcessor is called in any case.

Upvotes: 1

Related Questions