使用spring-task定时任务动态配置修改执行时间
作者:丶人生如梦 发布时间:2021-12-12 08:10:40
标签:spring-task,定时任务,配置,执行时间
spring-task定时任务动态配置修改执行时间
因项目需要,几个定时任务需要人为动态设置执行时间,于是乎吧,就查阅相关资料,是可以动态设置的,废话不多说,直接上代码,一目了然。
package com.seckill.quartz;
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();
}
}
}
运行测试类,查看结果,达到效果,亲测可用
spring schedule 动态配置执行时间
之前saas平台实现动态修改定时任务的时间,都是通过xx-job这样的框架来实现,这样我们可以单独一个服务来管理我们整个saas平台的定时任务,但是最近给银行做的一个小项目,需要本地化部署,所以我不想弄很多的服务,并且他们并没有要求修改以后即时生效,所以我直接采用了 spring schedule结合mysql动态配置执行时间。
之前我们用的schedule通过注解的方式,只能用静态的corn表达式,如果想实现动态的需要实现SchedulingConfigurer,并且通过注解@EnableScheduling。如下:
package com.zqf.marketing.task;
import com.zqf.db.marketingrobot.sys.model.RobotSysSwitch;
import com.zqf.marketing.sys.service.SwitchService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
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.Service;
import java.util.Date;
/**
* @author zhenghao
* @description
* @date 2019/1/22 21:50
*/
@Lazy(false)
@Service
@EnableScheduling
public class TestTaskService implements SchedulingConfigurer {
private static Logger log = LoggerFactory.getLogger(TestTaskService.class);
@Autowired
private SwitchService switchService;
private String SpringDynamicCronTask() {
String cron = "0/5 * * * * ?";
//从数据库获得配置的corn表达式
RobotSysSwitch switchById = switchService.getSwitchById(5L);
cron = switchById.getSwitchFlag();
log.info(cron);
return cron;
}
@Override
public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {
scheduledTaskRegistrar.addTriggerTask(new Runnable() {
@Override
public void run() {
// 任务逻辑
log.info("task_task_tak");
}
}, new Trigger() {
@Override
public Date nextExecutionTime(TriggerContext triggerContext) {
String s = SpringDynamicCronTask();
// 任务触发,可修改任务的执行周期
CronTrigger trigger = new CronTrigger(s);
Date nextExec = trigger.nextExecutionTime(triggerContext);
return nextExec;
}
});
}
}
这样我们就可以动态的修改task的执行时间,生效时间为,上一个任务的执行周期,也可以满足我们现在需求,这样就可以实习项目更加的灵活!
来源:https://blog.csdn.net/ll840768874/article/details/78507286


猜你喜欢
- MultipartResolver和ServletFileUpload冲突如果同时使用了MultipartResolver 和Servlet
- 在装2个不同版本JDK时遇到了这个问题,在网上钩了一吧!查到一个讲解比较好的资料。一:要解决的问题我们在尝鲜 JDK1.5 的时候,相信不少
- 很多C#的初学者在编程时都容易把抽象类和接口搞混,本文就为大家从概念上讲解抽象类和接口的区别:一、抽象类:含有abstract修饰符的cla
- 需求说明在对图像进行处理时,经常会有这类需求:想通过阈值对图像进行二值化分割,以提取自己感兴趣的区域,常见的阈值分割方法有常数分割、最大类间
- 看到某些App里面有读取联系人的功能,然后自己尝试了一下。发现这个挺简单的。然后自己就做了一个demo给大家,希望借这个demo可以让大家学
- 一、泛型的基本概念java与c#一样,都存在泛型的概念,及类型的参数化。java中的泛型是在jdk5.0后出现的,但是java中的泛型与C#
- 最近项目需要通过经纬度查询 具体的地址和区域名称,通过查询网络资源,发现提供的大多是得到具体的地址而对区域或城市名称的获取就不是很好把握;在
- explicit 关键字用于显式声明一个类构造函数是显式而非隐式的,从而禁用类构造函数的隐式自动类型转换。类构造函数默认情况下即声
- SpringBoot分离打Jar包的两种方式方式一:基于maven-jar-plugin此方式基于这个小伙伴的配置改的:https://ww
- 本节我们开始自我实现我们自己okhttp框架中的每个 * 。先简单回顾一下各个 * 的作用:RetryAndFollowUpIntercep
- 前言这几天同事跟我在升级Android target SDK和build tool版本的时候,碰到了一个非常搞笑的问题,基本可以算作是“坑”
- 本文实例讲述了C#文件分割的方法。分享给大家供大家参考。具体如下:1. 小文件分割(适用于小于等于64M的文件):using System;
- 1.先来张效果图2.自定义一个角标工具类BottomBarView 。** * Created by Administrator on 20
- ava:采用大端字节序存储数据【低地址存放数据的高位,高地址存放数据的低位,数据高位存放在数组的前面】windows(intel平台):采用
- 1. 抽象类关键字:abstract类:用来描述一类具体的事物抽象类:抽象的、模糊的、不具体的类在Java的普通类中是不允许多继承的,原因是
- 使用包管理器package com.liunan.myfirstapp.util;import android.content.Contex
- 本文为大家分享了Android自动检测版本及自动升级的具体代码,供大家参考,具体内容如下步骤:1.检测当前版本的信息AndroidManif
- 我们知道多线程因为同时处理子线程的能力,对于程序运行来说,能够达到很高的效率。不过很多人对于多线程的执行方法还没有尝试过,本篇我们将为大家介
- 一,描写叙述 在多线程下编程的时候。大家可能会遇到一种需求,就是我想在我开启的线程都结束时,同一时候获取
- 介绍  日常的业务开发,我们会重复编写一些代码:日期和字符串相互转换、发送Http请求调用接口、拷贝对象