io.baratine.timer.TimerService Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of api Show documentation
Show all versions of api Show documentation
A reactive Java web server.
/*
* Copyright (c) 1998-2015 Caucho Technology -- all rights reserved
*
* This file is part of Baratine(TM)
*
* Each copy or derived work must preserve the copyright notice and this
* notice unmodified.
*
* Baratine is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Baratine 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, or any warranty
* of NON-INFRINGEMENT. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License
* along with Baratine; if not, write to the
*
* Free Software Foundation, Inc.
* 59 Temple Place, Suite 330
* Boston, MA 02111-1307 USA
*
* @author Scott Ferguson
*/
package io.baratine.timer;
import io.baratine.service.Cancel;
import io.baratine.service.Direct;
import io.baratine.service.Result;
import io.baratine.service.Service;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
/**
* Timer service local to the JVM. The timer can be obtained by
* injection (CDI):
*
*
* @Inject @Lookup("timer:") TimerServicer _timer;
*
*
* or with the {@link io.baratine.service.ServiceManager}
:
*
*
* ServiceManager.current().lookup("timer:").as(TimerService.class);
*
*
* Service name: "timer:"
*
* @see io.baratine.service.ServiceManager
*/
public interface TimerService
{
/**
* Returns the current time. @{@link Direct}
indicates that this
* call bypasses the inbox for this service (i.e. callers call this method
* directly).
*
* @return the current time
*/
@Direct
long getCurrentTime();
/**
* Run the Runnable
at the given time.
*
*
* // run 5 seconds from now
* timeService.runAt(task, System.currentTimeMillis() + 5000);
*
*
* @param task the task to execute
* @param time millisecond time since epoch to run
* @param result holder for the cancel result
*/
void runAt(@Service Consumer super Cancel> task,
long time,
Result super Cancel> result);
/**
* Run the Runnable
once after the given delay.
*
*
* MyRunnable task = new MyRunnable();
*
* // run once 10 seconds from now
* timerService.runAfter(task, 10, TimeUnit.SECONDS);
*
*
* @param task the executable timer task
* @param delay time to delay in units
* @param unit timeunit to delay
* @param result holder for the timer
*/
void runAfter(@Service Consumer super Cancel> task,
long delay,
TimeUnit unit,
Result super Cancel> result);
/**
* Run the Runnable
periodically after the given delay.
*
*
* MyRunnable task = new MyRunnable();
*
* // run every 10 seconds
* timerService.runEvery(task, 10, TimeUnit.SECONDS);
*
*
* @param task
* @param delay
* @param unit
*/
void runEvery(@Service Consumer super Cancel> task,
long delay,
TimeUnit unit,
Result super Cancel> result);
/**
* Schedule a Runnable
where scheduling is controlled by a
* scheduler. {@link TimerScheduler#nextRunTime(long)} is run first
* to determine the initial execution of the task.
*
* Run every 2 seconds, starting 2 seconds from now:
*
* timerService.schedule(task, new TimerScheduler() {
* public long nextRunTime(long now) {
* return now + 2000;
* }
* };
*
*
* Run exactly 5 times, then unregister this task:
*
* timerService.schedule(task, new TimerScheduler() {
* int count = 0;
*
* public long nextRunTime(long now) {
* if (count++ >= 5) {
* return -1; // negative value to cancel
* }
* else {
* return now + 2000;
* }
* }
* };
*
*
* @param task
* @param scheduler
*/
void schedule(@Service Consumer super Cancel> task,
TimerScheduler scheduler,
Result super Cancel> result);
/**
* Schedule a Runnable
that is controlled by a cron scheduler.
*
* Run every 2 seconds:
*
* timerService.cron(task, "*/2 * * *");
*
*
* Run on the 5th second of the 6th minute of the 7th hour everyday:
*
* timerService.cron(task, "5 6 7 *");
*
*
* @param task
* @param cron basic cron syntax
*/
void cron(@Service Consumer super Cancel> task,
String cron,
Result super Cancel> result);
/**
* Unregisters the Runnable
from this timer.
*
* timerService.unregister(task);
*
*
* @param task
*/
// void cancel(CancelHandle timerHandle);
/**
* Returns the task info associated with this Runnable.
*/
//TaskInfo getTask(@Service Runnable task);
//void getTask(@Service Runnable task, Result result);
/**
* Returns the list of scheduled tasks.
*/
//List getTasks();
//void getTasks(Result> task);
}