KickJava   Java API By Example, From Geeks To Geeks.

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


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.JobDetail;
25 import org.quartz.Scheduler;
26 import org.quartz.SchedulerFactory;
27 import org.quartz.SchedulerMetaData;
28 import org.quartz.SimpleTrigger;
29 import org.quartz.TriggerUtils;
30 import org.quartz.impl.StdSchedulerFactory;
31
32 /**
33  * This Example will demonstrate how job parameters can be
34  * passed into jobs and how state can be maintained
35  *
36  * @author Bill Kratzer
37  */

38 public class JobStateExample {
39
40     public void run() throws Exception JavaDoc {
41         Log log = LogFactory.getLog(JobStateExample.class);
42
43         log.info("------- Initializing -------------------");
44
45         // First we must get a reference to a scheduler
46
SchedulerFactory sf = new StdSchedulerFactory();
47         Scheduler sched = sf.getScheduler();
48
49         log.info("------- Initialization Complete --------");
50
51         log.info("------- Scheduling Jobs ----------------");
52
53         // get a "nice round" time a few seconds in the future....
54
long ts = TriggerUtils.getNextGivenSecondDate(null, 10).getTime();
55
56         // job1 will only run 5 times, every 10 seconds
57
JobDetail job1 = new JobDetail("job1", "group1", ColorJob.class);
58         SimpleTrigger trigger1 = new SimpleTrigger("trigger1", "group1", "job1", "group1",
59                 new Date JavaDoc(ts), null, 4, 10000);
60         // pass initialization parameters into the job
61
job1.getJobDataMap().put(ColorJob.FAVORITE_COLOR, "Green");
62         job1.getJobDataMap().put(ColorJob.EXECUTION_COUNT, 1);
63         
64         // schedule the job to run
65
Date JavaDoc scheduleTime1 = sched.scheduleJob(job1, trigger1);
66         log.info(job1.getFullName() +
67                 " will run at: " + scheduleTime1 +
68                 " and repeat: " + trigger1.getRepeatCount() +
69                 " times, every " + trigger1.getRepeatInterval() / 1000 + " seconds");
70
71         // job2 will also run 5 times, every 10 seconds
72
JobDetail job2 = new JobDetail("job2", "group1", ColorJob.class);
73         SimpleTrigger trigger2 = new SimpleTrigger("trigger2", "group1", "job2", "group1",
74                 new Date JavaDoc(ts + 1000), null, 4, 10000);
75         // pass initialization parameters into the job
76
// this job has a different favorite color!
77
job2.getJobDataMap().put(ColorJob.FAVORITE_COLOR, "Red");
78         job2.getJobDataMap().put(ColorJob.EXECUTION_COUNT, 1);
79         
80         // schedule the job to run
81
Date JavaDoc scheduleTime2 = sched.scheduleJob(job2, trigger2);
82         log.info(job1.getFullName() +
83                 " will run at: " + scheduleTime1 +
84                 " and repeat: " + trigger1.getRepeatCount() +
85                 " times, every " + trigger1.getRepeatInterval() / 1000 + " seconds");
86
87
88
89         log.info("------- Starting Scheduler ----------------");
90
91         // All of the jobs have been added to the scheduler, but none of the jobs
92
// will run until the scheduler has been started
93
sched.start();
94
95         log.info("------- Started Scheduler -----------------");
96         
97         log.info("------- Waiting 60 seconds... -------------");
98         try {
99             // wait five minutes to show jobs
100
Thread.sleep(60L * 1000L);
101             // executing...
102
} catch (Exception JavaDoc e) {
103         }
104
105         log.info("------- Shutting Down ---------------------");
106
107         sched.shutdown(true);
108
109         log.info("------- Shutdown Complete -----------------");
110
111         SchedulerMetaData metaData = sched.getMetaData();
112         log.info("Executed " + metaData.numJobsExecuted() + " jobs.");
113
114     }
115
116     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
117
118         JobStateExample example = new JobStateExample();
119         example.run();
120     }
121
122 }
Popular Tags