001package io.ebean;
002
003import java.util.concurrent.Callable;
004import java.util.concurrent.ScheduledExecutorService;
005import java.util.concurrent.ScheduledFuture;
006import java.util.concurrent.TimeUnit;
007
008/**
009 * Background thread pool service for executing of tasks asynchronously.
010 * <p>
011 * This service is used internally by Ebean for executing background tasks such
012 * as the {@link Query#findFutureList()} and also for executing background tasks
013 * periodically.
014 * </p>
015 * <p>
016 * This service has been made available so you can use it for your application
017 * code if you want. It can be useful for some server caching implementations
018 * (background population and trimming of the cache etc).
019 * </p>
020 *
021 * @author rbygrave
022 */
023public interface BackgroundExecutor {
024
025  /**
026   * Execute a task in the background.
027   */
028  void execute(Runnable r);
029
030  /**
031   * Execute a task periodically with a fixed delay between each execution.
032   * <p>
033   * For example, execute a runnable every minute.
034   * </p>
035   * <p>
036   * The delay is the time between executions no matter how long the task took.
037   * That is, this method has the same behaviour characteristics as
038   * {@link ScheduledExecutorService#scheduleWithFixedDelay(Runnable, long, long, TimeUnit)}
039   * </p>
040   */
041  void executePeriodically(Runnable r, long delay, TimeUnit unit);
042
043  /**
044   * Schedules a Runnable for one-shot action that becomes enabled after the given delay.
045   *
046   * @return a ScheduledFuture representing pending completion of the task and
047   *         whose get() method will return null upon completion
048   */
049  ScheduledFuture<?> schedule(Runnable r, long delay, TimeUnit unit);
050
051  /**
052   * Schedules a Callable for one-shot action that becomes enabled after the given delay.
053   *
054   * @return a ScheduledFuture that can be used to extract result or cancel
055   */
056  <V> ScheduledFuture<V> schedule(Callable<V> c, long delay, TimeUnit unit);
057
058
059}