org.springframework.batch.core.repository.support
Class SimpleJobRepository

java.lang.Object
  extended by org.springframework.batch.core.repository.support.SimpleJobRepository
All Implemented Interfaces:
JobRepository

public class SimpleJobRepository
extends Object
implements JobRepository

Implementation of JobRepository that stores JobInstances, JobExecutions, and StepExecutions using the injected DAOs.

Author:
Lucas Ward, Dave Syer, Robert Kasanicky
See Also:
JobRepository, JobInstanceDao, JobExecutionDao, StepExecutionDao

Constructor Summary
SimpleJobRepository(JobInstanceDao jobInstanceDao, JobExecutionDao jobExecutionDao, StepExecutionDao stepExecutionDao)
           
 
Method Summary
 JobExecution createJobExecution(Job job, JobParameters jobParameters)
           Create a JobExecution based on the passed in Job and JobParameters.
 StepExecution getLastStepExecution(JobInstance jobInstance, Step step)
           
 int getStepExecutionCount(JobInstance jobInstance, Step step)
           
 void saveOrUpdate(JobExecution jobExecution)
          Save or Update a JobExecution.
 void saveOrUpdate(StepExecution stepExecution)
          Save or Update the given StepExecution.
 void saveOrUpdateExecutionContext(StepExecution stepExecution)
          Save the ExecutionContext of the given StepExecution.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SimpleJobRepository

public SimpleJobRepository(JobInstanceDao jobInstanceDao,
                           JobExecutionDao jobExecutionDao,
                           StepExecutionDao stepExecutionDao)
Method Detail

createJobExecution

public JobExecution createJobExecution(Job job,
                                       JobParameters jobParameters)
                                throws JobExecutionAlreadyRunningException,
                                       JobRestartException,
                                       JobInstanceAlreadyCompleteException

Create a JobExecution based on the passed in Job and JobParameters. However, unique identification of a job can only come from the database, and therefore must come from JobDao by either creating a new job instance or finding an existing one, which will ensure that the id of the job instance is populated with the correct value.

There are two ways in which the method determines if a job should be created or an existing one should be returned. The first is restartability. The Job restartable property will be checked first. If it is false, a new job will be created, regardless of whether or not one exists. If it is true, the JobInstanceDao will be checked to determine if the job already exists, if it does, it's steps will be populated (there must be at least 1) and a new JobExecution will be returned. If no job instance is found, a new one will be created.

A check is made to see if any job executions are already running, and an exception will be thrown if one is detected. To detect a running job execution we use the JobExecutionDao:

  1. First we find all jobs which match the given JobParameters and job name
  2. What happens then depends on how many existing job instances we find:
    • If there are none, or the Job is marked restartable, then we create a new JobInstance
    • If there is more than one and the Job is not marked as restartable, it is an error. This could be caused by a job whose restartable flag has changed to be more strict (true not false) after it has been executed at least once.
    • If there is precisely one existing JobInstance then we check the JobExecution instances for that job, and if any of them tells us it is running (see JobExecution.isRunning()) then it is an error.
If this method is run in a transaction (as it normally would be) with isolation level at Isolation.REPEATABLE_READ or better, then this method should block if another transaction is already executing it (for the same JobParameters and job name). The first transaction to complete in this scenario obtains a valid JobExecution, and others throw JobExecutionAlreadyRunningException (or timeout). There are no such guarantees if the JobInstanceDao and JobExecutionDao do not respect the transaction isolation levels (e.g. if using a non-relational data-store, or if the platform does not support the higher isolation levels).

Specified by:
createJobExecution in interface JobRepository
Parameters:
job - the job the execution should be associated with.
jobParameters - the runtime parameters for the job
Returns:
a valid job JobExecution for the arguments provided
Throws:
JobExecutionAlreadyRunningException - if there is a JobExecution already running for the job instance with the provided job and parameters.
JobRestartException - if one or more existing JobInstances is found with the same parameters and Job.isRestartable() is false.
JobInstanceAlreadyCompleteException - if a JobInstance is found and was already completed successfully.
See Also:
JobRepository.createJobExecution(Job, JobParameters)

saveOrUpdate

public void saveOrUpdate(JobExecution jobExecution)
Save or Update a JobExecution. A JobExecution is considered one 'execution' of a particular job. Therefore, it must have it's jobId field set before it is passed into this method. It also has it's own unique identifier, because it must be updatable separately. If an id isn't found, a new JobExecution is created, if one is found, the current row is updated.

Specified by:
saveOrUpdate in interface JobRepository
Parameters:
jobExecution - to be stored.
Throws:
IllegalArgumentException - if jobExecution is null.

saveOrUpdate

public void saveOrUpdate(StepExecution stepExecution)
Save or Update the given StepExecution. If it's id is null, it will be saved and an id will be set, otherwise it will be updated. It should be noted that assigning an ID randomly will likely cause an exception depending on the StepDao implementation.

Specified by:
saveOrUpdate in interface JobRepository
Parameters:
stepExecution - to be saved.
Throws:
IllegalArgumentException - if stepExecution is null.

saveOrUpdateExecutionContext

public void saveOrUpdateExecutionContext(StepExecution stepExecution)
Description copied from interface: JobRepository
Save the ExecutionContext of the given StepExecution. Implementations are allowed to ensure that the StepExecution is already saved by calling JobRepository.saveOrUpdate(StepExecution) before saving the ExecutionContext.

Specified by:
saveOrUpdateExecutionContext in interface JobRepository
Parameters:
stepExecution - the StepExecution containing the ExecutionContext to be saved.

getLastStepExecution

public StepExecution getLastStepExecution(JobInstance jobInstance,
                                          Step step)
Specified by:
getLastStepExecution in interface JobRepository
Returns:
the last execution of the step within given job instance

getStepExecutionCount

public int getStepExecutionCount(JobInstance jobInstance,
                                 Step step)
Specified by:
getStepExecutionCount in interface JobRepository
Returns:
number of executions of the step within given job instance


Copyright © 2009 SpringSource. All Rights Reserved.