1.利用java8 CompletableFuture 异步执行
@Resource
private Executor threadPoolTaskExecutor;
@Autowired
private KubernetesService kubernetesService;
/**
* 更新系统镜像
*
* @param version
* @return
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
public BaseResponse excuteUpdateImage(String version) {
String image = "android:" + version;
List<VirtualMachine> virtualMachineList = virtualMachineMapper.selectAllNotMaintenanceList();
List<String> nameList = virtualMachineList.stream().map(v -> v.getName()).collect(Collectors.toList());
/**
* 异步执行进行
*/
List<String> failList = new ArrayList<>(); // 失败结果
List<String> successList = new ArrayList<>(); // 成功结果
CompletableFuture[] cfs = nameList.parallelStream()
.map(name -> CompletableFuture
.supplyAsync(() -> kubernetesService.setVMImage(name, image), threadPo