org.clazzes.util.sched.impl
Class OneTimeSchedulerImpl

java.lang.Object
  extended by org.clazzes.util.sched.impl.OneTimeSchedulerImpl
All Implemented Interfaces:
IOneTimeScheduler

public class OneTimeSchedulerImpl
extends Object
implements IOneTimeScheduler

A bean, which implements 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 Summary
OneTimeSchedulerImpl()
           
 
Method Summary
 IJobStatus cancelJob(UUID jobId, boolean mayInterrupt)
          Cancels the given job.
 List<UUID> getAllJobsIds()
           
 ExecutorService getExecutorService()
           
 long getGcInterval()
           
 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(Runnable runnable)
          Schedule the start of a job that returns no result.
 void setExecutorService(ExecutorService executorService)
           
 void setGcInterval(long gcInterval)
           
 void setResultLifeTime(long resultLifeTime)
           
 void setThreadLocalValues(Map<String,Object> threadLocalValues)
           
 void shutdown()
           
 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.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

OneTimeSchedulerImpl

public OneTimeSchedulerImpl()
Method Detail

start

public void start()

shutdown

public void shutdown()

shutdownNow

public void shutdownNow()

scheduleJob

public UUID scheduleJob(Runnable runnable)
Description copied from interface: IOneTimeScheduler
Schedule the start of a job that returns no result.

Specified by:
scheduleJob in interface IOneTimeScheduler
Returns:
the uuid of the new job

scheduleJob

public <V> UUID scheduleJob(Callable<V> callable)
Description copied from interface: IOneTimeScheduler
Schedule the start of a job that returns a result.

Specified by:
scheduleJob in interface IOneTimeScheduler
Returns:
the uuid of the new job

getAllJobsIds

public List<UUID> getAllJobsIds()
Specified by:
getAllJobsIds in interface IOneTimeScheduler
Returns:
a list of all currently existing job ids. This may include jobs that are already completed.

getJobStatus

public IJobStatus getJobStatus(UUID jobId)
Description copied from interface: IOneTimeScheduler
Query the job status, if the scheduled job implemented HasCallback, the returned status will be an instance of IJobStatusWithCallback.

Specified by:
getJobStatus in interface IOneTimeScheduler
Parameters:
jobId - The ID of the job as returned by IOneTimeScheduler.scheduleJob(Callable) or IOneTimeScheduler.scheduleJob(Runnable).
Returns:
a job status or 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)

waitForFinish

public IJobStatus waitForFinish(UUID jobId)
                         throws InterruptedException,
                                ExecutionException
Description copied from interface: IOneTimeScheduler
Waits until the given job has finished. Returns the status of the job, or null if no such job exists, e.g. because the scheduler does no longer run.

Specified by:
waitForFinish in interface IOneTimeScheduler
Parameters:
jobId - job id
Returns:
job status as described, null if no such job status exists
Throws:
InterruptedException
ExecutionException

waitForFinish

public IJobStatus waitForFinish(UUID jobId,
                                long timeoutMillis)
                         throws InterruptedException,
                                ExecutionException,
                                TimeoutException
Description copied from interface: IOneTimeScheduler
Waits until the given job has finished, but no longer than the given timeout. Returns the status of the job, or null if no such job exists, e.g. because the scheduler does no longer run.

Specified by:
waitForFinish in interface IOneTimeScheduler
Parameters:
jobId - job id
timeoutMillis - timeout
Returns:
job status as described, null if no such job status exists
Throws:
InterruptedException
ExecutionException
TimeoutException

cancelJob

public IJobStatus cancelJob(UUID jobId,
                            boolean mayInterrupt)
Description copied from interface: IOneTimeScheduler
Cancels the given job. Returns the status of the job, or null if no such job exists, e.g. because the scheduler does no longer run.

Specified by:
cancelJob in interface IOneTimeScheduler
Parameters:
jobId - job id
mayInterrupt - true if and only if the job is allowed to be interrupted
Returns:
job status as described, null if no such job status exists

purgeResult

public IJobStatus purgeResult(UUID jobId)
Description copied from interface: IOneTimeScheduler
Purges the results of the given job.

Specified by:
purgeResult in interface IOneTimeScheduler
Parameters:
jobId - job id
Returns:
job status, null if no such job status exists

getExecutorService

public ExecutorService getExecutorService()
Returns:
the executorService

setExecutorService

public void setExecutorService(ExecutorService executorService)
Parameters:
executorService - the executorService to set

getGcInterval

public long getGcInterval()
Returns:
the garbage collection interval in milliseconds.

setGcInterval

public void setGcInterval(long gcInterval)
Parameters:
gcInterval - the gcInterval to set

getResultLifeTime

public long getResultLifeTime()
Returns:
the lifetime of results after a job has finished in milliseconds.

setResultLifeTime

public void setResultLifeTime(long resultLifeTime)
Parameters:
resultLifeTime - the lifetime of results after a job has finished in milliseconds to set.

getThreadLocalValues

public Map<String,Object> getThreadLocalValues()
Returns:
A list of thread local value to propagate to the jobs.

setThreadLocalValues

public void setThreadLocalValues(Map<String,Object> threadLocalValues)
Parameters:
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 © 2012. All Rights Reserved.