RESOLVED - issue BATCH-1483: Make the JobParametersValidator instance accessible from the Job interface
This commit is contained in:
@@ -15,6 +15,8 @@
|
||||
*/
|
||||
package org.springframework.batch.core;
|
||||
|
||||
import org.springframework.batch.core.job.JobParametersValidator;
|
||||
|
||||
/**
|
||||
* Batch domain object representing a job. Job is an explicit abstraction
|
||||
* representing the configuration of a job specified by a developer. It should
|
||||
@@ -34,7 +36,7 @@ public interface Job {
|
||||
* @return true if this job can be restarted after a failure
|
||||
*/
|
||||
boolean isRestartable();
|
||||
|
||||
|
||||
/**
|
||||
* Run the {@link JobExecution} and update the meta information like status
|
||||
* and statistics as necessary. This method should not throw any exceptions
|
||||
@@ -54,4 +56,14 @@ public interface Job {
|
||||
*/
|
||||
JobParametersIncrementer getJobParametersIncrementer();
|
||||
|
||||
/**
|
||||
* A validator for the job parameters of a {@link JobExecution}. Clients of
|
||||
* a Job may need to validate the parameters for a launch, before or during
|
||||
* the execution.
|
||||
*
|
||||
* @return a validator that can be used to check parameter values (never
|
||||
* null)
|
||||
*/
|
||||
JobParametersValidator getJobParametersValidator();
|
||||
|
||||
}
|
||||
@@ -18,6 +18,7 @@ package org.springframework.batch.core.configuration.support;
|
||||
import org.springframework.batch.core.Job;
|
||||
import org.springframework.batch.core.JobExecution;
|
||||
import org.springframework.batch.core.JobParametersIncrementer;
|
||||
import org.springframework.batch.core.job.JobParametersValidator;
|
||||
|
||||
/**
|
||||
* A {@link Job} that can optionally prepend a group name to another job's name,
|
||||
@@ -87,6 +88,10 @@ public class GroupAwareJob implements Job {
|
||||
return delegate.getJobParametersIncrementer();
|
||||
}
|
||||
|
||||
public JobParametersValidator getJobParametersValidator() {
|
||||
return delegate.getJobParametersValidator();
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
|
||||
@@ -157,6 +157,10 @@ public abstract class AbstractJob implements Job, StepLocator, BeanNameAware, In
|
||||
* @return the step names
|
||||
*/
|
||||
public abstract Collection<String> getStepNames();
|
||||
|
||||
public JobParametersValidator getJobParametersValidator() {
|
||||
return jobParametersValidator;
|
||||
}
|
||||
|
||||
/**
|
||||
* Boolean flag to prevent categorically a job from restarting, even if it
|
||||
|
||||
@@ -18,9 +18,32 @@ import org.springframework.util.Assert;
|
||||
*/
|
||||
public class DefaultJobParametersValidator implements JobParametersValidator, InitializingBean {
|
||||
|
||||
private Collection<String> requiredKeys = new HashSet<String>();
|
||||
private Collection<String> requiredKeys;
|
||||
|
||||
private Collection<String> optionalKeys = new HashSet<String>();
|
||||
private Collection<String> optionalKeys;
|
||||
|
||||
/**
|
||||
* Convenient default constructor for unconstrained validation.
|
||||
*/
|
||||
public DefaultJobParametersValidator() {
|
||||
this(new String[0], new String[0]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new validator with the required and optional job parameter keys
|
||||
* provided.
|
||||
*
|
||||
* @see DefaultJobParametersValidator#setOptionalKeys(String[])
|
||||
* @see DefaultJobParametersValidator#setRequiredKeys(String[])
|
||||
*
|
||||
* @param requiredKeys the required keys
|
||||
* @param optionalKeys the optional keys
|
||||
*/
|
||||
public DefaultJobParametersValidator(String[] requiredKeys, String[] optionalKeys) {
|
||||
super();
|
||||
setRequiredKeys(requiredKeys);
|
||||
setOptionalKeys(optionalKeys);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check that there are no overlaps between required and optional keys.
|
||||
|
||||
@@ -52,7 +52,10 @@ import org.springframework.util.Assert;
|
||||
* Repository can reliably recreate it.
|
||||
*
|
||||
* @author Lucas Ward
|
||||
* @Author Dave Syer
|
||||
*
|
||||
* @since 1.0
|
||||
*
|
||||
* @see JobRepository
|
||||
* @see TaskExecutor
|
||||
*/
|
||||
@@ -93,21 +96,20 @@ public class SimpleJobLauncher implements JobLauncher, InitializingBean {
|
||||
if (!job.isRestartable()) {
|
||||
throw new JobRestartException("JobInstance already exists and is not restartable");
|
||||
}
|
||||
else {
|
||||
/*
|
||||
* There is a very small probability that a non-restartable job
|
||||
* can be restarted, but only if another process or thread
|
||||
* manages to launch <i>and</i> fail a job execution for this
|
||||
* instance between the last assertion and the next method
|
||||
* returning successfully.
|
||||
*/
|
||||
jobExecution = jobRepository.createJobExecution(job.getName(), jobParameters);
|
||||
}
|
||||
}
|
||||
else {
|
||||
jobExecution = jobRepository.createJobExecution(job.getName(), jobParameters);
|
||||
}
|
||||
|
||||
// Check the validity of the parameters before doing creating anything
|
||||
// in the repository...
|
||||
job.getJobParametersValidator().validate(jobParameters);
|
||||
|
||||
/*
|
||||
* There is a very small probability that a non-restartable job can be
|
||||
* restarted, but only if another process or thread manages to launch
|
||||
* <i>and</i> fail a job execution for this instance between the last
|
||||
* assertion and the next method returning successfully.
|
||||
*/
|
||||
jobExecution = jobRepository.createJobExecution(job.getName(), jobParameters);
|
||||
|
||||
try {
|
||||
taskExecutor.execute(new Runnable() {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user