Package io.fathom.cloud.tasks

Source Code of io.fathom.cloud.tasks.ScheduledTask

package io.fathom.cloud.tasks;

import java.util.concurrent.TimeUnit;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fathomdb.TimeSpan;

public abstract class ScheduledTask {

    private static final Logger log = LoggerFactory.getLogger(ScheduledTask.class);

    public void schedule(TaskScheduler scheduler) {
        TimeSpan initialDelay = getInitialDelay();
        TimeSpan interval = getInterval();

        scheduler.scheduleWithFixedDelay(new Runnable() {
            @Override
            public void run() {
                try {
                    log.info("Running scheduled task: {}", getKey());
                    ScheduledTask.this.run();
                } catch (Exception e) {
                    log.error("Error running scheduled task: " + getKey(), e);
                }
            }
        }, initialDelay.getTotalMilliseconds(), interval.getTotalMilliseconds(), TimeUnit.MILLISECONDS);
    }

    protected String getKey() {
        return getClass().getSimpleName();
    }

    protected abstract TimeSpan getInterval();

    protected TimeSpan getInitialDelay() {
        return getInterval();
    }

    public abstract void run() throws Exception;
}
TOP

Related Classes of io.fathom.cloud.tasks.ScheduledTask

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.