KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > killingar > quartz > WebworkInitServlet


1 /* Copyright 2000-2005 Anders Hovmöller
2  *
3  * The person or persons who have associated their work with
4  * this document (the "Dedicator") hereby dedicate the entire
5  * copyright in the work of authorship identified below (the
6  * "Work") to the public domain.
7  *
8  * Dedicator makes this dedication for the benefit of the
9  * public at large and to the detriment of Dedicator's heirs
10  * and successors. Dedicator intends this dedication to be an
11  * overt act of relinquishment in perpetuity of all present
12  * and future rights under copyright law, whether vested or
13  * contingent, in the Work. Dedicator understands that such
14  * relinquishment of all rights includes the relinquishment of
15  * all rights to enforce (by lawsuit or otherwise) those
16  * copyrights in the Work.
17  *
18  * Dedicator recognizes that, once placed in the public
19  * domain, the Work may be freely reproduced, distributed,
20  * transmitted, used, modified, built upon, or otherwise
21  * exploited by anyone for any purpose, commercial or non-
22  * commercial, and in any way, including by methods that have
23  * not yet been invented or conceived.
24  */

25
26 package net.killingar.quartz;
27
28 import net.killingar.Utils;
29 import org.quartz.CronTrigger;
30 import org.quartz.JobDetail;
31 import org.quartz.Scheduler;
32 import org.quartz.SchedulerException;
33 import org.quartz.impl.StdSchedulerFactory;
34
35 import javax.servlet.ServletException JavaDoc;
36 import java.util.Iterator JavaDoc;
37 import java.util.Map JavaDoc;
38 import java.util.Properties JavaDoc;
39
40 /**
41  * Webwork-Quartz integration init class.
42  * Loads jobs from net.killingar.quartz.jobs.properties and schedules them.
43  */

44
45 public class WebworkInitServlet extends org.quartz.ee.servlet.QuartzInitializerServlet
46 {
47     public void init(javax.servlet.ServletConfig JavaDoc config) throws ServletException JavaDoc
48     {
49         super.init(config);
50
51         System.err.println("INFO: SKForum loading jobs...");
52
53         try
54         {
55             // load jobs from config file
56
Properties JavaDoc jobs = Utils.getProperties("quartz-webwork");
57
58             Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
59
60             for (Iterator JavaDoc i = jobs.entrySet().iterator(); i.hasNext(); )
61             {
62                 Map.Entry JavaDoc entry = (Map.Entry JavaDoc)i.next();
63
64                 try
65                 {
66                     String JavaDoc name = (String JavaDoc)entry.getKey();
67
68                     System.err.println("INFO: SKForum loading schedule job "+name+" ('"+entry.getValue()+"')");
69
70                     CronTrigger trigger = new CronTrigger(name+" trigger", name+" group", (String JavaDoc)entry.getValue());
71                     JobDetail jobDetail = new JobDetail(name, name+" group", WebworkJob.class);
72
73                     // load settings for the job
74
Map JavaDoc m = jobDetail.getJobDataMap();
75                     Properties JavaDoc properties = Utils.getProperties("quartz-webwork/"+name);
76                     for (Iterator JavaDoc j = properties.entrySet().iterator(); j.hasNext(); )
77                     {
78                         Map.Entry JavaDoc entry2 = (Map.Entry JavaDoc)j.next();
79
80                         m.put(entry2.getKey(), entry2.getValue());
81
82                         System.err.println("INFO: property: "+entry2.getKey()+"="+entry2.getValue());
83                     }
84
85                     scheduler.scheduleJob(jobDetail, trigger);
86             }
87                 catch (java.text.ParseException JavaDoc e)
88                 {
89                     e.printStackTrace();
90                 }
91             }
92             // debug print
93
/*String[] groups = scheduler.getJobGroupNames();
94             for (int i = 0; i != groups.length; i++)
95             {
96                 String[] jobnames = scheduler.getJobNames(groups[i]);
97                 for (int j = 0; j != jobnames.length; j++)
98                 {
99                     System.err.println("INFO: successfully loaded: "+groups[i] +"\\"+jobnames[j]);
100                 }
101             }*/

102         }
103         catch (SchedulerException e)
104         {
105             e.printStackTrace();
106             throw new ServletException JavaDoc(e);
107         }
108     }
109 }
110
Popular Tags