diff --git a/xlcs-parent/service/service-home/src/main/java/com/yovinchen/xlcs/home/service/impl/HomeServiceImpl.java b/xlcs-parent/service/service-home/src/main/java/com/yovinchen/xlcs/home/service/impl/HomeServiceImpl.java index 12bb205..9e1a2ae 100644 --- a/xlcs-parent/service/service-home/src/main/java/com/yovinchen/xlcs/home/service/impl/HomeServiceImpl.java +++ b/xlcs-parent/service/service-home/src/main/java/com/yovinchen/xlcs/home/service/impl/HomeServiceImpl.java @@ -8,12 +8,13 @@ import com.yovinchen.xlcs.model.product.Category; import com.yovinchen.xlcs.model.product.SkuInfo; import com.yovinchen.xlcs.model.search.SkuEs; import com.yovinchen.xlcs.vo.user.LeaderAddressVo; +import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; -import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.concurrent.*; /** * ClassName: HomeServiceImpl @@ -23,6 +24,7 @@ import java.util.Map; * @since 2023/9/25 12:12 */ @Service +@Slf4j public class HomeServiceImpl implements HomeService { @@ -43,30 +45,105 @@ public class HomeServiceImpl implements HomeService { */ @Override public Map homeData(Long userId) { + Map result = new ConcurrentHashMap<>(); - Map result = new HashMap<>(); - //1 根据userId获取当前登录用户提货地址信息 - // 远程调用service-user模块接口获取需要数据 - LeaderAddressVo leaderAddressVo = userFeignClient.getUserAddressByUserId(userId); - result.put("leaderAddressVo", leaderAddressVo); + ExecutorService executorService = Executors.newFixedThreadPool(4); + ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(4); - //2 获取所有分类 - // 远程调用service-product模块接口 - List categoryList = productFeignClient.findAllCategoryList(); - result.put("categoryList", categoryList); + // 1 根据userId获取当前登录用户地址信息 + CompletableFuture getAddressFuture = CompletableFuture.runAsync(() -> { + try { + LeaderAddressVo leaderAddressVo = userFeignClient.getUserAddressByUserId(userId); + if (leaderAddressVo != null) { + result.put("leaderAddressVo", leaderAddressVo); + } + } catch (Exception e) { + log.error("获取地址信息任务出错: {}", e.getMessage(), e); + } + }, executorService); - //3 获取新人专享商品 - // 远程调用service-product模块接口 - List newPersonSkuInfoList = productFeignClient.findNewPersonSkuInfoList(); - result.put("newPersonSkuInfoList", newPersonSkuInfoList); + // 设置获取地址信息任务的超时 + scheduler.schedule(() -> { + if (!getAddressFuture.isDone()) { + getAddressFuture.complete(null); + log.info("获取地址信息任务超时"); + } + }, 1, TimeUnit.SECONDS); - //4 获取爆款商品 - // 远程调用service-search模块接口 - // hotscore 热门评分降序排序 - List hotSkuList = skuFeignClient.findHotSkuList(); - result.put("hotSkuList", hotSkuList); + // 2 获取所有分类 + CompletableFuture getCategoryFuture = CompletableFuture.runAsync(() -> { + try { + List categoryList = productFeignClient.findAllCategoryList(); + if (categoryList != null) { + result.put("categoryList", categoryList); + } + } catch (Exception e) { + log.error("获取所有分类任务出错: {}", e.getMessage(), e); + } + }, executorService); + + // 设置获取所有分类任务的超时 + scheduler.schedule(() -> { + if (!getCategoryFuture.isDone()) { + getCategoryFuture.complete(null); + log.info("获取所有分类任务超时"); + } + }, 1, TimeUnit.SECONDS); + + // 3 获取新人专享商品 + CompletableFuture getNewPersonSkuInfoFuture = CompletableFuture.runAsync(() -> { + try { + List newPersonSkuInfoList = productFeignClient.findNewPersonSkuInfoList(); + if (newPersonSkuInfoList != null) { + result.put("newPersonSkuInfoList", newPersonSkuInfoList); + } + } catch (Exception e) { + log.error("获取新人专享商品任务出错: {}", e.getMessage(), e); + } + }, executorService); + + // 设置获取新人专享商品任务的超时 + scheduler.schedule(() -> { + if (!getNewPersonSkuInfoFuture.isDone()) { + getNewPersonSkuInfoFuture.complete(null); + log.info("获取新人专享商品任务超时"); + } + }, 1, TimeUnit.SECONDS); + + // 4 获取爆款商品 + CompletableFuture getHotSkuFuture = CompletableFuture.runAsync(() -> { + try { + List hotSkuList = skuFeignClient.findHotSkuList(); + if (hotSkuList != null) { + result.put("hotSkuList", hotSkuList); + } + } catch (Exception e) { + log.error("获取爆款商品任务出错: {}", e.getMessage(), e); + } + }, executorService); + + // 设置获取爆款商品任务的超时 + scheduler.schedule(() -> { + if (!getHotSkuFuture.isDone()) { + getHotSkuFuture.complete(null); + log.info("获取爆款商品任务超时"); + } + }, 1, TimeUnit.SECONDS); + + // 等待所有任务完成 + CompletableFuture allFutures = CompletableFuture.allOf(getAddressFuture, getCategoryFuture, getNewPersonSkuInfoFuture, getHotSkuFuture); + + try { + allFutures.get(); // 等待所有任务完成 + } catch (InterruptedException | ExecutionException e) { + log.error("任务执行出错: {}", e.getMessage(), e); + } finally { + executorService.shutdown(); // 关闭线程池 + scheduler.shutdown(); // 关闭调度器 + } - //5 封装获取数据到map集合,返回 return result; } + + }