ScheduleTask.java 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. package cz.senslog.watchdog.util.schedule;
  2. import cz.senslog.watchdog.messagebroker.email.EmailMessageBroker;
  3. import org.apache.logging.log4j.LogManager;
  4. import org.apache.logging.log4j.Logger;
  5. import java.time.LocalDateTime;
  6. import java.time.temporal.ChronoUnit;
  7. import java.util.concurrent.CountDownLatch;
  8. import java.util.concurrent.ScheduledExecutorService;
  9. import java.util.concurrent.ScheduledFuture;
  10. import static cz.senslog.watchdog.util.TimeConverter.secToMillis;
  11. import static java.util.concurrent.TimeUnit.MILLISECONDS;
  12. public final class ScheduleTask {
  13. private static final Logger logger = LogManager.getLogger(EmailMessageBroker.class);
  14. private static final int DEFAULT_DELAY_MILLIS = 2_000; // 2s
  15. private TaskDescription description;
  16. private final Runnable task;
  17. private final long periodMillis;
  18. private final LocalDateTime startAt;
  19. private final long delayMillis;
  20. private ScheduleTask(String name, Runnable task, long periodSec, LocalDateTime startAt, long delaySec) {
  21. this.description = new TaskDescription(name, Status.STOPPED);
  22. this.task = task;
  23. this.periodMillis = secToMillis(periodSec);
  24. this.startAt = startAt;
  25. this.delayMillis = secToMillis(delaySec);
  26. }
  27. public ScheduleTask(String name, Runnable task, long periodSec, LocalDateTime startAt) {
  28. this(name, task, periodSec, startAt, -1);
  29. }
  30. public ScheduleTask(String name, Runnable task, long periodSec, int delaySec) {
  31. this(name, task, periodSec, null, delaySec);
  32. }
  33. public ScheduleTask(String name, Runnable task, long periodSec) {
  34. this(name, task, periodSec, null);
  35. }
  36. public TaskDescription getDescription() {
  37. return description;
  38. }
  39. public Runnable getTask() {
  40. return task;
  41. }
  42. public long getPeriodMillis() {
  43. return periodMillis;
  44. }
  45. public void schedule(ScheduledExecutorService scheduledService, CountDownLatch latch) {
  46. long delay = DEFAULT_DELAY_MILLIS;
  47. if (startAt != null) {
  48. delay = LocalDateTime.now().until(startAt, ChronoUnit.MILLIS);
  49. } else if (delayMillis > 0) {
  50. delay = delayMillis;
  51. }
  52. logger.info("The task '{}' is going to be executed in {} ms.", description.getName(), delay);
  53. ScheduledFuture<?> future = scheduledService.scheduleAtFixedRate(task, delay, periodMillis, MILLISECONDS);
  54. description = new TaskDescription(description.getName(), Status.RUNNING);
  55. new Thread(() -> {
  56. try {
  57. future.get();
  58. } catch (Exception e) {
  59. e.printStackTrace();
  60. } finally {
  61. future.cancel(true);
  62. latch.countDown();
  63. description = new TaskDescription(description.getName(), Status.STOPPED);
  64. }
  65. }, "thread-" + description.getName()).start();
  66. }
  67. }