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

io.baratine.timer.TimerServiceSync Maven / Gradle / Ivy

There is a newer version: 1.0.1
Show newest version
/*
 * 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 TimerServiceSync extends TimerService { /** * Run the Runnable at the given time. * *

   *     // run 5 seconds from now
   *     timeService.runAt(task, System.currentTimeMillis() + 5000);
   * 
* * @param task * @param time */ Cancel runAt(@Service Consumer task, long time); /** * 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 * @param delay * @param unit */ Cancel runAfter(@Service Consumer task, long delay, TimeUnit unit); /** * 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 */ Cancel runEvery(@Service Consumer task, long delay, TimeUnit unit); /** * 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 */ Cancel schedule(@Service Consumer task, TimerScheduler scheduler); /** * 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 */ Cancel cron(@Service Consumer task, String cron); }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy