KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > quartz > examples > example14 > PriorityExample


1 /*
2  * Copyright 2006 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 package org.quartz.examples.example14;
18
19 import java.util.Calendar JavaDoc;
20
21 import org.apache.commons.logging.Log;
22 import org.apache.commons.logging.LogFactory;
23 import org.quartz.JobDetail;
24 import org.quartz.Scheduler;
25 import org.quartz.SchedulerFactory;
26 import org.quartz.SimpleTrigger;
27 import org.quartz.impl.StdSchedulerFactory;
28
29 /**
30  * This Example will demonstrate how Triggers are ordered by priority.
31  */

32 public class PriorityExample {
33     
34     public void run() throws Exception JavaDoc {
35         Log log = LogFactory.getLog(PriorityExample.class);
36
37         log.info("------- Initializing ----------------------");
38
39         // First we must get a reference to a scheduler
40
SchedulerFactory sf = new StdSchedulerFactory(
41                 "org/quartz/examples/example14/quartz_priority.properties");
42         Scheduler sched = sf.getScheduler();
43
44         log.info("------- Initialization Complete -----------");
45
46         log.info("------- Scheduling Jobs -------------------");
47
48         JobDetail job = new JobDetail("TriggerEchoJob", null, TriggerEchoJob.class);
49
50         // All three triggers will fire their first time at the same time,
51
// ordered by their priority, and then repeat once, firing in a
52
// staggered order that therefore ignores priority.
53
//
54
// We should see the following firing order:
55
// 1. Priority10Trigger15SecondRepeat
56
// 2. Priority5Trigger10SecondRepeat
57
// 3. PriorityNeg5Trigger5SecondRepeat
58
// 4. PriorityNeg5Trigger5SecondRepeat
59
// 5. Priority5Trigger10SecondRepeat
60
// 6. Priority10Trigger15SecondRepeat
61

62         // Calculate the start time of all triggers as 5 seconds from now
63
Calendar JavaDoc startTime = Calendar.getInstance();
64         startTime.add(Calendar.SECOND, 5);
65         
66         // First trigger has priority of -5, and will repeat after 5 seconds
67
SimpleTrigger trigger1 =
68             new SimpleTrigger("PriorityNeg5Trigger5SecondRepeat", null, startTime.getTime(), null, 1, 5L * 1000L);
69         trigger1.setPriority(-5);
70         trigger1.setJobName("TriggerEchoJob");
71
72         // Second trigger has default priority of 5, and will repeat after 10 seconds
73
SimpleTrigger trigger2 =
74             new SimpleTrigger("Priority5Trigger10SecondRepeat", null, startTime.getTime(), null, 1, 10L * 1000L);
75         trigger2.setJobName("TriggerEchoJob");
76         
77         // Third trigger has priority 10, and will repeat after 15 seconds
78
SimpleTrigger trigger3 =
79             new SimpleTrigger("Priority10Trigger15SecondRepeat", null, startTime.getTime(), null, 1, 15L * 1000L);
80         trigger3.setPriority(10);
81         trigger3.setJobName("TriggerEchoJob");
82         
83         // Tell quartz to schedule the job using our trigger
84
sched.scheduleJob(job, trigger1);
85         sched.scheduleJob(trigger2);
86         sched.scheduleJob(trigger3);
87
88         // Start up the scheduler (nothing can actually run until the
89
// scheduler has been started)
90
sched.start();
91         log.info("------- Started Scheduler -----------------");
92
93         // wait long enough so that the scheduler as an opportunity to
94
// fire the triggers
95
log.info("------- Waiting 30 seconds... -------------");
96         try {
97             Thread.sleep(30L * 1000L);
98             // executing...
99
} catch (Exception JavaDoc e) {
100         }
101
102         // shut down the scheduler
103
log.info("------- Shutting Down ---------------------");
104         sched.shutdown(true);
105         log.info("------- Shutdown Complete -----------------");
106     }
107
108     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
109         PriorityExample example = new PriorityExample();
110         example.run();
111     }
112 }
Popular Tags