浏览代码

定时任务开机启动bug

CX 3 年之前
父节点
当前提交
3fd000a237

+ 16 - 0
src/main/java/com/zhaxd/common/init/BootstartInit.java

@@ -0,0 +1,16 @@
1
+package com.zhaxd.common.init;
2
+
3
+import com.zhaxd.common.kettle.environment.StartInit;
4
+
5
+/**
6
+ * Created by CX on 2022/6/11.
7
+ */
8
+public class BootstartInit {
9
+
10
+    public void init() throws Exception{
11
+        new StartInit().init();
12
+
13
+        new QuartzInit().init();
14
+    }
15
+
16
+}

+ 24 - 0
src/main/java/com/zhaxd/common/init/QuartzInit.java

@@ -0,0 +1,24 @@
1
+package com.zhaxd.common.init;
2
+
3
+import com.zhaxd.utils.SpringContextHolder;
4
+import com.zhaxd.web.service.TransService;
5
+
6
+/**
7
+ * 项目一开始,要初始化现有定时任务,暂时只管没删除的都跑起来
8
+ * Created by CX on 2022/6/11.
9
+ */
10
+public class QuartzInit {
11
+
12
+    public void init() throws Exception {
13
+        TransService transService = SpringContextHolder.getBean("transService");
14
+
15
+        System.out.println("****** 启动转换任务调度 ******");
16
+        //用户id 1
17
+        //先停用所有,在启动所有。反正服务停了,其实都是停用状态
18
+        transService.stopAll(null, null, 1);
19
+        System.out.println("****** 转换任务调度全部停用完成 ******");
20
+        transService.startAll(null, null, 1);
21
+        System.out.println("****** 转换任务调度全部启动完成 ******");
22
+    }
23
+
24
+}

+ 4 - 4
src/main/java/com/zhaxd/common/kettle/environment/StartInit.java

@@ -1,11 +1,11 @@
1 1
 package com.zhaxd.common.kettle.environment;
2 2
 
3
-import org.springframework.beans.factory.InitializingBean;
4 3
 
5
-public class StartInit implements InitializingBean{
4
+public class StartInit {
5
+
6
+	public void init() throws Exception {
7
+		System.out.println("****** 启动kettle运行环境 ******");
6 8
 
7
-	@Override
8
-	public void afterPropertiesSet() throws Exception {
9 9
 		//如果是linux,加载器需要重新设置,要不然log4j匹配不上
10 10
 		if(isLinux()) {
11 11
 			Thread thread = Thread.currentThread();

+ 68 - 0
src/main/java/com/zhaxd/utils/SpringContextHolder.java

@@ -0,0 +1,68 @@
1
+package com.zhaxd.utils;
2
+
3
+import org.springframework.context.ApplicationContext;
4
+import org.springframework.context.ApplicationContextAware;
5
+
6
+import java.lang.annotation.Annotation;
7
+import java.util.ArrayList;
8
+import java.util.List;
9
+import java.util.Map;
10
+
11
+/**
12
+ * 以静态变量保存Spring ApplicationContext, 可在任何地方getBean
13
+ */
14
+public class SpringContextHolder implements ApplicationContextAware {
15
+    private static ApplicationContext applicationContext;
16
+
17
+    /**
18
+     * 实现ApplicationContextAware接口的context注入函数, 将其存入静态变量.
19
+     */
20
+    @Override
21
+    public void setApplicationContext(ApplicationContext applicationContext) {
22
+        SpringContextHolder.applicationContext = applicationContext; // NOSONAR
23
+    }
24
+
25
+    /**
26
+     * 取得存储在静态变量中的ApplicationContext.
27
+     */
28
+    public static ApplicationContext getApplicationContext() {
29
+        checkApplicationContext();
30
+        return applicationContext;
31
+    }
32
+
33
+    public static <T> T getBean(String name) {
34
+        return (T) getApplicationContext().getBean(name);
35
+    }
36
+
37
+    public static <T> T getBean(Class<T> clazz) {
38
+        return (T) getApplicationContext().getBeansOfType(clazz);
39
+    }
40
+
41
+    public static <T> T getBean(Class<T> beanType, Class<? extends Annotation> annotationType) {
42
+        if (annotationType == null) {
43
+            return getBean(beanType);
44
+        }
45
+        Map<String, T> results = getApplicationContext().getBeansOfType(beanType);
46
+        List<T> resultsWithAnnotation = new ArrayList<T>();
47
+        for (Map.Entry<String, T> entry : results.entrySet()) {
48
+            if (getApplicationContext().findAnnotationOnBean(entry.getKey(), annotationType) != null) {
49
+                resultsWithAnnotation.add(entry.getValue());
50
+            }
51
+        }
52
+        if (resultsWithAnnotation.isEmpty()) {
53
+            return null;
54
+        }
55
+        if (resultsWithAnnotation.size() == 1) {
56
+            return resultsWithAnnotation.get(0);
57
+        }
58
+        throw new IllegalStateException("参数错误!未获取到对应Bean");
59
+    }
60
+
61
+
62
+    private static void checkApplicationContext() {
63
+        if (applicationContext == null) {
64
+            throw new IllegalStateException("applicaitonContext未注入,请在applicationContext.xml中定义SpringContextHolder");
65
+        }
66
+    }
67
+
68
+}

+ 4 - 2
src/main/resources/spring/applicationContext-service.xml

@@ -7,7 +7,9 @@
7 7
 						http://www.springframework.org/schema/context/spring-context-4.3.xsd">
8 8
 	
9 9
 	<!-- 扫描包加载service实现类 -->
10
-	<context:component-scan base-package="com.zhaxd.web.service"/>
10
+	<context:component-scan base-package="com.zhaxd"/>
11
+	<bean class="com.zhaxd.utils.SpringContextHolder" lazy-init="false"/>
12
+
11 13
 	<!-- kettle的环境初始化 -->
12
-	<bean id="KettleEnvironmentInit" class="com.zhaxd.common.kettle.environment.StartInit"></bean>
14
+	<bean id="bootstartInit" class="com.zhaxd.common.init.BootstartInit" init-method="init" lazy-init="false" />
13 15
 </beans>