获取项目中所有使用spring的@Scheduled注解的定时任务相关信息

需求:获取项目中所有使用spring的@Scheduled注解的定时任务相关信息
思路:从IOC容器中获取ThreadPoolTaskScheduler实例,遍历所有定时任务,反射得到定时任务相关信息(哪个包下面的哪个类的哪个方法)
实现:
<%@ page import="org.springframework.web.context.support.WebApplicationContextUtils" %>
<%@ page import="org.springframework.web.context.WebApplicationContext" %>
<%@ page import="org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler" %>
<%@ page import="java.lang.reflect.Field" %>
<%@ page import="org.springframework.scheduling.support.DelegatingErrorHandlingRunnable" %>
<%@ page import="org.springframework.scheduling.support.ScheduledMethodRunnable" %>
<%@ page import="net.sf.json.JSONObject" %>
<%@ page import="java.lang.reflect.Method" %>
<%@ page import="java.util.Iterator" %>
<%@ page import="java.util.concurrent.BlockingQueue" %>
<%@ page import="org.springframework.beans.BeansException" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>scheduler</title>
</head>
<body>
    <%
        try {
            WebApplicationContext webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(application);
            ThreadPoolTaskScheduler bean = new ThreadPoolTaskScheduler();
            try {
                bean = (ThreadPoolTaskScheduler) webApplicationContext.getBean("myScheduler");
            } catch (BeansException e) {
                try {
                    bean = (ThreadPoolTaskScheduler) webApplicationContext.getBean("defaultSockJsTaskScheduler");
                } catch (BeansException beansException) {
                    bean = (ThreadPoolTaskScheduler) webApplicationContext.getBean("taskScheduler");
                }
            }
            BlockingQueue<Runnable> queue = bean.getScheduledThreadPoolExecutor().getQueue();
            Iterator<Runnable> iterator = queue.iterator();//循环取出定时任务
            while (iterator.hasNext()){
                Runnable runnable = iterator.next();
                Field field = runnable.getClass().getSuperclass().getDeclaredField("callable");
                field.setAccessible(true);
                Object o1 = field.get(runnable);
                Class<?> aClass = Class.forName("java.util.concurrent.Executors$RunnableAdapter");
                Field taskField = aClass.getDeclaredField("task");
                taskField.setAccessible(true);
                Object o2 = taskField.get(o1);
                Class o2Class = o2.getClass();
                Field delegateField;
                if (o2Class.equals( DelegatingErrorHandlingRunnable.class)){
                    delegateField= o2Class.getDeclaredField("delegate");
                    delegateField.setAccessible(true);
                }else{
                    delegateField = o2Class.getSuperclass().getDeclaredField("delegate");
                    delegateField.setAccessible(true);
                }
                ScheduledMethodRunnable scheduledMethodRunnable=(ScheduledMethodRunnable)delegateField.get(o2);
                Method method = scheduledMethodRunnable.getMethod();
                JSONObject data=new JSONObject();
                data.put("ScheduleClassName",method.getDeclaringClass().getName());
                data.put("ScheduleMethodName",method.getName());
                data.put("status", JSONObject.fromObject(runnable));
                System.out.println(data);
                out.println(data);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

    %>

</body>
</html>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
基于Spring Boot执行多个定时任务并动态获取定时任务信息,可以使用Spring框架提供的定时任务功能以及反射机制实现。 首先,在Spring Boot项目的pom.xml文件添加spring-boot-starter-quartz和spring-boot-starter-web依赖。 接下来,创建一个定时任务配置类,使用@EnableScheduling注解开启定时任务功能,并通过@Component注解将其作为一个组件被Spring管理。在配置类,可以定义多个定时任务的方法。 使用@Scheduled注解将方法标记为定时任务,并可以指定方法的执行时间。例如,@Scheduled(cron = "0 */5 * * * ?")表示每隔五分钟执行一次任务。 每个定时任务方法内部可以定义具体的业务逻辑。如果需要动态获取定时任务信息,可以使用反射机制获取注解上的属性值、方法名等信息。 为了能够动态获取定时任务信息,可以在配置类定义一个List存储定时任务信息。在每个定时任务方法内部,将任务的相关信息存储到List。 可以定义一个接口,提供获取定时任务信息的方法。在接口实现类,注入定时任务配置类,通过调用定时任务配置类的getTaskList方法获取定时任务信息。 最后,在Spring Boot的启动类添加@EnableAsync以及@EnableScheduling注解,开启异步和定时任务功能。 这样,就可以实现在Spring Boot项目执行多个定时任务,并且可以动态获取定时任务信息

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值