From 4c728b0ba906799afd30f0a337b540cf167a0cfd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Magdalena=20Grodzi=C5=84ska?= Date: Mon, 23 Dec 2019 19:50:46 +0100 Subject: Make timer inherit from module, move to modules package --- .../cloudatlas/agent/modules/TimerScheduler.java | 33 ++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/TimerScheduler.java (limited to 'src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/TimerScheduler.java') diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/TimerScheduler.java b/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/TimerScheduler.java new file mode 100644 index 0000000..b7ce435 --- /dev/null +++ b/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/TimerScheduler.java @@ -0,0 +1,33 @@ +package pl.edu.mimuw.cloudatlas.agent.modules; + +import pl.edu.mimuw.cloudatlas.agent.message.AgentMessage; +import pl.edu.mimuw.cloudatlas.agent.message.TimerSchedulerMessage; + +import java.util.Timer; +import java.util.TimerTask; + +/** + * Initializes a timer within a constructor during its attachment to the executor + * Runs in a thread separate from executor - maybe refactor so that it's attached to executor's thread + * + * Handle used to attach tasks to schedule + * Tasks declared as inherited from TimerTask + * + * TODO: add request id and custom time + */ +public class TimerScheduler extends Module { + private Timer timer; + + public TimerScheduler(AgentMessage.AgentModule moduleType) { + super(moduleType); + this.timer = new Timer(); + System.out.println("TimerScheduler instance initialized"); + } + + @Override + public void handle(AgentMessage event) throws InterruptedException { + TimerSchedulerMessage timerEvent = (TimerSchedulerMessage) event; + this.timer.schedule(timerEvent.getTask(), timerEvent.getDelay()); + System.out.println("Task with delay " + timerEvent.getDelay() + " scheduled"); + } +} -- cgit v1.2.3