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

org.fabric3.timer.spi.TimerService Maven / Gradle / Ivy

The newest version!
/*
 * Fabric3
 * Copyright (c) 2009-2015 Metaform Systems
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * Portions originally based on Apache Tuscany 2007
 * licensed under the Apache 2.0 license.
 */
package org.fabric3.timer.spi;

import java.util.concurrent.Callable;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

import org.fabric3.api.host.Fabric3Exception;

/**
 * Executes scheduled tasks.
 */
public interface TimerService {
    String DEFAULT_POOL = "default";

    /**
     * Allocates a timer thread pool.
     *
     * @param poolName the pool name
     * @param coreSize the  thread pool size
     * @throws Fabric3Exception if there is an error allocating the pool
     */
    void allocate(String poolName, int coreSize) throws Fabric3Exception;

    /**
     * Shuts down and de-allocates a timer thread pool.
     *
     * @param poolName the pool name
     * @throws Fabric3Exception if there is an error de-allocating the pool
     */
    void deallocate(String poolName) throws Fabric3Exception;

    /**
     * Creates and executes a recurring action.
     *
     * @param poolName the timer thread pool to schedule the task with
     * @param task     the task to execute
     * @return a ScheduledFuture representing pending completion of the task and whose get() method will return null upon
     *         completion
     * @throws RejectedExecutionException if the task cannot be scheduled for execution
     * @throws NullPointerException       if command is null
     */
    ScheduledFuture scheduleRecurring(String poolName, Task task);

    /**
     * Creates and executes a one-shot action that becomes enabled after the given delay.
     *
     * @param poolName the timer thread pool to schedule the task with
     * @param command  the task to execute
     * @param delay    the time from now to delay execution
     * @param unit     the time unit of the delay parameter
     * @return a ScheduledFuture representing pending completion of the task and whose get() method will return null upon
     *         completion
     * @throws RejectedExecutionException if the task cannot be scheduled for execution
     * @throws NullPointerException       if command is null
     */
    ScheduledFuture schedule(String poolName, Runnable command, long delay, TimeUnit unit);

    /**
     * Creates and executes a ScheduledFuture that becomes enabled after the given delay.
     *
     * @param poolName the timer thread pool to schedule the task with
     * @param callable the function to execute
     * @param delay    the time from now to delay execution
     * @param unit     the time unit of the delay parameter
     * @return a ScheduledFuture that can be used to extract result or cancel
     * @throws RejectedExecutionException if the task cannot be scheduled for execution
     * @throws NullPointerException       if callable is null
     */
     ScheduledFuture schedule(String poolName, Callable callable, long delay, TimeUnit unit);

    /**
     * Creates and executes a periodic action that becomes enabled first after the given initial delay, and subsequently with the given period; that
     * is executions will commence after initialDelay then initialDelay+period, then initialDelay + 2 * period, and so on.
     * If any execution of the task encounters an exception, subsequent executions are suppressed. Otherwise, the task will only terminate via
     * cancellation or termination of the executor.  If any execution of this task takes longer than its period, then subsequent executions may start
     * late, but will not concurrently execute.
     *
     * @param poolName     the timer thread pool to schedule the task with
     * @param command      the task to execute
     * @param initialDelay the time to delay first execution
     * @param period       the period between successive executions
     * @param unit         the time unit of the initialDelay and period parameters
     * @return a ScheduledFuture representing pending completion of the task, and whose get() method will throw an exception upon
     *         cancellation
     * @throws RejectedExecutionException if the task cannot be scheduled for execution
     * @throws NullPointerException       if command is null
     * @throws IllegalArgumentException   if period less than or equal to zero
     */
    ScheduledFuture scheduleAtFixedRate(String poolName, Runnable command, long initialDelay, long period, TimeUnit unit);

    /**
     * Creates and executes a periodic action that becomes enabled first after the given initial delay, and subsequently with the given delay between
     * the termination of one execution and the commencement of the next.  If any execution of the task encounters an exception, subsequent executions
     * are suppressed. Otherwise, the task will only terminate via cancellation or termination of the executor.
     *
     * @param poolName     the timer thread pool to schedule the task with
     * @param command      the task to execute
     * @param initialDelay the time to delay first execution
     * @param delay        the delay between the termination of one execution and the commencement of the next
     * @param unit         the time unit of the initialDelay and delay parameters
     * @return a ScheduledFuture representing pending completion of the task, and whose get() method will throw an exception upon
     *         cancellation
     * @throws RejectedExecutionException if the task cannot be scheduled for execution
     * @throws NullPointerException       if command is null
     * @throws IllegalArgumentException   if delay less than or equal to zero
     */
    ScheduledFuture scheduleWithFixedDelay(String poolName, Runnable command, long initialDelay, long delay, TimeUnit unit);


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy