From 5b6112890f80e7e710e0c116a3e0e2cd618c91f1 Mon Sep 17 00:00:00 2001 From: wangshuai Date: Sun, 4 Aug 2024 12:18:23 +0800 Subject: [PATCH] =?UTF-8?q?1=EF=BC=9B=E6=96=B0=E5=A2=9E=E7=BA=BF=E7=A8=8B?= =?UTF-8?q?=E7=AE=A1=E7=90=86=20=E6=89=B9=E9=87=8F=E5=90=8C=E6=AD=A5?= =?UTF-8?q?=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../framework/config/ThreadPoolConfig.java | 25 +++++++ .../java/com/ruoyi/quartz/task/RyTask.java | 5 ++ .../business/service/IMeituanService.java | 3 + .../business/service/impl/AsyncTaskImpl.java | 15 ++++ .../service/impl/MeituanServiceImpl.java | 68 ++++++++++--------- 5 files changed, 85 insertions(+), 31 deletions(-) diff --git a/ruoyi-framework/src/main/java/com/ruoyi/framework/config/ThreadPoolConfig.java b/ruoyi-framework/src/main/java/com/ruoyi/framework/config/ThreadPoolConfig.java index f97dbea..b59bddb 100644 --- a/ruoyi-framework/src/main/java/com/ruoyi/framework/config/ThreadPoolConfig.java +++ b/ruoyi-framework/src/main/java/com/ruoyi/framework/config/ThreadPoolConfig.java @@ -84,4 +84,29 @@ public class ThreadPoolConfig executor.initialize(); return executor; } + + //1、自定义asyncServiceExecutor线程池 + @Bean(name = "asyncDayServiceExecutor") + public ThreadPoolTaskExecutor asyncDayServiceExecutor() { + System.out.println(("start asyncDayServiceExecutor......")); + //在这里修改 + ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); + //配置核心线程数 + executor.setCorePoolSize(corePoolSize); + //配置最大线程数 + executor.setMaxPoolSize(maxPoolSize); + //设置线程空闲等待时间 s + executor.setKeepAliveSeconds(keepAliveSeconds); + //配置队列大小 设置任务等待队列的大小 + executor.setQueueCapacity(queueCapacity); + //配置线程池中的线程的名称前缀 + //设置线程池内线程名称的前缀-------阿里编码规约推荐--方便出错后进行调试 + executor.setThreadNamePrefix("youshi-day-schedule-pool-%d"); + // rejection-policy:当pool已经达到max size的时候,如何处理新任务 + // CALLER_RUNS:不在新线程中执行任务,而是有调用者所在的线程来执行 + executor.setRejectedExecutionHandler(new ThreadPoolExecutor.DiscardPolicy()); + //执行初始化 + executor.initialize(); + return executor; + } } diff --git a/ruoyi-quartz/src/main/java/com/ruoyi/quartz/task/RyTask.java b/ruoyi-quartz/src/main/java/com/ruoyi/quartz/task/RyTask.java index eadfbe6..52ca4f3 100644 --- a/ruoyi-quartz/src/main/java/com/ruoyi/quartz/task/RyTask.java +++ b/ruoyi-quartz/src/main/java/com/ruoyi/quartz/task/RyTask.java @@ -4,6 +4,7 @@ import com.ruoyi.business.service.IAiService; import com.ruoyi.business.service.IBusFifteenInfoService; import com.ruoyi.business.service.IMeituanService; import com.ruoyi.business.service.impl.AsyncTaskImpl; +import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @@ -12,6 +13,7 @@ import org.springframework.stereotype.Component; * * @author ruoyi */ +@Slf4j @Component("ryTask") public class RyTask { @Autowired @@ -50,7 +52,10 @@ public class RyTask { * 统计昨日营销数 每天8点执行一次 */ public void getReturnInfo() { + log.error("***********history************************"); + iMeituanService.getComments(null); iMeituanService.getReturnInfo(null); + log.error("***********historyEnd************************"); } diff --git a/ruoyi-system/src/main/java/com/ruoyi/business/service/IMeituanService.java b/ruoyi-system/src/main/java/com/ruoyi/business/service/IMeituanService.java index b716dc5..e4a30ad 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/business/service/IMeituanService.java +++ b/ruoyi-system/src/main/java/com/ruoyi/business/service/IMeituanService.java @@ -1,6 +1,7 @@ package com.ruoyi.business.service; import com.ruoyi.business.domain.BusOrderInfo; +import com.ruoyi.business.domain.BusStoreInfo; import com.ruoyi.business.domain.SysCookie; import com.ruoyi.common.core.domain.AjaxResult; import com.ruoyi.common.core.domain.R; @@ -61,4 +62,6 @@ public interface IMeituanService { String getComment(Long id); String receiveTime(String getUrl,String storeCode,String cookie,String orderNo); + + void getCOne(BusStoreInfo store); } diff --git a/ruoyi-system/src/main/java/com/ruoyi/business/service/impl/AsyncTaskImpl.java b/ruoyi-system/src/main/java/com/ruoyi/business/service/impl/AsyncTaskImpl.java index 9ed8bfa..84ab319 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/business/service/impl/AsyncTaskImpl.java +++ b/ruoyi-system/src/main/java/com/ruoyi/business/service/impl/AsyncTaskImpl.java @@ -49,6 +49,21 @@ public class AsyncTaskImpl { } } + @Async("asyncDayServiceExecutor") + public void executeDayAsync(List busStoreInfoList, + CountDownLatch countDownLatch) { + try{ + log.info("=============start executeDayAsync============="); + //异步线程要做的事情 + for (BusStoreInfo store : busStoreInfoList) { + iMeituanService.getCOne(store); + } + log.error("==================end executeDayAsync==============="); + }finally { + countDownLatch.countDown();// 很关键, 无论上面程序是否异常必须执行countDown,否则await无法释放 + } + } + public void clearStoreKey(){ LocalDate localDate = LocalDate.now(); LocalDate prevDay = localDate.minusDays(1); diff --git a/ruoyi-system/src/main/java/com/ruoyi/business/service/impl/MeituanServiceImpl.java b/ruoyi-system/src/main/java/com/ruoyi/business/service/impl/MeituanServiceImpl.java index 82fce70..e6aaed1 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/business/service/impl/MeituanServiceImpl.java +++ b/ruoyi-system/src/main/java/com/ruoyi/business/service/impl/MeituanServiceImpl.java @@ -198,7 +198,7 @@ public class MeituanServiceImpl implements IMeituanService { if("3".equals(storeInfo.getSelfDeliveryStatus())){ storeInfo.setSelfDeliveryStatus("1"); } - busStoreInfoService.updateBusStoreInfo(storeInfo); + busStoreInfoMapper.updateBusStoreInfo(storeInfo); } } else { return "授权失败,请联系管理员!"; @@ -223,28 +223,42 @@ public class MeituanServiceImpl implements IMeituanService { if(accountName != null){ busStoreInfo.setBindUser(SecurityUtils.getUsername()); } - List busStoreInfoList = busStoreInfoMapper.selectBusStoreInfoListA(busStoreInfo); - for (BusStoreInfo store : busStoreInfoList) { - Boolean flag1 = getScoreOne(store); - if (!flag1) { - log.error("获取评分信息返回错误..."); - } - Boolean flag2 = getCommentsOne(store); - if (!flag2) { - log.error("获取好评信息返回错误..."); - } - Boolean flag3 = yesterdayCountOne(store); - if (!flag3) { - log.error("获取昨日单量信息返回错误..."); - } - Boolean flag4 = updateDayInfo(store); - if (!flag4) { - log.error("获取昨日营销信息返回错误..."); - } + List busStoreInfoList = busStoreInfoMapper.selectBusStoreInfoList(busStoreInfo); + long startTime = System.currentTimeMillis(); // 开始时间 + List> list1= SplitListUtils.pagingList(busStoreInfoList,20); //拆分集合 + CountDownLatch countDownLatch = new CountDownLatch(list1.size()); + for (List list2 : list1) { + asyncTask.executeDayAsync(list2,countDownLatch); + } + try { + countDownLatch.await(); //保证之前的所有的线程都执行完成,才会走下面的; + long endTime = System.currentTimeMillis(); //结束时间 + log.error(("executeDayAsync一共耗时time: " + (endTime - startTime) / 1000 + " s")); + // 这样就可以在下面拿到所有线程执行完的集合结果 + } catch (Exception e) { + log.error("executeDayAsync阻塞异常:"+e.getMessage()); } return "ok"; } + public void getCOne(BusStoreInfo store){ + Boolean flag1 = getScoreOne(store); + if (!flag1) { + log.error("获取评分信息返回错误..."); + } + Boolean flag2 = getCommentsOne(store); + if (!flag2) { + log.error("获取好评信息返回错误..."); + } + Boolean flag3 = yesterdayCountOne(store); + if (!flag3) { + log.error("获取昨日单量信息返回错误..."); + } + Boolean flag4 = updateDayInfo(store); + if (!flag4) { + log.error("获取昨日营销信息返回错误..."); + } + } /** * 获取昨日评分、好评、有效单量 * @@ -268,8 +282,7 @@ public class MeituanServiceImpl implements IMeituanService { } return "ok"; } -// @Resource(name = "asyncServiceExecutor") -// private ThreadPoolTaskExecutor asyncServiceExecutor; + @Autowired private AsyncTaskImpl asyncTask; /** @@ -360,7 +373,7 @@ public class MeituanServiceImpl implements IMeituanService { } } else if ("1001".equals(code)) { storeInfo.setGrantStatus("2"); - busStoreInfoService.updateBusStoreInfo(storeInfo); + busStoreInfoMapper.updateBusStoreInfo(storeInfo); } } return true; @@ -842,7 +855,6 @@ public class MeituanServiceImpl implements IMeituanService { @Override public void getReturnInfo(Long id) { - log.error("***********history************************"); List addList = new ArrayList<>(); List editList = new ArrayList<>(); DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); @@ -851,26 +863,20 @@ public class MeituanServiceImpl implements IMeituanService { if (id != null){ storeInfo.setStoreId(id); } -// List storeInfoList = busStoreInfoMapper.selectBusStoreInfoListA(storeInfo); List list1 = busStoreDayInfoMapper.selectBusStoreDayInfoListByDate(storeInfo.getStoreId(),date.format(dateTimeFormatter)); for (BusStoreDayInfo busStoreDayInfo : list1) { -// if (list1.size() == 1) { -// BusStoreDayInfo busStoreDayInfo = list1.get(0); List list2 = busStoreDayHistoryInfoMapper.selectBusStoreDayHistoryInfoListByDate(busStoreDayInfo.getStoreId(),date.format(dateTimeFormatter)); if (list2.size() == 0){ BusStoreDayHistoryInfo target = new BusStoreDayHistoryInfo(); BeanUtils.copyProperties(busStoreDayInfo, target); target.setId(null); target.setCreateTime(Date.from(date.atZone(ZoneId.systemDefault()).toInstant())); -// busStoreDayHistoryInfoMapper.insertBusStoreDayHistoryInfo(target); addList.add(target); } -// } } if(addList.size()>0){ busStoreDayHistoryInfoMapper.insertBatchStoreDayHistoryInfo(addList); } - log.error("***********historyEnd************************"); } private String getRate(int a, int b){ @@ -954,7 +960,7 @@ public class MeituanServiceImpl implements IMeituanService { } }else if ("1001".equals(jsonObject.getString("code"))) { store.setGrantStatus("2"); - busStoreInfoService.updateBusStoreInfo(store); + busStoreInfoMapper.updateBusStoreInfo(store); return false; } } @@ -1000,7 +1006,7 @@ public class MeituanServiceImpl implements IMeituanService { if ("1001".equals(code)) { log.error("掉线"+result); store.setGrantStatus("2"); - busStoreInfoService.updateBusStoreInfo(store); + busStoreInfoMapper.updateBusStoreInfo(store); return false; } JSONArray array = jsonObject.getJSONObject("data").getJSONArray("list");