diff --git a/.idea/misc.xml b/.idea/misc.xml index 8509f1a..c6eabde 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -8,7 +8,7 @@ - + diff --git a/src/main/java/cn/van/business/config/AsyncConfig.java b/src/main/java/cn/van/business/config/AsyncConfig.java index 3d36047..87f724f 100644 --- a/src/main/java/cn/van/business/config/AsyncConfig.java +++ b/src/main/java/cn/van/business/config/AsyncConfig.java @@ -24,7 +24,7 @@ public class AsyncConfig implements AsyncConfigurer { @Bean(name = "threadPoolTaskExecutor") public Executor getAsyncExecutor() { ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); - executor.setCorePoolSize(10); // 核心线程数 + executor.setCorePoolSize(16); // 核心线程数 executor.setMaxPoolSize(128); // 最大线程数 executor.setQueueCapacity(500); // 队列容量 executor.setThreadNamePrefix("Async-Executor-"); diff --git a/src/main/java/cn/van/business/config/SchedulerConfig.java b/src/main/java/cn/van/business/config/SchedulerConfig.java index 953915e..195051d 100644 --- a/src/main/java/cn/van/business/config/SchedulerConfig.java +++ b/src/main/java/cn/van/business/config/SchedulerConfig.java @@ -6,8 +6,8 @@ import org.springframework.scheduling.config.ScheduledTaskRegistrar; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import jakarta.annotation.PreDestroy; // 使用 jakarta.annotation 包 import java.util.Collection; -import java.util.Collections; import java.util.List; import java.util.concurrent.*; @@ -15,20 +15,33 @@ import java.util.concurrent.*; public class SchedulerConfig implements SchedulingConfigurer { private static final Logger logger = LoggerFactory.getLogger(SchedulerConfig.class); + private ScheduledExecutorService scheduledExecutorService; @Override public void configureTasks(ScheduledTaskRegistrar taskRegistrar) { - ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(10); + // 动态配置线程池大小 + int poolSize = Integer.parseInt(System.getenv().getOrDefault("SCHEDULED_THREAD_POOL_SIZE", "10")); + scheduledExecutorService = Executors.newScheduledThreadPool(poolSize); taskRegistrar.setScheduler(new CustomScheduledExecutorService(scheduledExecutorService)); } - private static class CustomScheduledExecutorService implements ScheduledExecutorService { - private final ScheduledExecutorService delegate; - - public CustomScheduledExecutorService(ScheduledExecutorService delegate) { - this.delegate = delegate; + @PreDestroy + public void shutdown() { + if (scheduledExecutorService != null && !scheduledExecutorService.isShutdown()) { + scheduledExecutorService.shutdown(); + try { + if (!scheduledExecutorService.awaitTermination(60, TimeUnit.SECONDS)) { + scheduledExecutorService.shutdownNow(); + } + } catch (InterruptedException e) { + scheduledExecutorService.shutdownNow(); + } } + } + + private record CustomScheduledExecutorService( + ScheduledExecutorService delegate) implements ScheduledExecutorService { @Override public ScheduledFuture schedule(Runnable command, long delay, TimeUnit unit) { @@ -49,8 +62,8 @@ public class SchedulerConfig implements SchedulingConfigurer { return () -> { try { command.run(); - } catch (Exception e) { - logger.error("Scheduled task error", e); + } catch (Throwable t) { // 捕获所有类型的异常 + logger.error("Scheduled task error", t); } }; } @@ -83,37 +96,37 @@ public class SchedulerConfig implements SchedulingConfigurer { @Override public Future submit(Callable task) { - return null; + return delegate.submit(task); } @Override public Future submit(Runnable task, T result) { - return null; + return delegate.submit(task, result); } @Override public Future submit(Runnable task) { - return null; + return delegate.submit(task); } @Override public List> invokeAll(Collection> tasks) throws InterruptedException { - return Collections.emptyList(); + return delegate.invokeAll(tasks); } @Override public List> invokeAll(Collection> tasks, long timeout, TimeUnit unit) throws InterruptedException { - return Collections.emptyList(); + return delegate.invokeAll(tasks, timeout, unit); } @Override public T invokeAny(Collection> tasks) throws InterruptedException, ExecutionException { - return null; + return delegate.invokeAny(tasks); } @Override public T invokeAny(Collection> tasks, long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException { - return null; + return delegate.invokeAny(tasks, timeout, unit); } @Override diff --git a/src/main/java/cn/van/business/util/JDUtil.java b/src/main/java/cn/van/business/util/JDUtil.java index 2bea06c..d8e8b1b 100644 --- a/src/main/java/cn/van/business/util/JDUtil.java +++ b/src/main/java/cn/van/business/util/JDUtil.java @@ -303,16 +303,21 @@ public class JDUtil { @Scheduled(cron = "0 0 */4 * * ?") public void fetchHistoricalOrders3090() { - for (Map.Entry entry : super_admins.entrySet()) { - //String wxid = entry.getKey(); - WXUtil.SuperAdmin admin = entry.getValue(); - String appKey = admin.getAppKey(); - String secretKey = admin.getSecretKey(); - if (Util.isAnyEmpty(appKey, secretKey)) { - continue; + try { + for (Map.Entry entry : super_admins.entrySet()) { + //String wxid = entry.getKey(); + WXUtil.SuperAdmin admin = entry.getValue(); + String appKey = admin.getAppKey(); + String secretKey = admin.getSecretKey(); + if (Util.isAnyEmpty(appKey, secretKey)) { + continue; + } + fetchHistoricalOrders3090Do(appKey, secretKey); } - fetchHistoricalOrders3090Do(appKey, secretKey); + } catch (Exception e) { + logger.error("拉取历史订单3090异常", e); } + } private int fetchHistoricalOrders3090Do(String appKey, String secretKey) { @@ -369,16 +374,21 @@ public class JDUtil { */ @Scheduled(cron = "0 0 * * * ?") public void fetchHistoricalOrders1430() { - for (Map.Entry entry : super_admins.entrySet()) { - //String wxid = entry.getKey(); - WXUtil.SuperAdmin admin = entry.getValue(); - String appKey = admin.getAppKey(); - String secretKey = admin.getSecretKey(); - if (Util.isAnyEmpty(appKey, secretKey)) { - continue; + try { + for (Map.Entry entry : super_admins.entrySet()) { + //String wxid = entry.getKey(); + WXUtil.SuperAdmin admin = entry.getValue(); + String appKey = admin.getAppKey(); + String secretKey = admin.getSecretKey(); + if (Util.isAnyEmpty(appKey, secretKey)) { + continue; + } + fetchHistoricalOrders1430Do(appKey, secretKey); } - fetchHistoricalOrders1430Do(appKey, secretKey); + }catch (Exception e){ + logger.error("拉取历史订单1430异常", e); } + } private int fetchHistoricalOrders1430Do(String appKey, String secretKey) { @@ -433,16 +443,21 @@ public class JDUtil { */ @Scheduled(cron = "0 0 * * * ?") public void fetchHistoricalOrders0714() { - for (Map.Entry entry : super_admins.entrySet()) { - //String wxid = entry.getKey(); - WXUtil.SuperAdmin admin = entry.getValue(); - String appKey = admin.getAppKey(); - String secretKey = admin.getSecretKey(); - if (Util.isAnyEmpty(appKey, secretKey)) { - continue; + try { + for (Map.Entry entry : super_admins.entrySet()) { + //String wxid = entry.getKey(); + WXUtil.SuperAdmin admin = entry.getValue(); + String appKey = admin.getAppKey(); + String secretKey = admin.getSecretKey(); + if (Util.isAnyEmpty(appKey, secretKey)) { + continue; + } + fetchHistoricalOrders0714Do(appKey, secretKey); } - fetchHistoricalOrders0714Do(appKey, secretKey); + }catch (Exception e){ + logger.error("拉取历史订单0714异常", e); } + } private int fetchHistoricalOrders0714Do(String appKey, String secretKey) { @@ -493,16 +508,21 @@ public class JDUtil { @Scheduled(cron = "0 */5 * * * ?") public void fetchHistoricalOrders0007() { - for (Map.Entry entry : super_admins.entrySet()) { - //String wxid = entry.getKey(); - WXUtil.SuperAdmin admin = entry.getValue(); - String appKey = admin.getAppKey(); - String secretKey = admin.getSecretKey(); - if (Util.isAnyEmpty(appKey, secretKey)) { - continue; + try { + for (Map.Entry entry : super_admins.entrySet()) { + //String wxid = entry.getKey(); + WXUtil.SuperAdmin admin = entry.getValue(); + String appKey = admin.getAppKey(); + String secretKey = admin.getSecretKey(); + if (Util.isAnyEmpty(appKey, secretKey)) { + continue; + } + fetchHistoricalOrders0007Do(appKey, secretKey); } - fetchHistoricalOrders0007Do(appKey, secretKey); + }catch (Exception e){ + logger.error("拉取历史订单0007异常", e); } + } private int fetchHistoricalOrders0007Do(String appKey, String secretKey) { @@ -1197,11 +1217,11 @@ public void sendOrderToWxByOrderDefault(String order, String fromWxid) { switch (state.getCurrentState()) { case "INIT": - if ("消毒柜".equals(message)) { + if ("订单".equals(message)) { //1,查询消毒柜订单;2,输入新的订单;3,修改订单 String sb = """ 请选择您要执行的操作: - 1,查询消毒柜订单 + 1,查询订单 2,输入新的订单 3,修改订单"""; wxUtil.sendTextMessage(fromWxid, sb, 1, fromWxid); @@ -1216,6 +1236,7 @@ public void sendOrderToWxByOrderDefault(String order, String fromWxid) { String sb = ""; + wxUtil.sendTextMessage(fromWxid, sb, 1, fromWxid); state.setCurrentState("INIT"); logger.debug("User {} queried disinfectant cabinet orders", fromWxid); diff --git a/src/main/java/cn/van/business/util/WxMessageConsumer.java b/src/main/java/cn/van/business/util/WxMessageConsumer.java index 0e1bad3..a2ca864 100644 --- a/src/main/java/cn/van/business/util/WxMessageConsumer.java +++ b/src/main/java/cn/van/business/util/WxMessageConsumer.java @@ -255,7 +255,7 @@ private void handlePrivateMessage(WxMessage wxMessage) throws Exception { logger.info("消息内容为空,不处理"); return; } else { - logger.info("消息内容:" + innerData.getMsg()); + logger.info("消息内容:{}", innerData.getMsg()); } String fromWxid = innerData.getFromWxid();