定时执行程序-Quartz简单实例

定时执行程序-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, - * /


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值