org.clazzes.util.sched.impl
public class OneTimeSchedulerImpl extends Object implements IOneTimeScheduler
IOneTimeScheduler
.
In the context of blueprint, it can be used like this:
<bp:bean id="executorService" class="java.util.concurrent.Executors" factory-method="newFixedThreadPool"> <bp:argument value="3"/> </bp:bean> <bp:bean id="oneTimeScheduler" class="org.clazzes.util.sched.impl.OneTimeSchedulerImpl" init-method="start" destroy-method="shutdownNow"> <bp:property name="executorService" ref="executorService"/> <bp:property name="resultLifeTime" value="10000"/> <bp:property name="gcInterval" value="10000"/> </bp:bean>Then inject the oneTimeScheduler as a bean into your service or servlet, write a Callable or Runnable, and call
scheduleJob
on it.
Probably you also want to write a service for querying the state of your scheduled jobs, using getJobState
.
In that case, transmit the uuid returned by scheduleJob
to the client, and use it as a parameter for the querying
service method lateron.Constructor and Description |
---|
OneTimeSchedulerImpl() |
Modifier and Type | Method and Description |
---|---|
IJobStatus |
cancelJob(UUID jobId,
boolean mayInterrupt)
Cancels the given job.
|
List<UUID> |
getAllJobsIds() |
ExecutorService |
getExecutorService() |
long |
getGcInterval() |
ScheduledExecutorService |
getGcService() |
IJobStatus |
getJobStatus(UUID jobId)
Query the job status, if the scheduled job implemented
HasCallback ,
the returned status will be an instance of IJobStatusWithCallback . |
long |
getResultLifeTime() |
Map<String,Object> |
getThreadLocalValues() |
IJobStatus |
purgeResult(UUID jobId)
Purges the results of the given job.
|
<V> UUID |
scheduleJob(Callable<V> callable)
Schedule the start of a job that returns a result.
|
UUID |
scheduleJob(org.aopalliance.intercept.Joinpoint joinpoint)
Schedule the start of a job that returns a result.
|
UUID |
scheduleJob(Runnable runnable)
Schedule the start of a job that returns no result.
|
void |
setExecutorService(ExecutorService executorService) |
void |
setGcInterval(long gcInterval) |
void |
setGcService(ScheduledExecutorService gcService)
Set a scheduled executor service for garbage collection.
|
void |
setResultLifeTime(long resultLifeTime) |
void |
setThreadLocalValues(Map<String,Object> threadLocalValues) |
void |
shutdown()
Cancel all pending jobs scheduled by this instance.
|
protected void |
shutdownGracefully(boolean hard) |
void |
shutdownNow() |
void |
start() |
IJobStatus |
waitForFinish(UUID jobId)
Waits until the given job has finished.
|
IJobStatus |
waitForFinish(UUID jobId,
long timeoutMillis)
Waits until the given job has finished, but no longer than the given timeout.
|
public void start()
protected void shutdownGracefully(boolean hard)
public void shutdown()
Cancel all pending jobs scheduled by this instance. Currently running jobs will not be interrupted.
Warning: Unlike the semantics of sched-util-1.2.0
and earlier versions, this method will now shut down the underlying
executor instance.
public void shutdownNow()
public UUID scheduleJob(Runnable runnable)
IOneTimeScheduler
Schedule the start of a job that returns no result.
The passed instance may implement ITimedJob
in
which case, the job is called repeatedly according to the
results of ITimedJob.getNextExecutionDelay()
.
If runnable
moreover implements HasCallback
,
the job status will be IJobStatusWithCallback
instance,
which bears additional application-specific information.
scheduleJob
in interface IOneTimeScheduler
runnable
- Teh runnbel to schedule.public UUID scheduleJob(org.aopalliance.intercept.Joinpoint joinpoint)
IOneTimeScheduler
Schedule the start of a job that returns a result.
This method exists in order to schedule AOP instances like
ReflectiveMethodInvocation
. If you have the choice,
please implement Callable
in favor of Joinpoint
,
because this avoids an internal indirection layer.
The passed instance may implement ITimedJob
in
which case, the job is called repeatedly according to the
results of ITimedJob.getNextExecutionDelay()
.
If joinpoint
moreover implements HasCallback
,
the job status will be IJobStatusWithCallback
instance,
which bears additional application-specific information.
scheduleJob
in interface IOneTimeScheduler
public <V> UUID scheduleJob(Callable<V> callable)
IOneTimeScheduler
Schedule the start of a job that returns a result.
The passed instance may implement ITimedJob
in
which case, the job is called repeatedly according to the
results of ITimedJob.getNextExecutionDelay()
.
If callable
moreover implements HasCallback
,
the job status will be IJobStatusWithCallback
instance,
which bears additional application-specific information.
scheduleJob
in interface IOneTimeScheduler
callable
- The callable to be scheduled.public List<UUID> getAllJobsIds()
getAllJobsIds
in interface IOneTimeScheduler
public IJobStatus getJobStatus(UUID jobId)
IOneTimeScheduler
HasCallback
,
the returned status will be an instance of IJobStatusWithCallback
.getJobStatus
in interface IOneTimeScheduler
jobId
- The ID of the job as returned by IOneTimeScheduler.scheduleJob(Callable)
or IOneTimeScheduler.scheduleJob(Runnable)
.null
if no such job status exists (e.g.
because the job has completed and was already garbage collected, or
because the scheduler was shut down)public IJobStatus waitForFinish(UUID jobId) throws InterruptedException, ExecutionException
IOneTimeScheduler
null
if no such job exists, e.g. because the scheduler does no
longer run.waitForFinish
in interface IOneTimeScheduler
jobId
- job idnull
if no such job status existsInterruptedException
ExecutionException
public IJobStatus waitForFinish(UUID jobId, long timeoutMillis) throws InterruptedException, ExecutionException, TimeoutException
IOneTimeScheduler
null
if no such job exists,
e.g. because the scheduler does no longer run.waitForFinish
in interface IOneTimeScheduler
jobId
- job idtimeoutMillis
- timeoutnull
if no such job status existsInterruptedException
ExecutionException
TimeoutException
public IJobStatus cancelJob(UUID jobId, boolean mayInterrupt)
IOneTimeScheduler
null
if no such job exists,
e.g. because the scheduler does no longer run.cancelJob
in interface IOneTimeScheduler
jobId
- job idmayInterrupt
- true
if and only if the job is allowed to be interruptednull
if no such job status existspublic IJobStatus purgeResult(UUID jobId)
IOneTimeScheduler
purgeResult
in interface IOneTimeScheduler
jobId
- job idnull
if no such job status existspublic ExecutorService getExecutorService()
public void setExecutorService(ExecutorService executorService)
executorService
- the executorService to setpublic ScheduledExecutorService getGcService()
public void setGcService(ScheduledExecutorService gcService)
setExecutorService(ExecutorService)
is used, if this service implements ScheduledExecutorService
or an internal single-threaded executor service is internally used.gcService
- The new scheduled exectuor service used for the garbage collector.public long getGcInterval()
public void setGcInterval(long gcInterval)
gcInterval
- the gcInterval to setpublic long getResultLifeTime()
public void setResultLifeTime(long resultLifeTime)
resultLifeTime
- the lifetime of results after a job has finished in
milliseconds to set.public Map<String,Object> getThreadLocalValues()
public void setThreadLocalValues(Map<String,Object> threadLocalValues)
threadLocalValues
- A list of thread local value to propagate to the
jobs to set. If the value for a given key is null
,
the thread local value for that key of the scheduling thread is
propagated to the asynchronous job.Copyright © 2014 Clazzes.org. All Rights Reserved.