Class JsrJobOperator
- java.lang.Object
- org.springframework.batch.core.jsr.launch.JsrJobOperator
- All Implemented Interfaces:
javax.batch.operations.JobOperator
,org.springframework.beans.factory.Aware
,org.springframework.beans.factory.InitializingBean
,org.springframework.context.ApplicationContextAware
public class JsrJobOperator extends java.lang.Object implements javax.batch.operations.JobOperator, org.springframework.context.ApplicationContextAware, org.springframework.beans.factory.InitializingBean
The entrance for executing batch jobs as defined by JSR-352. This class provides a single baseApplicationContext
that is the equivalent to the following: <beans> <batch:job-repository id="jobRepository" ... /> <bean id="jobLauncher" class="org.springframework.batch.core.launch.support.SimpleJobLauncher"> ... </bean> <bean id="batchJobOperator" class="org.springframework.batch.core.launch.support.SimpleJobOperator"> ... </bean> <bean id="jobExplorer" class="org.springframework.batch.core.explore.support.JobExplorerFactoryBean"> ... </bean> <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource"> ... </bean> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> ... </bean> <bean id="jobParametersConverter" class="org.springframework.batch.core.jsr.JsrJobParametersConverter"/> <bean id="jobRegistry" class="org.springframework.batch.core.configuration.support.MapJobRegistry"/> <bean id="placeholderProperties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> ... </bean> </beans> A custom configuration of the above components can be specified by providing a system property JSR-352-BASE-CONTEXT. The location that is provided by this system property will override any beans as defined in baseContext.xml. Calls toJobOperator.start(String, Properties)
will provide a child context to the above context using the job definition and batch.xml if provided. By default, calls to start/restart will result in asynchronous execution of the batch job (via an asynchronousTaskExecutor
. For synchronous behavior or customization of thread behavior, a differentTaskExecutor
implementation is required to be provided. Note: This class is intended to only be used for JSR-352 configured jobs. Use of thisJobOperator
to start/stop/restart Spring Batch jobs may result in unexpected behaviors due to how job instances are identified differently.- Since:
- 3.0
- Author:
- Michael Minella, Chris Schaefer
Nested Class Summary
Nested Classes Modifier and Type Class Description protected static class
JsrJobOperator.BaseContextHolder
A singleton holder used to lazily bootstrap the base context used in JSR-352.
Constructor Summary
Constructors Constructor Description JsrJobOperator()
Public constructor used byBatchRuntime.getJobOperator()
.JsrJobOperator(JobExplorer jobExplorer, JobRepository jobRepository, JobParametersConverter jobParametersConverter, org.springframework.transaction.PlatformTransactionManager transactionManager)
The no-arg constructor is used by theBatchRuntime.getJobOperator()
and so bootstraps anApplicationContext
.
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
abandon(long jobExecutionId)
void
afterPropertiesSet()
javax.batch.runtime.JobExecution
getJobExecution(long executionId)
java.util.List<javax.batch.runtime.JobExecution>
getJobExecutions(javax.batch.runtime.JobInstance jobInstance)
javax.batch.runtime.JobInstance
getJobInstance(long executionId)
int
getJobInstanceCount(java.lang.String jobName)
java.util.List<javax.batch.runtime.JobInstance>
getJobInstances(java.lang.String jobName, int start, int count)
java.util.Set<java.lang.String>
getJobNames()
protected java.util.Properties
getJobRestartProperties(java.util.Properties params, JobExecution previousJobExecution)
java.util.Properties
getParameters(long executionId)
java.util.List<java.lang.Long>
getRunningExecutions(java.lang.String name)
java.util.List<javax.batch.runtime.StepExecution>
getStepExecutions(long executionId)
protected org.springframework.core.task.TaskExecutor
getTaskExecutor()
long
restart(long executionId, java.util.Properties params)
Creates a childApplicationContext
for the job being requested based upon the /META-INF/batch.xml (if exists) and the /META-INF/batch-jobs/<jobName>.xml configuration and restart the job.void
setApplicationContext(org.springframework.context.ApplicationContext applicationContext)
void
setJobExplorer(JobExplorer jobExplorer)
void
setJobParametersConverter(JobParametersConverter converter)
Used to convert theProperties
objects used by JSR-352 to theJobParameters
objects used in Spring Batch.void
setJobRepository(JobRepository jobRepository)
void
setTaskExecutor(org.springframework.core.task.TaskExecutor taskExecutor)
void
setTransactionManager(org.springframework.transaction.PlatformTransactionManager transactionManager)
long
start(java.lang.String jobName, java.util.Properties params)
Creates a childApplicationContext
for the job being requested based upon the /META-INF/batch.xml (if exists) and the /META-INF/batch-jobs/<jobName>.xml configuration and launches the job.void
stop(long executionId)
Stops the running job execution if it is currently running.
Constructor Detail
JsrJobOperator
public JsrJobOperator()
Public constructor used byBatchRuntime.getJobOperator()
. This will bootstrap a singleton ApplicationContext if one has not already been created (and will utilize the existing one if it has) to populate itself.
JsrJobOperator
public JsrJobOperator(JobExplorer jobExplorer, JobRepository jobRepository, JobParametersConverter jobParametersConverter, org.springframework.transaction.PlatformTransactionManager transactionManager)
The no-arg constructor is used by theBatchRuntime.getJobOperator()
and so bootstraps anApplicationContext
. This constructor does not and is therefore dependency injection friendly. Also useful for unit testing.- Parameters:
jobExplorer
- an instance of Spring Batch'sJobExplorer
.jobRepository
- an instance of Spring Batch'sJobOperator
.jobParametersConverter
- an instance of Spring Batch'sJobParametersConverter
.transactionManager
- aTransactionManager
.
Method Detail
setJobExplorer
public void setJobExplorer(JobExplorer jobExplorer)
setJobRepository
public void setJobRepository(JobRepository jobRepository)
setTransactionManager
public void setTransactionManager(org.springframework.transaction.PlatformTransactionManager transactionManager)
setTaskExecutor
public void setTaskExecutor(org.springframework.core.task.TaskExecutor taskExecutor)
getTaskExecutor
protected org.springframework.core.task.TaskExecutor getTaskExecutor()
afterPropertiesSet
public void afterPropertiesSet() throws java.lang.Exception
- Specified by:
afterPropertiesSet
in interfaceorg.springframework.beans.factory.InitializingBean
- Throws:
java.lang.Exception
setJobParametersConverter
public void setJobParametersConverter(JobParametersConverter converter)
Used to convert theProperties
objects used by JSR-352 to theJobParameters
objects used in Spring Batch. The default implementation used will configure all parameters to be non-identifying (per the JSR).- Parameters:
converter
- AConverter
implementation used to convertProperties
toJobParameters
abandon
public void abandon(long jobExecutionId) throws javax.batch.operations.NoSuchJobExecutionException, javax.batch.operations.JobExecutionIsRunningException, javax.batch.operations.JobSecurityException
- Specified by:
abandon
in interfacejavax.batch.operations.JobOperator
- Throws:
javax.batch.operations.NoSuchJobExecutionException
javax.batch.operations.JobExecutionIsRunningException
javax.batch.operations.JobSecurityException
getJobExecution
public javax.batch.runtime.JobExecution getJobExecution(long executionId) throws javax.batch.operations.NoSuchJobExecutionException, javax.batch.operations.JobSecurityException
- Specified by:
getJobExecution
in interfacejavax.batch.operations.JobOperator
- Throws:
javax.batch.operations.NoSuchJobExecutionException
javax.batch.operations.JobSecurityException
getJobExecutions
public java.util.List<javax.batch.runtime.JobExecution> getJobExecutions(javax.batch.runtime.JobInstance jobInstance) throws javax.batch.operations.NoSuchJobInstanceException, javax.batch.operations.JobSecurityException
- Specified by:
getJobExecutions
in interfacejavax.batch.operations.JobOperator
- Throws:
javax.batch.operations.NoSuchJobInstanceException
javax.batch.operations.JobSecurityException
getJobInstance
public javax.batch.runtime.JobInstance getJobInstance(long executionId) throws javax.batch.operations.NoSuchJobExecutionException, javax.batch.operations.JobSecurityException
- Specified by:
getJobInstance
in interfacejavax.batch.operations.JobOperator
- Throws:
javax.batch.operations.NoSuchJobExecutionException
javax.batch.operations.JobSecurityException
getJobInstanceCount
public int getJobInstanceCount(java.lang.String jobName) throws javax.batch.operations.NoSuchJobException, javax.batch.operations.JobSecurityException
- Specified by:
getJobInstanceCount
in interfacejavax.batch.operations.JobOperator
- Throws:
javax.batch.operations.NoSuchJobException
javax.batch.operations.JobSecurityException
getJobInstances
public java.util.List<javax.batch.runtime.JobInstance> getJobInstances(java.lang.String jobName, int start, int count) throws javax.batch.operations.NoSuchJobException, javax.batch.operations.JobSecurityException
- Specified by:
getJobInstances
in interfacejavax.batch.operations.JobOperator
- Throws:
javax.batch.operations.NoSuchJobException
javax.batch.operations.JobSecurityException
getJobNames
public java.util.Set<java.lang.String> getJobNames() throws javax.batch.operations.JobSecurityException
- Specified by:
getJobNames
in interfacejavax.batch.operations.JobOperator
- Throws:
javax.batch.operations.JobSecurityException
getParameters
public java.util.Properties getParameters(long executionId) throws javax.batch.operations.NoSuchJobExecutionException, javax.batch.operations.JobSecurityException
- Specified by:
getParameters
in interfacejavax.batch.operations.JobOperator
- Throws:
javax.batch.operations.NoSuchJobExecutionException
javax.batch.operations.JobSecurityException
getRunningExecutions
public java.util.List<java.lang.Long> getRunningExecutions(java.lang.String name) throws javax.batch.operations.NoSuchJobException, javax.batch.operations.JobSecurityException
- Specified by:
getRunningExecutions
in interfacejavax.batch.operations.JobOperator
- Throws:
javax.batch.operations.NoSuchJobException
javax.batch.operations.JobSecurityException
getStepExecutions
public java.util.List<javax.batch.runtime.StepExecution> getStepExecutions(long executionId) throws javax.batch.operations.NoSuchJobExecutionException, javax.batch.operations.JobSecurityException
- Specified by:
getStepExecutions
in interfacejavax.batch.operations.JobOperator
- Throws:
javax.batch.operations.NoSuchJobExecutionException
javax.batch.operations.JobSecurityException
restart
public long restart(long executionId, java.util.Properties params) throws javax.batch.operations.JobExecutionAlreadyCompleteException, javax.batch.operations.NoSuchJobExecutionException, javax.batch.operations.JobExecutionNotMostRecentException, javax.batch.operations.JobRestartException, javax.batch.operations.JobSecurityException
Creates a childApplicationContext
for the job being requested based upon the /META-INF/batch.xml (if exists) and the /META-INF/batch-jobs/<jobName>.xml configuration and restart the job.- Specified by:
restart
in interfacejavax.batch.operations.JobOperator
- Parameters:
executionId
- the database id of the job execution to be restarted.params
- any job parameters to be used during the execution of this job.- Throws:
javax.batch.operations.JobExecutionAlreadyCompleteException
- thrown if the requested job execution has a status of COMPLETEjavax.batch.operations.NoSuchJobExecutionException
- throw if the requested job execution does not exist in the repositoryjavax.batch.operations.JobExecutionNotMostRecentException
- thrown if the requested job execution is not the most recent attempt for the job instance it's related to.javax.batch.operations.JobRestartException
- thrown for any general errors during the job restart processjavax.batch.operations.JobSecurityException
getJobRestartProperties
protected java.util.Properties getJobRestartProperties(java.util.Properties params, JobExecution previousJobExecution)
start
public long start(java.lang.String jobName, java.util.Properties params) throws javax.batch.operations.JobStartException, javax.batch.operations.JobSecurityException
Creates a childApplicationContext
for the job being requested based upon the /META-INF/batch.xml (if exists) and the /META-INF/batch-jobs/<jobName>.xml configuration and launches the job. Per JSR-352, calls to this method will always create a newJobInstance
(and relatedJobExecution
).- Specified by:
start
in interfacejavax.batch.operations.JobOperator
- Parameters:
jobName
- the name of the job XML file without the .xml that is located within the /META-INF/batch-jobs directory.params
- any job parameters to be used during the execution of this job.- Throws:
javax.batch.operations.JobStartException
javax.batch.operations.JobSecurityException
stop
public void stop(long executionId) throws javax.batch.operations.NoSuchJobExecutionException, javax.batch.operations.JobExecutionNotRunningException, javax.batch.operations.JobSecurityException
Stops the running job execution if it is currently running.- Specified by:
stop
in interfacejavax.batch.operations.JobOperator
- Parameters:
executionId
- the database id for theJobExecution
to be stopped.- Throws:
javax.batch.operations.NoSuchJobExecutionException
- thrown ifJobExecution
instance does not exist.javax.batch.operations.JobExecutionNotRunningException
- thrown ifJobExecution
is not running.javax.batch.operations.JobSecurityException
setApplicationContext
public void setApplicationContext(org.springframework.context.ApplicationContext applicationContext) throws org.springframework.beans.BeansException
- Specified by:
setApplicationContext
in interfaceorg.springframework.context.ApplicationContextAware
- Throws:
org.springframework.beans.BeansException