5fa7cf86c4714a4f1acc158b643a1b3d3f305859
[utils] /
1 /*
2  * Copyright 2006 the original author or authors.
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy 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,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */ 
16
17 package org.wamblee.crawler.kiss.scheduling.quartz;
18
19 import java.util.Date;
20 import java.util.List;
21
22 import org.quartz.JobDetail;
23 import org.quartz.Scheduler;
24 import org.quartz.SchedulerException;
25 import org.quartz.SchedulerFactory;
26 import org.quartz.SimpleTrigger;
27 import org.quartz.Trigger;
28 import org.quartz.TriggerUtils;
29 import org.quartz.impl.StdSchedulerFactory;
30 import org.wamblee.crawler.kiss.scheduling.CrawlerScheduler;
31
32 /**
33  * Interface to the Quartz scheduler.
34  */
35 public class QuartzCrawlerScheduler implements CrawlerScheduler {
36     
37     /**
38      * 
39      */
40     private static final String TRIGGER_NAME = "interval";
41
42     /**
43      * 
44      */
45     private static final String JOB_NAME = "kisscrawler";
46
47     private Scheduler _scheduler; 
48     
49     private int _intervalInSeconds;
50
51     /**
52      * Constructs the quartz interface. 
53      * @param aIntervalInSeconds Scheduling interval in seconds. 
54      * @throws SchedulerException
55      */
56     public QuartzCrawlerScheduler(int aIntervalInSeconds) throws SchedulerException { 
57         SchedulerFactory schedulerFactory = new StdSchedulerFactory();
58         _scheduler = schedulerFactory.getScheduler();
59         _intervalInSeconds = aIntervalInSeconds; 
60     }
61     
62     /**
63      * Initializes the scheduler.
64      * @throws SchedulerException
65      */
66     public void initialize() throws SchedulerException { 
67         _scheduler.start();
68
69         JobDetail jobDetail = new JobDetail(JOB_NAME, null, CrawlerJob.class);
70         Trigger trigger = TriggerUtils.makeSecondlyTrigger(_intervalInSeconds);
71         //trigger.setStartTime(TriggerUtils.getEvenHourDate(new Date()));
72         trigger.setStartTime(new Date());
73         trigger.setName(TRIGGER_NAME);
74
75         _scheduler.scheduleJob(jobDetail, trigger);
76     }
77     
78     /* (non-Javadoc)
79      * @see org.wamblee.crawler.kiss.scheduling.CrawlerScheduler#isCrawlerRunning()
80      */
81     public boolean isCrawlerRunning() throws Exception {
82         List jobs = _scheduler.getCurrentlyExecutingJobs(); 
83         return jobs.size() > 0; 
84     }
85     
86     /* (non-Javadoc)
87      * @see org.wamblee.crawler.kiss.scheduling.CrawlerScheduler#scheduleNow()
88      */
89     public void scheduleNow() throws Exception {
90         Trigger trigger = new SimpleTrigger("immediate", null);
91         trigger.setJobName(JOB_NAME);
92         _scheduler.scheduleJob(trigger);
93     }
94     
95     /**
96      * Shuts down the scheduler. 
97      * @throws SchedulerException
98      */
99     public void shutdown() throws SchedulerException { 
100         _scheduler.shutdown();
101     }
102 }