异步执行
- 远程查询库存
@FeignClient("gulimall-ware")
public interface WmFeignService {
//查询是否有库存
@PostMapping("ware/waresku/hasstock")//ware/waresku
public R getSkusHassStock(@RequestBody List<Long> skuIds);
}
gulimall-ware 中注册了发现 EnableDiscoveryClient
@EnableFeignClients
@EnableDiscoveryClient //注册发现
注入
@Autowired
WmFeignService wmFeignService;
//异步执行
CompletableFuture<Void> getCartFuture =
CompletableFuture.runAsync(
() -> {
System.out.println("副线程..." + Thread.currentThread().getId());
// 查询购物车所有选中的购物项
RequestContextHolder.setRequestAttributes(requestAttributes);
List<OrderItemVo> items = cartFeginService.getCurrentUserCartItems();
confirmVo.setItems(items);
},
executor).thenRunAsync(()->{
//继续执行异步操作
List<OrderItemVo> items = confirmVo.getItems();
//拿到商品id
List<Long> collect = items.stream().map(item -> item.getSkuId()).collect(Collectors.toList());
//远程查询库存
R hasStock = wmFeignService.getSkusHasStock(collect);
List<SkuStockVo> data = hasStock.getData(new TypeReference<List<SkuStockVo>>() {
});
if(data!=null){
Map<Long, Boolean> collect1 = data.stream().collect(Collectors.toMap(SkuStockVo::getSkuId, SkuStockVo::getHsaStock));
confirmVo.setStocks(collect1);
}
});
标签:异步,执行,confirmVo,items,List,collect,ware
From: https://blog.51cto.com/u_15993308/6412118