KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > quartz > examples > example13 > SimpleRecoveryJob


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.example13;
19
20 import java.util.Date JavaDoc;
21
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24 import org.quartz.Job;
25 import org.quartz.JobDataMap;
26 import org.quartz.JobExecutionContext;
27 import org.quartz.JobExecutionException;
28
29 /**
30  * <p>
31  * A dumb implementation of Job, for unittesting purposes.
32  * </p>
33  *
34  * @author James House
35  */

36 public class SimpleRecoveryJob implements Job {
37
38     private static Log _log = LogFactory.getLog(SimpleRecoveryJob.class);
39
40     private static final String JavaDoc COUNT = "count";
41     
42     /**
43      * Quartz requires a public empty constructor so that the
44      * scheduler can instantiate the class whenever it needs.
45      */

46     public SimpleRecoveryJob() {
47     }
48
49     /**
50      * <p>
51      * Called by the <code>{@link org.quartz.Scheduler}</code> when a
52      * <code>{@link org.quartz.Trigger}</code> fires that is associated with
53      * the <code>Job</code>.
54      * </p>
55      *
56      * @throws JobExecutionException
57      * if there is an exception while executing the job.
58      */

59     public void execute(JobExecutionContext context)
60         throws JobExecutionException {
61
62         String JavaDoc jobName = context.getJobDetail().getFullName();
63
64         // if the job is recovering print a message
65
if (context.isRecovering()) {
66             _log.info("SimpleRecoveryJob: " + jobName + " RECOVERING at " + new Date JavaDoc());
67         } else {
68             _log.info("SimpleRecoveryJob: " + jobName + " starting at " + new Date JavaDoc());
69         }
70
71         // delay for ten seconds
72
long delay = 10L * 1000L;
73         try {
74             Thread.sleep(delay);
75         } catch (Exception JavaDoc e) {
76         }
77
78         JobDataMap data = context.getJobDetail().getJobDataMap();
79         int count;
80         if (data.containsKey(COUNT)) {
81             count = data.getInt(COUNT);
82         } else {
83             count = 0;
84         }
85         count++;
86         data.put(COUNT, count);
87         
88         _log.info("SimpleRecoveryJob: " + jobName +
89                 " done at " + new Date JavaDoc() +
90                 "\n Execution #" + count);
91          
92     }
93
94     
95
96 }
Popular Tags