1. MyJob类
public class HelloJob implements Job { @Override public void execute(JobExecutionContext context) throws JobExecutionException { System.out.println("hello ,,, "); }
}
2. 使用scheduler
public class MyQuartz { public static void main(String[] args) { try { StdSchedulerFactory stdSchedulerFactory = new StdSchedulerFactory(); // 1.创建调度器 Scheduler scheduler = stdSchedulerFactory.getScheduler(); scheduler.start(); // 2. // define the job and tie it to our HelloJob class JobDetail job = newJob(HelloJob.class) .withIdentity("JOBS1", "GROUP1") .build(); // 3. 创建触发器 // Trigger the job to run now, and then repeat every 3 seconds Trigger trigger = newTrigger() .withIdentity("trigger1", "group1") .startNow() .withSchedule(simpleSchedule() .withIntervalInSeconds(3) .repeatForever()) .build(); // 4. // Tell quartz to schedule the job using our trigger scheduler.scheduleJob(job, trigger); // sleep 20秒以便观察 输出及调度情况 try { TimeUnit.SECONDS.sleep(20); } catch (InterruptedException e) { e.printStackTrace(); } scheduler.shutdown(); } catch (SchedulerException e) { e.printStackTrace(); } } }