I'm working with Spring WebFlux and I need to perform an asynchronous task as part of a method that should not block the main response to the user. Specifically, I want to call an asynchronous method after completing the main task, but without delaying the response.
Here's a simplified version of what I'm trying to achieve:
public MonopublishPackage(RequestDTO requestDTO) { return publishPackageService.doSomething(requestDTO) .flatMap(responseDTO -> doSomethingInAsync(requestDTO, responseDTO) .thenReturn(responseDTO) ); } // Method that simulates an asynchronous task with a 5-second delay public Mono doSomethingInAsync(RequestDTO requestDTO, ResponseDTO responseDTO) { return Mono.delay(Duration.ofSeconds(5)) .then(); // Converts the delayed Mono to Mono }
After this call completes, I want to execute doSomethingInAsync(requestDTO, responseDTO) asynchronously.
The doSomethingInAsync method should be non-blocking and not delay the main response.
Problem:
The doSomethingInAsync method is being executed, but it seems like it might be blocking the response or not running asynchronously as intended. How can I ensure that doSomethingInAsync runs asynchronously and does not block the response to the user?
Details:
publishPackageService.doSomething(requestDTO): Returns a Mono.
doSomethingInAsync(requestDTO, responseDTO): Is an asynchronous method that I want to run without blocking the response.
Questions:
How can I ensure doSomethingInAsync runs in the background without blocking the response?
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3