转自:http://www.blogchong.com/post/96.html
在应用里经常都有用到在后台跑定时任务的需求。
举个例子,比如需要在服务后台跑一个定时任务来进行非实时计算,清除临时数据、文件等;又比如博客资讯网站需要定时更新最新最热资讯列表等;又比如后台需要定时获取数据等。
其实,不止这些场景,定时任务始终会在业务处理中占据很重要的位置,因此,熟悉定时任务框架的使用非常有必要。
通常有四种实现定时任务的方式:
(1)直接使用线程Thread来达到定时的目的
我们创建一个thread,然后让他一直while循环,并且使用sleep的方式达到定时的目的。
这种方法简单,但是略显粗暴,并且功能上也有所欠缺,难以控制定时任务启停。
简单例子:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
package
com.blogchong.util;
/**
* @author:blogchong
* @E-mail: blogchong@163.com
* @Version:1.0
* @Blog: www.blogchong.com
* @CreateTime:2015年4月1日 上午11:09:29
* @Description: Thread实现定时任务
*/
public
class
Timing {
public
static
void
main(String[] args) {
//设置时间间隔
final
long
timeInterval =
1000
;
Runnable runnable =
new
Runnable() {
public
void
run() {
while
(
true
) {
//执行定时
System.out.println(
"Hello,blogchong~!欢迎继续关注博客虫!"
);
try
{
//进入定时
Thread.sleep(timeInterval);
}
catch
(InterruptedException e) {
e.printStackTrace();
}
}
}
};
Thread thread =
new
Thread(runnable);
thread.start();
}
}
|
(2)使用Timer和TimerTask
使用Timer类,可以调度任务,并且可以控制取消任务,第一次执行时,可以设置延迟执行时间,并且Timer类是线程安全的。
使用实例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
package
com.blogchong.util;
/**
* @author:blogchong
* @E-mail: blogchong@163.com
* @Version:1.0
* @Blog: www.blogchong.com
* @CreateTime:2015年4月1日 上午11:09:29
* @Description: Thread实现定时任务
*/
import
java.util.Timer;
import
java.util.TimerTask;
public
class
Timing {
public
static
void
main(String[] args) {
TimerTask task =
new
TimerTask() {
public
void
run() {
System.out.println(
"欢迎关注博客虫公众微信号blogchong!"
);
}
};
Timer timer =
new
Timer();
long
delay =
0
;
long
intevalPeriod =
1
*
1000
;
timer.scheduleAtFixedRate(task, delay, intevalPeriod);
}
}
|
(3)使用ScheduledExecutorService
在Timer的基础上,ScheduledExecutorService是通过线程池来执行的,同样支持延迟执行,提供良好的入口设置时间间隔。
代码实例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
package
com.blogchong.util;
/**
* @author:blogchong
* @E-mail: blogchong@163.com
* @Version:1.0
* @Blog: www.blogchong.com
* @CreateTime:2015年4月1日 上午11:09:29
* @Description: Thread实现定时任务
*/
import
java.util.concurrent.Executors;
import
java.util.concurrent.ScheduledExecutorService;
import
java.util.concurrent.TimeUnit;
public
class
Timing {
public
static
void
main(String[] args) {
Runnable runnable =
new
Runnable() {
//run里头的是执行主体
public
void
run() {
System.out.println(
"博客虫讲解的storm视频,马上会在极客学院上线,欢迎关注!"
);
}
};
ScheduledExecutorService service = Executors
.newSingleThreadScheduledExecutor();
// 设置时间间隔,如示例,0为延迟时间,1为间隔,最后一个为时间单位
service.scheduleAtFixedRate(runnable,
0
,
1
, TimeUnit.SECONDS);
}
}
|
(4)使用quartz框架
quartz是一个定时任务调度框架,或者说是作业调度框架。想比与之前三种定时任务的实现,更为复杂。他不止能支持成千上百个作业任务的调度,同时它还能对所有的定时任务进行操作,比如启动,停止,暂停,查看等。
此外,quartz还支持集群的部署方式,支持多节点,通过mysql进行中间状态的保存,支持在单节点故障的情况下依然能够保证任务的正常调度。