spring 定时任务 taskScheduler详解
spring 3.0版本后,自带了一个定时任务工具,而且使用简单方便,不用配置文件,可以动态改变执行状态。也可以使用cron表达式设置定时任务。
被执行的类要实现Runnable接口
TaskScheduler接口
TaskScheduler是一个接口
TaskScheduler接口下定义了6个方法
- schedule(Runnable task, Trigger trigger);
指定一个触发器执行定时任务。可以使用CronTrigger来指定Cron表达式,执行定时任务
CronTrigger t = new CronTrigger("0 0 10,14,16 * * ?");
taskScheduler.schedule(this, t);
- schedule(Runnable task, Date startTime);
指定一个具体时间点执行定时任务,可以动态的指定时间,开启任务。只执行一次。(比Timer好用多了。早发现这接口就好了。。。) - scheduleAtFixedRate(Runnable task, long period);
立即执行,循环任务,指定一个执行周期(毫秒计时)
PS:不管上一个周期是否执行完,到时间下个周期就开始执行 - scheduleAtFixedRate(Runnable task, Date startTime, long period);
指定时间开始执行,循环任务,指定一个间隔周期(毫秒计时)
PS:不管上一个周期是否执行完,到时间下个周期就开始执行 - scheduleWithFixedDelay(Runnable task, long delay);
立即执行,循环任务,指定一个间隔周期(毫秒计时)
PS:上一个周期执行完,等待delay时间,下个周期开始执行 - scheduleWithFixedDelay(Runnable task, Date startTime, long delay);
指定时间开始执行,循环任务,指定一个间隔周期(毫秒计时)
PS:上一个周期执行完,等待delay时间,下个周期开始执行
TaskScheduler下有五个实现类
- ConcurrentTaskScheduler
以当前线程执行任务。如果任务简单,可以直接使用这个类来执行。快捷方便。
PS:这是单线程运行
public class LocTest implements Runnable {
private ConcurrentTaskScheduler tpts = new ConcurrentTaskScheduler();
private void start() {
tpts.schedule(this, new Date());
}
public void run() {
Thread ct = Thread.currentThread();
System.out.println("current id:"+ct.getId());
System.out.println("current name:"+ct.getName());
}
public static void main(String[] args) {
new LocTest().start();
}
}
- DefaultManagedTaskScheduler
以当前线程执行任务,这是ConcurrentTaskScheduler的子类,添加了JNDI的支持。和ConcurrentTaskScheduler一样的用法,需要使用JNDI可以单独设置 - ThreadPoolTaskScheduler
TaskScheduler接口的默认实现类,多线程定时任务执行。可以设置执行线程池数(默认一个线程)。
PS:- 使用前必须得先调用initialize()【初始化方法】
- 有shutDown()方法,执行完后可以关闭线程
public class LocTest implements Runnable {
private ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
private void start() {
taskScheduler.setPoolSize(10);
//必须得先初始化,才能使用
taskScheduler.initialize();
taskScheduler.schedule(this, new Date());
}
public void run() {
Thread ct = Thread.currentThread();
System.out.println("current id:"+ct.getId());
System.out.println("current name:"+ct.getName());
}
public static void main(String[] args) {
new LocTest().start();
}
}
- TimerManagerTaskScheduler
这个没有使用到,就没具体了解,有时间再补充。有了解的欢迎来补充。
spring 定时任务 taskScheduler详解的更多相关文章
- (转)Spring JdbcTemplate 方法详解
Spring JdbcTemplate方法详解 文章来源:http://blog.csdn.net/dyllove98/article/details/7772463 JdbcTemplate主要提供 ...
- Spring jar包详解
Spring jar包详解 org.springframework.aop ——Spring的面向切面编程,提供AOP(面向切面编程)的实现 org.springframework.asm——spri ...
- Spring——jar包详解(转)
Spring——jar包详解 org.springframework.aop ——Spring的面向切面编程,提供AOP(面向切面编程)的实现 org.springframework.asm——spr ...
- Spring Boot异常处理详解
在Spring MVC异常处理详解中,介绍了Spring MVC的异常处理体系,本文将讲解在此基础上Spring Boot为我们做了哪些工作.下图列出了Spring Boot中跟MVC异常处理相关的类 ...
- spring事务配置详解
一.前言 好几天没有在对spring进行学习了,由于这几天在赶项目,没有什么时间闲下来继续学习,导致spring核心架构详解没有继续下去,在接下来的时间里面,会继续对spring的核心架构在继续进行学 ...
- spring注入参数详解
spring注入参数详解 在Spring配置文件中, 用户不但可以将String, int等字面值注入到Bean中, 还可以将集合, Map等类型的数据注入到Bean中, 此外还可以注入配置文件中定义 ...
- Spring的lazy-init详解
1.Spring中lazy-init详解ApplicationContext实现的默认行为就是在启动服务器时将所有singleton bean提前进行实例化(也就是依赖注入).提前实例化意味着作为初始 ...
- Spring Security Filter详解
Spring Security Filter详解 汇总 Filter 作用 DelegatingFilterProxy Spring Security基于这个Filter建立拦截机制 Abstract ...
- Spring Boot 配置文件详解
Spring Boot配置文件详解 Spring Boot提供了两种常用的配置文件,分别是properties文件和yml文件.他们的作用都是修改Spring Boot自动配置的默认值.相对于prop ...
随机推荐
- keil mdk中如何确保某一段程序不被优化掉(转)
源:keil mdk中如何确保某一段程序不被优化掉 使用mdk编程,假如有一个有用的函数你定义了但是没有显式的调用,mdk在默认方式下,将会把这个函数从整个程序总删除掉,以节省ROM. 比如,你在RO ...
- 苹果应用商店AppStore审核中文指南
目录 1. 条款与条件2. 功能3. 元数据.评级与排名4. 位置5. 推送通知6. 游戏中心7. 广告8. 商标与商业外观9. 媒体内容10. 用户界面11. 购买与货币12. 抓取与聚合13. 设 ...
- iOS 简易环形进度条
demo下载地址:https://github.com/haozheMa/LoopProgressDemo/tree/master ViewController中的代码 #import "V ...
- phpMyAdmin安装与配置(涉及LAMP配置)
作者:zccst 安装一个phpMyAdmin还真麻烦,遇到很多问题.不过在解决过程中发现,PHP的水还真深,不是短时间可以看透的. 1,下载 建议去百度软件中心下载 2,使用 (1)解压后,复制配置 ...
- spring 自动化构建项目
STS 3.7.0.RELEASE http://spring.io/tools/sts/legacy
- IIS日志路径,修改存放位置,清除日志方法
IIS存放日志文件的默认存储路径是c:\windows\system32\logfiles 我们依次打开“我的电脑”,C盘,Windows文件夹,system32文件夹,logfiles文件夹,发现里 ...
- module_param()函数
1.定义模块参数的方法: module_param(name, type, perm); 其中,name:表示参数的名字; type:表示参数的类型; perm:表示参数的访问权限; ...
- HUSTOJ 2796 && SPOJ1811
传送门:http://begin.lydsy.com/JudgeOnline/problem.php?id=2796 题解:后缀自动机,很裸,但是感觉对后缀自动机还不是特别理解,毕竟我太蒟蒻,等我精通 ...
- python中numpy对函数进行矢量化转换
在对numpy的数组进行操作时,我们应该尽量避免循环操作,尽可能利用矢量化函数来避免循环. 但是,直接将自定义函数应用在numpy数组之上会报错,我们需要将函数进行矢量化转换. def Theta(x ...
- 堡垒机--paramiko模块
做堡垒机之前,来了解一下paramiko模块. 实际上底层封装的SSH. SSHclient(1) import paramiko #实例化一个ssh ssh = paramiko.SSHClient ...