Kaynağa Gözat

疏浚配置xxl-job

zoro 1 yıl önce
ebeveyn
işleme
99e8cf56c1

+ 74 - 0
src/main/java/com/shcd/conf/XxlJobConfig.java

@@ -0,0 +1,74 @@
+package com.shcd.conf;
+
+
+import com.xxl.job.core.executor.impl.XxlJobSpringExecutor;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.ComponentScan;
+import org.springframework.context.annotation.Configuration;
+
+/**
+ * @Author: zhangkai
+ * @Date: 2021/6/29 14:10
+ */
+@Slf4j
+@Configuration
+@ComponentScan(basePackages = "com.shcd.scheduling")
+public class XxlJobConfig {
+
+    @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(initMethod = "start", destroyMethod = "destroy")
+    public XxlJobSpringExecutor xxlJobExecutor() {
+        log.info(">>>>>>>>>>> xxl-job config init."+adminAddresses);
+        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();
+     */
+
+}

+ 7 - 4
src/main/java/com/shcd/scheduling/DredgingSchedule.java

@@ -3,6 +3,8 @@ package com.shcd.scheduling;
 
 import com.shcd.entity.dredging.DredgingImportRule;
 import com.shcd.service.dredging.DredgingService;
+import com.xxl.job.core.biz.model.ReturnT;
+import com.xxl.job.core.handler.annotation.XxlJob;
 import com.xxl.job.core.log.XxlJobLogger;
 import org.springframework.scheduling.annotation.Scheduled;
 import org.springframework.stereotype.Component;
@@ -14,9 +16,9 @@ import java.util.List;
 public class DredgingSchedule {
     @Resource
     private DredgingService dredgingService;
-    //@XxlJob("Dredging")
-    @Scheduled(cron = "0 0 1 * * ?")
-    public void dredging(){
+    @XxlJob("Dredging")
+    //@Scheduled(cron = "0 0 1 * * ?")
+    public ReturnT<String> dredging(String param){
        List<DredgingImportRule> dredgingImportRules =  dredgingService.getAllProjectId();
        if (dredgingImportRules != null && dredgingImportRules.size() > 0){
            for (DredgingImportRule dredgingImportRule : dredgingImportRules) {
@@ -25,10 +27,11 @@ public class DredgingSchedule {
                } catch (Exception e) {
                    e.printStackTrace();
                    XxlJobLogger.log(e.getMessage() + "疏浚数据导入失败");
-                   return;
+                   return ReturnT.FAIL;
                }
            }
            System.out.println("ok");
        }
+       return ReturnT.SUCCESS;
     }
 }