KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > quartz > examples > example5 > StatefulDumbJob


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

34 public class StatefulDumbJob implements StatefulJob {
35
36     /*
37      * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
38      *
39      * Constants.
40      *
41      * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
42      */

43
44     public static final String JavaDoc NUM_EXECUTIONS = "NumExecutions";
45
46     public static final String JavaDoc EXECUTION_DELAY = "ExecutionDelay";
47
48     /*
49      * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
50      *
51      * Constructors.
52      *
53      * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
54      */

55
56     public StatefulDumbJob() {
57     }
58
59     /*
60      * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
61      *
62      * Interface.
63      *
64      * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
65      */

66
67     /**
68      * <p>
69      * Called by the <code>{@link org.quartz.Scheduler}</code> when a <code>{@link org.quartz.Trigger}</code>
70      * fires that is associated with the <code>Job</code>.
71      * </p>
72      *
73      * @throws JobExecutionException
74      * if there is an exception while executing the job.
75      */

76     public void execute(JobExecutionContext context)
77         throws JobExecutionException {
78         System.err.println("---" + context.getJobDetail().getFullName()
79                 + " executing.[" + new Date JavaDoc() + "]");
80
81         JobDataMap map = context.getJobDetail().getJobDataMap();
82
83         int executeCount = 0;
84         if (map.containsKey(NUM_EXECUTIONS)) {
85             executeCount = map.getInt(NUM_EXECUTIONS);
86         }
87
88         executeCount++;
89
90         map.put(NUM_EXECUTIONS, executeCount);
91
92         long delay = 5000l;
93         if (map.containsKey(EXECUTION_DELAY)) {
94             delay = map.getLong(EXECUTION_DELAY);
95         }
96
97         try {
98             Thread.sleep(delay);
99         } catch (Exception JavaDoc ignore) {
100         }
101
102         System.err.println(" -" + context.getJobDetail().getFullName()
103                 + " complete (" + executeCount + ").");
104
105     }
106
107 }
Popular Tags