void execute(JobExecutionContext context)
二、JobDetailjava
import org.quartz.*; import org.quartz.impl.StdSchedulerFactory; import java.util.Date; /** * quartz定时器测试 * * @author leizhimin 2009-7-23 8:49:01 */ public class MyJob implements Job { public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException { System.out.println(new Date() + ": doing something..."); } } class Test { public static void main(String[] args) { //一、建立JobDetial对象 JobDetail jobDetail = new JobDetail(); //设置工做项 jobDetail.setJobClass(MyJob.class); jobDetail.setName("MyJob_1"); jobDetail.setGroup("JobGroup_1"); //二、建立Trigger对象 SimpleTrigger strigger = new SimpleTrigger(); strigger.setName("Trigger_1"); strigger.setGroup("Trigger_Group_1"); strigger.setStartTime(new Date()); //设置重复中止时间,并销毁该Trigger对象 java.util.Calendar c = java.util.Calendar.getInstance(); c.setTimeInMillis(System.currentTimeMillis() + 1000 * 1L); strigger.setEndTime(c.getTime()); strigger.setFireInstanceId("Trigger_1_id_001"); //设置重复间隔时间 strigger.setRepeatInterval(1000 * 1L); //设置重复执行次数 strigger.setRepeatCount(3); //三、建立Scheduler对象,并配置JobDetail和Trigger对象 SchedulerFactory sf = new StdSchedulerFactory(); Scheduler scheduler = null; try { scheduler = sf.getScheduler(); scheduler.scheduleJob(jobDetail, strigger); //四、并执行启动、关闭等操做 scheduler.start(); } catch (SchedulerException e) { e.printStackTrace(); } // try { // //关闭调度器 // scheduler.shutdown(true); // } catch (SchedulerException e) { // e.printStackTrace(); // } } }
执行结果:数据库
//设置重复中止时间,并销毁该Trigger对象 java.util.Calendar c = java.util.Calendar.getInstance(); c.setTimeInMillis(System.currentTimeMillis() + 1000 * 1L); strigger.setEndTime(c.getTime());
执行结果:测试
//四、并执行启动、关闭等操做 scheduler.start(); scheduler.shutdown(true);
程序执行结果:spa
Thu Jul 23 10:11:50 CST 2009: doing something...
Process finished with exit code 0.net