KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > quartz > examples > example9 > ListenerExample


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.example9;
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.JobListener;
26 import org.quartz.Scheduler;
27 import org.quartz.SchedulerFactory;
28 import org.quartz.SchedulerMetaData;
29 import org.quartz.SimpleTrigger;
30 import org.quartz.examples.example2.SimpleJob;
31 import org.quartz.impl.StdSchedulerFactory;
32
33 /**
34  * Demonstrates the behavior of <code>JobListener</code>s. In particular,
35  * this example will use a job listener to trigger another job after one
36  * job succesfully executes.
37  *
38  */

39 public class ListenerExample {
40
41     public void run() throws Exception JavaDoc {
42         Log log = LogFactory.getLog(ListenerExample.class);
43
44         log.info("------- Initializing ----------------------");
45
46         // First we must get a reference to a scheduler
47
SchedulerFactory sf = new StdSchedulerFactory();
48         Scheduler sched = sf.getScheduler();
49
50         log.info("------- Initialization Complete -----------");
51
52         log.info("------- Scheduling Jobs -------------------");
53
54         // schedule a job to run immediately
55
JobDetail job = new JobDetail("job1", "group1", SimpleJob.class);
56         SimpleTrigger trigger = new SimpleTrigger("trigger1", "group1",
57                 new Date JavaDoc(),
58                 null,
59                 0,
60                 0);
61         // Set up the listener
62
JobListener listener = new Job1Listener();
63         sched.addJobListener(listener);
64
65         // make sure the listener is associated with the job
66
job.addJobListener(listener.getName());
67         
68         // schedule the job to run
69
sched.scheduleJob(job, trigger);
70         
71         // All of the jobs have been added to the scheduler, but none of the jobs
72
// will run until the scheduler has been started
73
log.info("------- Starting Scheduler ----------------");
74         sched.start();
75
76         // wait 30 seconds:
77
// note: nothing will run
78
log.info("------- Waiting 30 seconds... --------------");
79         try {
80             // wait 30 seconds to show jobs
81
Thread.sleep(30L * 1000L);
82             // executing...
83
} catch (Exception JavaDoc e) {
84         }
85         
86         
87         // shut down the scheduler
88
log.info("------- Shutting Down ---------------------");
89         sched.shutdown(true);
90         log.info("------- Shutdown Complete -----------------");
91
92         SchedulerMetaData metaData = sched.getMetaData();
93         log.info("Executed " + metaData.numJobsExecuted() + " jobs.");
94
95     }
96
97     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
98
99         ListenerExample example = new ListenerExample();
100         example.run();
101     }
102
103 }
Popular Tags