View Javadoc

1   /*
2    * #%L
3    * Bitrepository Integrity Client
4    * 
5    * $Id$
6    * $HeadURL$
7    * %%
8    * Copyright (C) 2010 - 2011 The State and University Library, The Royal Library and The State Archives, Denmark
9    * %%
10   * This program is free software: you can redistribute it and/or modify
11   * it under the terms of the GNU Lesser General Public License as 
12   * published by the Free Software Foundation, either version 2.1 of the 
13   * License, or (at your option) any later version.
14   * 
15   * This program is distributed in the hope that it will be useful,
16   * but WITHOUT ANY WARRANTY; without even the implied warranty of
17   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18   * GNU General Lesser Public License for more details.
19   * 
20   * You should have received a copy of the GNU General Lesser Public 
21   * License along with this program.  If not, see
22   * <http://www.gnu.org/licenses/lgpl-2.1.html>.
23   * #L%
24   */
25  package org.bitrepository.service.scheduler;
26  
27  import org.bitrepository.common.utils.TimeUtils;
28  import org.bitrepository.service.workflow.JobID;
29  import org.bitrepository.service.workflow.JobTimerTask;
30  import org.bitrepository.service.workflow.SchedulableJob;
31  import org.slf4j.Logger;
32  import org.slf4j.LoggerFactory;
33  
34  import java.util.*;
35  
36  /**
37   * Scheduler that uses Timer to run workflows.
38   */
39  public class TimerbasedScheduler implements JobScheduler {
40      private Logger log = LoggerFactory.getLogger(getClass());
41  
42      /** The timer that schedules events. */
43      private final Timer timer;
44      /** The map between the running timertasks and their names. */
45      private Map<JobID, JobTimerTask> intervalTasks = new HashMap<JobID, JobTimerTask>();
46      public static final long SCHEDULE_INTERVAL = 60000;
47  
48      /** The name of the timer.*/
49      private static final String TIMER_NAME = "Service Scheduler";
50      /** Whether the timer is a daemon.*/
51      private static final boolean TIMER_IS_DEAMON = false;
52      /** A timer delay of 0 seconds.*/
53      private static final Long NO_DELAY = 0L;
54      private List<JobEventListener> jobListeners = new LinkedList<JobEventListener>();
55  
56      /** Setup a timer task for running the workflows at requested interval.
57       */
58      public TimerbasedScheduler() {
59          timer = new Timer(TIMER_NAME, TIMER_IS_DEAMON);
60      }
61  
62      @Override
63      public void schedule(SchedulableJob workflow, Long interval) {
64          log.info("Scheduling job : " + workflow.getJobID() + " to run every " + TimeUtils.millisecondsToHuman(interval));
65  
66          JobTimerTask task = new JobTimerTask(interval, workflow, Collections.unmodifiableList(jobListeners));
67          timer.scheduleAtFixedRate(task, NO_DELAY, SCHEDULE_INTERVAL);
68          intervalTasks.put(workflow.getJobID(), task);
69      }
70  
71      @Override
72      public String startJob(SchedulableJob job) {
73          long timeBetweenRuns = -1;
74          JobTimerTask oldTask = cancelJob(job.getJobID());
75          if (oldTask != null) {
76              timeBetweenRuns = oldTask.getIntervalBetweenRuns();
77          }
78  
79          JobTimerTask task = new JobTimerTask(timeBetweenRuns, job, jobListeners);
80          timer.scheduleAtFixedRate(task, NO_DELAY, SCHEDULE_INTERVAL);
81          intervalTasks.put(job.getJobID(), task);
82          return null;
83      }
84  
85      @Override
86      public Date getNextRun(JobID jobId) {
87          if (intervalTasks.containsKey(jobId)) {
88              return intervalTasks.get(jobId).getNextRun();
89          } else return null;
90      }
91  
92      @Override
93      public long getRunInterval(JobID jobId) {
94          if (intervalTasks.containsKey(jobId)) {
95              return intervalTasks.get(jobId).getIntervalBetweenRuns();
96          } else return -1;
97      }
98  
99      @Override
100     public void addJobEventListener(JobEventListener listener) {
101         jobListeners.add(listener);
102     }
103 
104     @Override
105     public JobTimerTask cancelJob(JobID jobID) {
106         JobTimerTask task = intervalTasks.remove(jobID);
107         if(task == null) {
108             return null;
109         }
110         task.cancel();
111 
112         return task;
113     }
114 }