2 * Copyright 2006 the original author or authors.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.wamblee.crawler.kiss.scheduling.quartz;
19 import java.util.Date;
20 import java.util.List;
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;
33 * Interface to the Quartz scheduler.
35 public class QuartzCrawlerScheduler implements CrawlerScheduler {
40 private static final String TRIGGER_NAME = "interval";
45 private static final String JOB_NAME = "kisscrawler";
47 private Scheduler _scheduler;
49 private int _intervalInSeconds;
52 * Constructs the quartz interface.
53 * @param aIntervalInSeconds Scheduling interval in seconds.
54 * @throws SchedulerException
56 public QuartzCrawlerScheduler(int aIntervalInSeconds) throws SchedulerException {
57 SchedulerFactory schedulerFactory = new StdSchedulerFactory();
58 _scheduler = schedulerFactory.getScheduler();
59 _intervalInSeconds = aIntervalInSeconds;
63 * Initializes the scheduler.
64 * @throws SchedulerException
66 public void initialize() throws SchedulerException {
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);
75 _scheduler.scheduleJob(jobDetail, trigger);
79 * @see org.wamblee.crawler.kiss.scheduling.CrawlerScheduler#isCrawlerRunning()
81 public boolean isCrawlerRunning() throws Exception {
82 List jobs = _scheduler.getCurrentlyExecutingJobs();
83 return jobs.size() > 0;
87 * @see org.wamblee.crawler.kiss.scheduling.CrawlerScheduler#scheduleNow()
89 public void scheduleNow() throws Exception {
90 Trigger trigger = new SimpleTrigger("immediate", null);
91 trigger.setJobName(JOB_NAME);
92 _scheduler.scheduleJob(trigger);
96 * Shuts down the scheduler.
97 * @throws SchedulerException
99 public void shutdown() throws SchedulerException {
100 _scheduler.shutdown();