Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2014
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.19 KB | None | 0 0
  1. package system;
  2.  
  3. import java.util.Date;
  4. import java.util.HashSet;
  5. import java.util.List;
  6. import java.util.Set;
  7. import java.util.Timer;
  8. import java.util.TimerTask;
  9. import java.util.concurrent.Callable;
  10. import java.util.concurrent.ExecutionException;
  11. import java.util.concurrent.ExecutorService;
  12. import java.util.concurrent.Executors;
  13. import java.util.concurrent.Future;
  14. import java.util.concurrent.TimeUnit;
  15. import java.util.concurrent.TimeoutException;
  16. import org.apache.commons.logging.Log;
  17. import org.apache.commons.logging.LogFactory;
  18.  
  19. /**
  20. * a generic scheduler that may be extended to provide fixed-delay execution to tasks
  21. * @author Willie Scholtz
  22. * @param <T> the type of bean to be scheduled
  23. */
  24. public abstract class Scheduler<T extends Callable<T>> {
  25.  
  26. private static final Log LOG = LogFactory.getLog(Scheduler.class);
  27. private static final int MAX_THREADS = Runtime.getRuntime().availableProcessors();
  28.  
  29. private Timer timer = null;
  30. private final int delay;
  31.  
  32. /**
  33. * creates a new scheduler
  34. * @param delay the delay in seconds between executions
  35. */
  36. public Scheduler(final int delay) {
  37. this.delay = delay;
  38. }
  39.  
  40. /**
  41. * creates a new timer for executing tasks
  42. * @param seconds number of seconds between each execution
  43. * @return a Timer
  44. */
  45. private Timer getSchedulerTimer(int seconds) {
  46. final String cName = this.getClass().getSimpleName();
  47.  
  48. final Timer sTimer = new Timer(cName + " Scheduler", false);
  49. final TimerTask sTask = new TimerTask() {
  50. @Override
  51. public void run() {
  52. LOG.debug("before running " + cName + " timer");
  53. Scheduler.this.runScheduler();
  54. LOG.debug("after running " + cName + " timer");
  55. }
  56. };
  57.  
  58. sTimer.schedule(sTask, 0, (1000 * seconds));
  59. return sTimer;
  60. }
  61.  
  62. /**
  63. * starts running this Scheduler, if the scheduler is currently executing,
  64. * tasks will be canceled and a new timer will be scheduled.
  65. */
  66. public void start() {
  67. this.stop();
  68.  
  69. LOG.info("starting scheduler[" + getClass().getSimpleName() + "]...");
  70. this.timer = getSchedulerTimer(this.delay);
  71. }
  72.  
  73. /**
  74. * stops the execution of this Scheduler.
  75. */
  76. public void stop() {
  77. LOG.info("stopping scheduler[" + this.getClass().getSimpleName() + "]...");
  78. if (this.timer != null) {
  79. this.timer.cancel();
  80. }
  81. }
  82.  
  83. /**
  84. * retrieves a list of tasks to execute
  85. * @param currentDate the current date of the scheduler
  86. * @return a non-null List of tasks
  87. */
  88. public abstract List<T> getTasksForExecution(final Date currentDate);
  89.  
  90. /**
  91. * runs the scheduler according to the specified delay
  92. */
  93. private void runScheduler() {
  94. final Set<Future<T>> futures = new HashSet<Future<T>>();
  95. final ExecutorService pool = Executors.newFixedThreadPool(MAX_THREADS);
  96. final Date now = new Date();
  97.  
  98. try {
  99. final List<T> tasks = this.getTasksForExecution(now);
  100. if (!tasks.isEmpty()) {
  101. LOG.info("executing " + tasks.size() + " task" + (tasks.size() != 1 ? "s" : ""));
  102.  
  103. // submit messages for execution
  104. for (final T task : tasks) {
  105. futures.add(pool.submit(task));
  106. }
  107.  
  108. // wait for completion
  109. for (final Future<T> future : futures) {
  110. try {
  111. // max wait time for 1 minute
  112. final T sendTaskOp = future.get(1L, TimeUnit.MINUTES);
  113. LOG.info("task[" + sendTaskOp + "] executed...");
  114. } catch (InterruptedException ex) {
  115. LOG.error("interupted while executing task - " + ex.getMessage(), ex);
  116. } catch (ExecutionException ex) {
  117. LOG.error("error while executiong task - " + ex.getMessage(), ex);
  118. } catch (TimeoutException ex) {
  119. LOG.error("executing the task timed out! - " + ex.getMessage(), ex);
  120. }
  121. }
  122. }
  123. } finally {
  124. pool.shutdown();
  125. }
  126. }
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement