diff --git a/src/main/java/com/glxp/api/res/thrsys/ThirdSysInterfaceExecuteVo.java b/src/main/java/com/glxp/api/res/thrsys/ThirdSysInterfaceExecuteVo.java index 2a3e66406..c77eee823 100644 --- a/src/main/java/com/glxp/api/res/thrsys/ThirdSysInterfaceExecuteVo.java +++ b/src/main/java/com/glxp/api/res/thrsys/ThirdSysInterfaceExecuteVo.java @@ -23,6 +23,6 @@ public class ThirdSysInterfaceExecuteVo { /** * 任务是否执行完毕 */ - private boolean isFinished; + private boolean finished; } diff --git a/src/main/java/com/glxp/api/task/ThirdSysInterfaceTask.java b/src/main/java/com/glxp/api/task/ThirdSysInterfaceTask.java index 5cb05089d..a190b23d6 100644 --- a/src/main/java/com/glxp/api/task/ThirdSysInterfaceTask.java +++ b/src/main/java/com/glxp/api/task/ThirdSysInterfaceTask.java @@ -20,10 +20,7 @@ import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import javax.annotation.Resource; -import java.util.Date; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ExecutorService; @@ -35,13 +32,13 @@ import java.util.concurrent.ExecutorService; @Component public class ThirdSysInterfaceTask { - private ExecutorService executor; - /** * redis key 前缀 */ private static final Map> keyMap = new ConcurrentHashMap<>(5); + private volatile ExecutorService executor; + /** * 获取执行任务的线程池 * @@ -49,8 +46,10 @@ public class ThirdSysInterfaceTask { */ private ExecutorService getExecutor() { if (null == executor) { - log.info("初始化第三方系统接口执行线程池"); - executor = ThreadUtil.newExecutor(10, 100, Integer.MAX_VALUE); + synchronized (this) { + log.info("初始化第三方系统接口执行线程池"); + executor = ThreadUtil.newExecutor(10, 100, Integer.MAX_VALUE); + } } return executor; } @@ -70,7 +69,7 @@ public class ThirdSysInterfaceTask { @Resource private IoOrderService orderService; - @Scheduled(fixedRate = 60 * 1000, initialDelay = 60 * 1000) + @Scheduled(fixedRate = 20 * 1000, initialDelay = 60 * 1000) public void scanInterface() { log.info("开始扫描自动执行的第三方接口列表"); List list = thrSystemDetailDao.selectAutoExecuteList(); @@ -118,8 +117,13 @@ public class ThirdSysInterfaceTask { if (verifyTask(thrSystemDetailEntity)) { getExecutor().submit(() -> { log.info("开始提交单据到第三方系统"); - orderService.submitOrderToThrSys(thrSystemDetailEntity); - updateTask(getTaskKey(thrSystemDetailEntity)); + try { + orderService.submitOrderToThrSys(thrSystemDetailEntity); + } catch (Exception e) { + log.error("提交单据到第三方系统异常", e); + } finally { + updateTask(getTaskKey(thrSystemDetailEntity)); + } log.info("提交单据到第三方系统完成"); }); } @@ -135,8 +139,13 @@ public class ThirdSysInterfaceTask { if (verifyTask(thrSystemDetailEntity)) { getExecutor().submit(() -> { log.info("开始下载第三方单据类型"); - thrBusTypeOriginService.downloadThrBusType(thrSystemDetailEntity); - updateTask(getTaskKey(thrSystemDetailEntity)); + try { + thrBusTypeOriginService.downloadThrBusType(thrSystemDetailEntity); + } catch (Exception e) { + log.error("下载第三方单据类型异常", e); + } finally { + updateTask(getTaskKey(thrSystemDetailEntity)); + } log.info("第三方单据类型下载完成"); }); } @@ -152,8 +161,13 @@ public class ThirdSysInterfaceTask { if (verifyTask(thrSystemDetailEntity)) { getExecutor().submit(() -> { log.info("开始下载第三方产品信息"); - thrProductsService.downloadThrPi(thrSystemDetailEntity); - updateTask(getTaskKey(thrSystemDetailEntity)); + try { + thrProductsService.downloadThrPi(thrSystemDetailEntity); + } catch (Exception e) { + log.error("下载第三方产品信息异常", e); + } finally { + updateTask(getTaskKey(thrSystemDetailEntity)); + } log.info("第三方产品信息下载完成"); }); } @@ -169,8 +183,13 @@ public class ThirdSysInterfaceTask { if (verifyTask(thrSystemDetailEntity)) { getExecutor().submit(() -> { log.info("开始下载第三方往来单位信息"); - thrCorpService.downloadThrCorp(thrSystemDetailEntity); - updateTask(getTaskKey(thrSystemDetailEntity)); + try { + thrCorpService.downloadThrCorp(thrSystemDetailEntity); + } catch (Exception e) { + log.error("下载第三方往来单位异常", e); + } finally { + updateTask(getTaskKey(thrSystemDetailEntity)); + } log.info("第三方往来单位信息下载完成"); }); } @@ -186,8 +205,14 @@ public class ThirdSysInterfaceTask { if (verifyTask(thrSystemDetailEntity)) { getExecutor().submit(() -> { log.info("开始下载第三方仓库信息"); - thrInvWarehouseService.downloadThrInv(thrSystemDetailEntity); - updateTask(getTaskKey(thrSystemDetailEntity)); + try { + thrInvWarehouseService.downloadThrInv(thrSystemDetailEntity); + } catch (Exception e) { + log.error("下载第三方仓库信息异常", e); + } finally { + //保证任务标识一定会被修改回去 + updateTask(getTaskKey(thrSystemDetailEntity)); + } log.info("第三方仓库信息下载完成"); }); } @@ -216,17 +241,14 @@ public class ThirdSysInterfaceTask { if (null != vo && !vo.isFinished()) { log.info("有任务尚未执行完成,当前任务key:{}", taskKey); return false; - } else { - if (vo == null) { - vo = new ThirdSysInterfaceExecuteVo(); - vo.setKey(taskKey); - } - vo.setNextTime(DateUtil.offsetMonth(new Date(), thrSystemDetailEntity.getTime()).getTime()); - vo.setFinished(false); - - redisUtil.set(taskKey, vo); - return true; } + long nextTime = DateUtil.offsetMonth(new Date(), thrSystemDetailEntity.getTime()).getTime(); + vo = Optional.ofNullable(vo).orElse(new ThirdSysInterfaceExecuteVo()); + vo.setKey(taskKey); + vo.setNextTime(nextTime); + vo.setFinished(false); + redisUtil.set(taskKey, vo); + return true; } /**