定时执行程序-Quartz简单实例
2012-04-28 13:09 javaspring 阅读(283) 评论(0) 编辑 收藏 举报定时执行程序-Quartz简单实例
1、加jar包:Quartz自己quartz-1.8.3.jar和依赖包commons-logging.jar 、slf4j-log4j12-1.5.10.jar 、slf4j-api-1.5.10.jar
2、一个类搞定!!!,直接run
import org.quartz.CronTrigger;
import org.quartz.Job;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.Scheduler;
import org.quartz.SchedulerFactory;
import org.quartz.impl.StdSchedulerFactory;
import java.util.Date;
public class QuartzReport implements Job{
public void execute(JobExecutionContext cntxt) throws JobExecutionException {
System.out.println( "输出:"+cntxt.getJobDetail().getJobDataMap().get("name")+new Date());
}
public static void main(String[] args) {
try {
SchedulerFactory schedFact=new StdSchedulerFactory();
Scheduler sched=schedFact.getScheduler();
sched.start();
JobDetail jobDetail=new JobDetail("a","b",QuartzReport.class);
jobDetail.getJobDataMap().put("name","lucy");
CronTrigger trigger=new CronTrigger("c","d");
trigger.setCronExpression("0/1 * * * * ? " ); // 启动之后立即执行 每一秒继续重复。
sched.scheduleJob(jobDetail, trigger);
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* 输出:
*log4j:WARN No appenders could be found for logger (org.quartz.simpl.SimpleThreadPool).
log4j:WARN Please initialize the log4j system properly.
输出:lucyWed Apr 13 15:11:54 CST 2011
输出:lucyWed Apr 13 15:11:55 CST 2011
输出:lucyWed Apr 13 15:11:56 CST 2011
输出:lucyWed Apr 13 15:11:57 CST 2011
输出:lucyWed Apr 13 15:11:58 CST 2011
输出:lucyWed Apr 13 15:11:59 CST 2011
输出:lucyWed Apr 13 15:12:00 CST 2011
输出:lucyWed Apr 13 15:12:01 CST 2011
输出:lucyWed Apr 13 15:12:02 CST 2011
输出:lucyWed Apr 13 15:12:03 CST 2011
输出:lucyWed Apr 13 15:12:04 CST 2011
输出:lucyWed Apr 13 15:12:05 CST 2011
输出:lucyWed Apr 13 15:12:06 CST 2011
输出:lucyWed Apr 13 15:12:07 CST 2011
输出:lucyWed Apr 13 15:12:08 CST 2011
输出:lucyWed Apr 13 15:12:09 CST 2011
输出:lucyWed Apr 13 15:12:10 CST 2011
输出:lucyWed Apr 13 15:12:11 CST 2011
输出:lucyWed Apr 13 15:12:12 CST 2011
输出:lucyWed Apr 13 15:12:13 CST 2011
输出:lucyWed Apr 13 15:12:14 CST 2011
*/
附:cronExpression配置说明
秒 | 0-59 | , - * / | ||
分 | 0-59 | , - * / | ||
小时 | 0-23 | , - * / | ||
日期 | 1-31 | , - * ? / L W C | ||
月份 | 1-12 或者 JAN-DEC | , - * / | ||
星期 | 1-7 或者 SUN-SAT | , - * ? / L C # | ||
年(可选) | 留空, 1970-2099 | , - * / |