Quartz1.8.5例子(二)
/* * Copyright 2005 - 2009 Terracotta, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); you may not * use this file except in compliance with the License. You may obtain a copy * of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations * under the License. * */ package org.quartz.examples.example2; import java.util.Date; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.quartz.Job; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; /** * <p> * This is just a simple job that gets fired off many times by example 1 * </p> * * @author Bill Kratzer */ public class SimpleJob implements Job { private static Logger _log = LoggerFactory.getLogger(SimpleJob.class); /** * Empty constructor for job initilization */ public SimpleJob() { } /** * <p> * Called by the <code>{@link org.quartz.Scheduler}</code> when a * <code>{@link org.quartz.Trigger}</code> fires that is associated with * the <code>Job</code>. * </p> * * @throws JobExecutionException * if there is an exception while executing the job. */ public void execute(JobExecutionContext context) throws JobExecutionException { // This job simply prints out its job name and the // date and time that it is running String jobName = context.getJobDetail().getFullName(); _log.info("SimpleJob says: " + jobName + " executing at " + new Date()); } }
/* * Copyright 2005 - 2009 Terracotta, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); you may not * use this file except in compliance with the License. You may obtain a copy * of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations * under the License. * */ package org.quartz.examples.example2; import java.util.Date; import org.quartz.JobDetail; import org.quartz.Scheduler; import org.quartz.SchedulerFactory; import org.quartz.SchedulerMetaData; import org.quartz.SimpleTrigger; import org.quartz.TriggerUtils; import org.quartz.impl.StdSchedulerFactory; import org.slf4j.LoggerFactory; import org.slf4j.Logger; /** * This Example will demonstrate all of the basics of scheduling capabilities * of Quartz using Simple Triggers. * * @author Bill Kratzer */ public class SimpleTriggerExample { public void run() throws Exception { Logger log = LoggerFactory.getLogger(SimpleTriggerExample.class); log.info("------- Initializing -------------------"); // First we must get a reference to a scheduler SchedulerFactory sf = new StdSchedulerFactory(); Scheduler sched = sf.getScheduler(); log.info("------- Initialization Complete --------"); log.info("------- Scheduling Jobs ----------------"); // jobs can be scheduled before sched.start() has been called // get a "nice round" time a few seconds in the future... long ts = TriggerUtils.getNextGivenSecondDate(null, 15).getTime(); // job1 will only fire once at date/time "ts" JobDetail job = new JobDetail("job1", "group1", SimpleJob.class); SimpleTrigger trigger = new SimpleTrigger("trigger1", "group1", new Date(ts)); // schedule it to run! Date ft = sched.scheduleJob(job, trigger); log.info(job.getFullName() + " will run at: " + ft + " and repeat: " + trigger.getRepeatCount() + " times, every " + trigger.getRepeatInterval() / 1000 + " seconds"); // job2 will only fire once at date/time "ts" job = new JobDetail("job2", "group1", SimpleJob.class); trigger = new SimpleTrigger("trigger2", "group1", "job2", "group1", new Date(ts), null, 0, 0); ft = sched.scheduleJob(job, trigger); log.info(job.getFullName() + " will run at: " + ft + " and repeat: " + trigger.getRepeatCount() + " times, every " + trigger.getRepeatInterval() / 1000 + " seconds"); // job3 will run 11 times (run once and repeat 10 more times) // job3 will repeat every 10 seconds (10000 ms) job = new JobDetail("job3", "group1", SimpleJob.class); trigger = new SimpleTrigger("trigger3", "group1", "job3", "group1", new Date(ts), null, 10, 10000L); ft = sched.scheduleJob(job, trigger); log.info(job.getFullName() + " will run at: " + ft + " and repeat: " + trigger.getRepeatCount() + " times, every " + trigger.getRepeatInterval() / 1000 + " seconds"); // the same job (job3) will be scheduled by a another trigger // this time will only run every 70 seocnds (70000 ms) trigger = new SimpleTrigger("trigger3", "group2", "job3", "group1", new Date(ts), null, 2, 70000L); ft = sched.scheduleJob(trigger); log.info(job.getFullName() + " will [also] run at: " + ft + " and repeat: " + trigger.getRepeatCount() + " times, every " + trigger.getRepeatInterval() / 1000 + " seconds"); // job4 will run 6 times (run once and repeat 5 more times) // job4 will repeat every 10 seconds (10000 ms) job = new JobDetail("job4", "group1", SimpleJob.class); trigger = new SimpleTrigger("trigger4", "group1", "job4", "group1", new Date(ts), null, 5, 10000L); ft = sched.scheduleJob(job, trigger); log.info(job.getFullName() + " will run at: " + ft + " and repeat: " + trigger.getRepeatCount() + " times, every " + trigger.getRepeatInterval() / 1000 + " seconds"); // job5 will run once, five minutes past "ts" (300 seconds past "ts") job = new JobDetail("job5", "group1", SimpleJob.class); trigger = new SimpleTrigger("trigger5", "group1", "job5", "group1", new Date(ts + 300000L), null, 0, 0); ft = sched.scheduleJob(job, trigger); log.info(job.getFullName() + " will run at: " + ft + " and repeat: " + trigger.getRepeatCount() + " times, every " + trigger.getRepeatInterval() / 1000 + " seconds"); // job6 will run indefinitely, every 50 seconds job = new JobDetail("job6", "group1", SimpleJob.class); trigger = new SimpleTrigger("trigger6", "group1", "job6", "group1", new Date(ts), null, SimpleTrigger.REPEAT_INDEFINITELY, 50000L); ft = sched.scheduleJob(job, trigger); log.info(job.getFullName() + " will run at: " + ft + " and repeat: " + trigger.getRepeatCount() + " times, every " + trigger.getRepeatInterval() / 1000 + " seconds"); log.info("------- Starting Scheduler ----------------"); // All of the jobs have been added to the scheduler, but none of the jobs // will run until the scheduler has been started sched.start(); log.info("------- Started Scheduler -----------------"); // jobs can also be scheduled after start() has been called... // job7 will repeat 20 times, repeat every five minutes job = new JobDetail("job7", "group1", SimpleJob.class); trigger = new SimpleTrigger("trigger7", "group1", "job7", "group1", new Date(ts), null, 20, 300000L); ft = sched.scheduleJob(job, trigger); log.info(job.getFullName() + " will run at: " + ft + " and repeat: " + trigger.getRepeatCount() + " times, every " + trigger.getRepeatInterval() / 1000 + " seconds"); // jobs can be fired directly... (rather than waiting for a trigger) job = new JobDetail("job8", "group1", SimpleJob.class); job.setDurability(true); sched.addJob(job, true); log.info("'Manually' triggering job8..."); sched.triggerJob("job8", "group1"); log.info("------- Waiting 30 seconds... --------------"); try { // wait 30 seconds to show jobs Thread.sleep(30L * 1000L); // executing... } catch (Exception e) { } // jobs can be re-scheduled... // job 7 will run immediately and repeat 10 times for every second log.info("------- Rescheduling... --------------------"); trigger = new SimpleTrigger("trigger7", "group1", "job7", "group1", new Date(), null, 10, 1000L); ft = sched.rescheduleJob("trigger7", "group1", trigger); log.info("job7 rescheduled to run at: " + ft); log.info("------- Waiting five minutes... ------------"); try { // wait five minutes to show jobs Thread.sleep(300L * 1000L); // executing... } catch (Exception e) { } log.info("------- Shutting Down ---------------------"); sched.shutdown(true); log.info("------- Shutdown Complete -----------------"); // display some stats about the schedule that just ran SchedulerMetaData metaData = sched.getMetaData(); log.info("Executed " + metaData.getNumberOfJobsExecuted() + " jobs."); } public static void main(String[] args) throws Exception { SimpleTriggerExample example = new SimpleTriggerExample(); example.run(); } }
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"> <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/"> <appender name="default" class="org.apache.log4j.ConsoleAppender"> <param name="target" value="System.out"/> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="[%p] %d{dd MMM hh:mm:ss.SSS aa} %t [%c]%n%m%n%n"/> </layout> </appender> <logger name="org.quartz"> <level value="debug" /> </logger> <root> <level value="debug" /> <appender-ref ref="default" /> </root> </log4j:configuration>
[INFO] 02 二月 01:28:00.759 下午 main [org.quartz.examples.example2.SimpleTriggerExample] ------- Initializing ------------------- [INFO] 02 二月 01:28:00.782 下午 main [org.quartz.simpl.SimpleThreadPool] Job execution threads will use class loader of thread: main [INFO] 02 二月 01:28:00.794 下午 main [org.quartz.core.SchedulerSignalerImpl] Initialized Scheduler Signaller of type: class org.quartz.core.SchedulerSignalerImpl [INFO] 02 二月 01:28:00.795 下午 main [org.quartz.core.QuartzScheduler] Quartz Scheduler v.1.8.5 created. [INFO] 02 二月 01:28:00.796 下午 main [org.quartz.simpl.RAMJobStore] RAMJobStore initialized. [INFO] 02 二月 01:28:00.797 下午 main [org.quartz.core.QuartzScheduler] Scheduler meta-data: Quartz Scheduler (v1.8.5) '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. [INFO] 02 二月 01:28:00.797 下午 main [org.quartz.impl.StdSchedulerFactory] Quartz scheduler 'DefaultQuartzScheduler' initialized from default resource file in Quartz package: 'quartz.properties' [INFO] 02 二月 01:28:00.797 下午 main [org.quartz.impl.StdSchedulerFactory] Quartz scheduler version: 1.8.5 [INFO] 02 二月 01:28:00.797 下午 main [org.quartz.examples.example2.SimpleTriggerExample] ------- Initialization Complete -------- [INFO] 02 二月 01:28:00.797 下午 main [org.quartz.examples.example2.SimpleTriggerExample] ------- Scheduling Jobs ---------------- [INFO] 02 二月 01:28:00.802 下午 main [org.quartz.examples.example2.SimpleTriggerExample] group1.job1 will run at: Tue Feb 02 13:28:15 CST 2016 and repeat: 0 times, every 0 seconds [INFO] 02 二月 01:28:00.802 下午 main [org.quartz.examples.example2.SimpleTriggerExample] group1.job2 will run at: Tue Feb 02 13:28:15 CST 2016 and repeat: 0 times, every 0 seconds [INFO] 02 二月 01:28:00.802 下午 main [org.quartz.examples.example2.SimpleTriggerExample] group1.job3 will run at: Tue Feb 02 13:28:15 CST 2016 and repeat: 10 times, every 10 seconds [INFO] 02 二月 01:28:00.803 下午 main [org.quartz.examples.example2.SimpleTriggerExample] group1.job3 will [also] run at: Tue Feb 02 13:28:15 CST 2016 and repeat: 2 times, every 70 seconds [INFO] 02 二月 01:28:00.803 下午 main [org.quartz.examples.example2.SimpleTriggerExample] group1.job4 will run at: Tue Feb 02 13:28:15 CST 2016 and repeat: 5 times, every 10 seconds [INFO] 02 二月 01:28:00.803 下午 main [org.quartz.examples.example2.SimpleTriggerExample] group1.job5 will run at: Tue Feb 02 13:33:15 CST 2016 and repeat: 0 times, every 0 seconds [INFO] 02 二月 01:28:00.803 下午 main [org.quartz.examples.example2.SimpleTriggerExample] group1.job6 will run at: Tue Feb 02 13:28:15 CST 2016 and repeat: -1 times, every 50 seconds [INFO] 02 二月 01:28:00.803 下午 main [org.quartz.examples.example2.SimpleTriggerExample] ------- Starting Scheduler ---------------- [INFO] 02 二月 01:28:00.803 下午 main [org.quartz.core.QuartzScheduler] Scheduler DefaultQuartzScheduler_$_NON_CLUSTERED started. [INFO] 02 二月 01:28:00.803 下午 main [org.quartz.examples.example2.SimpleTriggerExample] ------- Started Scheduler ----------------- [INFO] 02 二月 01:28:00.804 下午 main [org.quartz.examples.example2.SimpleTriggerExample] group1.job7 will run at: Tue Feb 02 13:28:15 CST 2016 and repeat: 20 times, every 300 seconds [INFO] 02 二月 01:28:00.804 下午 main [org.quartz.examples.example2.SimpleTriggerExample] 'Manually' triggering job8... [INFO] 02 二月 01:28:00.804 下午 main [org.quartz.examples.example2.SimpleTriggerExample] ------- Waiting 30 seconds... -------------- [DEBUG] 02 二月 01:28:00.806 下午 DefaultQuartzScheduler_QuartzSchedulerThread [org.quartz.simpl.SimpleJobFactory] Producing instance of Job 'group1.job8', class=org.quartz.examples.example2.SimpleJob [DEBUG] 02 二月 01:28:00.809 下午 DefaultQuartzScheduler_Worker-1 [org.quartz.core.JobRunShell] Calling execute on job group1.job8 [INFO] 02 二月 01:28:00.809 下午 DefaultQuartzScheduler_Worker-1 [org.quartz.examples.example2.SimpleJob] SimpleJob says: group1.job8 executing at Tue Feb 02 13:28:00 CST 2016 [DEBUG] 02 二月 01:28:01.796 下午 Timer-0 [org.quartz.utils.UpdateChecker] Checking for available updated version of Quartz... [DEBUG] 02 二月 01:28:15.001 下午 DefaultQuartzScheduler_QuartzSchedulerThread [org.quartz.simpl.SimpleJobFactory] Producing instance of Job 'group1.job1', class=org.quartz.examples.example2.SimpleJob [DEBUG] 02 二月 01:28:15.002 下午 DefaultQuartzScheduler_QuartzSchedulerThread [org.quartz.simpl.SimpleJobFactory] Producing instance of Job 'group1.job2', class=org.quartz.examples.example2.SimpleJob [DEBUG] 02 二月 01:28:15.002 下午 DefaultQuartzScheduler_Worker-2 [org.quartz.core.JobRunShell] Calling execute on job group1.job1 [DEBUG] 02 二月 01:28:15.002 下午 DefaultQuartzScheduler_QuartzSchedulerThread [org.quartz.simpl.SimpleJobFactory] Producing instance of Job 'group1.job3', class=org.quartz.examples.example2.SimpleJob [DEBUG] 02 二月 01:28:15.002 下午 DefaultQuartzScheduler_Worker-3 [org.quartz.core.JobRunShell] Calling execute on job group1.job2 [DEBUG] 02 二月 01:28:15.003 下午 DefaultQuartzScheduler_QuartzSchedulerThread [org.quartz.simpl.SimpleJobFactory] Producing instance of Job 'group1.job4', class=org.quartz.examples.example2.SimpleJob [DEBUG] 02 二月 01:28:15.003 下午 DefaultQuartzScheduler_Worker-4 [org.quartz.core.JobRunShell] Calling execute on job group1.job3 [INFO] 02 二月 01:28:15.003 下午 DefaultQuartzScheduler_Worker-2 [org.quartz.examples.example2.SimpleJob] SimpleJob says: group1.job1 executing at Tue Feb 02 13:28:15 CST 2016 [INFO] 02 二月 01:28:15.005 下午 DefaultQuartzScheduler_Worker-4 [org.quartz.examples.example2.SimpleJob] SimpleJob says: group1.job3 executing at Tue Feb 02 13:28:15 CST 2016 [DEBUG] 02 二月 01:28:15.004 下午 DefaultQuartzScheduler_QuartzSchedulerThread [org.quartz.simpl.SimpleJobFactory] Producing instance of Job 'group1.job6', class=org.quartz.examples.example2.SimpleJob [DEBUG] 02 二月 01:28:15.004 下午 DefaultQuartzScheduler_Worker-5 [org.quartz.core.JobRunShell] Calling execute on job group1.job4 [INFO] 02 二月 01:28:15.004 下午 DefaultQuartzScheduler_Worker-3 [org.quartz.examples.example2.SimpleJob] SimpleJob says: group1.job2 executing at Tue Feb 02 13:28:15 CST 2016 [INFO] 02 二月 01:28:15.007 下午 DefaultQuartzScheduler_Worker-5 [org.quartz.examples.example2.SimpleJob] SimpleJob says: group1.job4 executing at Tue Feb 02 13:28:15 CST 2016 [DEBUG] 02 二月 01:28:15.006 下午 DefaultQuartzScheduler_QuartzSchedulerThread [org.quartz.simpl.SimpleJobFactory] Producing instance of Job 'group1.job7', class=org.quartz.examples.example2.SimpleJob [DEBUG] 02 二月 01:28:15.006 下午 DefaultQuartzScheduler_Worker-6 [org.quartz.core.JobRunShell] Calling execute on job group1.job6 [DEBUG] 02 二月 01:28:15.008 下午 DefaultQuartzScheduler_QuartzSchedulerThread [org.quartz.simpl.SimpleJobFactory] Producing instance of Job 'group1.job3', class=org.quartz.examples.example2.SimpleJob [DEBUG] 02 二月 01:28:15.008 下午 DefaultQuartzScheduler_Worker-7 [org.quartz.core.JobRunShell] Calling execute on job group1.job7 [DEBUG] 02 二月 01:28:15.010 下午 DefaultQuartzScheduler_Worker-8 [org.quartz.core.JobRunShell] Calling execute on job group1.job3 [INFO] 02 二月 01:28:15.009 下午 DefaultQuartzScheduler_Worker-6 [org.quartz.examples.example2.SimpleJob] SimpleJob says: group1.job6 executing at Tue Feb 02 13:28:15 CST 2016 [INFO] 02 二月 01:28:15.011 下午 DefaultQuartzScheduler_Worker-8 [org.quartz.examples.example2.SimpleJob] SimpleJob says: group1.job3 executing at Tue Feb 02 13:28:15 CST 2016 [INFO] 02 二月 01:28:15.010 下午 DefaultQuartzScheduler_Worker-7 [org.quartz.examples.example2.SimpleJob] SimpleJob says: group1.job7 executing at Tue Feb 02 13:28:15 CST 2016 [DEBUG] 02 二月 01:28:25.000 下午 DefaultQuartzScheduler_QuartzSchedulerThread [org.quartz.simpl.SimpleJobFactory] Producing instance of Job 'group1.job3', class=org.quartz.examples.example2.SimpleJob [DEBUG] 02 二月 01:28:25.001 下午 DefaultQuartzScheduler_Worker-9 [org.quartz.core.JobRunShell] Calling execute on job group1.job3 [DEBUG] 02 二月 01:28:25.001 下午 DefaultQuartzScheduler_QuartzSchedulerThread [org.quartz.simpl.SimpleJobFactory] Producing instance of Job 'group1.job4', class=org.quartz.examples.example2.SimpleJob [INFO] 02 二月 01:28:25.001 下午 DefaultQuartzScheduler_Worker-9 [org.quartz.examples.example2.SimpleJob] SimpleJob says: group1.job3 executing at Tue Feb 02 13:28:25 CST 2016 [DEBUG] 02 二月 01:28:25.001 下午 DefaultQuartzScheduler_Worker-10 [org.quartz.core.JobRunShell] Calling execute on job group1.job4 [INFO] 02 二月 01:28:25.017 下午 DefaultQuartzScheduler_Worker-10 [org.quartz.examples.example2.SimpleJob] SimpleJob says: group1.job4 executing at Tue Feb 02 13:28:25 CST 2016 [INFO] 02 二月 01:28:30.818 下午 main [org.quartz.examples.example2.SimpleTriggerExample] ------- Rescheduling... -------------------- [INFO] 02 二月 01:28:30.819 下午 main [org.quartz.examples.example2.SimpleTriggerExample] job7 rescheduled to run at: Tue Feb 02 13:28:30 CST 2016 [DEBUG] 02 二月 01:28:30.819 下午 DefaultQuartzScheduler_QuartzSchedulerThread [org.quartz.simpl.SimpleJobFactory] Producing instance of Job 'group1.job7', class=org.quartz.examples.example2.SimpleJob [INFO] 02 二月 01:28:30.820 下午 main [org.quartz.examples.example2.SimpleTriggerExample] ------- Waiting five minutes... ------------ [DEBUG] 02 二月 01:28:30.820 下午 DefaultQuartzScheduler_Worker-1 [org.quartz.core.JobRunShell] Calling execute on job group1.job7 [INFO] 02 二月 01:28:30.821 下午 DefaultQuartzScheduler_Worker-1 [org.quartz.examples.example2.SimpleJob] SimpleJob says: group1.job7 executing at Tue Feb 02 13:28:30 CST 2016 [DEBUG] 02 二月 01:28:31.819 下午 DefaultQuartzScheduler_QuartzSchedulerThread [org.quartz.simpl.SimpleJobFactory] Producing instance of Job 'group1.job7', class=org.quartz.examples.example2.SimpleJob [DEBUG] 02 二月 01:28:31.819 下午 DefaultQuartzScheduler_Worker-2 [org.quartz.core.JobRunShell] Calling execute on job group1.job7 [INFO] 02 二月 01:28:31.819 下午 DefaultQuartzScheduler_Worker-2 [org.quartz.examples.example2.SimpleJob] SimpleJob says: group1.job7 executing at Tue Feb 02 13:28:31 CST 2016 [DEBUG] 02 二月 01:28:32.833 下午 DefaultQuartzScheduler_QuartzSchedulerThread [org.quartz.simpl.SimpleJobFactory] Producing instance of Job 'group1.job7', class=org.quartz.examples.example2.SimpleJob [DEBUG] 02 二月 01:28:32.833 下午 DefaultQuartzScheduler_Worker-4 [org.quartz.core.JobRunShell] Calling execute on job group1.job7 [INFO] 02 二月 01:28:32.833 下午 DefaultQuartzScheduler_Worker-4 [org.quartz.examples.example2.SimpleJob] SimpleJob says: group1.job7 executing at Tue Feb 02 13:28:32 CST 2016 [DEBUG] 02 二月 01:28:33.820 下午 DefaultQuartzScheduler_QuartzSchedulerThread [org.quartz.simpl.SimpleJobFactory] Producing instance of Job 'group1.job7', class=org.quartz.examples.example2.SimpleJob [DEBUG] 02 二月 01:28:33.820 下午 DefaultQuartzScheduler_Worker-3 [org.quartz.core.JobRunShell] Calling execute on job group1.job7 [INFO] 02 二月 01:28:33.821 下午 DefaultQuartzScheduler_Worker-3 [org.quartz.examples.example2.SimpleJob] SimpleJob says: group1.job7 executing at Tue Feb 02 13:28:33 CST 2016 [DEBUG] 02 二月 01:28:34.828 下午 DefaultQuartzScheduler_QuartzSchedulerThread [org.quartz.simpl.SimpleJobFactory] Producing instance of Job 'group1.job7', class=org.quartz.examples.example2.SimpleJob [DEBUG] 02 二月 01:28:34.829 下午 DefaultQuartzScheduler_Worker-5 [org.quartz.core.JobRunShell] Calling execute on job group1.job7 [INFO] 02 二月 01:28:34.829 下午 DefaultQuartzScheduler_Worker-5 [org.quartz.examples.example2.SimpleJob] SimpleJob says: group1.job7 executing at Tue Feb 02 13:28:34 CST 2016 [DEBUG] 02 二月 01:28:35.001 下午 DefaultQuartzScheduler_QuartzSchedulerThread [org.quartz.simpl.SimpleJobFactory] Producing instance of Job 'group1.job3', class=org.quartz.examples.example2.SimpleJob [DEBUG] 02 二月 01:28:35.018 下午 DefaultQuartzScheduler_Worker-6 [org.quartz.core.JobRunShell] Calling execute on job group1.job3 [DEBUG] 02 二月 01:28:35.018 下午 DefaultQuartzScheduler_QuartzSchedulerThread [org.quartz.simpl.SimpleJobFactory] Producing instance of Job 'group1.job4', class=org.quartz.examples.example2.SimpleJob [INFO] 02 二月 01:28:35.018 下午 DefaultQuartzScheduler_Worker-6 [org.quartz.examples.example2.SimpleJob] SimpleJob says: group1.job3 executing at Tue Feb 02 13:28:35 CST 2016 [DEBUG] 02 二月 01:28:35.019 下午 DefaultQuartzScheduler_Worker-8 [org.quartz.core.JobRunShell] Calling execute on job group1.job4 [INFO] 02 二月 01:28:35.019 下午 DefaultQuartzScheduler_Worker-8 [org.quartz.examples.example2.SimpleJob] SimpleJob says: group1.job4 executing at Tue Feb 02 13:28:35 CST 2016 [DEBUG] 02 二月 01:28:35.819 下午 DefaultQuartzScheduler_QuartzSchedulerThread [org.quartz.simpl.SimpleJobFactory] Producing instance of Job 'group1.job7', class=org.quartz.examples.example2.SimpleJob [DEBUG] 02 二月 01:28:35.820 下午 DefaultQuartzScheduler_Worker-7 [org.quartz.core.JobRunShell] Calling execute on job group1.job7 [INFO] 02 二月 01:28:35.820 下午 DefaultQuartzScheduler_Worker-7 [org.quartz.examples.example2.SimpleJob] SimpleJob says: group1.job7 executing at Tue Feb 02 13:28:35 CST 2016 [DEBUG] 02 二月 01:28:36.821 下午 DefaultQuartzScheduler_QuartzSchedulerThread [org.quartz.simpl.SimpleJobFactory] Producing instance of Job 'group1.job7', class=org.quartz.examples.example2.SimpleJob [DEBUG] 02 二月 01:28:36.822 下午 DefaultQuartzScheduler_Worker-9 [org.quartz.core.JobRunShell] Calling execute on job group1.job7 [INFO] 02 二月 01:28:36.822 下午 DefaultQuartzScheduler_Worker-9 [org.quartz.examples.example2.SimpleJob] SimpleJob says: group1.job7 executing at Tue Feb 02 13:28:36 CST 2016 [DEBUG] 02 二月 01:28:37.820 下午 DefaultQuartzScheduler_QuartzSchedulerThread [org.quartz.simpl.SimpleJobFactory] Producing instance of Job 'group1.job7', class=org.quartz.examples.example2.SimpleJob [DEBUG] 02 二月 01:28:37.820 下午 DefaultQuartzScheduler_Worker-10 [org.quartz.core.JobRunShell] Calling execute on job group1.job7 [INFO] 02 二月 01:28:37.821 下午 DefaultQuartzScheduler_Worker-10 [org.quartz.examples.example2.SimpleJob] SimpleJob says: group1.job7 executing at Tue Feb 02 13:28:37 CST 2016 [DEBUG] 02 二月 01:28:38.834 下午 DefaultQuartzScheduler_QuartzSchedulerThread [org.quartz.simpl.SimpleJobFactory] Producing instance of Job 'group1.job7', class=org.quartz.examples.example2.SimpleJob [DEBUG] 02 二月 01:28:38.834 下午 DefaultQuartzScheduler_Worker-1 [org.quartz.core.JobRunShell] Calling execute on job group1.job7 [INFO] 02 二月 01:28:38.834 下午 DefaultQuartzScheduler_Worker-1 [org.quartz.examples.example2.SimpleJob] SimpleJob says: group1.job7 executing at Tue Feb 02 13:28:38 CST 2016 [DEBUG] 02 二月 01:28:39.820 下午 DefaultQuartzScheduler_QuartzSchedulerThread [org.quartz.simpl.SimpleJobFactory] Producing instance of Job 'group1.job7', class=org.quartz.examples.example2.SimpleJob [DEBUG] 02 二月 01:28:39.820 下午 DefaultQuartzScheduler_Worker-2 [org.quartz.core.JobRunShell] Calling execute on job group1.job7 [INFO] 02 二月 01:28:39.821 下午 DefaultQuartzScheduler_Worker-2 [org.quartz.examples.example2.SimpleJob] SimpleJob says: group1.job7 executing at Tue Feb 02 13:28:39 CST 2016 [DEBUG] 02 二月 01:28:40.831 下午 DefaultQuartzScheduler_QuartzSchedulerThread [org.quartz.simpl.SimpleJobFactory] Producing instance of Job 'group1.job7', class=org.quartz.examples.example2.SimpleJob [DEBUG] 02 二月 01:28:40.832 下午 DefaultQuartzScheduler_Worker-4 [org.quartz.core.JobRunShell] Calling execute on job group1.job7 [INFO] 02 二月 01:28:40.832 下午 DefaultQuartzScheduler_Worker-4 [org.quartz.examples.example2.SimpleJob] SimpleJob says: group1.job7 executing at Tue Feb 02 13:28:40 CST 2016 [DEBUG] 02 二月 01:28:45.014 下午 DefaultQuartzScheduler_QuartzSchedulerThread [org.quartz.simpl.SimpleJobFactory] Producing instance of Job 'group1.job3', class=org.quartz.examples.example2.SimpleJob [DEBUG] 02 二月 01:28:45.015 下午 DefaultQuartzScheduler_Worker-3 [org.quartz.core.JobRunShell] Calling execute on job group1.job3 [DEBUG] 02 二月 01:28:45.015 下午 DefaultQuartzScheduler_QuartzSchedulerThread [org.quartz.simpl.SimpleJobFactory] Producing instance of Job 'group1.job4', class=org.quartz.examples.example2.SimpleJob [INFO] 02 二月 01:28:45.015 下午 DefaultQuartzScheduler_Worker-3 [org.quartz.examples.example2.SimpleJob] SimpleJob says: group1.job3 executing at Tue Feb 02 13:28:45 CST 2016 [DEBUG] 02 二月 01:28:45.016 下午 DefaultQuartzScheduler_Worker-5 [org.quartz.core.JobRunShell] Calling execute on job group1.job4 [INFO] 02 二月 01:28:45.017 下午 DefaultQuartzScheduler_Worker-5 [org.quartz.examples.example2.SimpleJob] SimpleJob says: group1.job4 executing at Tue Feb 02 13:28:45 CST 2016 [DEBUG] 02 二月 01:28:55.000 下午 DefaultQuartzScheduler_QuartzSchedulerThread [org.quartz.simpl.SimpleJobFactory] Producing instance of Job 'group1.job3', class=org.quartz.examples.example2.SimpleJob [DEBUG] 02 二月 01:28:55.000 下午 DefaultQuartzScheduler_Worker-6 [org.quartz.core.JobRunShell] Calling execute on job group1.job3 [DEBUG] 02 二月 01:28:55.000 下午 DefaultQuartzScheduler_QuartzSchedulerThread [org.quartz.simpl.SimpleJobFactory] Producing instance of Job 'group1.job4', class=org.quartz.examples.example2.SimpleJob [INFO] 02 二月 01:28:55.001 下午 DefaultQuartzScheduler_Worker-6 [org.quartz.examples.example2.SimpleJob] SimpleJob says: group1.job3 executing at Tue Feb 02 13:28:55 CST 2016 [DEBUG] 02 二月 01:28:55.002 下午 DefaultQuartzScheduler_Worker-8 [org.quartz.core.JobRunShell] Calling execute on job group1.job4 [INFO] 02 二月 01:28:55.003 下午 DefaultQuartzScheduler_Worker-8 [org.quartz.examples.example2.SimpleJob] SimpleJob says: group1.job4 executing at Tue Feb 02 13:28:55 CST 2016 [DEBUG] 02 二月 01:29:05.001 下午 DefaultQuartzScheduler_QuartzSchedulerThread [org.quartz.simpl.SimpleJobFactory] Producing instance of Job 'group1.job3', class=org.quartz.examples.example2.SimpleJob [DEBUG] 02 二月 01:29:05.001 下午 DefaultQuartzScheduler_Worker-7 [org.quartz.core.JobRunShell] Calling execute on job group1.job3 [DEBUG] 02 二月 01:29:05.001 下午 DefaultQuartzScheduler_QuartzSchedulerThread [org.quartz.simpl.SimpleJobFactory] Producing instance of Job 'group1.job4', class=org.quartz.examples.example2.SimpleJob [INFO] 02 二月 01:29:05.001 下午 DefaultQuartzScheduler_Worker-7 [org.quartz.examples.example2.SimpleJob] SimpleJob says: group1.job3 executing at Tue Feb 02 13:29:05 CST 2016 [DEBUG] 02 二月 01:29:05.002 下午 DefaultQuartzScheduler_Worker-9 [org.quartz.core.JobRunShell] Calling execute on job group1.job4 [DEBUG] 02 二月 01:29:05.002 下午 DefaultQuartzScheduler_QuartzSchedulerThread [org.quartz.simpl.SimpleJobFactory] Producing instance of Job 'group1.job6', class=org.quartz.examples.example2.SimpleJob [INFO] 02 二月 01:29:05.002 下午 DefaultQuartzScheduler_Worker-9 [org.quartz.examples.example2.SimpleJob] SimpleJob says: group1.job4 executing at Tue Feb 02 13:29:05 CST 2016 [DEBUG] 02 二月 01:29:05.003 下午 DefaultQuartzScheduler_Worker-10 [org.quartz.core.JobRunShell] Calling execute on job group1.job6 [INFO] 02 二月 01:29:05.003 下午 DefaultQuartzScheduler_Worker-10 [org.quartz.examples.example2.SimpleJob] SimpleJob says: group1.job6 executing at Tue Feb 02 13:29:05 CST 2016 [DEBUG] 02 二月 01:29:15.000 下午 DefaultQuartzScheduler_QuartzSchedulerThread [org.quartz.simpl.SimpleJobFactory] Producing instance of Job 'group1.job3', class=org.quartz.examples.example2.SimpleJob [DEBUG] 02 二月 01:29:15.001 下午 DefaultQuartzScheduler_Worker-1 [org.quartz.core.JobRunShell] Calling execute on job group1.job3 [INFO] 02 二月 01:29:15.001 下午 DefaultQuartzScheduler_Worker-1 [org.quartz.examples.example2.SimpleJob] SimpleJob says: group1.job3 executing at Tue Feb 02 13:29:15 CST 2016 [DEBUG] 02 二月 01:29:25.000 下午 DefaultQuartzScheduler_QuartzSchedulerThread [org.quartz.simpl.SimpleJobFactory] Producing instance of Job 'group1.job3', class=org.quartz.examples.example2.SimpleJob [DEBUG] 02 二月 01:29:25.000 下午 DefaultQuartzScheduler_Worker-2 [org.quartz.core.JobRunShell] Calling execute on job group1.job3 [DEBUG] 02 二月 01:29:25.001 下午 DefaultQuartzScheduler_QuartzSchedulerThread [org.quartz.simpl.SimpleJobFactory] Producing instance of Job 'group1.job3', class=org.quartz.examples.example2.SimpleJob [INFO] 02 二月 01:29:25.001 下午 DefaultQuartzScheduler_Worker-2 [org.quartz.examples.example2.SimpleJob] SimpleJob says: group1.job3 executing at Tue Feb 02 13:29:25 CST 2016 [DEBUG] 02 二月 01:29:25.002 下午 DefaultQuartzScheduler_Worker-4 [org.quartz.core.JobRunShell] Calling execute on job group1.job3 [INFO] 02 二月 01:29:25.003 下午 DefaultQuartzScheduler_Worker-4 [org.quartz.examples.example2.SimpleJob] SimpleJob says: group1.job3 executing at Tue Feb 02 13:29:25 CST 2016 [DEBUG] 02 二月 01:29:35.008 下午 DefaultQuartzScheduler_QuartzSchedulerThread [org.quartz.simpl.SimpleJobFactory] Producing instance of Job 'group1.job3', class=org.quartz.examples.example2.SimpleJob [DEBUG] 02 二月 01:29:35.009 下午 DefaultQuartzScheduler_Worker-3 [org.quartz.core.JobRunShell] Calling execute on job group1.job3 [INFO] 02 二月 01:29:35.010 下午 DefaultQuartzScheduler_Worker-3 [org.quartz.examples.example2.SimpleJob] SimpleJob says: group1.job3 executing at Tue Feb 02 13:29:35 CST 2016 [DEBUG] 02 二月 01:29:45.012 下午 DefaultQuartzScheduler_QuartzSchedulerThread [org.quartz.simpl.SimpleJobFactory] Producing instance of Job 'group1.job3', class=org.quartz.examples.example2.SimpleJob [DEBUG] 02 二月 01:29:45.012 下午 DefaultQuartzScheduler_Worker-5 [org.quartz.core.JobRunShell] Calling execute on job group1.job3 [INFO] 02 二月 01:29:45.012 下午 DefaultQuartzScheduler_Worker-5 [org.quartz.examples.example2.SimpleJob] SimpleJob says: group1.job3 executing at Tue Feb 02 13:29:45 CST 2016 [DEBUG] 02 二月 01:29:55.000 下午 DefaultQuartzScheduler_QuartzSchedulerThread [org.quartz.simpl.SimpleJobFactory] Producing instance of Job 'group1.job3', class=org.quartz.examples.example2.SimpleJob [DEBUG] 02 二月 01:29:55.001 下午 DefaultQuartzScheduler_QuartzSchedulerThread [org.quartz.simpl.SimpleJobFactory] Producing instance of Job 'group1.job6', class=org.quartz.examples.example2.SimpleJob [DEBUG] 02 二月 01:29:55.001 下午 DefaultQuartzScheduler_Worker-6 [org.quartz.core.JobRunShell] Calling execute on job group1.job3 [DEBUG] 02 二月 01:29:55.001 下午 DefaultQuartzScheduler_Worker-8 [org.quartz.core.JobRunShell] Calling execute on job group1.job6 [INFO] 02 二月 01:29:55.001 下午 DefaultQuartzScheduler_Worker-6 [org.quartz.examples.example2.SimpleJob] SimpleJob says: group1.job3 executing at Tue Feb 02 13:29:55 CST 2016 [INFO] 02 二月 01:29:55.001 下午 DefaultQuartzScheduler_Worker-8 [org.quartz.examples.example2.SimpleJob] SimpleJob says: group1.job6 executing at Tue Feb 02 13:29:55 CST 2016 [DEBUG] 02 二月 01:30:35.000 下午 DefaultQuartzScheduler_QuartzSchedulerThread [org.quartz.simpl.SimpleJobFactory] Producing instance of Job 'group1.job3', class=org.quartz.examples.example2.SimpleJob [DEBUG] 02 二月 01:30:35.001 下午 DefaultQuartzScheduler_Worker-7 [org.quartz.core.JobRunShell] Calling execute on job group1.job3 [INFO] 02 二月 01:30:35.001 下午 DefaultQuartzScheduler_Worker-7 [org.quartz.examples.example2.SimpleJob] SimpleJob says: group1.job3 executing at Tue Feb 02 13:30:35 CST 2016 [DEBUG] 02 二月 01:30:45.000 下午 DefaultQuartzScheduler_QuartzSchedulerThread [org.quartz.simpl.SimpleJobFactory] Producing instance of Job 'group1.job6', class=org.quartz.examples.example2.SimpleJob [DEBUG] 02 二月 01:30:45.001 下午 DefaultQuartzScheduler_Worker-9 [org.quartz.core.JobRunShell] Calling execute on job group1.job6 [INFO] 02 二月 01:30:45.001 下午 DefaultQuartzScheduler_Worker-9 [org.quartz.examples.example2.SimpleJob] SimpleJob says: group1.job6 executing at Tue Feb 02 13:30:45 CST 2016 [DEBUG] 02 二月 01:31:35.000 下午 DefaultQuartzScheduler_QuartzSchedulerThread [org.quartz.simpl.SimpleJobFactory] Producing instance of Job 'group1.job6', class=org.quartz.examples.example2.SimpleJob [DEBUG] 02 二月 01:31:35.001 下午 DefaultQuartzScheduler_Worker-10 [org.quartz.core.JobRunShell] Calling execute on job group1.job6 [INFO] 02 二月 01:31:35.001 下午 DefaultQuartzScheduler_Worker-10 [org.quartz.examples.example2.SimpleJob] SimpleJob says: group1.job6 executing at Tue Feb 02 13:31:35 CST 2016 [DEBUG] 02 二月 01:32:25.000 下午 DefaultQuartzScheduler_QuartzSchedulerThread [org.quartz.simpl.SimpleJobFactory] Producing instance of Job 'group1.job6', class=org.quartz.examples.example2.SimpleJob [DEBUG] 02 二月 01:32:25.000 下午 DefaultQuartzScheduler_Worker-1 [org.quartz.core.JobRunShell] Calling execute on job group1.job6 [INFO] 02 二月 01:32:25.001 下午 DefaultQuartzScheduler_Worker-1 [org.quartz.examples.example2.SimpleJob] SimpleJob says: group1.job6 executing at Tue Feb 02 13:32:25 CST 2016 [DEBUG] 02 二月 01:33:15.001 下午 DefaultQuartzScheduler_QuartzSchedulerThread [org.quartz.simpl.SimpleJobFactory] Producing instance of Job 'group1.job5', class=org.quartz.examples.example2.SimpleJob [DEBUG] 02 二月 01:33:15.002 下午 DefaultQuartzScheduler_Worker-2 [org.quartz.core.JobRunShell] Calling execute on job group1.job5 [DEBUG] 02 二月 01:33:15.002 下午 DefaultQuartzScheduler_QuartzSchedulerThread [org.quartz.simpl.SimpleJobFactory] Producing instance of Job 'group1.job6', class=org.quartz.examples.example2.SimpleJob [INFO] 02 二月 01:33:15.002 下午 DefaultQuartzScheduler_Worker-2 [org.quartz.examples.example2.SimpleJob] SimpleJob says: group1.job5 executing at Tue Feb 02 13:33:15 CST 2016 [DEBUG] 02 二月 01:33:15.003 下午 DefaultQuartzScheduler_Worker-4 [org.quartz.core.JobRunShell] Calling execute on job group1.job6 [INFO] 02 二月 01:33:15.004 下午 DefaultQuartzScheduler_Worker-4 [org.quartz.examples.example2.SimpleJob] SimpleJob says: group1.job6 executing at Tue Feb 02 13:33:15 CST 2016 [INFO] 02 二月 01:33:30.824 下午 main [org.quartz.examples.example2.SimpleTriggerExample] ------- Shutting Down --------------------- [INFO] 02 二月 01:33:30.824 下午 main [org.quartz.core.QuartzScheduler] Scheduler DefaultQuartzScheduler_$_NON_CLUSTERED shutting down. [INFO] 02 二月 01:33:30.825 下午 main [org.quartz.core.QuartzScheduler] Scheduler DefaultQuartzScheduler_$_NON_CLUSTERED paused. [DEBUG] 02 二月 01:33:30.825 下午 main [org.quartz.simpl.SimpleThreadPool] shutdown complete [INFO] 02 二月 01:33:30.826 下午 main [org.quartz.core.QuartzScheduler] Scheduler DefaultQuartzScheduler_$_NON_CLUSTERED shutdown complete. [INFO] 02 二月 01:33:30.826 下午 main [org.quartz.examples.example2.SimpleTriggerExample] ------- Shutdown Complete ----------------- [INFO] 02 二月 01:33:30.827 下午 main [org.quartz.examples.example2.SimpleTriggerExample] Executed 43 jobs. [DEBUG] 02 二月 01:33:30.978 下午 DefaultQuartzScheduler_Worker-1 [org.quartz.simpl.SimpleThreadPool] WorkerThread is shut down. [DEBUG] 02 二月 01:33:31.027 下午 DefaultQuartzScheduler_Worker-7 [org.quartz.simpl.SimpleThreadPool] WorkerThread is shut down. [DEBUG] 02 二月 01:33:31.027 下午 DefaultQuartzScheduler_Worker-6 [org.quartz.simpl.SimpleThreadPool] WorkerThread is shut down. [DEBUG] 02 二月 01:33:31.027 下午 DefaultQuartzScheduler_Worker-9 [org.quartz.simpl.SimpleThreadPool] WorkerThread is shut down. [DEBUG] 02 二月 01:33:31.027 下午 DefaultQuartzScheduler_Worker-5 [org.quartz.simpl.SimpleThreadPool] WorkerThread is shut down. [DEBUG] 02 二月 01:33:31.027 下午 DefaultQuartzScheduler_Worker-3 [org.quartz.simpl.SimpleThreadPool] WorkerThread is shut down. [DEBUG] 02 二月 01:33:31.027 下午 DefaultQuartzScheduler_Worker-8 [org.quartz.simpl.SimpleThreadPool] WorkerThread is shut down. [DEBUG] 02 二月 01:33:31.118 下午 DefaultQuartzScheduler_Worker-2 [org.quartz.simpl.SimpleThreadPool] WorkerThread is shut down. [DEBUG] 02 二月 01:33:31.118 下午 DefaultQuartzScheduler_Worker-4 [org.quartz.simpl.SimpleThreadPool] WorkerThread is shut down. [DEBUG] 02 二月 01:33:31.233 下午 DefaultQuartzScheduler_Worker-10 [org.quartz.simpl.SimpleThreadPool] WorkerThread is shut down.
从这个例子中我们看到JobDetail对象是可以重复利用的,Trigger对象也是,原因在哪里?
看源代码就可以发现,真正放置进入RAMJobStore存储的都是他们的拷贝对象
public void storeJob(SchedulingContext ctxt, JobDetail newJob, boolean replaceExisting) throws ObjectAlreadyExistsException { JobWrapper jw = new JobWrapper((JobDetail)newJob.clone()); boolean repl = false; synchronized (lock) { if (jobsByFQN.get(jw.key) != null) { if (!replaceExisting) { throw new ObjectAlreadyExistsException(newJob); } repl = true; } if (!repl) { // get job group HashMap grpMap = (HashMap) jobsByGroup.get(newJob.getGroup()); if (grpMap == null) { grpMap = new HashMap(100); jobsByGroup.put(newJob.getGroup(), grpMap); } // add to jobs by group grpMap.put(newJob.getName(), jw); // add to jobs by FQN map jobsByFQN.put(jw.key, jw); } else { // update job detail JobWrapper orig = (JobWrapper) jobsByFQN.get(jw.key); orig.jobDetail = jw.jobDetail; // already cloned } } } /** * <p> * Store the given <code>{@link org.quartz.Trigger}</code>. * </p> * * @param newTrigger * The <code>Trigger</code> to be stored. * @param replaceExisting * If <code>true</code>, any <code>Trigger</code> existing in * the <code>JobStore</code> with the same name & group should * be over-written. * @throws ObjectAlreadyExistsException * if a <code>Trigger</code> with the same name/group already * exists, and replaceExisting is set to false. * * @see #pauseTriggerGroup(SchedulingContext, String) */ public void storeTrigger(SchedulingContext ctxt, Trigger newTrigger, boolean replaceExisting) throws JobPersistenceException { TriggerWrapper tw = new TriggerWrapper((Trigger)newTrigger.clone()); synchronized (lock) { if (triggersByFQN.get(tw.key) != null) { if (!replaceExisting) { throw new ObjectAlreadyExistsException(newTrigger); } removeTrigger(ctxt, newTrigger.getName(), newTrigger.getGroup(), false); } if (retrieveJob(ctxt, newTrigger.getJobName(), newTrigger.getJobGroup()) == null) { throw new JobPersistenceException("The job (" + newTrigger.getFullJobName() + ") referenced by the trigger does not exist."); } // add to triggers array triggers.add(tw); // add to triggers by group HashMap grpMap = (HashMap) triggersByGroup.get(newTrigger .getGroup()); if (grpMap == null) { grpMap = new HashMap(100); triggersByGroup.put(newTrigger.getGroup(), grpMap); } grpMap.put(newTrigger.getName(), tw); // add to triggers by FQN map triggersByFQN.put(tw.key, tw); if (pausedTriggerGroups.contains(newTrigger.getGroup()) || pausedJobGroups.contains(newTrigger.getJobGroup())) { tw.state = TriggerWrapper.STATE_PAUSED; if (blockedJobs.contains(tw.jobKey)) { tw.state = TriggerWrapper.STATE_PAUSED_BLOCKED; } } else if (blockedJobs.contains(tw.jobKey)) { tw.state = TriggerWrapper.STATE_BLOCKED; } else { timeTriggers.add(tw); } } }