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 ...
随机推荐
- (简单) POJ 2387 Til the Cows Come Home,Dijkstra。
Description Bessie is out in the field and wants to get back to the barn to get as much sleep as pos ...
- Unity3D ——强大的跨平台3D游戏开发工具(三)
第四章 为地形添加水源.水流以及水下的模糊效果 制作好了地形的各种效果,接下来我们给场景添加一些水效果,使场景更加丰富. 第一步:添加水面 由于我在上一次的地形创作中就已经在山峰之间制作了一块洼地,它 ...
- jquery $提示缺少对象$提示缺少对象
jquery $提示缺少对象 项目中存在不同版本的jquery,有1.4也有1.2, 之前运行项目不会报错 " $提示缺少对象 ",但是我的IE9重置之后就报错,从网上找了一大堆, ...
- 在Eclipse中配置tomcat
为了在Eclipse中进行struts2的测试,才发现自己机器上的Eclipse没有集成Tomcat,在网上找了半天,不是这个插件没有下载地址,就是那个有好多注意事项或者版本问题. 结果,自己到tom ...
- intel Xeon(R) CPU E5-2650 v2 性能测试报告
intel Xeon(R) CPU E5-2650 v2 ...
- 编写PHP规则
PHP是运行在服务器端的语言,可以动态生成html页面.这篇博客介绍它的一些编码规则. 一.基本规则 1.PHP代码总是用<?php和?>包围,例如 <?php echo " ...
- bzoj2120: 数颜色 &&bzoj2453: 维护队列
题目大意: 你小时候玩过弹珠吗? 小朋友A有一些弹珠,A喜欢把它们排成队列,从左到右编号为1到N.为了整个队列鲜艳美观,小朋友想知道某一段连续弹珠中,不同颜色的弹珠有多少.当然,A有时候会依据个人喜好 ...
- bzoj 2286 [Sdoi2011]消耗战 虚树+dp
题目大意:多次给出关键点,求切断边使所有关键点与1断开的最小费用 分析:每次造出虚树,dp[i]表示将i和i子树与父亲断开费用 对于父亲x,儿子y ①y为关键点:\(dp[x]\)+=\(dismn( ...
- animate的{queue:false,duration:400}意思
示例:$(document).ready(function(){ $('.tmplS').hover(function(){ $(".cover", this).stop().an ...
- Struts2 属性驱动、模型驱动、异常机制
模型驱动使用单独的VO(值对象)来封装请求参数和处理结果,属性驱动则使用Action实例来封装请求参数和处理结果. 一.使用模型驱动 1.login.action采用模型驱动模式实现,采用模型驱动时必 ...