钟日健
2022-05-18 ed41701a2954172d20b85da0ae01436375210840
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package org.springblade.modules.quartz.util;
 
import liquibase.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springblade.modules.quartz.entity.ScheduledJob;
import org.springframework.util.ReflectionUtils;
 
import java.lang.reflect.Method;
 
 
/**
 * 定时任务spring bean 执行定时任务
 * @author LGH
 */
@Slf4j
public class SpringBeanTaskUtil {
 
    public static void invokeMethod(ScheduledJob scheduleJob) {
        Object target = SpringContextUtils.getBean(scheduleJob.getBeanName());
        try {
            if (StringUtils.isNotEmpty(scheduleJob.getParams())) {
                Method method = target.getClass().getDeclaredMethod(scheduleJob.getMethodName(), String.class);
                ReflectionUtils.makeAccessible(method);
                method.invoke(target, scheduleJob.getParams());
            } else {
                Method method = target.getClass().getDeclaredMethod(scheduleJob.getMethodName());
                ReflectionUtils.makeAccessible(method);
                method.invoke(target);
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("执行定时任务失败", e);
        }
    }
}