All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.marid.ide.timers.IdeTimers Maven / Gradle / Ivy

There is a newer version: 0.9.8.10
Show newest version
/*
 * Copyright (c) 2016 Dmitry Ovchinnikov
 * Marid, the free data acquisition and visualization software
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see .
 */

package org.marid.ide.timers;

import org.marid.concurrent.MaridTimerTask;
import org.marid.logging.LogSupport;

import javax.annotation.PreDestroy;
import javax.enterprise.context.ApplicationScoped;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import java.util.function.Consumer;

/**
 * @author Dmitry Ovchinnikov
 */
@ApplicationScoped
public class IdeTimers implements LogSupport {

    private final Timer timer = new Timer();

    public TimerTask schedule(long delayMillis, long periodMillis, Consumer task) {
        final TimerTask timerTask = new MaridTimerTask(task);
        timer.schedule(timerTask, delayMillis, periodMillis);
        return timerTask;
    }

    public TimerTask schedule(long periodMillis, Consumer task) {
        return schedule(0L, periodMillis, task);
    }

    public TimerTask delayed(long delayMillis, Consumer task) {
        final TimerTask timerTask = new MaridTimerTask(task);
        timer.schedule(timerTask, delayMillis);
        return timerTask;
    }

    public TimerTask schedule(Date date, Consumer task) {
        final TimerTask timerTask = new MaridTimerTask(task);
        timer.schedule(timerTask, date);
        return timerTask;
    }

    public TimerTask schedule(Date date, long periodMillis, Consumer task) {
        final TimerTask timerTask = new MaridTimerTask(task);
        timer.schedule(timerTask, date, periodMillis);
        return timerTask;
    }

    @PreDestroy
    private void destroy() {
        timer.cancel();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy