Class AbstractJob
java.lang.Object
org.springframework.batch.core.job.AbstractJob
- All Implemented Interfaces:
Job,StepLocator,org.springframework.beans.factory.Aware,org.springframework.beans.factory.BeanNameAware,org.springframework.beans.factory.InitializingBean
public abstract class AbstractJob
extends Object
implements Job, StepLocator, org.springframework.beans.factory.BeanNameAware, org.springframework.beans.factory.InitializingBean
Abstract implementation of the
Job interface. Common dependencies such as a
JobRepository, JobExecutionListeners, and various configuration
parameters are set here. Therefore, common error handling and listener calling
activities are abstracted away from implementations.- Author:
- Lucas Ward, Dave Syer, Mahmoud Ben Hassine
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionDefault constructor.AbstractJob(String name) Convenience constructor to immediately add name (which is mandatory but not final). -
Method Summary
Modifier and TypeMethodDescriptionvoidAssert mandatory properties:JobRepository.protected abstract voiddoExecute(JobExecution execution) Extension point for subclasses allowing them to concentrate on processing logic and ignore listeners and repository calls.final voidexecute(JobExecution execution) Run the specified job, handling all listener and repository calls, and delegating the actual processing todoExecute(JobExecution).protected ExitStatusgetDefaultExitStatusForFailure(Throwable ex, JobExecution execution) Default mapping from throwable toExitStatus.If clients need to generate new parameters for the next execution in a sequence, they can use this incrementer.A validator for the job parameters of aJobExecution.protected JobRepositoryConvenience method for subclasses to access the job repository.getName()abstract StepRetrieve the step with the given name.abstract Collection<String>Retrieve the step names.protected final StepExecutionhandleStep(Step step, JobExecution execution) Convenience method for subclasses to delegate the handling of a specific step in the context of the currentJobExecution.booleanFlag to indicate if this job can be restarted, at least in principle.voidRegister a single listener for theJobExecutionListenercallbacks.voidsetBeanName(String name) Set the name property if it is not already set.voidsetJobExecutionListeners(JobExecutionListener[] listeners) Public setter for injectingJobExecutionListeners.voidsetJobParametersIncrementer(JobParametersIncrementer jobParametersIncrementer) Public setter for theJobParametersIncrementer.voidsetJobParametersValidator(JobParametersValidator jobParametersValidator) A validator for job parameters.voidsetJobRepository(JobRepository jobRepository) Public setter for theJobRepositorythat is needed to manage the state of the batch meta domain (jobs, steps, executions) during the life of a job.voidsetMeterRegistry(io.micrometer.core.instrument.MeterRegistry meterRegistry) voidSet the name property.voidsetObservationConvention(BatchJobObservationConvention observationConvention) voidsetObservationRegistry(io.micrometer.observation.ObservationRegistry observationRegistry) voidsetRestartable(boolean restartable) Boolean flag to prevent categorically a job from restarting, even if it has failed previously.toString()
-
Field Details
-
logger
protected static final org.apache.commons.logging.Log logger
-
-
Constructor Details
-
AbstractJob
public AbstractJob()Default constructor. -
AbstractJob
Convenience constructor to immediately add name (which is mandatory but not final).- Parameters:
name- name of the job
-
-
Method Details
-
setJobParametersValidator
A validator for job parameters. Defaults to a vanillaDefaultJobParametersValidator.- Parameters:
jobParametersValidator- a validator instance
-
afterPropertiesSet
Assert mandatory properties:JobRepository.- Specified by:
afterPropertiesSetin interfaceorg.springframework.beans.factory.InitializingBean- Throws:
Exception- See Also:
-
InitializingBean.afterPropertiesSet()
-
setBeanName
Set the name property if it is not already set. Because of the order of the callbacks in a Spring container the name property will be set first if it is present. Care is needed with bean definition inheritance - if a parent bean has a name, then its children need an explicit name as well, otherwise they will not be unique.- Specified by:
setBeanNamein interfaceorg.springframework.beans.factory.BeanNameAware- See Also:
-
BeanNameAware.setBeanName(java.lang.String)
-
setName
Set the name property. Always overrides the default value if this object is a Spring bean.- Parameters:
name- the name to be associated with the job.- See Also:
-
getName
-
getStep
Retrieve the step with the given name. If there is no Step with the given name, then return null.- Specified by:
getStepin interfaceStepLocator- Parameters:
stepName- name of the step- Returns:
- the Step
-
getStepNames
Retrieve the step names.- Specified by:
getStepNamesin interfaceStepLocator- Returns:
- the step names
-
getJobParametersValidator
Description copied from interface:JobA validator for the job parameters of aJobExecution. Clients of aJobmay need to validate the parameters for a launch or before or during the execution.- Specified by:
getJobParametersValidatorin interfaceJob- Returns:
- a validator that can be used to check parameter values (never
null). Defaults toDefaultJobParametersValidator.
-
setRestartable
public void setRestartable(boolean restartable) Boolean flag to prevent categorically a job from restarting, even if it has failed previously.- Parameters:
restartable- the value of the flag to set (default true)
-
isRestartable
public boolean isRestartable()Description copied from interface:JobFlag to indicate if this job can be restarted, at least in principle.- Specified by:
isRestartablein interfaceJob- Returns:
- true if this job can be restarted after a failure. Defaults to
true. - See Also:
-
setJobParametersIncrementer
Public setter for theJobParametersIncrementer.- Parameters:
jobParametersIncrementer- theJobParametersIncrementerto set
-
getJobParametersIncrementer
Description copied from interface:JobIf clients need to generate new parameters for the next execution in a sequence, they can use this incrementer. The return value may benull, when this job does not have a natural sequence.- Specified by:
getJobParametersIncrementerin interfaceJob- Returns:
- an incrementer to be used for creating new parameters. Defaults to
null.
-
setJobExecutionListeners
Public setter for injectingJobExecutionListeners. They will all be given the listener callbacks at the appropriate point in the job.- Parameters:
listeners- the listeners to set.
-
registerJobExecutionListener
Register a single listener for theJobExecutionListenercallbacks.- Parameters:
listener- aJobExecutionListener
-
setJobRepository
Public setter for theJobRepositorythat is needed to manage the state of the batch meta domain (jobs, steps, executions) during the life of a job.- Parameters:
jobRepository- repository to use during the job execution
-
getJobRepository
Convenience method for subclasses to access the job repository.- Returns:
- the jobRepository
-
doExecute
Extension point for subclasses allowing them to concentrate on processing logic and ignore listeners and repository calls. Implementations usually are concerned with the ordering of steps, and delegate actual step processing tohandleStep(Step, JobExecution).- Parameters:
execution- the currentJobExecution- Throws:
JobExecutionException- to signal a fatal batch framework error (not a business or validation exception)
-
execute
Run the specified job, handling all listener and repository calls, and delegating the actual processing todoExecute(JobExecution).- Specified by:
executein interfaceJob- Parameters:
execution- aJobExecution- Throws:
StartLimitExceededException- if start limit of one of the steps was exceeded- See Also:
-
handleStep
protected final StepExecution handleStep(Step step, JobExecution execution) throws JobInterruptedException, JobRestartException, StartLimitExceededException Convenience method for subclasses to delegate the handling of a specific step in the context of the currentJobExecution. Clients of this method do not need access to theJobRepository, nor do they need to worry about populating the execution context on a restart, nor detecting the interrupted state (in job or step execution).- Parameters:
step- theStepto executeexecution- the currentJobExecution- Returns:
- the
StepExecutioncorresponding to this step - Throws:
JobInterruptedException- if theJobExecutionhas been interrupted, and in particular ifBatchStatus.ABANDONEDorBatchStatus.STOPPINGis detectedStartLimitExceededException- if the start limit has been exceeded for this stepJobRestartException- if the job is in an inconsistent state from an earlier failure
-
getDefaultExitStatusForFailure
Default mapping from throwable toExitStatus.- Parameters:
ex- the cause of the failureexecution- theJobExecutioninstance.- Returns:
- an
ExitStatus
-
setObservationConvention
-
setObservationRegistry
public void setObservationRegistry(io.micrometer.observation.ObservationRegistry observationRegistry) -
setMeterRegistry
public void setMeterRegistry(io.micrometer.core.instrument.MeterRegistry meterRegistry) -
toString
-