m-chrzan.xyz
aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/pl/edu/mimuw/cloudatlas/agent/timer/TimerScheduler.java
blob: 6408a5282e4592f1653a6a134ff864025ff53c7f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package pl.edu.mimuw.cloudatlas.agent.timer;

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 {
    private Timer timer;

    TimerScheduler() {
        this.timer = new Timer();
        System.out.println("TimerScheduler instance initialized");
    }

    public void handle(TimerTask task, long delay, long period) {
        this.timer.scheduleAtFixedRate(task, delay, period);
        System.out.println("Task with delay " + delay + " and period " + period + " scheduled");
    }

    public void handle(TimerTask task, long delay) {
        this.timer.schedule(task, delay);
        System.out.println("Task with delay " + delay + " scheduled");
    }
}