KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > quartz > examples > example11 > LoadExample


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.example11;
19
20 import java.util.Date JavaDoc;
21
22 import org.quartz.JobDetail;
23 import org.quartz.Scheduler;
24 import org.quartz.SchedulerFactory;
25 import org.quartz.SchedulerMetaData;
26 import org.quartz.SimpleTrigger;
27 import org.quartz.impl.StdSchedulerFactory;
28
29 import org.apache.commons.logging.LogFactory;
30 import org.apache.commons.logging.Log;
31
32 /**
33  * This example will spawn a large number of jobs to run
34  *
35  * @author James House, Bill Kratzer
36  */

37 public class LoadExample {
38
39     private int _numberOfJobs = 500;
40     
41     public LoadExample(int inNumberOfJobs) {
42         _numberOfJobs = inNumberOfJobs;
43     }
44     
45     public void run() throws Exception JavaDoc {
46         Log log = LogFactory.getLog(LoadExample.class);
47
48         // First we must get a reference to a scheduler
49
SchedulerFactory sf = new StdSchedulerFactory();
50         Scheduler sched = sf.getScheduler();
51
52         log.info("------- Initialization Complete -----------");
53
54         log.info("------- (Not Scheduling any Jobs - relying on XML definitions --");
55
56         String JavaDoc schedId = sched.getSchedulerInstanceId();
57
58         // schedule 500 jobs to run
59
for (int count=1; count <= _numberOfJobs; count++) {
60             JobDetail job = new JobDetail("job" + count, "group1",
61                     SimpleJob.class);
62             // tell the job to wait one minute (60 seconds)
63
job.getJobDataMap().put(SimpleJob.DELAY_TIME, 60000L);
64             // ask scheduler to re-execute this job if it was in progress when
65
// the scheduler went down...
66
job.setRequestsRecovery(true);
67             SimpleTrigger trigger = new SimpleTrigger("trigger_" + count, "group_1");
68             trigger.setStartTime(new Date JavaDoc(System.currentTimeMillis() + 10000L
69                     + (count * 100)));
70             sched.scheduleJob(job, trigger);
71             if (count % 25 == 0) {
72                 log.info("...scheduled " + count + " jobs");
73             }
74         }
75         
76         
77         log.info("------- Starting Scheduler ----------------");
78
79         // start the schedule
80
sched.start();
81
82         log.info("------- Started Scheduler -----------------");
83
84         log.info("------- Waiting five minutes... -----------");
85
86         // wait five minutes to give our jobs a chance to run
87
try {
88             Thread.sleep(300L * 1000L);
89         } catch (Exception JavaDoc e) {
90         }
91
92         // shut down the scheduler
93
log.info("------- Shutting Down ---------------------");
94         sched.shutdown(true);
95         log.info("------- Shutdown Complete -----------------");
96
97         SchedulerMetaData metaData = sched.getMetaData();
98         log.info("Executed " + metaData.numJobsExecuted() + " jobs.");
99     }
100
101     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
102
103         int numberOfJobs = 500;
104         if (args.length == 1) {
105             numberOfJobs = Integer.parseInt(args[0]);
106         }
107         if (args.length > 1) {
108             System.out.println(
109                     "Usage: java " +
110                     LoadExample.class.getName() +
111                     "[# of jobs]");
112             return;
113         }
114         LoadExample example = new LoadExample(numberOfJobs);
115         example.run();
116     }
117
118 }
Popular Tags