KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > quartz > examples > example6 > JobExceptionExample


1 /*
2  * Copyright 2005 OpenSymphony
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy
6  * of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations
14  * under the License.
15  *
16  */

17
18 package org.quartz.examples.example6;
19
20 import java.util.Date JavaDoc;
21
22 import org.quartz.JobDetail;
23 import org.quartz.Scheduler;
24 import org.quartz.SchedulerFactory;
25 import org.quartz.SchedulerMetaData;
26 import org.quartz.SimpleTrigger;
27 import org.quartz.TriggerUtils;
28 import org.quartz.impl.StdSchedulerFactory;
29
30 import org.apache.commons.logging.LogFactory;
31 import org.apache.commons.logging.Log;
32
33 /**
34  *
35  * This job demonstrates how Quartz can handle JobExecutionExceptions that are
36  * thrown by jobs.
37  *
38  * @author Bill Kratzer
39  */

40 public class JobExceptionExample {
41
42     public void run() throws Exception JavaDoc {
43         Log log = LogFactory.getLog(JobExceptionExample.class);
44
45         log.info("------- Initializing ----------------------");
46
47         // First we must get a reference to a scheduler
48
SchedulerFactory sf = new StdSchedulerFactory();
49         Scheduler sched = sf.getScheduler();
50
51         log.info("------- Initialization Complete ------------");
52
53         log.info("------- Scheduling Jobs -------------------");
54
55         // jobs can be scheduled before start() has been called
56

57         // get a "nice round" time a few seconds in the future...
58
long ts = TriggerUtils.getNextGivenSecondDate(null, 15).getTime();
59
60         // badJob1 will run every three seconds
61
// this job will throw an exception and refire
62
// immediately
63
JobDetail job = new JobDetail("badJob1", "group1", BadJob1.class);
64         SimpleTrigger trigger = new SimpleTrigger("trigger1", "group1",
65                 new Date JavaDoc(ts), null, SimpleTrigger.REPEAT_INDEFINITELY, 3000L);
66         Date JavaDoc ft = sched.scheduleJob(job, trigger);
67         log.info(job.getFullName() + " will run at: " + ft + " and repeat: "
68                 + trigger.getRepeatCount() + " times, every "
69                 + trigger.getRepeatInterval() / 1000 + " seconds");
70
71         // badJob2 will run every three seconds
72
// this job will throw an exception and never
73
// refire
74
job = new JobDetail("badJob2", "group1", BadJob2.class);
75         trigger = new SimpleTrigger("trigger2", "group1", new Date JavaDoc(ts), null,
76                 SimpleTrigger.REPEAT_INDEFINITELY, 3000L);
77         ft = sched.scheduleJob(job, trigger);
78         log.info(job.getFullName() + " will run at: " + ft + " and repeat: "
79                 + trigger.getRepeatCount() + " times, every "
80                 + trigger.getRepeatInterval() / 1000 + " seconds");
81
82         log.info("------- Starting Scheduler ----------------");
83
84         // jobs don't start firing until start() has been called...
85
sched.start();
86
87         log.info("------- Started Scheduler -----------------");
88
89         try {
90             // sleep for 60 seconds
91
Thread.sleep(60L * 1000L);
92         } catch (Exception JavaDoc e) {
93         }
94
95         log.info("------- Shutting Down ---------------------");
96
97         sched.shutdown(true);
98
99         log.info("------- Shutdown Complete -----------------");
100
101         SchedulerMetaData metaData = sched.getMetaData();
102         log.info("Executed " + metaData.numJobsExecuted() + " jobs.");
103     }
104
105     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
106
107         JobExceptionExample example = new JobExceptionExample();
108         example.run();
109     }
110
111 }
Popular Tags