, @Async findEmail UserRepository, , , , . User user = userRepository.findByEmail(email); findByEmail List.
@Async , @EnableAsync, @EnableAsync @Async findEmail .
return userService.findByEmail(email); CompletableFuture, UserService.
, thenApplyAsync CompletableFuture , userService.findByEmail(email) , CompletableFuture.
return userService.findByEmail(email).thenApplyAsync(user -> {
return user;
})
, @Async findByEmail, , , @EnableAsync
, , , 2 , , method1, method2 , , method3, 6 . , 2 6.
:
@Async
public CompletableFuture<Boolean> veryLongMethod() {
try {
Thread.sleep(2000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
return CompletableFuture.completedFuture(true);
}
,
@RequestMapping(value = "test")
public @ResponseBody CompletableFuture<User> test(@RequestParam(value = "email", required = true) String email) throws InterruptedException {
CompletableFuture<Boolean> boolean1= siteService.veryLongMethod();
CompletableFuture<Boolean> boolean2= siteService.veryLongMethod();
CompletableFuture<Boolean> boolean3= siteService.veryLongMethod();
CompletableFuture.allOf(boolean1,boolean2,boolean3).join();
return userService.findByEmail(email);
}
, , , 6 , , 2 , .
. : @Async, Spring, CompletableFuture
, .