说明:调用第三方服务接口超时处理,可使用以下代码设置接口方法超时时间,比如设置10s,如果10s接口数据未返回,则会抛出TimeoutException ,此时可以捕获此异常,进行其它业务处理或者给出相应的提示。
public List<UserVO> listUser() {
ExecutorService executor = Executors.newSingleThreadExecutor();
FutureTask<List<UserVO>> future = new FutureTask<>(() -> {
// 调用第三方服务接口获取数据
List<UserVO> userVOList = feignClient.listUser().getData();
return userVOList;
});
executor.execute(future);
try {
future.get(10000, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
log.error("InterruptedException:", e);
Thread.currentThread().interrupt();
} catch (ExecutionException e) {
log.error("ExecutionException:", e);
} catch (TimeoutException e) {
// 10s后超时进行其他业务处理,或者在此处设置个标记返回调用方进行处理
} finally {
future.cancel(true);
executor.shutdown();
}
return new ArrayList<>();
}
如有问题,请留言,谢谢~