KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > quartz > examples > example1 > SimpleExample


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.example1;
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.SimpleTrigger;
28 import org.quartz.TriggerUtils;
29 import org.quartz.impl.StdSchedulerFactory;
30
31 /**
32  * This Example will demonstrate how to start and shutdown the Quartz
33  * scheduler and how to schedule a job to run in Quartz.
34  *
35  * @author Bill Kratzer
36  */

37 public class SimpleExample {
38
39     
40     public void run() throws Exception JavaDoc {
41         Log log = LogFactory.getLog(SimpleExample.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         // computer a time that is on the next round minute
54
Date JavaDoc runTime = TriggerUtils.getEvenMinuteDate(new Date JavaDoc());
55
56         // define the job and tie it to our HelloJob class
57
JobDetail job = new JobDetail("job1", "group1", HelloJob.class);
58         
59         // Trigger the job to run on the next round minute
60
SimpleTrigger trigger =
61             new SimpleTrigger("trigger1", "group1", runTime);
62         
63         // Tell quartz to schedule the job using our trigger
64
sched.scheduleJob(job, trigger);
65         log.info(job.getFullName() + " will run at: " + runTime);
66
67         // Start up the scheduler (nothing can actually run until the
68
// scheduler has been started)
69
sched.start();
70         log.info("------- Started Scheduler -----------------");
71
72         // wait long enough so that the scheduler as an opportunity to
73
// run the job!
74
log.info("------- Waiting 90 seconds... -------------");
75         try {
76             // wait 90 seconds to show jobs
77
Thread.sleep(90L * 1000L);
78             // executing...
79
} catch (Exception JavaDoc e) {
80         }
81
82         // shut down the scheduler
83
log.info("------- Shutting Down ---------------------");
84         sched.shutdown(true);
85         log.info("------- Shutdown Complete -----------------");
86     }
87
88     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
89
90         SimpleExample example = new SimpleExample();
91         example.run();
92
93     }
94
95 }
Popular Tags