Parcourir la source

update 重构 抽取 ruoyi-common-job 公共调度模块

疯狂的狮子li il y a 2 ans
Parent
commit
a0eaa5a0f6

+ 1 - 0
ruoyi-common/pom.xml

@@ -17,6 +17,7 @@
         <module>ruoyi-common-doc</module>
         <module>ruoyi-common-excel</module>
         <module>ruoyi-common-idempotent</module>
+        <module>ruoyi-common-job</module>
         <module>ruoyi-common-log</module>
         <module>ruoyi-common-mail</module>
         <module>ruoyi-common-mybatis</module>

+ 7 - 0
ruoyi-common/ruoyi-common-bom/pom.xml

@@ -54,6 +54,13 @@
                 <version>${revision}</version>
             </dependency>
 
+            <!-- 调度模块 -->
+            <dependency>
+                <groupId>com.ruoyi</groupId>
+                <artifactId>ruoyi-common-job</artifactId>
+                <version>${revision}</version>
+            </dependency>
+
             <!-- 日志记录 -->
             <dependency>
                 <groupId>com.ruoyi</groupId>

+ 43 - 0
ruoyi-common/ruoyi-common-job/pom.xml

@@ -0,0 +1,43 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xmlns="http://maven.apache.org/POM/4.0.0"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+    <parent>
+        <groupId>com.ruoyi</groupId>
+        <artifactId>ruoyi-common</artifactId>
+        <version>${revision}</version>
+        <relativePath>../pom.xml</relativePath>
+    </parent>
+    <modelVersion>4.0.0</modelVersion>
+
+    <artifactId>ruoyi-common-job</artifactId>
+
+    <description>
+        ruoyi-common-job 定时任务
+    </description>
+
+	<dependencies>
+
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-autoconfigure</artifactId>
+        </dependency>
+
+        <!-- xxl-job-core -->
+        <dependency>
+            <groupId>com.xuxueli</groupId>
+            <artifactId>xxl-job-core</artifactId>
+        </dependency>
+
+        <dependency>
+            <groupId>org.projectlombok</groupId>
+            <artifactId>lombok</artifactId>
+        </dependency>
+
+        <dependency>
+            <groupId>com.ruoyi</groupId>
+            <artifactId>ruoyi-common-core</artifactId>
+        </dependency>
+
+    </dependencies>
+</project>

+ 4 - 6
ruoyi-modules/ruoyi-job/src/main/java/com/ruoyi/job/config/XxlJobConfig.java → ruoyi-common/ruoyi-common-job/src/main/java/com/ruoyi/common/job/config/XxlJobConfig.java

@@ -1,13 +1,12 @@
-package com.ruoyi.job.config;
+package com.ruoyi.common.job.config;
 
-import com.ruoyi.job.config.properties.XxlJobProperties;
+import com.ruoyi.common.job.config.properties.XxlJobProperties;
 import com.xxl.job.core.executor.impl.XxlJobSpringExecutor;
-import lombok.AllArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
+import org.springframework.boot.autoconfigure.AutoConfiguration;
 import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
 import org.springframework.boot.context.properties.EnableConfigurationProperties;
 import org.springframework.context.annotation.Bean;
-import org.springframework.context.annotation.Configuration;
 
 /**
  * xxl-job config
@@ -15,9 +14,8 @@ import org.springframework.context.annotation.Configuration;
  * @author Lion Li
  */
 @Slf4j
-@Configuration
+@AutoConfiguration
 @EnableConfigurationProperties(XxlJobProperties.class)
-@AllArgsConstructor
 @ConditionalOnProperty(prefix = "xxl.job", name = "enabled", havingValue = "true")
 public class XxlJobConfig {
 

+ 1 - 1
ruoyi-modules/ruoyi-job/src/main/java/com/ruoyi/job/config/properties/XxlJobProperties.java → ruoyi-common/ruoyi-common-job/src/main/java/com/ruoyi/common/job/config/properties/XxlJobProperties.java

@@ -1,4 +1,4 @@
-package com.ruoyi.job.config.properties;
+package com.ruoyi.common.job.config.properties;
 
 import lombok.Data;
 import lombok.NoArgsConstructor;

+ 80 - 0
ruoyi-common/ruoyi-common-job/src/main/java/com/xxl/job/core/glue/impl/SpringGlueFactory.java

@@ -0,0 +1,80 @@
+package com.xxl.job.core.glue.impl;
+
+import com.xxl.job.core.executor.impl.XxlJobSpringExecutor;
+import com.xxl.job.core.glue.GlueFactory;
+import jakarta.annotation.Resource;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Qualifier;
+import org.springframework.core.annotation.AnnotationUtils;
+
+import java.lang.reflect.Field;
+import java.lang.reflect.Modifier;
+
+/**
+ * @author xuxueli 2018-11-01
+ */
+public class SpringGlueFactory extends GlueFactory {
+    private static Logger logger = LoggerFactory.getLogger(SpringGlueFactory.class);
+
+
+    /**
+     * inject action of spring
+     * @param instance
+     */
+    @Override
+    public void injectService(Object instance){
+        if (instance==null) {
+            return;
+        }
+
+        if (XxlJobSpringExecutor.getApplicationContext() == null) {
+            return;
+        }
+
+        Field[] fields = instance.getClass().getDeclaredFields();
+        for (Field field : fields) {
+            if (Modifier.isStatic(field.getModifiers())) {
+                continue;
+            }
+
+            Object fieldBean = null;
+            // with bean-id, bean could be found by both @Resource and @Autowired, or bean could only be found by @Autowired
+
+            if (AnnotationUtils.getAnnotation(field, Resource.class) != null) {
+                try {
+                    Resource resource = AnnotationUtils.getAnnotation(field, Resource.class);
+                    if (resource.name()!=null && resource.name().length()>0){
+                        fieldBean = XxlJobSpringExecutor.getApplicationContext().getBean(resource.name());
+                    } else {
+                        fieldBean = XxlJobSpringExecutor.getApplicationContext().getBean(field.getName());
+                    }
+                } catch (Exception e) {
+                }
+                if (fieldBean==null ) {
+                    fieldBean = XxlJobSpringExecutor.getApplicationContext().getBean(field.getType());
+                }
+            } else if (AnnotationUtils.getAnnotation(field, Autowired.class) != null) {
+                Qualifier qualifier = AnnotationUtils.getAnnotation(field, Qualifier.class);
+                if (qualifier!=null && qualifier.value()!=null && qualifier.value().length()>0) {
+                    fieldBean = XxlJobSpringExecutor.getApplicationContext().getBean(qualifier.value());
+                } else {
+                    fieldBean = XxlJobSpringExecutor.getApplicationContext().getBean(field.getType());
+                }
+            }
+
+            if (fieldBean!=null) {
+                field.setAccessible(true);
+                try {
+                    field.set(instance, fieldBean);
+                } catch (IllegalArgumentException e) {
+                    logger.error(e.getMessage(), e);
+                } catch (IllegalAccessException e) {
+                    logger.error(e.getMessage(), e);
+                }
+            }
+        }
+    }
+
+}

+ 1 - 0
ruoyi-common/ruoyi-common-job/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports

@@ -0,0 +1 @@
+com.ruoyi.common.job.config.XxlJobConfig

+ 8 - 3
ruoyi-extend/ruoyi-xxl-job-admin/pom.xml

@@ -57,6 +57,11 @@
             <artifactId>spring-boot-starter-actuator</artifactId>
         </dependency>
 
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-aop</artifactId>
+        </dependency>
+
         <!-- mybatis-starter:mybatis + mybatis-spring + hikari(default) -->
         <dependency>
             <groupId>org.mybatis.spring.boot</groupId>
@@ -73,10 +78,10 @@
             <artifactId>spring-boot-admin-starter-client</artifactId>
         </dependency>
 
-        <!-- xxl-job-core -->
+        <!-- 调度模块 -->
         <dependency>
-            <groupId>com.xuxueli</groupId>
-            <artifactId>xxl-job-core</artifactId>
+            <groupId>com.ruoyi</groupId>
+            <artifactId>ruoyi-common-job</artifactId>
         </dependency>
 
     </dependencies>

+ 3 - 3
ruoyi-modules/ruoyi-job/pom.xml

@@ -24,10 +24,10 @@
             <artifactId>ruoyi-common-core</artifactId>
         </dependency>
 
-        <!-- xxl-job-core -->
+        <!-- 调度模块 -->
         <dependency>
-            <groupId>com.xuxueli</groupId>
-            <artifactId>xxl-job-core</artifactId>
+            <groupId>com.ruoyi</groupId>
+            <artifactId>ruoyi-common-job</artifactId>
         </dependency>
 
     </dependencies>