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.example6;

import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.quartz.StatefulJob;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

/**
 * <p>
 * A job dumb job that will throw a job execution exception
 * </p>
 * 
 * @author Bill Kratzer
 */
public class BadJob1 implements StatefulJob {

	// Logging
	private static Logger _log = LoggerFactory.getLogger(BadJob1.class);

	/**
	 * Empty public constructor for job initilization
	 */
	public BadJob1() {
	}

	/**
	 * <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 {
		String jobName = context.getJobDetail().getFullName();
		_log.info("---" + jobName + " executing at " + new Date());

		// a contrived example of an exception that
		// will be generated by this job due to a
		// divide by zero error
		try {
			int zero = 0;
			int calculation = 4815 / zero;
		} catch (Exception e) {
			_log.info("--- Error in job!");
			JobExecutionException e2 = new JobExecutionException(e);
			// this job will refire immediately
			e2.setRefireImmediately(true);
			throw e2;
		}

		_log.info("---" + jobName + " completed 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.example6;

import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.quartz.StatefulJob;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

/**
 * <p>
 * A job dumb job that will throw a job execution exception
 * </p>
 * 
 * @author Bill Kratzer
 */
public class BadJob2 implements StatefulJob {

	// Logging
	private static Logger _log = LoggerFactory.getLogger(BadJob2.class);

	/**
	 * Empty public constructor for job initilization
	 */
	public BadJob2() {
	}

	/**
	 * <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 {
		String jobName = context.getJobDetail().getFullName();
		_log.info("---" + jobName + " executing at " + new Date());

		// a contrived example of an exception that
		// will be generated by this job due to a
		// divide by zero error
		try {
			int zero = 0;
			int calculation = 4815 / zero;
		} catch (Exception e) {
			_log.info("--- Error in job!");
			JobExecutionException e2 = new JobExecutionException(e);
			// Quartz will automatically unschedule
			// all triggers associated with this job
			// so that it does not run again
			e2.setUnscheduleAllTriggers(true);
			throw e2;
		}

		_log.info("---" + jobName + " completed 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.example6;

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 job demonstrates how Quartz can handle JobExecutionExceptions that are
 * thrown by jobs.
 * 
 * @author Bill Kratzer
 */
public class JobExceptionExample {

	public void run() throws Exception {
		Logger log = LoggerFactory.getLogger(JobExceptionExample.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 start() has been called

		// get a "nice round" time a few seconds in the future...
		long ts = TriggerUtils.getNextGivenSecondDate(null, 15).getTime();

		// badJob1 will run every three seconds
		// this job will throw an exception and refire
		// immediately
		JobDetail job = new JobDetail("badJob1", "group1", BadJob1.class);
		SimpleTrigger trigger = new SimpleTrigger("trigger1", "group1",
				new Date(ts), null, SimpleTrigger.REPEAT_INDEFINITELY, 3000L);
		Date ft = sched.scheduleJob(job, trigger);
		log.info(job.getFullName() + " will run at: " + ft + " and repeat: "
				+ trigger.getRepeatCount() + " times, every "
				+ trigger.getRepeatInterval() / 1000 + " seconds");

		// badJob2 will run every three seconds
		// this job will throw an exception and never
		// refire
		job = new JobDetail("badJob2", "group1", BadJob2.class);
		trigger = new SimpleTrigger("trigger2", "group1", new Date(ts), null,
				SimpleTrigger.REPEAT_INDEFINITELY, 3000L);
		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 ----------------");

		// jobs don't start firing until start() has been called...
		sched.start();

		log.info("------- Started Scheduler -----------------");

		try {
			// sleep for 60 seconds
			Thread.sleep(60L * 1000L);
		} catch (Exception e) {
		}

		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 {

		JobExceptionExample example = new JobExceptionExample();
		example.run();
	}

}

  

 

[INFO] 02 二月 03:27:57.488 下午 main [org.quartz.examples.example6.JobExceptionExample]
------- Initializing ----------------------

[INFO] 02 二月 03:27:57.511 下午 main [org.quartz.simpl.SimpleThreadPool]
Job execution threads will use class loader of thread: main

[INFO] 02 二月 03:27:57.523 下午 main [org.quartz.core.SchedulerSignalerImpl]
Initialized Scheduler Signaller of type: class org.quartz.core.SchedulerSignalerImpl

[INFO] 02 二月 03:27:57.525 下午 main [org.quartz.core.QuartzScheduler]
Quartz Scheduler v.1.8.5 created.

[INFO] 02 二月 03:27:57.526 下午 main [org.quartz.simpl.RAMJobStore]
RAMJobStore initialized.

[INFO] 02 二月 03:27:57.526 下午 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:27:57.527 下午 main [org.quartz.impl.StdSchedulerFactory]
Quartz scheduler 'DefaultQuartzScheduler' initialized from default resource file in Quartz package: 'quartz.properties'

[INFO] 02 二月 03:27:57.527 下午 main [org.quartz.impl.StdSchedulerFactory]
Quartz scheduler version: 1.8.5

[INFO] 02 二月 03:27:57.527 下午 main [org.quartz.examples.example6.JobExceptionExample]
------- Initialization Complete ------------

[INFO] 02 二月 03:27:57.527 下午 main [org.quartz.examples.example6.JobExceptionExample]
------- Scheduling Jobs -------------------

[INFO] 02 二月 03:27:57.532 下午 main [org.quartz.examples.example6.JobExceptionExample]
group1.badJob1 will run at: Tue Feb 02 15:28:00 CST 2016 and repeat: -1 times, every 3 seconds

[INFO] 02 二月 03:27:57.532 下午 main [org.quartz.examples.example6.JobExceptionExample]
group1.badJob2 will run at: Tue Feb 02 15:28:00 CST 2016 and repeat: -1 times, every 3 seconds

[INFO] 02 二月 03:27:57.532 下午 main [org.quartz.examples.example6.JobExceptionExample]
------- Starting Scheduler ----------------

[INFO] 02 二月 03:27:57.533 下午 main [org.quartz.core.QuartzScheduler]
Scheduler DefaultQuartzScheduler_$_NON_CLUSTERED started.

[INFO] 02 二月 03:27:57.533 下午 main [org.quartz.examples.example6.JobExceptionExample]
------- Started Scheduler -----------------

[DEBUG] 02 二月 03:27:58.531 下午 Timer-0 [org.quartz.utils.UpdateChecker]
Checking for available updated version of Quartz...

[DEBUG] 02 二月 03:28:00.008 下午 DefaultQuartzScheduler_QuartzSchedulerThread [org.quartz.simpl.SimpleJobFactory]
Producing instance of Job 'group1.badJob1', class=org.quartz.examples.example6.BadJob2

[DEBUG] 02 二月 03:28:00.030 下午 DefaultQuartzScheduler_QuartzSchedulerThread [org.quartz.simpl.SimpleJobFactory]
Producing instance of Job 'group1.badJob2', class=org.quartz.examples.example6.BadJob2

[DEBUG] 02 二月 03:28:00.030 下午 DefaultQuartzScheduler_Worker-1 [org.quartz.core.JobRunShell]
Calling execute on job group1.badJob1

[DEBUG] 02 二月 03:28:00.030 下午 DefaultQuartzScheduler_Worker-2 [org.quartz.core.JobRunShell]
Calling execute on job group1.badJob2

[INFO] 02 二月 03:28:00.031 下午 DefaultQuartzScheduler_Worker-1 [org.quartz.examples.example6.BadJob2]
---group1.badJob1 executing at Tue Feb 02 15:28:00 CST 2016

[INFO] 02 二月 03:28:00.031 下午 DefaultQuartzScheduler_Worker-2 [org.quartz.examples.example6.BadJob2]
---group1.badJob2 executing at Tue Feb 02 15:28:00 CST 2016

[INFO] 02 二月 03:28:00.031 下午 DefaultQuartzScheduler_Worker-1 [org.quartz.examples.example6.BadJob2]
--- Error in job!

[INFO] 02 二月 03:28:00.031 下午 DefaultQuartzScheduler_Worker-2 [org.quartz.examples.example6.BadJob2]
--- Error in job!

[INFO] 02 二月 03:28:00.033 下午 DefaultQuartzScheduler_Worker-1 [org.quartz.core.JobRunShell]
Job group1.badJob1 threw a JobExecutionException: 

org.quartz.JobExecutionException: java.lang.ArithmeticException: / by zero [See nested exception: java.lang.ArithmeticException: / by zero]
	at org.quartz.examples.example6.BadJob2.execute(BadJob2.java:69)
	at org.quartz.core.JobRunShell.run(JobRunShell.java:216)
	at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:549)
Caused by: java.lang.ArithmeticException: / by zero
	at org.quartz.examples.example6.BadJob2.execute(BadJob2.java:65)
	... 2 more
[INFO] 02 二月 03:28:00.033 下午 DefaultQuartzScheduler_Worker-2 [org.quartz.core.JobRunShell]
Job group1.badJob2 threw a JobExecutionException: 

org.quartz.JobExecutionException: java.lang.ArithmeticException: / by zero [See nested exception: java.lang.ArithmeticException: / by zero]
	at org.quartz.examples.example6.BadJob2.execute(BadJob2.java:69)
	at org.quartz.core.JobRunShell.run(JobRunShell.java:216)
	at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:549)
Caused by: java.lang.ArithmeticException: / by zero
	at org.quartz.examples.example6.BadJob2.execute(BadJob2.java:65)
	... 2 more
[INFO] 02 二月 03:28:57.545 下午 main [org.quartz.examples.example6.JobExceptionExample]
------- Shutting Down ---------------------

[INFO] 02 二月 03:28:57.546 下午 main [org.quartz.core.QuartzScheduler]
Scheduler DefaultQuartzScheduler_$_NON_CLUSTERED shutting down.

[INFO] 02 二月 03:28:57.546 下午 main [org.quartz.core.QuartzScheduler]
Scheduler DefaultQuartzScheduler_$_NON_CLUSTERED paused.

[DEBUG] 02 二月 03:28:57.547 下午 main [org.quartz.simpl.SimpleThreadPool]
shutdown complete

[INFO] 02 二月 03:28:57.547 下午 main [org.quartz.core.QuartzScheduler]
Scheduler DefaultQuartzScheduler_$_NON_CLUSTERED shutdown complete.

[INFO] 02 二月 03:28:57.562 下午 main [org.quartz.examples.example6.JobExceptionExample]
------- Shutdown Complete -----------------

[INFO] 02 二月 03:28:57.562 下午 main [org.quartz.examples.example6.JobExceptionExample]
Executed 2 jobs.

[DEBUG] 02 二月 03:28:57.941 下午 DefaultQuartzScheduler_Worker-3 [org.quartz.simpl.SimpleThreadPool]
WorkerThread is shut down.

[DEBUG] 02 二月 03:28:57.941 下午 DefaultQuartzScheduler_Worker-2 [org.quartz.simpl.SimpleThreadPool]
WorkerThread is shut down.

[DEBUG] 02 二月 03:28:57.941 下午 DefaultQuartzScheduler_Worker-5 [org.quartz.simpl.SimpleThreadPool]
WorkerThread is shut down.

[DEBUG] 02 二月 03:28:57.941 下午 DefaultQuartzScheduler_Worker-10 [org.quartz.simpl.SimpleThreadPool]
WorkerThread is shut down.

[DEBUG] 02 二月 03:28:57.941 下午 DefaultQuartzScheduler_Worker-6 [org.quartz.simpl.SimpleThreadPool]
WorkerThread is shut down.

[DEBUG] 02 二月 03:28:57.941 下午 DefaultQuartzScheduler_Worker-8 [org.quartz.simpl.SimpleThreadPool]
WorkerThread is shut down.

[DEBUG] 02 二月 03:28:57.941 下午 DefaultQuartzScheduler_Worker-9 [org.quartz.simpl.SimpleThreadPool]
WorkerThread is shut down.

[DEBUG] 02 二月 03:28:57.941 下午 DefaultQuartzScheduler_Worker-1 [org.quartz.simpl.SimpleThreadPool]
WorkerThread is shut down.

[DEBUG] 02 二月 03:28:57.941 下午 DefaultQuartzScheduler_Worker-7 [org.quartz.simpl.SimpleThreadPool]
WorkerThread is shut down.

[DEBUG] 02 二月 03:28:57.941 下午 DefaultQuartzScheduler_Worker-4 [org.quartz.simpl.SimpleThreadPool]
WorkerThread is shut down.

 

两个都改成BadJob2的结果,如果按照原来的额程序,BadJob1将会不停的执行,并且不停的报错!!!!这是不行的

posted @ 2016-02-02 15:30  无心流泪  阅读(1000)  评论(0编辑  收藏  举报