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.example1; 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 says "Hello" to the world. * </p> * * @author Bill Kratzer */ public class HelloJob implements Job { private static Logger _log = LoggerFactory.getLogger(HelloJob.class); /** * <p> * Empty constructor for job initilization * </p> * <p> * Quartz requires a public empty constructor so that the * scheduler can instantiate the class whenever it needs. * </p> */ public HelloJob() { } /** * <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 { // Say Hello to the World and display the date/time _log.info("Hello World! - " + 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.example1; import java.util.Date; 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.TriggerUtils; import org.quartz.impl.StdSchedulerFactory; /** * This Example will demonstrate how to start and shutdown the Quartz * scheduler and how to schedule a job to run in Quartz. * * @author Bill Kratzer */ public class SimpleExample { public void run() throws Exception { Logger log = LoggerFactory.getLogger(SimpleExample.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 -------------------"); // computer a time that is on the next round minute Date runTime = TriggerUtils.getEvenMinuteDate(new Date()); // define the job and tie it to our HelloJob class JobDetail job = new JobDetail("job1", "group1", HelloJob.class); // Trigger the job to run on the next round minute SimpleTrigger trigger = new SimpleTrigger("trigger1", "group1", runTime); // Tell quartz to schedule the job using our trigger sched.scheduleJob(job, trigger); log.info(job.getFullName() + " will run at: " + runTime); // 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 // run the job! log.info("------- Waiting 90 seconds... -------------"); try { // wait 90 seconds to show jobs Thread.sleep(90L * 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 { SimpleExample example = new SimpleExample(); 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:17:54.674 下午 main [org.quartz.examples.example1.SimpleExample] ------- Initializing ---------------------- [INFO] 02 二月 01:17:54.701 下午 main [org.quartz.simpl.SimpleThreadPool] Job execution threads will use class loader of thread: main [INFO] 02 二月 01:17:54.714 下午 main [org.quartz.core.SchedulerSignalerImpl] Initialized Scheduler Signaller of type: class org.quartz.core.SchedulerSignalerImpl [INFO] 02 二月 01:17:54.715 下午 main [org.quartz.core.QuartzScheduler] Quartz Scheduler v.1.8.5 created. [INFO] 02 二月 01:17:54.716 下午 main [org.quartz.simpl.RAMJobStore] RAMJobStore initialized. [INFO] 02 二月 01:17:54.717 下午 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:17:54.717 下午 main [org.quartz.impl.StdSchedulerFactory] Quartz scheduler 'DefaultQuartzScheduler' initialized from default resource file in Quartz package: 'quartz.properties' [INFO] 02 二月 01:17:54.717 下午 main [org.quartz.impl.StdSchedulerFactory] Quartz scheduler version: 1.8.5 [INFO] 02 二月 01:17:54.717 下午 main [org.quartz.examples.example1.SimpleExample] ------- Initialization Complete ----------- [INFO] 02 二月 01:17:54.717 下午 main [org.quartz.examples.example1.SimpleExample] ------- Scheduling Jobs ------------------- [INFO] 02 二月 01:17:54.723 下午 main [org.quartz.examples.example1.SimpleExample] group1.job1 will run at: Tue Feb 02 13:18:00 CST 2016 [INFO] 02 二月 01:17:54.723 下午 main [org.quartz.core.QuartzScheduler] Scheduler DefaultQuartzScheduler_$_NON_CLUSTERED started. [INFO] 02 二月 01:17:54.723 下午 main [org.quartz.examples.example1.SimpleExample] ------- Started Scheduler ----------------- [INFO] 02 二月 01:17:54.723 下午 main [org.quartz.examples.example1.SimpleExample] ------- Waiting 90 seconds... ------------- [DEBUG] 02 二月 01:17:55.715 下午 Timer-0 [org.quartz.utils.UpdateChecker] Checking for available updated version of Quartz... [DEBUG] 02 二月 01:18:00.001 下午 DefaultQuartzScheduler_QuartzSchedulerThread [org.quartz.simpl.SimpleJobFactory] Producing instance of Job 'group1.job1', class=org.quartz.examples.example1.HelloJob [DEBUG] 02 二月 01:18:00.019 下午 DefaultQuartzScheduler_Worker-1 [org.quartz.core.JobRunShell] Calling execute on job group1.job1 [INFO] 02 二月 01:18:00.030 下午 DefaultQuartzScheduler_Worker-1 [org.quartz.examples.example1.HelloJob] Hello World! - Tue Feb 02 13:18:00 CST 2016 [INFO] 02 二月 01:19:24.723 下午 main [org.quartz.examples.example1.SimpleExample] ------- Shutting Down --------------------- [INFO] 02 二月 01:19:24.723 下午 main [org.quartz.core.QuartzScheduler] Scheduler DefaultQuartzScheduler_$_NON_CLUSTERED shutting down. [INFO] 02 二月 01:19:24.723 下午 main [org.quartz.core.QuartzScheduler] Scheduler DefaultQuartzScheduler_$_NON_CLUSTERED paused. [DEBUG] 02 二月 01:19:24.723 下午 main [org.quartz.simpl.SimpleThreadPool] shutdown complete [INFO] 02 二月 01:19:24.723 下午 main [org.quartz.core.QuartzScheduler] Scheduler DefaultQuartzScheduler_$_NON_CLUSTERED shutdown complete. [INFO] 02 二月 01:19:24.724 下午 main [org.quartz.examples.example1.SimpleExample] ------- Shutdown Complete ----------------- [DEBUG] 02 二月 01:19:25.000 下午 DefaultQuartzScheduler_Worker-1 [org.quartz.simpl.SimpleThreadPool] WorkerThread is shut down. [DEBUG] 02 二月 01:19:25.221 下午 DefaultQuartzScheduler_Worker-4 [org.quartz.simpl.SimpleThreadPool] WorkerThread is shut down. [DEBUG] 02 二月 01:19:25.221 下午 DefaultQuartzScheduler_Worker-8 [org.quartz.simpl.SimpleThreadPool] WorkerThread is shut down. [DEBUG] 02 二月 01:19:25.221 下午 DefaultQuartzScheduler_Worker-2 [org.quartz.simpl.SimpleThreadPool] WorkerThread is shut down. [DEBUG] 02 二月 01:19:25.221 下午 DefaultQuartzScheduler_Worker-5 [org.quartz.simpl.SimpleThreadPool] WorkerThread is shut down. [DEBUG] 02 二月 01:19:25.221 下午 DefaultQuartzScheduler_Worker-7 [org.quartz.simpl.SimpleThreadPool] WorkerThread is shut down. [DEBUG] 02 二月 01:19:25.221 下午 DefaultQuartzScheduler_Worker-6 [org.quartz.simpl.SimpleThreadPool] WorkerThread is shut down. [DEBUG] 02 二月 01:19:25.221 下午 DefaultQuartzScheduler_Worker-9 [org.quartz.simpl.SimpleThreadPool] WorkerThread is shut down. [DEBUG] 02 二月 01:19:25.221 下午 DefaultQuartzScheduler_Worker-3 [org.quartz.simpl.SimpleThreadPool] WorkerThread is shut down. [DEBUG] 02 二月 01:19:25.221 下午 DefaultQuartzScheduler_Worker-10 [org.quartz.simpl.SimpleThreadPool] WorkerThread is shut down.