import java.util.Date;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors; import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
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.Component; @Lazy(false)
@Component
@EnableScheduling
public class DynamicScheduledTask implements SchedulingConfigurer { /**
* 通过自动注入启动任务调度
*
* @Autowired
* DynamicScheduledTask dynamicScheduledTask;
*
*/ private final Logger logger = LoggerFactory.getLogger(this.getClass()); private static final String DEFAULT_CRON = "0 0/10 * * * ? ";
private String cron = DEFAULT_CRON; /**
* 获取任务执行规则
* @return
*/
public String getCron() {
return cron;
} /**
* 设置任务执行规则
* @param cron
*/
public void setCron(String cron) {
this.cron = cron;
} @Bean(destroyMethod = "shutdown")
public Executor taskExecutor() {
return Executors.newScheduledThreadPool(20);
} @Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
//用于设置定时任务线程数,默认不设置的话为单线程
taskRegistrar.setScheduler(taskExecutor());
taskRegistrar.addTriggerTask(new Runnable() {
@Override
public void run() {
// 任务逻辑
logger.debug("dynamicCronTask is running...");
}
}, new Trigger() {
@Override
public Date nextExecutionTime(TriggerContext triggerContext) {
// 任务触发,可修改任务的执行周期
CronTrigger trigger = new CronTrigger(cron);
Date nextExec = trigger.nextExecutionTime(triggerContext);
return nextExec;
}
});
}
}

Spring 注解 @Scheduled(cron = "0 0/10 * * * ? ") 动态改变时间的更多相关文章

  1. Spring 注解 @Scheduled(cron = "0 0/10 * * * ? ") 任务调度动态改变时间

    不需要重启应用就可以动态的改变Cron表达式的值 import java.util.Date; import java.util.concurrent.Executor; import java.ut ...

  2. spring注解 @Scheduled(cron = "0 0 1 * * *")实现定时的执行任务

    @Scheduled(cron = "0 0 1 * * *") 在使用该注解以前请做好以下准备工作,配置好相应的xm文件. 配置定时注解的步骤:http://blog.csdn. ...

  3. spring注解scheduled实现定时任务

    只想说,spring注解scheduled实现定时任务使用真的非常简单. 一.配置spring.xml文件 1.在beans加入xmlns:task="http://www.springfr ...

  4. Spring 计时器 @Scheduled cron 含义

    Spring 计时器 @Scheduled cron 含义 学习:http://blog.csdn.net/prisonbreak_/article/details/49180307 http://b ...

  5. spring注解@Scheduled中fixedDelay、fixedRate和cron表达式的区别

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

  6. Spring注解@Scheduled定时任务

    一.首先配置applicationContext-task.xml (1)添加 xmlns:task="http://www.springframework.org/schema/task& ...

  7. Spring boot @Scheduled(cron = "* * * * * *") cron表达式详解

    //@Scheduled(cron = "0 0/15 * * * ?") //每15分钟触发一次 //@Scheduled(cron = "5/10 * * * * ? ...

  8. spring 定时任务 scheduled Cron表达式

    转载:https://blog.csdn.net/u011789653/article/details/51153536 可以借鉴:https://www.cnblogs.com/softidea/p ...

  9. js动态改变时间

    <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="C ...

随机推荐

  1. ABBYY FineReader 12使用教程

    说到OCR图文识别软件,自然少不了ABBYY FineReader 12.ABBYY FineReader 12可快速方便地将扫描纸质文档.PDF文件和数码相机的图像转换成可编辑.可搜索的文本.我们已 ...

  2. [Hinton] Neural Networks for Machine Learning - Hopfield Nets and Boltzmann Machine

    Lecture 11 — Hopfield Nets Lecture 12 — Boltzmann machine learning Ref: 能量模型(EBM).限制波尔兹曼机(RBM) 高大上的模 ...

  3. Ajax简单整理-思维导图

    图片和mmap下载地址:http://pan.baidu.com/s/1jGqUpxk

  4. SpringBoot------单元测试

    1.添加测试依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId&g ...

  5. 解决在html中引入font-awesome的css文件后, 图标显示不出来

    今天小颖在做项目时,需要在html文件中引入font-awesome.min.css,但是引入后: 以前小颖在用font-awesome库里的图标时,都是直接从node中下包,然后在main.js中引 ...

  6. Windows-NTFS-ADS在渗透测试中的利用

    0.什么是ADS Windows:微软公司的一款视窗操作系统,其内核为WindowsNT. NTFS:WindowsNT环境的限制级专用文件系统. ADS:NTFS的系统特性,交换数据流(Altern ...

  7. python string.py 源码分析 一

    # Some strings for ctype-style character classification c风格字符串 whitespace = ' \t\n\r\v\f' #空白字符 \t 制 ...

  8. 【vue】如何在 Vue-cli 创建的项目中引入 iView

    根据vue项目的搭建教程,以下记录如何在Vue-cli创建的项目中引入iView. 1)iView的安装,在项目下使用 npm 安装iView cnpm install  iview  --save ...

  9. 一、在windows环境下修改pip镜像源的方法(以python3为例)

    在windows环境下修改pip镜像源的方法(以python3为例) 1.在windows文件管理器中,输入 %APPDATA% 2.会定位到一个新的目录下,在该目录下新建pip文件夹,然后到pip文 ...

  10. ELK(Elasticsearch6.0以上版本head插件安装)

    参考:https://www.cnblogs.com/Onlywjy/p/Elasticsearch.html Elasticsearch6.0不能使用命令直接安装head插件 修改配置文件/etc/ ...