KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > quartz > examples > example8 > CalendarExample


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.example8;
19
20 import java.util.Calendar JavaDoc;
21 import java.util.Date JavaDoc;
22 import java.util.GregorianCalendar JavaDoc;
23
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.examples.example2.SimpleJob;
30 import org.quartz.TriggerUtils;
31 import org.quartz.impl.StdSchedulerFactory;
32 import org.quartz.impl.calendar.AnnualCalendar;
33 import org.apache.commons.logging.LogFactory;
34 import org.apache.commons.logging.Log;
35
36 /**
37  * This example will demonstrate how calendars can be used
38  * to exclude periods of time when scheduling should not
39  * take place.
40  *
41  */

42 public class CalendarExample {
43
44     public void run() throws Exception JavaDoc {
45         final Log log = LogFactory.getLog(CalendarExample.class);
46
47         log.info("------- Initializing ----------------------");
48
49         // First we must get a reference to a scheduler
50
SchedulerFactory sf = new StdSchedulerFactory();
51         Scheduler sched = sf.getScheduler();
52
53         log.info("------- Initialization Complete -----------");
54
55         log.info("------- Scheduling Jobs -------------------");
56
57         // Add the holiday calendar to the schedule
58
AnnualCalendar holidays = new AnnualCalendar();
59
60         // fourth of July (July 4)
61
Calendar JavaDoc fourthOfJuly = new GregorianCalendar JavaDoc(2005, 6, 4);
62         holidays.setDayExcluded(fourthOfJuly, true);
63         // halloween (Oct 31)
64
Calendar JavaDoc halloween = new GregorianCalendar JavaDoc(2005, 9, 31);
65         holidays.setDayExcluded(halloween, true);
66         // christmas (Dec 25)
67
Calendar JavaDoc christmas = new GregorianCalendar JavaDoc(2005, 11, 25);
68         holidays.setDayExcluded(christmas, true);
69
70         // tell the schedule about our holiday calendar
71
sched.addCalendar("holidays", holidays, false, false);
72         
73
74         // schedule a job to run hourly, starting on halloween
75
// at 10 am
76
Date JavaDoc runDate = TriggerUtils.getDateOf(0,0, 10, 31, 10);
77         JobDetail job = new JobDetail("job1", "group1", SimpleJob.class);
78         SimpleTrigger trigger = new SimpleTrigger("trigger1", "group1",
79                 runDate,
80                 null,
81                 SimpleTrigger.REPEAT_INDEFINITELY,
82                 60L * 60L * 1000L);
83         // tell the trigger to obey the Holidays calendar!
84
trigger.setCalendarName("holidays");
85         
86         // schedule the job and print the first run date
87
Date JavaDoc firstRunTime = sched.scheduleJob(job, trigger);
88         
89         // print out the first execution date.
90
// Note: Since Halloween (Oct 31) is a holiday, then
91
// we will not run unti the next day! (Nov 1)
92
log.info(job.getFullName() +
93                 " will run at: " + firstRunTime +
94                 " and repeat: " + trigger.getRepeatCount() +
95                 " times, every " + trigger.getRepeatInterval() / 1000 + " seconds");
96         
97         // All of the jobs have been added to the scheduler, but none of the jobs
98
// will run until the scheduler has been started
99
log.info("------- Starting Scheduler ----------------");
100         sched.start();
101
102         // wait 30 seconds:
103
// note: nothing will run
104
log.info("------- Waiting 30 seconds... --------------");
105         try {
106             // wait 30 seconds to show jobs
107
Thread.sleep(30L * 1000L);
108             // executing...
109
} catch (Exception JavaDoc e) {
110         }
111         
112         
113         // shut down the scheduler
114
log.info("------- Shutting Down ---------------------");
115         sched.shutdown(true);
116         log.info("------- Shutdown Complete -----------------");
117
118         SchedulerMetaData metaData = sched.getMetaData();
119         log.info("Executed " + metaData.numJobsExecuted() + " jobs.");
120
121     }
122
123     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
124
125         CalendarExample example = new CalendarExample();
126         example.run();
127     }
128
129 }
Popular Tags