Class JobSupport
- All Implemented Interfaces:
Runnable,ManagedJob
- Direct Known Subclasses:
DirectoryCleanerJob,JdbcCallJob,SharedValueCacheCleaner
This job supports running a set of tasks in parallel by configuring the
setParallelism(int) property to something greater than 1.
The parallel tasks can compete for a pool of task iterations so that
each task competes for a bounded amount of work. This is useful for scenarios
like batch processing a queue, where the job is designed to process up to a
maximum number of queued items each time it runs using parallel workers.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringThescheduleproperty default value.static final longThejitterproperty default value.static final intThemaximumIterationsproperty default value.static final longThemaximumWaitMsproperty default value.static final intTheparallelismproperty default value.protected final org.slf4j.LoggerA class-level logger. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected intexecuteJobTask(AtomicInteger remainingIterataions) Execute a parallel job task.protected final booleanexecuteParallelJob(String taskName) Execute the job in parallel via multiple threads.Get the job group to use.getId()Get the unique ID of the job to schedule.longGet a maximum amount of time, in milliseconds, to randomly add to the start of parallel tasks so they don't all try to start so closely together.intGet the maximum number of iterations of the job task to run.longGet the maximum time, in milliseconds, to allow for the job to execute before it is considered a failed job.intGet the number of parallel threads to use while processing task iterations.org.springframework.core.task.AsyncTaskExecutorGet the executor to handle parallel job tasks with.Get the job cron expression to use for scheduling this job.voidsetGroupId(String jobGroup) Set the job group to use.voidSet the unique ID of the job to schedule.voidsetJitter(long jitter) Set et a maximum amount of time, in milliseconds, to randomly add to the start of parallel tasks so they don't all try to start so closely together.voidsetMaximumIterations(int maximumIterations) Set the maximum number of claims to acquire per execution of this job.voidsetMaximumWaitMs(long maximumWaitMs) Set the maximum time, in milliseconds, to allow for the job to execute before it is considered a failed job.voidsetParallelism(int parallelism) Set the number of parallel threads to use while processing task iterations.voidsetParallelTaskExecutor(org.springframework.core.task.AsyncTaskExecutor parallelTaskExecutorService) Set the executor to handle parallel job tasks with.voidsetSchedule(String jobCron) Set the job cron expression to use for scheduling this job.
-
Field Details
-
DEFAULT_MAX_WAIT
public static final long DEFAULT_MAX_WAITThemaximumWaitMsproperty default value.- See Also:
-
DEFAULT_CRON
Thescheduleproperty default value.- See Also:
-
DEFAULT_MAX_ITERATIONS
public static final int DEFAULT_MAX_ITERATIONSThemaximumIterationsproperty default value.- See Also:
-
DEFAULT_PARALLELISM
public static final int DEFAULT_PARALLELISMTheparallelismproperty default value.- See Also:
-
DEFAULT_JITTER
public static final long DEFAULT_JITTERThejitterproperty default value.- See Also:
-
log
protected final org.slf4j.Logger logA class-level logger.
-
-
Constructor Details
-
JobSupport
public JobSupport()
-
-
Method Details
-
executeParallelJob
Execute the job in parallel via multiple threads.This method can be invoked by extending classes from their
Runnable.run()method. When invoked, this method will createparallelismtasks and submit them to the configuredparallelTaskExecutor. Each task will call theexecuteJobTask(AtomicInteger)method, passing a sharedAtomicIntegerinitially set tomaximumIterations, which serves as a hint as to the number of overall iterations all tasks are trying to perform. The tasks thus compete for iterations and should decrement theAtomicIntegerby the number of iterations they process, and stop processing iterations when the count reaches 0 or less.Note that this method does not track the iteration count. It merely waits for each task to complete, by returning or throwing an exception, for up to
maximumWaitMsmilliseconds.If
parallelismis 1 then theexecuteJobTask(AtomicInteger)method is called directly from this method, without submitting the task to the configuredexecutorService.- Parameters:
taskName- a descriptive name for the job task, to use in loggingjob- the job event- Returns:
- the true if all tasks were completed
- Throws:
Exception- if any error occurs- Since:
- 1.7
-
executeJobTask
Execute a parallel job task.This method is called from the
executeParallelJob(String)method by each thread. This method is supposed to execute up toremainingIterataionsof the job's task, updatingremainingIterataionsas each iteration is processed. Keep in mind that each job task thread will be mutating (competing for)remainingIterataions.This method throws a
UnsupportedOperationExceptionand must be overridden by extending classes.- Parameters:
remainingIterataions- the number of iterations left to perform- Returns:
- the number of iterations performed
- Throws:
Exception- if any error occurs- Since:
- 1.7
-
getId
Get the unique ID of the job to schedule.- Specified by:
getIdin interfaceManagedJob- Returns:
- the job ID
-
setId
Set the unique ID of the job to schedule.- Parameters:
id- the job ID
-
getMaximumWaitMs
public long getMaximumWaitMs()Get the maximum time, in milliseconds, to allow for the job to execute before it is considered a failed job.- Returns:
- the maximum wait, in milliseconds; defaults to 15 minutes
-
setMaximumWaitMs
public void setMaximumWaitMs(long maximumWaitMs) Set the maximum time, in milliseconds, to allow for the job to execute before it is considered a failed job.- Parameters:
maximumWaitMs- the maximum wait
-
getSchedule
Get the job cron expression to use for scheduling this job.- Specified by:
getSchedulein interfaceManagedJob- Returns:
- the cron expression; defaults to 0 0/1 * * * ? (once per minute)
-
setSchedule
Set the job cron expression to use for scheduling this job.- Parameters:
schedule- the cron expression
-
getGroupId
Get the job group to use.- Specified by:
getGroupIdin interfaceManagedJob- Returns:
- the job group
-
setGroupId
Set the job group to use.- Parameters:
groupId- the job group
-
getParallelTaskExecutor
public org.springframework.core.task.AsyncTaskExecutor getParallelTaskExecutor()Get the executor to handle parallel job tasks with.If not defined, then
will be used.invalid reference
#getExecutorService()- Returns:
- the service
-
setParallelTaskExecutor
public void setParallelTaskExecutor(org.springframework.core.task.AsyncTaskExecutor parallelTaskExecutorService) Set the executor to handle parallel job tasks with.- Parameters:
parallelTaskExecutor- the service to set
-
getMaximumIterations
public int getMaximumIterations()Get the maximum number of iterations of the job task to run.- Returns:
- the maximum iterations; defaults to 1
- Since:
- 1.7
-
setMaximumIterations
public void setMaximumIterations(int maximumIterations) Set the maximum number of claims to acquire per execution of this job.- Parameters:
maximumIterations- the maximum iterations- Since:
- 1.7
-
getParallelism
public int getParallelism()Get the number of parallel threads to use while processing task iterations.- Returns:
- the parallelism; defaults to 1
- Since:
- 1.7
-
setParallelism
public void setParallelism(int parallelism) Set the number of parallel threads to use while processing task iterations.- Parameters:
parallelism- the parallelism to set; will be forced to 1 if < 1- Since:
- 1.7
-
getJitter
public long getJitter()Get a maximum amount of time, in milliseconds, to randomly add to the start of parallel tasks so they don't all try to start so closely together.- Returns:
- the jitter, in milliseconds; defaults to
DEFAULT_JITTER
-
setJitter
public void setJitter(long jitter) Set et a maximum amount of time, in milliseconds, to randomly add to the start of parallel tasks so they don't all try to start so closely together.This time is added to tasks started by the
executeParallelJob(String)method, and only whengetParallelism()is greater than 1. Set to 0 to disable adding any random jitter to the start of tasks.- Parameters:
jitter- the jitter to set, in milliseconds
-