您的位置:首页 > 编程语言 > Java开发

quartz一分钟教程 java任务调度

2009-11-27 12:19 471 查看
quartz是干啥的? 任务调度用的

任务调度是干啥的?就是在预定的时间做预定的事

1.在quart网站下最新的JAR包

2.建个项目,把quartz的JAR包加入到项目

3.新建一个任务

public class MyJob implements org.quartz.Job {
@Override
public void execute(JobExecutionContext arg0) throws JobExecutionException {
System.out.println("我被调用了");
}

}


4.开启任务:

public class StartSchedule {
public static void main(String[] args) throws Exception {
SchedulerFactory sf = new StdSchedulerFactory();
Scheduler sched = sf.getScheduler();
JobDetail job = new JobDetail("job1", "group1", MyJob.class);
CronTrigger t = new CronTrigger("trigger1", "group1", "job1", "group1", "0/2 * * * * ?");
sched.scheduleJob(job, t);
sched.start();
Thread.sleep(500000);
sched.shutdown(true);
}
}


5.运行StartSchedule即可

其中:

"0/2 * * * * ?" 是cronExpression表达式

一共有7组数据组成,位与位之间用空间分隔

分别代表 “秒 分 时 日 月 周 年” 其中年是可以省略的,所以在例子中用了6组

官方的例子:

Examples

Here are some full examples:

Expression Meaning
0 0 12 * * ?
Fire at 12pm (noon) every day
0 15 10 ? * *
Fire at 10:15am every day
0 15 10 * * ?
Fire at 10:15am every day
0 15 10 * * ? *
Fire at 10:15am every day
0 15 10 * * ? 2005
Fire at 10:15am every day during the year 2005
0 * 14 * * ?
Fire every minute starting at 2pm and ending at 2:59pm, every day
0 0/5 14 * * ?
Fire every 5 minutes starting at 2pm and ending at 2:55pm, every day
0 0/5 14,18 * * ?
Fire every 5 minutes starting at 2pm and
ending at 2:55pm, AND fire every 5 minutes starting at 6pm and ending
at 6:55pm, every day
0 0-5 14 * * ?
Fire every minute starting at 2pm and ending at 2:05pm, every day
0 10,44 14 ? 3 WED
Fire at 2:10pm and at 2:44pm every Wednesday in the month of March.
0 15 10 ? * MON-FRI
Fire at 10:15am every Monday, Tuesday, Wednesday, Thursday and Friday
0 15 10 15 * ?
Fire at 10:15am on the 15th day of every month
0 15 10 L * ?
Fire at 10:15am on the last day of every month
0 15 10 ? * 6L
Fire at 10:15am on the last Friday of every month
0 15 10 ? * 6L
Fire at 10:15am on the last Friday of every month
0 15 10 ? * 6L 2002-2005
Fire at 10:15am on every last friday of every month during the years 2002, 2003, 2004 and 2005
0 15 10 ? * 6#3
Fire at 10:15am on the third Friday of every month
0 0 12 1/5 * ?
Fire at 12pm (noon) every 5 days every month, starting on the first day of the month.
0 11 11 11 11 ?
Fire every November 11th at 11:11am.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: