From acff6b1cd8adefb180ebd369b9903fc1799a8678 Mon Sep 17 00:00:00 2001 From: JiyangTang Date: Wed, 10 Jan 2024 10:08:35 +0800 Subject: [PATCH] add schedule config --- .../schedule/AppleInventorySchedule.java | 16 ++++++------- .../common/config/ExecutorConfig.java | 23 +++++++++---------- .../common/config/ScheduleConfig.java | 10 ++++---- 3 files changed, 24 insertions(+), 25 deletions(-) diff --git a/baogutang-admin/src/main/java/top/baogutang/admin/schedule/AppleInventorySchedule.java b/baogutang-admin/src/main/java/top/baogutang/admin/schedule/AppleInventorySchedule.java index cb993bf..a0799ef 100644 --- a/baogutang-admin/src/main/java/top/baogutang/admin/schedule/AppleInventorySchedule.java +++ b/baogutang-admin/src/main/java/top/baogutang/admin/schedule/AppleInventorySchedule.java @@ -53,6 +53,7 @@ public class AppleInventorySchedule { @Resource private IphoneProductParserUtils iphoneProductParserUtils; + @Resource private DingTalkMsgPushUtils dingTalkMsgPushUtils; @@ -68,7 +69,7 @@ public class AppleInventorySchedule { products.forEach(product -> { this.doMonitor(product); try { - Thread.sleep(1500); + Thread.sleep(2000); } catch (InterruptedException e) { log.error(">>>>>>>>>>apple inventory monitor error:{}<<<<<<<<<<", e.getMessage(), e); } @@ -96,7 +97,6 @@ public class AppleInventorySchedule { return; } JSONObject responseJsonObject = JSON.parseObject(httpResponse.body()); -// log.info(">>>>>>>>>>query apple inventory response:{}<<<<<<<<<<", responseJsonObject); JSONObject pickupMessage = responseJsonObject.getJSONObject("body").getJSONObject("content").getJSONObject("pickupMessage"); JSONArray stores = pickupMessage.getJSONArray("stores"); if (stores == null) { @@ -110,12 +110,9 @@ public class AppleInventorySchedule { StringBuilder pushContentBuilder = new StringBuilder(); List availableStoreNameList = stores.stream() .filter(store -> filterStore((JSONObject) store)) - .filter(k -> - judgingStoreInventory((JSONObject) k, product.getModel())) - .map(store -> { - JSONObject storeJson = (JSONObject) store; - return storeJson.getString("storeName").trim(); - }) + .filter(k -> judgingStoreInventory((JSONObject) k, product.getModel())) + .map(store -> ((JSONObject) store).getString("storeName") + .trim()) .collect(Collectors.toList()); if (CollectionUtils.isEmpty(availableStoreNameList)) { return; @@ -161,7 +158,8 @@ public class AppleInventorySchedule { return Boolean.TRUE; } String storeName = storeInfo.getString("storeName"); - return storeList.stream().anyMatch(k -> storeName.contains(k) || k.contains(storeName)); + return storeList.stream() + .anyMatch(k -> storeName.contains(k) || k.contains(storeName)); } /** diff --git a/baogutang-common/src/main/java/top/baogutang/common/config/ExecutorConfig.java b/baogutang-common/src/main/java/top/baogutang/common/config/ExecutorConfig.java index 0b23bc4..72f61c2 100644 --- a/baogutang-common/src/main/java/top/baogutang/common/config/ExecutorConfig.java +++ b/baogutang-common/src/main/java/top/baogutang/common/config/ExecutorConfig.java @@ -4,7 +4,9 @@ import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.scheduling.TaskScheduler; import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; +import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler; import java.util.concurrent.ThreadPoolExecutor; @@ -46,18 +48,15 @@ public class ExecutorConfig { return executor; } - @Bean("scheduleExecutor") - public ThreadPoolTaskExecutor scheduleExecutor() { - ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); - executor.setThreadNamePrefix(appName + "-schedule-"); - executor.setCorePoolSize(corePoolSize); - executor.setMaxPoolSize(maxPoolSize); - executor.setKeepAliveSeconds(keepAliveSecond); - executor.setQueueCapacity(queueCapacity); - executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); - executor.setWaitForTasksToCompleteOnShutdown(true); - executor.initialize(); - return executor; + @Bean("taskExecutor") + public TaskScheduler taskScheduler() { + ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler(); + taskScheduler.setPoolSize(maxPoolSize); + taskScheduler.setThreadNamePrefix(appName + "-schedule-"); + taskScheduler.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); + taskScheduler.setWaitForTasksToCompleteOnShutdown(true); + taskScheduler.setPoolSize(10); + return taskScheduler; } } diff --git a/baogutang-common/src/main/java/top/baogutang/common/config/ScheduleConfig.java b/baogutang-common/src/main/java/top/baogutang/common/config/ScheduleConfig.java index 9808470..b24b148 100644 --- a/baogutang-common/src/main/java/top/baogutang/common/config/ScheduleConfig.java +++ b/baogutang-common/src/main/java/top/baogutang/common/config/ScheduleConfig.java @@ -1,8 +1,8 @@ package top.baogutang.common.config; import org.springframework.context.annotation.Configuration; +import org.springframework.scheduling.TaskScheduler; import org.springframework.scheduling.annotation.SchedulingConfigurer; -import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; import org.springframework.scheduling.config.ScheduledTaskRegistrar; import javax.annotation.Resource; @@ -15,11 +15,13 @@ import javax.annotation.Resource; @Configuration public class ScheduleConfig implements SchedulingConfigurer { - @Resource(name = "scheduleExecutor") - private ThreadPoolTaskExecutor scheduleExecutor; + @Resource(name = "taskExecutor") + private TaskScheduler taskScheduler; + @Override public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) { - scheduledTaskRegistrar.setScheduler(scheduleExecutor); + + scheduledTaskRegistrar.setScheduler(taskScheduler); } }