Browse Source

增加任务调度中心

15810770710@163.com 3 years ago
parent
commit
53debdcc05

+ 6 - 0
pom.xml

@@ -37,6 +37,7 @@
         <commons.beanutils.version>1.9.4</commons.beanutils.version>
         <mybatis.starter.version>2.1.1</mybatis.starter.version>
         <sdk.b2b.protocol.version>1.5</sdk.b2b.protocol.version>
+        <xxl.job.core.version>2.1.2</xxl.job.core.version>
     </properties>
 
     <dependencies>
@@ -245,6 +246,11 @@
         <!--            <scope>system</scope>-->
         <!--            <systemPath>${project.basedir}/src/main/resources/lib/sdk-b2b-protocol-1.5.jar</systemPath>-->
         <!--        </dependency>-->
+        <dependency>
+            <groupId>com.xuxueli</groupId>
+            <artifactId>xxl-job-core</artifactId>
+            <version>${xxl.job.core.version}</version>
+        </dependency>
     </dependencies>
 
     <build>

+ 74 - 0
src/main/java/com/jkcredit/invoice/hub/config/XxlJobConfig.java

@@ -0,0 +1,74 @@
+package com.jkcredit.invoice.hub.config;
+
+import com.xxl.job.core.executor.impl.XxlJobSpringExecutor;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+
+/**
+ * xxl-job config
+ *
+ * @author xuxueli 2017-04-28
+ */
+@Configuration
+public class XxlJobConfig {
+    private Logger logger = LoggerFactory.getLogger(XxlJobConfig.class);
+
+    @Value("${xxl.job.admin.addresses}")
+    private String adminAddresses;
+
+    @Value("${xxl.job.executor.appname}")
+    private String appName;
+
+    @Value("${xxl.job.executor.ip}")
+    private String ip;
+
+    @Value("${xxl.job.executor.port}")
+    private int port;
+
+    @Value("${xxl.job.accessToken}")
+    private String accessToken;
+
+    @Value("${xxl.job.executor.logpath}")
+    private String logPath;
+
+    @Value("${xxl.job.executor.logretentiondays}")
+    private int logRetentionDays;
+
+
+    @Bean
+    public XxlJobSpringExecutor xxlJobExecutor() {
+        logger.info(">>>>>>>>>>> xxl-job config init.");
+        XxlJobSpringExecutor xxlJobSpringExecutor = new XxlJobSpringExecutor();
+        xxlJobSpringExecutor.setAdminAddresses(adminAddresses);
+        xxlJobSpringExecutor.setAppName(appName);
+        xxlJobSpringExecutor.setIp(ip);
+        xxlJobSpringExecutor.setPort(port);
+        xxlJobSpringExecutor.setAccessToken(accessToken);
+        xxlJobSpringExecutor.setLogPath(logPath);
+        xxlJobSpringExecutor.setLogRetentionDays(logRetentionDays);
+
+        return xxlJobSpringExecutor;
+    }
+
+    /**
+     * 针对多网卡、容器内部署等情况,可借助 "spring-cloud-commons" 提供的 "InetUtils" 组件灵活定制注册IP;
+     *
+     *      1、引入依赖:
+     *          <dependency>
+     *             <groupId>org.springframework.cloud</groupId>
+     *             <artifactId>spring-cloud-commons</artifactId>
+     *             <version>${version}</version>
+     *         </dependency>
+     *
+     *      2、配置文件,或者容器启动变量
+     *          spring.cloud.inetutils.preferred-networks: 'xxx.xxx.xxx.'
+     *
+     *      3、获取IP
+     *          String ip_ = inetUtils.findFirstNonLoopbackHostInfo().getIpAddress();
+     */
+
+
+}

+ 8 - 1
src/main/java/com/jkcredit/invoice/hub/task/SendMailTask.java

@@ -2,6 +2,8 @@ package com.jkcredit.invoice.hub.task;
 
 import com.jkcredit.invoice.hub.model.dto.userBalance.UserBalanceForMailDto;
 import com.jkcredit.invoice.hub.service.user.UserService;
+import com.xxl.job.core.biz.model.ReturnT;
+import com.xxl.job.core.handler.annotation.XxlJob;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
@@ -39,7 +41,12 @@ public class SendMailTask {
     @Autowired
     UserService userService;
 
-    @Scheduled(cron = "0 0 0 * * ?", zone = "Asia/Shanghai")
+//    @Scheduled(cron = "0 0 0 * * ?", zone = "Asia/Shanghai")
+    @XxlJob("sendBalanceMailHandler")
+    public ReturnT<String> execute(String param) {
+        sendBalanceMail();
+        return ReturnT.SUCCESS;
+    }
     public void sendBalanceMail() {
         List<UserBalanceForMailDto> userBalanceForMailDtoList = userService.getUserBalance();
         //核心类

+ 7 - 3
src/main/java/com/jkcredit/invoice/hub/task/WayBillTask.java

@@ -3,6 +3,7 @@ package com.jkcredit.invoice.hub.task;
 import com.jkcredit.invoice.hub.model.po.carFreeCarrierBillStart.CarFreeCarrierBillStartPo;
 import com.jkcredit.invoice.hub.service.apiCarFree.ApiCarFreeChargeService;
 import com.jkcredit.invoice.hub.service.carFreeCarrierBillStart.CarFreeCarrierBillStartService;
+import com.xxl.job.core.handler.annotation.XxlJob;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.scheduling.annotation.Scheduled;
@@ -25,7 +26,8 @@ public class WayBillTask {
     ApiCarFreeChargeService chargeService;
 
     // 实时运单
-    @Scheduled(cron = "0 1 23 * * ?", zone = "Asia/Shanghai")
+//    @Scheduled(cron = "0 1 23 * * ?", zone = "Asia/Shanghai")
+    @XxlJob("realTimeWayBillHandler")
     public void realTimeWayBill() {
         log.info("实时运单定时处理开始");
         // 查询实时订单,且状态为已结束和开票中
@@ -41,8 +43,9 @@ public class WayBillTask {
     }
 
     // 历史运单
-    @Scheduled(cron = "0 1 * * * ?", zone = "Asia/Shanghai")
+//    @Scheduled(cron = "0 1 * * * ?", zone = "Asia/Shanghai")
 //    @Scheduled(cron = "0 11 17 * * ?", zone = "Asia/Shanghai")
+    @XxlJob("historyWayBillHandler")
     public void historyWayBill() {
         log.info("历史运单定时处理开始");
         // 查询历史订单,且状态为已结束和开票中
@@ -58,8 +61,9 @@ public class WayBillTask {
     }
 
     // // 查询状态为空的运单(状态为空为老平台遗留数据,通过此项目接口上传的运单状态不为空),is_history为空则不能区分是历史运单还是实时运单
-    @Scheduled(cron = "0 0 0/2 * * ?", zone = "Asia/Shanghai")
+//    @Scheduled(cron = "0 0 0/2 * * ?", zone = "Asia/Shanghai")
 //    @Scheduled(cron = "0 1/3 * * * ?", zone = "Asia/Shanghai")
+    @XxlJob("billsWithoutIsHistoryFlagHandler")
     public void billsWithoutIsHistoryFlag() {
         // 查询实时/历史订单,且状态为已结束和开票中
         // 调用运单查询发票接口

+ 20 - 0
src/main/resources/application-dev.yml

@@ -32,3 +32,23 @@ redis:
         max-concurrency: 10
       direct:
         acknowledge-mode: manual
+mail:
+  host: smtp.163.com
+  username: jkcredit2021@163.com
+  password: jkCredit@#!
+  authentication: EFIVTVVVAQYBOQIF
+  sendFrom: jkcredit2021@163.com
+  sendTo: xusonglin@jkcredit.com
+  sendCc: wgj@jkcredit.com
+xxl:
+  job:
+    admin:
+      addresses: http://127.0.0.1:18083/xxl-job-admin
+    executor:
+      appname: send-mail-executor
+      ip:
+      port: -1
+      logpath: /Users/jkxy/Desktop/project/invoice-hub/logs
+      logretentiondays: 30
+    accessToken:
+

+ 11 - 0
src/main/resources/application-prod.yml

@@ -47,3 +47,14 @@ mail:
   sendFrom: jkcredit2021@163.com
   sendTo: liuliu@jkcredit.com
   sendCc: wgj@jkcredit.com
+xxl:
+  job:
+    admin:
+      addresses: http://127.0.0.1:18083/xxl-job-admin
+    executor:
+      appname: send-mail-executor
+      ip:
+      port: -1
+      logpath: /home/invoiceSrv/xxlJob/bin/logs/xxlJob
+      logretentiondays: 30
+    accessToken: