我想用假冒伪劣客户端调用异步rest端点,并做了以下更改。
当调用它时,CompletableFuture.get()没有完成。
一直在循环..。
while(!combinedFuture.isDone()) { log.info("useraccount - waiting for combinedFuture 2: " + request.toString()); }接口调用API:
@FeignClient(value = "clientUser", url = "http://localhost:8898/springboot", fallback = UserFallback.class)
public interface User {
@RequestMapping(method = RequestMethod.GET, value = "/user/", produces = "application/json")
@Async
CompletableFuture<UserInfo> findUserInfo(@RequestHeader(value = "Authorization", required = true) String authorizationHeader);
}控制器方法:
@PostMapping(value = "/springboot/useraccount/", consumes = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<?> userAccount(@requestbody @Valid AuthRequest request) {
log.info("useraccount - request received with request body: " + request.toString());
try {
if (Strings.isBlank(request.getUsername()) || Strings.isBlank(request.getPassword())) {
throw new BadRequestException("invalid username or password");
}
String token = authorisationService.obtainAuthToken(request.getUsername(), request.getPassword());
CompletableFuture<UserInfo> userInfo = clientUser.findUserInfo(token);
CompletableFuture<UserAccountInfo> userAccountInfo = clientAccount.findAccountInfo(token);
CompletableFuture<Void> combinedFuture
= CompletableFuture.allOf(userInfo, userAccountInfo);
while(!combinedFuture.isDone()) {
log.info("useraccount - waiting for combinedFuture 2: " + request.toString());
}
Optional<UserAccountResponse> userAccountResponse = userAccountService.getAccountInfo(
userAccountInfo.get(), userInfo.get()
);
if (userAccountResponse.isEmpty()) {
throw new BadCredentialsException("Bad Credentials");
}
return ResponseEntity.ok().body(userAccountResponse);
} catch (BadCredentialsException | UnAuthorizedException ex) {
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
} catch (BadRequestException ex) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
} catch (ExecutionException e) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
} catch (InterruptedException e) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
}
}(更新)已经改变了一点,以使用CompletableFuture.supplyAsync
但现在对象总是空的..。

@Service
public class AccountService {
@Autowired
Account accountClient;
@Async
public Optional<UserAccountInfo> getAccountInfo(String token) {
return Optional.of(accountClient.findAccountInfo(token));
}
}发布于 2022-11-30 17:38:12
设法像这样解决这个问题:
@Async
public CompletableFuture<UserAccountInfo> getAccountInfo(String token) {
try {
System.out.println(
"Current Thread account Name: "
+ Thread.currentThread().getName());
Thread.currentThread().sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return CompletableFuture.completedFuture(accountClient.findAccountInfo(token).getBody());
}userInfo服务也是如此。然后在控制器上:
CompletableFuture<UserAccountInfo> userAccountInfo = accountService.getAccountInfo(token);
CompletableFuture<UserInfo> userInfo = userService.getUserInfo(token);
Optional<UserAccountResponse> userAccountResponse = userAccountService.getAccountInfo(
userAccountInfo.get(),userInfo.get()
);因此,这意味着两个服务都将在一个新线程中开始运行,而主线程将继续运行,直到找到第一个.get()。
通过这样做,完成的最大等待时间是线程花费更多时间的时间,而不是两者之和(如果是同步的话)。
谢谢!
https://stackoverflow.com/questions/74616003
复制相似问题