View Javadoc

1   package com.atlassian.sal.core.scheduling;
2   
3   import java.util.Collections;
4   import java.util.Date;
5   import java.util.HashMap;
6   import java.util.Map;
7   import java.util.Timer;
8   import java.util.TimerTask;
9   
10  import org.apache.log4j.Logger;
11  
12  import com.atlassian.sal.api.scheduling.PluginJob;
13  import com.atlassian.sal.api.scheduling.PluginScheduler;
14  
15  /**
16   * Plugin scheduler that uses java.util.Timer
17   */
18  public class TimerPluginScheduler implements PluginScheduler
19  {
20  
21      private final Map<String, Timer> tasks;
22  
23      public TimerPluginScheduler()
24      {
25          tasks = Collections.synchronizedMap(new HashMap<String, Timer>());
26      }
27  
28      public synchronized void scheduleJob(final String name, final Class<? extends PluginJob> job, final Map<String, Object> jobDataMap, final Date startTime, final long repeatInterval)
29      {
30          // Use one timer per task, this will allow us to remove them if that functionality is wanted in future
31          Timer timer = tasks.get(name);
32          PluginTimerTask task;
33          if (timer != null)
34          {
35              timer.cancel();
36          }
37          timer = new Timer("PluginSchedulerTask-" + name);
38          tasks.put(name, timer);
39          task = new PluginTimerTask();
40          task.setJobClass(job);
41          task.setJobDataMap(jobDataMap);
42          timer.scheduleAtFixedRate(task, startTime, repeatInterval);
43      }
44  
45      /**
46       * TimerTask that executes a PluginJob
47       */
48      private static class PluginTimerTask extends TimerTask
49      {
50          private Class<? extends PluginJob> jobClass;
51          private Map<String, Object> jobDataMap;
52          private static final Logger log = Logger.getLogger(PluginTimerTask.class);
53  
54          @Override
55  		public void run()
56          {
57              PluginJob job;
58              try
59              {
60                  job = jobClass.newInstance();
61              }
62              catch (final InstantiationException ie)
63              {
64                  log.error("Error instantiating job", ie);
65                  return;
66              }
67              catch (final IllegalAccessException iae)
68              {
69                  log.error("Cannot access job class", iae);
70                  return;
71              }
72              job.execute(jobDataMap);
73          }
74  
75          public Class<? extends PluginJob> getJobClass()
76          {
77              return jobClass;
78          }
79  
80          public void setJobClass(final Class<? extends PluginJob> jobClass)
81          {
82              this.jobClass = jobClass;
83          }
84  
85          public Map<String, Object> getJobDataMap()
86          {
87              return jobDataMap;
88          }
89  
90          public void setJobDataMap(final Map<String, Object> jobDataMap)
91          {
92              this.jobDataMap = jobDataMap;
93          }
94      }
95  
96  	public void unscheduleJob(final String name)
97  	{
98          final Timer timer = tasks.remove(name);
99          if (timer != null)
100         {
101             timer.cancel();
102         }
103         else
104         {
105             throw new IllegalArgumentException("Attempted to unschedule unknown job: " + name);
106         }
107     }
108 }