Jobs can be created and stored in the job scheduler independent of a trigger, and many triggers can be associated with the same job. Another benefit of this loose-coupling is the ability to configure jobs that remain in the scheduler after their associated triggers have expired, so that that it can be rescheduled later, without having to re-define it. It also allows you to modify or replace a trigger without having to re-define its associated job.A good design principle is to use loose-coupling between the objects.
We continue this application.
- New a Java class: HelloJob.java
package demo; import org.quartz.Job; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; public class HelloJob implements Job { public void execute(JobExecutionContext context) throws JobExecutionException { System.err.println("Hello! HelloJob is executing."); } }
- Modify QuartzTest.java:
package demo; import org.quartz.JobDetail; import org.quartz.Scheduler; import org.quartz.SchedulerException; import org.quartz.Trigger; import org.quartz.impl.StdSchedulerFactory; import static org.quartz.TriggerBuilder.*; import static org.quartz.JobBuilder.*; import static org.quartz.SimpleScheduleBuilder.*; public class QuartzTest { public static void run() { try { Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler(); scheduler.start(); JobDetail job = newJob(HelloJob.class) .withIdentity("job1", "group1") .build(); Trigger trigger = newTrigger() .withIdentity("trigger1", "group1") .startNow() .withSchedule(simpleSchedule() .withIntervalInSeconds(40) .repeatForever()) .build(); scheduler.scheduleJob(job, trigger); Thread.sleep(60000); scheduler.shutdown(); } catch (SchedulerException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } } }
- Run:
You should see the greeting 'Hello! HelloJob is executing.' twice.29 [main] INFO org.quartz.impl.StdSchedulerFactory - Using default implementation for ThreadExecutor 32 [main] INFO org.quartz.simpl.SimpleThreadPool - Job execution threads will use class loader of thread: main 49 [main] INFO org.quartz.core.SchedulerSignalerImpl - Initialized Scheduler Signaller of type: class org.quartz.core.SchedulerSignalerImpl 50 [main] INFO org.quartz.core.QuartzScheduler - Quartz Scheduler v.2.0.2 created. 51 [main] INFO org.quartz.simpl.RAMJobStore - RAMJobStore initialized. 52 [main] INFO org.quartz.core.QuartzScheduler - Scheduler meta-data: Quartz Scheduler (v2.0.2) 'DefaultQuartzScheduler' with instanceId 'NON_CLUSTERED' Scheduler class: 'org.quartz.core.QuartzScheduler' - running locally. NOT STARTED. Currently in standby mode. Number of jobs executed: 0 Using thread pool 'org.quartz.simpl.SimpleThreadPool' - with 10 threads. Using job-store 'org.quartz.simpl.RAMJobStore' - which does not support persistence. and is not clustered. 52 [main] INFO org.quartz.impl.StdSchedulerFactory - Quartz scheduler 'DefaultQuartzScheduler' initialized from default resource file in Quartz package: 'quartz.properties' 52 [main] INFO org.quartz.impl.StdSchedulerFactory - Quartz scheduler version: 2.0.2 52 [main] INFO org.quartz.core.QuartzScheduler - Scheduler DefaultQuartzScheduler_$_NON_CLUSTERED started. Hello! HelloJob is executing. 29106 [Timer-0] INFO org.quartz.utils.UpdateChecker - New Quartz update(s) found: 2.1.0 [http://www.terracotta.org/kit/reflector?kitID=default&pageID=QuartzChangeLog] Hello! HelloJob is executing. 60074 [main] INFO org.quartz.core.QuartzScheduler - Scheduler DefaultQuartzScheduler_$_NON_CLUSTERED shutting down. 60074 [main] INFO org.quartz.core.QuartzScheduler - Scheduler DefaultQuartzScheduler_$_NON_CLUSTERED paused. 60074 [main] INFO org.quartz.core.QuartzScheduler - Scheduler DefaultQuartzScheduler_$_NON_CLUSTERED shutdown complete.
沒有留言:
張貼留言