001/*
002 * Copyright 2002-2018 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.core.task;
018
019import java.util.concurrent.Callable;
020import java.util.concurrent.Future;
021
022/**
023 * Extended interface for asynchronous {@link TaskExecutor} implementations,
024 * offering an overloaded {@link #execute(Runnable, long)} variant with a start
025 * timeout parameter as well support for {@link java.util.concurrent.Callable}.
026 *
027 * <p>Note: The {@link java.util.concurrent.Executors} class includes a set of
028 * methods that can convert some other common closure-like objects, for example,
029 * {@link java.security.PrivilegedAction} to {@link Callable} before executing them.
030 *
031 * <p>Implementing this interface also indicates that the {@link #execute(Runnable)}
032 * method will not execute its Runnable in the caller's thread but rather
033 * asynchronously in some other thread.
034 *
035 * @author Juergen Hoeller
036 * @since 2.0.3
037 * @see SimpleAsyncTaskExecutor
038 * @see org.springframework.scheduling.SchedulingTaskExecutor
039 * @see java.util.concurrent.Callable
040 * @see java.util.concurrent.Executors
041 */
042public interface AsyncTaskExecutor extends TaskExecutor {
043
044        /** Constant that indicates immediate execution. */
045        long TIMEOUT_IMMEDIATE = 0;
046
047        /** Constant that indicates no time limit. */
048        long TIMEOUT_INDEFINITE = Long.MAX_VALUE;
049
050
051        /**
052         * Execute the given {@code task}.
053         * @param task the {@code Runnable} to execute (never {@code null})
054         * @param startTimeout the time duration (milliseconds) within which the task is
055         * supposed to start. This is intended as a hint to the executor, allowing for
056         * preferred handling of immediate tasks. Typical values are {@link #TIMEOUT_IMMEDIATE}
057         * or {@link #TIMEOUT_INDEFINITE} (the default as used by {@link #execute(Runnable)}).
058         * @throws TaskTimeoutException in case of the task being rejected because
059         * of the timeout (i.e. it cannot be started in time)
060         * @throws TaskRejectedException if the given task was not accepted
061         */
062        void execute(Runnable task, long startTimeout);
063
064        /**
065         * Submit a Runnable task for execution, receiving a Future representing that task.
066         * The Future will return a {@code null} result upon completion.
067         * @param task the {@code Runnable} to execute (never {@code null})
068         * @return a Future representing pending completion of the task
069         * @throws TaskRejectedException if the given task was not accepted
070         * @since 3.0
071         */
072        Future<?> submit(Runnable task);
073
074        /**
075         * Submit a Callable task for execution, receiving a Future representing that task.
076         * The Future will return the Callable's result upon completion.
077         * @param task the {@code Callable} to execute (never {@code null})
078         * @return a Future representing pending completion of the task
079         * @throws TaskRejectedException if the given task was not accepted
080         * @since 3.0
081         */
082        <T> Future<T> submit(Callable<T> task);
083
084}