|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.restlet.service.Service
org.restlet.service.TaskService
public class TaskService
Application service capable of running and scheduling tasks asynchronously.
The service instance returned will not invoke the runnable task in the
current thread.
In addition to allowing pooling, this method will ensure that the threads
executing the tasks will have the thread local variables copied from the
calling thread. This will ensure that call to static methods like
Application.getCurrent()
still work.
Also, note that this executor service will be shared among all Restlets and
Resources that are part of your context. In general this context corresponds
to a parent Application's context. If you want to have your own service
instance, you can use the wrap(ScheduledExecutorService)
method to ensure that thread local variables are correctly set.
Constructor Summary | |
---|---|
TaskService()
Constructor. |
|
TaskService(int corePoolSize)
Constructor. |
Method Summary | ||
---|---|---|
boolean |
awaitTermination(long timeout,
TimeUnit unit)
Blocks until all tasks have completed execution after a shutdown request, or the timeout occurs, or the current thread is interrupted, whichever happens first. |
|
protected ScheduledExecutorService |
createExecutorService(int corePoolSize)
Creates a new JDK executor service that will be wrapped. |
|
protected ThreadFactory |
createThreadFactory()
Creates a new thread factory that will properly name the Restlet created threads with a "restlet-" prefix. |
|
void |
execute(Runnable command)
Executes the given command asynchronously. |
|
int |
getCorePoolSize()
Returns the core pool size defining the maximum number of threads. |
|
List |
invokeAll(Collection tasks)
Executes the given tasks, returning a list of Futures holding their status and results when all complete. Due to a breaking change between Java SE versions 5 and 6, and in order to maintain compatibility both at the source and binary level, we have removed the generic information from this method. |
|
List |
invokeAll(Collection tasks,
long timeout,
TimeUnit unit)
Executes the given tasks, returning a list of Futures holding their status and results when all complete or the timeout expires, whichever happens first. |
|
Object |
invokeAny(Collection tasks)
Executes the given tasks, returning the result of one that has completed successfully (i.e., without throwing an exception), if any do. |
|
Object |
invokeAny(Collection tasks,
long timeout,
TimeUnit unit)
Executes the given tasks, returning the result of one that has completed successfully (i.e., without throwing an exception), if any do before the given timeout elapses. |
|
boolean |
isShutdown()
Returns true if this executor has been shut down. |
|
boolean |
isShutdownAllowed()
Indicates if the shutdown() and shutdownNow() methods
are allowed to effectively shutdown the wrapped executor service. |
|
boolean |
isTerminated()
Returns true if all tasks have completed following shut down. |
|
|
schedule(Callable<V> callable,
long delay,
TimeUnit unit)
Creates and executes a ScheduledFuture that becomes enabled after the given delay. |
|
ScheduledFuture<?> |
schedule(Runnable command,
long delay,
TimeUnit unit)
Creates and executes a one-shot action that becomes enabled after the given delay. |
|
ScheduledFuture<?> |
scheduleAtFixedRate(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 period; that is executions will commence after initialDelay then initialDelay+period, then initialDelay + 2 * period, and so on. |
|
ScheduledFuture<?> |
scheduleWithFixedDelay(Runnable command,
long initialDelay,
long delay,
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. |
|
void |
setCorePoolSize(int corePoolSize)
Sets the core pool size defining the maximum number of threads. |
|
void |
setShutdownAllowed(boolean allowShutdown)
Indicates if the shutdown() and shutdownNow() methods
are allowed to effectively shutdown the wrapped executor service. |
|
void |
shutdown()
Initiates an orderly shutdown in which previously submitted tasks are executed, but no new tasks will be accepted. |
|
List<Runnable> |
shutdownNow()
Attempts to stop all actively executing tasks, halts the processing of waiting tasks, and returns a list of the tasks that were awaiting execution. |
|
void |
start()
Starts the Restlet. |
|
void |
stop()
Stops the Restlet. |
|
|
submit(Callable<T> task)
Submits a value-returning task for execution and returns a Future representing the pending results of the task. |
|
Future<?> |
submit(Runnable task)
|
|
|
submit(Runnable task,
T result)
|
|
static ScheduledExecutorService |
wrap(ScheduledExecutorService executorService)
Wraps a JDK executor service to ensure that the threads executing the tasks will have the thread local variables copied from the calling thread. |
Methods inherited from class org.restlet.service.Service |
---|
createInboundFilter, createOutboundFilter, getContext, isEnabled, isStarted, isStopped, setContext, setEnabled |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public TaskService()
public TaskService(int corePoolSize)
Method Detail |
---|
public static ScheduledExecutorService wrap(ScheduledExecutorService executorService)
Application.getCurrent()
still work.
executorService
- The JDK service to wrap.
public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException
awaitTermination
in interface ExecutorService
timeout
- The maximum time to wait.unit
- The time unit.
InterruptedException
protected ScheduledExecutorService createExecutorService(int corePoolSize)
Executors.newCachedThreadPool(ThreadFactory)
, passing the
result of createThreadFactory()
as a parameter.
corePoolSize
- The core pool size defining the maximum number of threads.
protected ThreadFactory createThreadFactory()
public void execute(Runnable command)
execute
in interface Executor
command
- The command to execute.public int getCorePoolSize()
public List invokeAll(Collection tasks) throws InterruptedException
ExecutorService
interface for typing details.
invokeAll
in interface ExecutorService
tasks
- The task to execute.
InterruptedException
public List invokeAll(Collection tasks, long timeout, TimeUnit unit) throws InterruptedException
ExecutorService
interface for typing details.
invokeAll
in interface ExecutorService
tasks
- The task to execute.timeout
- The maximum time to wait.unit
- The time unit.
InterruptedException
public Object invokeAny(Collection tasks) throws InterruptedException, ExecutionException
ExecutorService
interface for typing details.
invokeAny
in interface ExecutorService
tasks
- The task to execute.
InterruptedException
ExecutionException
public Object invokeAny(Collection tasks, long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException
ExecutorService
interface for typing details.
invokeAny
in interface ExecutorService
tasks
- The task to execute.timeout
- The maximum time to wait.unit
- The time unit.
InterruptedException
ExecutionException
TimeoutException
public boolean isShutdown()
isShutdown
in interface ExecutorService
public boolean isShutdownAllowed()
shutdown()
and shutdownNow()
methods
are allowed to effectively shutdown the wrapped executor service. Return
false by default.
public boolean isTerminated()
isTerminated
in interface ExecutorService
public <V> ScheduledFuture<V> schedule(Callable<V> callable, long delay, TimeUnit unit)
schedule
in interface ScheduledExecutorService
callable
- The function to execute.delay
- The time from now to delay execution.unit
- The time unit of the delay parameter.
RejectedExecutionException
- if task cannot be scheduled for execution.
NullPointerException
- if callable is nullpublic ScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit)
schedule
in interface ScheduledExecutorService
command
- The task to execute.delay
- The time from now to delay execution.unit
- The time unit of the delay parameter.
RejectedExecutionException
- if task cannot be scheduled for execution.
NullPointerException
- if command is nullpublic ScheduledFuture<?> scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit)
scheduleAtFixedRate
in interface ScheduledExecutorService
command
- The task to execute.initialDelay
- The time to delay first execution.period
- The period between successive executions.unit
- The time unit of the initialDelay and period parameters
RejectedExecutionException
- if task cannot be scheduled for execution.
NullPointerException
- if command is null
IllegalArgumentException
- if period less than or equal to zero.public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit)
scheduleWithFixedDelay
in interface ScheduledExecutorService
command
- The task to execute.initialDelay
- The time to delay first execution.delay
- The delay between the termination of one execution and the
commencement of the next.unit
- The time unit of the initialDelay and delay parameters
RejectedExecutionException
- if task cannot be scheduled for execution.
NullPointerException
- if command is null
IllegalArgumentException
- if delay less than or equal to zero.public void setCorePoolSize(int corePoolSize)
corePoolSize
- The core pool size defining the maximum number of threads.public void setShutdownAllowed(boolean allowShutdown)
shutdown()
and shutdownNow()
methods
are allowed to effectively shutdown the wrapped executor service.
allowShutdown
- True if shutdown is allowed.public void shutdown()
shutdown
in interface ExecutorService
public List<Runnable> shutdownNow()
shutdownNow
in interface ExecutorService
public void start() throws Exception
Service
start
in class Service
Exception
public void stop() throws Exception
Service
stop
in class Service
Exception
public <T> Future<T> submit(Callable<T> task)
submit
in interface ExecutorService
task
- The task to submit.
public Future<?> submit(Runnable task)
submit
in interface ExecutorService
task
- The task to submit.
public <T> Future<T> submit(Runnable task, T result)
submit
in interface ExecutorService
task
- The task to submit.result
- The result to return.
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |