Search in sources :

Example 1 with TimeSource

use of com.serotonin.timer.TimeSource in project ma-core-public by infiniteautomation.

the class FixedRateTest method main.

public static void main(String[] args) {
    RealTimeTimer timer = new RealTimeTimer();
    timer.setTimeSource(new TimeSource() {

        int count = 10;

        @Override
        public long currentTimeMillis() {
            long time;
            if (count-- > 0)
                time = System.currentTimeMillis();
            else
                time = System.currentTimeMillis() - 1000001000;
            System.out.println("Returning " + new Date(time));
            return time;
        }
    });
    timer.init();
    long period = 5000;
    long delay = period - (System.currentTimeMillis() % period);
    FixedRateTrigger trigger = new FixedRateTrigger(delay, period);
    TimerTask task = new TimerTask(trigger, "Test Timer Task") {

        @Override
        public void run(long runtime) {
            System.out.println("executed at " + new Date(runtime));
        }

        @Override
        public void rejected(RejectedTaskReason reason) {
            System.out.println("task rejected: " + reason.getDescription());
        }
    };
    timer.schedule(task);
    try {
        synchronized (timer) {
            timer.wait(30000);
        }
    } catch (InterruptedException e) {
    // no op
    }
    timer.cancel();
}
Also used : FixedRateTrigger(com.serotonin.timer.FixedRateTrigger) TimerTask(com.serotonin.timer.TimerTask) RealTimeTimer(com.serotonin.timer.RealTimeTimer) TimeSource(com.serotonin.timer.TimeSource) Date(java.util.Date) RejectedTaskReason(com.serotonin.timer.RejectedTaskReason)

Aggregations

FixedRateTrigger (com.serotonin.timer.FixedRateTrigger)1 RealTimeTimer (com.serotonin.timer.RealTimeTimer)1 RejectedTaskReason (com.serotonin.timer.RejectedTaskReason)1 TimeSource (com.serotonin.timer.TimeSource)1 TimerTask (com.serotonin.timer.TimerTask)1 Date (java.util.Date)1