zoukankan      html  css  js  c++  java
  • spring-task定时任务动态配置修改执行时间

    原文https://blog.csdn.net/ll840768874/article/details/78507286

    import org.springframework.scheduling.Trigger;
    import org.springframework.scheduling.TriggerContext;
    import org.springframework.scheduling.annotation.EnableScheduling;
    import org.springframework.scheduling.annotation.SchedulingConfigurer;
    import org.springframework.scheduling.config.ScheduledTaskRegistrar;
    import org.springframework.scheduling.support.CronTrigger;
    import org.springframework.stereotype.Component;
     
    import java.text.SimpleDateFormat;
    import java.util.Date;
     
    /**
     * Created by loup on 2017/11/11.
     */
    @Component
    @EnableScheduling
    public class DynamicScheduledTask implements SchedulingConfigurer {
     
        //时间表达式  每2秒执行一次
        private String cron = "0/2 * * * * ?";
     
        private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
     
        @Override
        public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {
            scheduledTaskRegistrar.addTriggerTask(new Runnable() {
                @Override
                public void run() {
                    //任务逻辑
                    System.out.println("---------------start-------------------");
                    System.out.println("动态修改定时任务参数,时间表达式cron为:" + cron);
                    System.out.println("当前时间为:" + sdf.format(new Date()));
                    System.out.println("----------------end--------------------");
                }
            }, new Trigger() {
                @Override
                public Date nextExecutionTime(TriggerContext triggerContext) {
                    CronTrigger cronTrigger = new CronTrigger(cron);
                    Date nextExecDate = cronTrigger.nextExecutionTime(triggerContext);
                    return nextExecDate;
                }
            });
        }
     
        public void setCron(String cron) {
            this.cron = cron;
        }
    }

    这个是定时任务调度执行器,采用的是注解的方式。首先要动态配置,要设置为@EnableScheduling,这是确保能够动态,然后实现SchedulingConfigurer,重写configureTasks方法,接下来就是这个的相关spring配置文件,要引入下面这个task,不然识别不了啊,配置文件就是这么简单

    http://www.springframework.org/schema/task
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:task="http://www.springframework.org/schema/task"
           xmlns:context="http://www.springframework.org/schema/context"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/task
            http://www.springframework.org/schema/task/spring-task.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
     
        <context:component-scan base-package="com.seckill.quartz"/>
     
        <task:annotation-driven />
     
    </beans>

    接下来就是写测试类,测试可不可行啊

    package com.seckill.quartz;
     
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.test.context.ContextConfiguration;
    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
     
    import java.io.IOException;
     
    /**
     * Created by loup on 2017/11/11.
     */
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration({"classpath*:/conf/spring-quartz.xml"})
    public class QuartzTest {
     
        @Autowired
        private DynamicScheduledTask dynamicScheduledTask;
     
        @Test
        public void test1(){
            try {
                Thread.sleep(10000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            dynamicScheduledTask.setCron("0/10 * * * * ?");
            try {
                System.in.read();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    运行测试类,查看结果,达到效果,亲测可用

  • 相关阅读:
    java 基础7
    java 基础5
    java 基础6
    java 基础4
    java 基础2
    java 基础3
    java 基础1
    使用HTML的基本结构创建网页
    jsp Servlet 文件上传
    Filter过滤器 不登陆无法访问其他页面
  • 原文地址:https://www.cnblogs.com/kitor/p/12809841.html
Copyright © 2011-2022 走看看