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