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.example9; import java.util.Date; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.quartz.JobDetail; import org.quartz.JobListener; import org.quartz.Scheduler; import org.quartz.SchedulerFactory; import org.quartz.SchedulerMetaData; import org.quartz.SimpleTrigger; import org.quartz.examples.example2.SimpleJob; import org.quartz.impl.StdSchedulerFactory; /** * Demonstrates the behavior of <code>JobListener</code>s. In particular, * this example will use a job listener to trigger another job after one * job succesfully executes. * */ public class ListenerExample { public void run() throws Exception { Logger log = LoggerFactory.getLogger(ListenerExample.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 -------------------"); // schedule a job to run immediately JobDetail job = new JobDetail("job1", "group1", SimpleJob.class); SimpleTrigger trigger = new SimpleTrigger("trigger1", "group1", new Date(), null, 0, 0); // Set up the listener JobListener listener = new Job1Listener(); sched.addJobListener(listener); // make sure the listener is associated with the job job.addJobListener(listener.getName()); // schedule the job to run sched.scheduleJob(job, trigger); // All of the jobs have been added to the scheduler, but none of the jobs // will run until the scheduler has been started log.info("------- Starting Scheduler ----------------"); sched.start(); // wait 30 seconds: // note: nothing will run log.info("------- Waiting 30 seconds... --------------"); try { // wait 30 seconds to show jobs Thread.sleep(30L * 1000L); // executing... } catch (Exception e) { } // shut down the scheduler log.info("------- Shutting Down ---------------------"); sched.shutdown(true); log.info("------- Shutdown Complete -----------------"); SchedulerMetaData metaData = sched.getMetaData(); log.info("Executed " + metaData.getNumberOfJobsExecuted() + " jobs."); } public static void main(String[] args) throws Exception { ListenerExample example = new ListenerExample(); example.run(); } }
/* * 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.example9; import java.util.Date; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.quartz.JobDetail; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; import org.quartz.JobListener; import org.quartz.Scheduler; import org.quartz.SchedulerException; import org.quartz.SimpleTrigger; /** * @author wkratzer * * TODO To change the template for this generated type comment go to * Window - Preferences - Java - Code Style - Code Templates */ public class Job1Listener implements JobListener { private static Logger _log = LoggerFactory.getLogger(Job1Listener.class); public String getName() { return "job1_to_job2"; } public void jobToBeExecuted(JobExecutionContext inContext) { _log.info("Job1Listener says: Job Is about to be executed."); } public void jobExecutionVetoed(JobExecutionContext inContext) { _log.info("Job1Listener says: Job Execution was vetoed."); } public void jobWasExecuted(JobExecutionContext inContext, JobExecutionException inException) { _log.info("Job1Listener says: Job was executed."); // Simple job #2 JobDetail job2 = new JobDetail("job2", Scheduler.DEFAULT_GROUP, SimpleJob2.class); // Simple trigger to fire immediately SimpleTrigger trigger = new SimpleTrigger("job2Trigger", Scheduler.DEFAULT_GROUP, new Date(), null, 0, 0L); try { // schedule the job to run! inContext.getScheduler().scheduleJob(job2, trigger); } catch (SchedulerException e) { _log.warn("Unable to schedule job2!"); e.printStackTrace(); } } }
/* * 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.example9; 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 SimpleJob1 implements Job { private static Logger _log = LoggerFactory.getLogger(SimpleJob1.class); /** * Empty constructor for job initilization */ public SimpleJob1() { } /** * <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("SimpleJob1 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.example9; 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 SimpleJob2 implements Job { private static Logger _log = LoggerFactory.getLogger(SimpleJob2.class); /** * Empty constructor for job initilization */ public SimpleJob2() { } /** * <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("SimpleJob2 says: " + jobName + " executing at " + new Date()); } }
[INFO] 02 二月 03:56:06.271 下午 main [org.quartz.examples.example9.ListenerExample] ------- Initializing ---------------------- [INFO] 02 二月 03:56:06.293 下午 main [org.quartz.simpl.SimpleThreadPool] Job execution threads will use class loader of thread: main [INFO] 02 二月 03:56:06.307 下午 main [org.quartz.core.SchedulerSignalerImpl] Initialized Scheduler Signaller of type: class org.quartz.core.SchedulerSignalerImpl [INFO] 02 二月 03:56:06.308 下午 main [org.quartz.core.QuartzScheduler] Quartz Scheduler v.1.8.5 created. [INFO] 02 二月 03:56:06.309 下午 main [org.quartz.simpl.RAMJobStore] RAMJobStore initialized. [INFO] 02 二月 03:56:06.310 下午 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 二月 03:56:06.310 下午 main [org.quartz.impl.StdSchedulerFactory] Quartz scheduler 'DefaultQuartzScheduler' initialized from default resource file in Quartz package: 'quartz.properties' [INFO] 02 二月 03:56:06.310 下午 main [org.quartz.impl.StdSchedulerFactory] Quartz scheduler version: 1.8.5 [INFO] 02 二月 03:56:06.310 下午 main [org.quartz.examples.example9.ListenerExample] ------- Initialization Complete ----------- [INFO] 02 二月 03:56:06.311 下午 main [org.quartz.examples.example9.ListenerExample] ------- Scheduling Jobs ------------------- [INFO] 02 二月 03:56:06.314 下午 main [org.quartz.examples.example9.ListenerExample] ------- Starting Scheduler ---------------- [INFO] 02 二月 03:56:06.314 下午 main [org.quartz.core.QuartzScheduler] Scheduler DefaultQuartzScheduler_$_NON_CLUSTERED started. [INFO] 02 二月 03:56:06.314 下午 main [org.quartz.examples.example9.ListenerExample] ------- Waiting 30 seconds... -------------- [DEBUG] 02 二月 03:56:06.316 下午 DefaultQuartzScheduler_QuartzSchedulerThread [org.quartz.simpl.SimpleJobFactory] Producing instance of Job 'group1.job1', class=org.quartz.examples.example2.SimpleJob [INFO] 02 二月 03:56:06.319 下午 DefaultQuartzScheduler_Worker-1 [org.quartz.examples.example9.Job1Listener] Job1Listener says: Job Is about to be executed. [DEBUG] 02 二月 03:56:06.319 下午 DefaultQuartzScheduler_Worker-1 [org.quartz.core.JobRunShell] Calling execute on job group1.job1 [INFO] 02 二月 03:56:06.320 下午 DefaultQuartzScheduler_Worker-1 [org.quartz.examples.example2.SimpleJob] SimpleJob says: group1.job1 executing at Tue Feb 02 15:56:06 CST 2016 [INFO] 02 二月 03:56:06.320 下午 DefaultQuartzScheduler_Worker-1 [org.quartz.examples.example9.Job1Listener] Job1Listener says: Job was executed. [DEBUG] 02 二月 03:56:06.321 下午 DefaultQuartzScheduler_QuartzSchedulerThread [org.quartz.simpl.SimpleJobFactory] Producing instance of Job 'DEFAULT.job2', class=org.quartz.examples.example9.SimpleJob2 [DEBUG] 02 二月 03:56:06.321 下午 DefaultQuartzScheduler_Worker-2 [org.quartz.core.JobRunShell] Calling execute on job DEFAULT.job2 [INFO] 02 二月 03:56:06.321 下午 DefaultQuartzScheduler_Worker-2 [org.quartz.examples.example9.SimpleJob2] SimpleJob2 says: DEFAULT.job2 executing at Tue Feb 02 15:56:06 CST 2016 [DEBUG] 02 二月 03:56:07.308 下午 Timer-0 [org.quartz.utils.UpdateChecker] Checking for available updated version of Quartz... [INFO] 02 二月 03:56:36.314 下午 main [org.quartz.examples.example9.ListenerExample] ------- Shutting Down --------------------- [INFO] 02 二月 03:56:36.314 下午 main [org.quartz.core.QuartzScheduler] Scheduler DefaultQuartzScheduler_$_NON_CLUSTERED shutting down. [INFO] 02 二月 03:56:36.314 下午 main [org.quartz.core.QuartzScheduler] Scheduler DefaultQuartzScheduler_$_NON_CLUSTERED paused. [DEBUG] 02 二月 03:56:36.315 下午 main [org.quartz.simpl.SimpleThreadPool] shutdown complete [INFO] 02 二月 03:56:36.315 下午 main [org.quartz.core.QuartzScheduler] Scheduler DefaultQuartzScheduler_$_NON_CLUSTERED shutdown complete. [INFO] 02 二月 03:56:36.315 下午 main [org.quartz.examples.example9.ListenerExample] ------- Shutdown Complete ----------------- [INFO] 02 二月 03:56:36.315 下午 main [org.quartz.examples.example9.ListenerExample] Executed 2 jobs. [DEBUG] 02 二月 03:56:36.495 下午 DefaultQuartzScheduler_Worker-10 [org.quartz.simpl.SimpleThreadPool] WorkerThread is shut down. [DEBUG] 02 二月 03:56:36.495 下午 DefaultQuartzScheduler_Worker-3 [org.quartz.simpl.SimpleThreadPool] WorkerThread is shut down. [DEBUG] 02 二月 03:56:36.495 下午 DefaultQuartzScheduler_Worker-8 [org.quartz.simpl.SimpleThreadPool] WorkerThread is shut down. [DEBUG] 02 二月 03:56:36.495 下午 DefaultQuartzScheduler_Worker-4 [org.quartz.simpl.SimpleThreadPool] WorkerThread is shut down. [DEBUG] 02 二月 03:56:36.495 下午 DefaultQuartzScheduler_Worker-5 [org.quartz.simpl.SimpleThreadPool] WorkerThread is shut down. [DEBUG] 02 二月 03:56:36.495 下午 DefaultQuartzScheduler_Worker-7 [org.quartz.simpl.SimpleThreadPool] WorkerThread is shut down. [DEBUG] 02 二月 03:56:36.495 下午 DefaultQuartzScheduler_Worker-9 [org.quartz.simpl.SimpleThreadPool] WorkerThread is shut down. [DEBUG] 02 二月 03:56:36.495 下午 DefaultQuartzScheduler_Worker-6 [org.quartz.simpl.SimpleThreadPool] WorkerThread is shut down. [DEBUG] 02 二月 03:56:36.546 下午 DefaultQuartzScheduler_Worker-2 [org.quartz.simpl.SimpleThreadPool] WorkerThread is shut down. [DEBUG] 02 二月 03:56:36.546 下午 DefaultQuartzScheduler_Worker-1 [org.quartz.simpl.SimpleThreadPool] WorkerThread is shut down.