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 * @author Erik Brakkee
37 public class QuartzCrawlerScheduler implements CrawlerScheduler {
42 private static final String TRIGGER_NAME = "interval";
47 private static final String JOB_NAME = "kisscrawler";
49 private Scheduler _scheduler;
51 private int _intervalInSeconds;
54 * Constructs the quartz interface.
55 * @param aIntervalInSeconds Scheduling interval in seconds.
56 * @throws SchedulerException
58 public QuartzCrawlerScheduler(int aIntervalInSeconds) throws SchedulerException {
59 SchedulerFactory schedulerFactory = new StdSchedulerFactory();
60 _scheduler = schedulerFactory.getScheduler();
61 _intervalInSeconds = aIntervalInSeconds;
65 * Initializes the scheduler.
66 * @throws SchedulerException
68 public void initialize() throws SchedulerException {
71 JobDetail jobDetail = new JobDetail(JOB_NAME, null, CrawlerJob.class);
72 Trigger trigger = TriggerUtils.makeSecondlyTrigger(_intervalInSeconds);
73 //trigger.setStartTime(TriggerUtils.getEvenHourDate(new Date()));
74 trigger.setStartTime(new Date());
75 trigger.setName(TRIGGER_NAME);
77 _scheduler.scheduleJob(jobDetail, trigger);
81 * @see org.wamblee.crawler.kiss.scheduling.CrawlerScheduler#isCrawlerRunning()
83 public boolean isCrawlerRunning() throws Exception {
84 List jobs = _scheduler.getCurrentlyExecutingJobs();
85 return jobs.size() > 0;
89 * @see org.wamblee.crawler.kiss.scheduling.CrawlerScheduler#scheduleNow()
91 public void scheduleNow() throws Exception {
92 Trigger trigger = new SimpleTrigger("immediate", null);
93 trigger.setJobName(JOB_NAME);
94 _scheduler.scheduleJob(trigger);
98 * Shuts down the scheduler.
99 * @throws SchedulerException
101 public void shutdown() throws SchedulerException {
102 _scheduler.shutdown();