Quartz1.8.5例子(十四)
org.quartz.scheduler.instanceName: PriorityExampleScheduler # Set thread count to 1 to force Triggers scheduled for the same time to # to be ordered by priority. org.quartz.threadPool.threadCount: 1 org.quartz.threadPool.class: org.quartz.simpl.SimpleThreadPool org.quartz.jobStore.class: org.quartz.simpl.RAMJobStore
/* * Copyright 2006-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.example14; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.quartz.Job; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; /** * This is just a simple job that echos the name of the Trigger * that fired it. */ public class TriggerEchoJob implements Job { private static final Logger LOG = LoggerFactory.getLogger(TriggerEchoJob.class); /** * Empty constructor for job initilization * * <p> * Quartz requires a public empty constructor so that the * scheduler can instantiate the class whenever it needs. * </p> */ public TriggerEchoJob() { } /** * <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 { LOG.info("TRIGGER: " + context.getTrigger().getName()); } }
/* * Copyright 2006-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.example14; import java.util.Calendar; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.quartz.JobDetail; import org.quartz.Scheduler; import org.quartz.SchedulerFactory; import org.quartz.SimpleTrigger; import org.quartz.impl.StdSchedulerFactory; /** * This Example will demonstrate how Triggers are ordered by priority. */ public class PriorityExample { public void run() throws Exception { Logger log = LoggerFactory.getLogger(PriorityExample.class); log.info("------- Initializing ----------------------"); // First we must get a reference to a scheduler SchedulerFactory sf = new StdSchedulerFactory( "org/quartz/examples/example14/quartz_priority.properties"); Scheduler sched = sf.getScheduler(); log.info("------- Initialization Complete -----------"); log.info("------- Scheduling Jobs -------------------"); JobDetail job = new JobDetail("TriggerEchoJob", null, TriggerEchoJob.class); // All three triggers will fire their first time at the same time, // ordered by their priority, and then repeat once, firing in a // staggered order that therefore ignores priority. // // We should see the following firing order: // 1. Priority10Trigger15SecondRepeat // 2. Priority5Trigger10SecondRepeat // 3. PriorityNeg5Trigger5SecondRepeat // 4. PriorityNeg5Trigger5SecondRepeat // 5. Priority5Trigger10SecondRepeat // 6. Priority10Trigger15SecondRepeat // Calculate the start time of all triggers as 5 seconds from now Calendar startTime = Calendar.getInstance(); startTime.add(Calendar.SECOND, 5); // First trigger has priority of 1, and will repeat after 5 seconds SimpleTrigger trigger1 = new SimpleTrigger("PriorityNeg5Trigger5SecondRepeat", null, startTime.getTime(), null, 1, 5L * 1000L); trigger1.setPriority(1); trigger1.setJobName("TriggerEchoJob"); // Second trigger has default priority of 5, and will repeat after 10 seconds SimpleTrigger trigger2 = new SimpleTrigger("Priority5Trigger10SecondRepeat", null, startTime.getTime(), null, 1, 10L * 1000L); trigger2.setJobName("TriggerEchoJob"); // Third trigger has priority 10, and will repeat after 15 seconds SimpleTrigger trigger3 = new SimpleTrigger("Priority10Trigger15SecondRepeat", null, startTime.getTime(), null, 1, 15L * 1000L); trigger3.setPriority(10); trigger3.setJobName("TriggerEchoJob"); // Tell quartz to schedule the job using our trigger sched.scheduleJob(job, trigger1); sched.scheduleJob(trigger2); sched.scheduleJob(trigger3); // Start up the scheduler (nothing can actually run until the // scheduler has been started) sched.start(); log.info("------- Started Scheduler -----------------"); // wait long enough so that the scheduler as an opportunity to // fire the triggers log.info("------- Waiting 30 seconds... -------------"); try { Thread.sleep(30L * 1000L); // executing... } catch (Exception e) { } // shut down the scheduler log.info("------- Shutting Down ---------------------"); sched.shutdown(true); log.info("------- Shutdown Complete -----------------"); } public static void main(String[] args) throws Exception { PriorityExample example = new PriorityExample(); example.run(); } }
[INFO] 02 二月 04:57:35.735 下午 main [org.quartz.examples.example14.PriorityExample] ------- Initializing ---------------------- [INFO] 02 二月 04:57:35.768 下午 main [org.quartz.core.SchedulerSignalerImpl] Initialized Scheduler Signaller of type: class org.quartz.core.SchedulerSignalerImpl [INFO] 02 二月 04:57:35.769 下午 main [org.quartz.core.QuartzScheduler] Quartz Scheduler v.1.8.5 created. [INFO] 02 二月 04:57:35.770 下午 main [org.quartz.simpl.RAMJobStore] RAMJobStore initialized. [INFO] 02 二月 04:57:35.771 下午 main [org.quartz.core.QuartzScheduler] Scheduler meta-data: Quartz Scheduler (v1.8.5) 'PriorityExampleScheduler' 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 1 threads. Using job-store 'org.quartz.simpl.RAMJobStore' - which does not support persistence. and is not clustered. [INFO] 02 二月 04:57:35.771 下午 main [org.quartz.impl.StdSchedulerFactory] Quartz scheduler 'PriorityExampleScheduler' initialized from the specified file : 'org/quartz/examples/example14/quartz_priority.properties' from the class resource path. [INFO] 02 二月 04:57:35.771 下午 main [org.quartz.impl.StdSchedulerFactory] Quartz scheduler version: 1.8.5 [INFO] 02 二月 04:57:35.771 下午 main [org.quartz.examples.example14.PriorityExample] ------- Initialization Complete ----------- [INFO] 02 二月 04:57:35.771 下午 main [org.quartz.examples.example14.PriorityExample] ------- Scheduling Jobs ------------------- [INFO] 02 二月 04:57:35.774 下午 main [org.quartz.core.QuartzScheduler] Scheduler PriorityExampleScheduler_$_NON_CLUSTERED started. [INFO] 02 二月 04:57:35.774 下午 main [org.quartz.examples.example14.PriorityExample] ------- Started Scheduler ----------------- [INFO] 02 二月 04:57:35.774 下午 main [org.quartz.examples.example14.PriorityExample] ------- Waiting 30 seconds... ------------- [DEBUG] 02 二月 04:57:36.773 下午 Timer-0 [org.quartz.utils.UpdateChecker] Checking for available updated version of Quartz... [DEBUG] 02 二月 04:57:39.846 下午 Timer-0 [org.quartz.utils.UpdateChecker] Quartz version update check failed: java.net.SocketTimeoutException: connect timed out [DEBUG] 02 二月 04:57:40.784 下午 PriorityExampleScheduler_QuartzSchedulerThread [org.quartz.simpl.SimpleJobFactory] Producing instance of Job 'DEFAULT.TriggerEchoJob', class=org.quartz.examples.example14.TriggerEchoJob [DEBUG] 02 二月 04:57:40.802 下午 PriorityExampleScheduler_Worker-1 [org.quartz.core.JobRunShell] Calling execute on job DEFAULT.TriggerEchoJob [INFO] 02 二月 04:57:40.802 下午 PriorityExampleScheduler_Worker-1 [org.quartz.examples.example14.TriggerEchoJob] TRIGGER: Priority10Trigger15SecondRepeat [DEBUG] 02 二月 04:57:40.802 下午 PriorityExampleScheduler_QuartzSchedulerThread [org.quartz.simpl.SimpleJobFactory] Producing instance of Job 'DEFAULT.TriggerEchoJob', class=org.quartz.examples.example14.TriggerEchoJob [DEBUG] 02 二月 04:57:40.803 下午 PriorityExampleScheduler_Worker-1 [org.quartz.core.JobRunShell] Calling execute on job DEFAULT.TriggerEchoJob [INFO] 02 二月 04:57:40.803 下午 PriorityExampleScheduler_Worker-1 [org.quartz.examples.example14.TriggerEchoJob] TRIGGER: Priority5Trigger10SecondRepeat [DEBUG] 02 二月 04:57:40.803 下午 PriorityExampleScheduler_QuartzSchedulerThread [org.quartz.simpl.SimpleJobFactory] Producing instance of Job 'DEFAULT.TriggerEchoJob', class=org.quartz.examples.example14.TriggerEchoJob [DEBUG] 02 二月 04:57:40.803 下午 PriorityExampleScheduler_Worker-1 [org.quartz.core.JobRunShell] Calling execute on job DEFAULT.TriggerEchoJob [INFO] 02 二月 04:57:40.804 下午 PriorityExampleScheduler_Worker-1 [org.quartz.examples.example14.TriggerEchoJob] TRIGGER: PriorityNeg5Trigger5SecondRepeat [DEBUG] 02 二月 04:57:45.777 下午 PriorityExampleScheduler_QuartzSchedulerThread [org.quartz.simpl.SimpleJobFactory] Producing instance of Job 'DEFAULT.TriggerEchoJob', class=org.quartz.examples.example14.TriggerEchoJob [DEBUG] 02 二月 04:57:45.778 下午 PriorityExampleScheduler_Worker-1 [org.quartz.core.JobRunShell] Calling execute on job DEFAULT.TriggerEchoJob [INFO] 02 二月 04:57:45.778 下午 PriorityExampleScheduler_Worker-1 [org.quartz.examples.example14.TriggerEchoJob] TRIGGER: PriorityNeg5Trigger5SecondRepeat [DEBUG] 02 二月 04:57:50.783 下午 PriorityExampleScheduler_QuartzSchedulerThread [org.quartz.simpl.SimpleJobFactory] Producing instance of Job 'DEFAULT.TriggerEchoJob', class=org.quartz.examples.example14.TriggerEchoJob [DEBUG] 02 二月 04:57:50.784 下午 PriorityExampleScheduler_Worker-1 [org.quartz.core.JobRunShell] Calling execute on job DEFAULT.TriggerEchoJob [INFO] 02 二月 04:57:50.785 下午 PriorityExampleScheduler_Worker-1 [org.quartz.examples.example14.TriggerEchoJob] TRIGGER: Priority5Trigger10SecondRepeat [DEBUG] 02 二月 04:57:55.776 下午 PriorityExampleScheduler_QuartzSchedulerThread [org.quartz.simpl.SimpleJobFactory] Producing instance of Job 'DEFAULT.TriggerEchoJob', class=org.quartz.examples.example14.TriggerEchoJob [DEBUG] 02 二月 04:57:55.777 下午 PriorityExampleScheduler_Worker-1 [org.quartz.core.JobRunShell] Calling execute on job DEFAULT.TriggerEchoJob [INFO] 02 二月 04:57:55.777 下午 PriorityExampleScheduler_Worker-1 [org.quartz.examples.example14.TriggerEchoJob] TRIGGER: Priority10Trigger15SecondRepeat [INFO] 02 二月 04:58:05.775 下午 main [org.quartz.examples.example14.PriorityExample] ------- Shutting Down --------------------- [INFO] 02 二月 04:58:05.775 下午 main [org.quartz.core.QuartzScheduler] Scheduler PriorityExampleScheduler_$_NON_CLUSTERED shutting down. [INFO] 02 二月 04:58:05.775 下午 main [org.quartz.core.QuartzScheduler] Scheduler PriorityExampleScheduler_$_NON_CLUSTERED paused. [DEBUG] 02 二月 04:58:05.776 下午 main [org.quartz.simpl.SimpleThreadPool] shutdown complete [INFO] 02 二月 04:58:05.776 下午 main [org.quartz.core.QuartzScheduler] Scheduler PriorityExampleScheduler_$_NON_CLUSTERED shutdown complete. [INFO] 02 二月 04:58:05.776 下午 main [org.quartz.examples.example14.PriorityExample] ------- Shutdown Complete ----------------- [DEBUG] 02 二月 04:58:05.885 下午 PriorityExampleScheduler_Worker-1 [org.quartz.simpl.SimpleThreadPool] WorkerThread is shut down.