'Method returns result immidiately after first Async call is complete
Below mentioned is the code sample for reference.
public void myMethod() {
myService.getState().ifPresentOrElse(
id -> sendAsync(doSomeOpeartion1(), msg).
.exceptionally(t-> {log.error("error 1")})
.thenCompose(receipt -> sendAsync(doSomeOpeartion2(), msg )).
.exceptionally(t-> {log.error("error 2")})
.thenCompose(receipt -> sendAsync(doSomeOpeartion3(), msg )).
.exceptionally(t-> {log.error("error 3")})
.thenCompose(receipt -> sendAsync(doSomeOpeartion4(), msg )).
.exceptionally(t-> {log.error("error 4")})
.thenCompose(receipt -> sendAsync(doSomeOpeartion5(), msg ))).
.exceptionally(t-> {log.error("error 5")})
.thenAccept(receipt-> dosomeprocessing()),
() -> {throw nwe TransactionException("");});
log.info("Processing complete");
}
My problem is that only doSomeOpeartion1() is executed and the the processing jumps to .thenAccept() and finally prints "Processing complete" message. Then it immediately returns the response to the calling method. However, in the background it continues to execute remianing methods that are doSomeOpeartion2(), doSomeOpeartion3(), doSomeOpeartion4() and doSomeOpeartion5().
How do I make my program to first execute all the async calls and only then finish the execution of myMethod()?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|