首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >FeignClient Springboot与CompletableFuture异步调用

FeignClient Springboot与CompletableFuture异步调用
EN

Stack Overflow用户
提问于 2022-11-29 14:57:57
回答 1查看 40关注 0票数 0

我想用假冒伪劣客户端调用异步rest端点,并做了以下更改。

当调用它时,CompletableFuture.get()没有完成。

一直在循环..。

代码语言:javascript
复制
while(!combinedFuture.isDone()) { log.info("useraccount - waiting for combinedFuture 2: " + request.toString()); }

接口调用API:

代码语言:javascript
复制
@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);
}

控制器方法:

代码语言:javascript
复制
@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

但现在对象总是空的..。

代码语言:javascript
复制
@Service
public class AccountService {

@Autowired
Account accountClient;

@Async
public Optional<UserAccountInfo> getAccountInfo(String token) {
    return Optional.of(accountClient.findAccountInfo(token));
}

}
EN

回答 1

Stack Overflow用户

发布于 2022-11-30 17:38:12

设法像这样解决这个问题:

代码语言:javascript
复制
 @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服务也是如此。然后在控制器上:

代码语言:javascript
复制
CompletableFuture<UserAccountInfo> userAccountInfo = accountService.getAccountInfo(token);
       CompletableFuture<UserInfo> userInfo = userService.getUserInfo(token);

       Optional<UserAccountResponse> userAccountResponse = userAccountService.getAccountInfo(
               userAccountInfo.get(),userInfo.get()
       );

因此,这意味着两个服务都将在一个新线程中开始运行,而主线程将继续运行,直到找到第一个.get()。

通过这样做,完成的最大等待时间是线程花费更多时间的时间,而不是两者之和(如果是同步的话)。

谢谢!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74616003

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档