KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > quartz > examples > example4 > ColorJob


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.example4;
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.JobDataMap;
25 import org.quartz.JobExecutionContext;
26 import org.quartz.JobExecutionException;
27 import org.quartz.StatefulJob;
28
29 /**
30  * <p>
31  * This is just a simple job that receives parameters and
32  * maintains state
33  * </p>
34  *
35  * @author Bill Kratzer
36  */

37 public class ColorJob implements StatefulJob {
38
39     private static Log _log = LogFactory.getLog(ColorJob.class);
40     
41     // parameter names specific to this job
42
public static final String JavaDoc FAVORITE_COLOR = "favorite color";
43     public static final String JavaDoc EXECUTION_COUNT = "count";
44     
45     // Since Quartz will re-instantiate a class every time it
46
// gets executed, members non-static member variables can
47
// not be used to maintain state!
48
private int _counter = 1;
49
50     /**
51      * <p>
52      * Empty constructor for job initilization
53      * </p>
54      * <p>
55      * Quartz requires a public empty constructor so that the
56      * scheduler can instantiate the class whenever it needs.
57      * </p>
58      */

59     public ColorJob() {
60     }
61
62     /**
63      * <p>
64      * Called by the <code>{@link org.quartz.Scheduler}</code> when a
65      * <code>{@link org.quartz.Trigger}</code> fires that is associated with
66      * the <code>Job</code>.
67      * </p>
68      *
69      * @throws JobExecutionException
70      * if there is an exception while executing the job.
71      */

72     public void execute(JobExecutionContext context)
73         throws JobExecutionException {
74
75         // This job simply prints out its job name and the
76
// date and time that it is running
77
String JavaDoc jobName = context.getJobDetail().getFullName();
78         
79         // Grab and print passed parameters
80
JobDataMap data = context.getJobDetail().getJobDataMap();
81         String JavaDoc favoriteColor = data.getString(FAVORITE_COLOR);
82         int count = data.getInt(EXECUTION_COUNT);
83         _log.info("ColorJob: " + jobName + " executing at " + new Date JavaDoc() + "\n" +
84             " favorite color is " + favoriteColor + "\n" +
85             " execution count (from job map) is " + count + "\n" +
86             " execution count (from job member variable) is " + _counter);
87         
88         // increment the count and store it back into the
89
// job map so that job state can be properly maintained
90
count++;
91         data.put(EXECUTION_COUNT, count);
92         
93         // Increment the local member variable
94
// This serves no real purpose since job state can not
95
// be maintained via member variables!
96
_counter++;
97     }
98
99 }
Popular Tags