RESOLVED - issue BATCH-110: Potential simplification of DefaultJobExecutor by not having to include the StepExecutorFactory always
http://opensource.atlassian.com/projects/spring/browse/BATCH-110
This commit is contained in:
@@ -33,13 +33,13 @@ import org.springframework.batch.core.executor.StepInterruptedException;
|
||||
import org.springframework.batch.core.repository.JobRepository;
|
||||
import org.springframework.batch.core.runtime.JobExecutionContext;
|
||||
import org.springframework.batch.core.runtime.StepExecutionContext;
|
||||
import org.springframework.batch.execution.step.DefaultStepExecutorFactory;
|
||||
import org.springframework.batch.execution.step.SimpleStepExecutorFactory;
|
||||
import org.springframework.batch.io.exception.BatchCriticalException;
|
||||
import org.springframework.batch.repeat.ExitStatus;
|
||||
import org.springframework.batch.repeat.RepeatContext;
|
||||
|
||||
/**
|
||||
* Default implementation of (@JobLifecycle) interface. Sequentially executes a
|
||||
* Default implementation of (@link JobExecutor} interface. Sequentially executes a
|
||||
* job by iterating it's life of steps. Interruption of a job run is pluggable
|
||||
* by passing in various interruption policies.
|
||||
*
|
||||
@@ -47,9 +47,11 @@ import org.springframework.batch.repeat.RepeatContext;
|
||||
*/
|
||||
public class DefaultJobExecutor implements JobExecutor {
|
||||
|
||||
private static final SimpleStepExecutorFactory DEFAULT_STEP_EXECUTOR_FACTORY = new SimpleStepExecutorFactory();
|
||||
|
||||
private JobRepository jobRepository;
|
||||
|
||||
private StepExecutorFactory stepExecutorResolver = new DefaultStepExecutorFactory();
|
||||
private StepExecutorFactory stepExecutorFactory = DEFAULT_STEP_EXECUTOR_FACTORY;
|
||||
|
||||
public ExitStatus run(JobConfiguration configuration, JobExecutionContext jobExecutionContext)
|
||||
throws BatchCriticalException {
|
||||
@@ -69,7 +71,7 @@ public class DefaultJobExecutor implements JobExecutor {
|
||||
StepConfiguration stepConfiguration = (StepConfiguration) j.next();
|
||||
if (shouldStart(step, stepConfiguration)) {
|
||||
updateStatus(jobExecutionContext, BatchStatus.STARTED);
|
||||
StepExecutor stepExecutor = stepExecutorResolver.getExecutor(stepConfiguration);
|
||||
StepExecutor stepExecutor = stepExecutorFactory.getExecutor(stepConfiguration);
|
||||
StepExecutionContext stepExecutionContext = new StepExecutionContext(jobExecutionContext, step);
|
||||
status = stepExecutor.process(stepConfiguration, stepExecutionContext);
|
||||
}
|
||||
@@ -145,10 +147,11 @@ public class DefaultJobExecutor implements JobExecutor {
|
||||
|
||||
public void setJobRepository(JobRepository jobRepository) {
|
||||
this.jobRepository = jobRepository;
|
||||
DEFAULT_STEP_EXECUTOR_FACTORY.setJobRepository(jobRepository);
|
||||
}
|
||||
|
||||
public void setStepExecutorResolver(StepExecutorFactory stepExecutorResolver) {
|
||||
this.stepExecutorResolver = stepExecutorResolver;
|
||||
public void setStepExecutorFactory(StepExecutorFactory stepExecutorResolver) {
|
||||
this.stepExecutorFactory = stepExecutorResolver;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,78 @@
|
||||
/*
|
||||
* Copyright 2006-2007 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.batch.execution.step;
|
||||
|
||||
import org.springframework.batch.core.configuration.StepConfiguration;
|
||||
import org.springframework.batch.core.executor.StepExecutor;
|
||||
import org.springframework.batch.core.executor.StepExecutorFactory;
|
||||
import org.springframework.batch.core.repository.JobRepository;
|
||||
import org.springframework.batch.execution.step.simple.SimpleStepConfiguration;
|
||||
import org.springframework.batch.execution.step.simple.SimpleStepExecutor;
|
||||
import org.springframework.batch.repeat.RepeatOperations;
|
||||
import org.springframework.batch.repeat.policy.SimpleCompletionPolicy;
|
||||
import org.springframework.batch.repeat.support.RepeatTemplate;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* A {@link StepExecutorFactory} that only knows how to create
|
||||
* {@link SimpleStepExecutor} instances.
|
||||
*
|
||||
* @author Dave Syer
|
||||
*
|
||||
*/
|
||||
public class SimpleStepExecutorFactory implements StepExecutorFactory {
|
||||
|
||||
private JobRepository jobRepository;
|
||||
|
||||
/**
|
||||
* Create a {@link SimpleStepExecutor} for this configuration. If the
|
||||
* configuration is a {@link SimpleStepConfiguration} then a
|
||||
* {@link StepExecutor} is created with policies matching the commit
|
||||
* interval of the configuration. <br/>
|
||||
*
|
||||
* @throws IllegalStateException
|
||||
* if the configuration is not a {@link SimpleStepConfiguration}.
|
||||
* @throws IllegalStateException
|
||||
* if the {@link JobRepository} is null.
|
||||
*
|
||||
* @see StepExecutorFactory#getExecutor(StepConfiguration)
|
||||
*/
|
||||
public StepExecutor getExecutor(StepConfiguration configuration) {
|
||||
|
||||
Assert.notNull(jobRepository, "JobRepository cannot be null");
|
||||
Assert.state(configuration instanceof SimpleStepConfiguration,
|
||||
"StepConfiguration must be instance of SimpleStepConfiguration - found: ["
|
||||
+ (configuration == null ? null : configuration
|
||||
.getClass()) + "]");
|
||||
|
||||
SimpleStepExecutor executor = new SimpleStepExecutor();
|
||||
executor.setRepository(jobRepository);
|
||||
RepeatTemplate template = new RepeatTemplate();
|
||||
RepeatOperations repeatOperations = template;
|
||||
template.setCompletionPolicy(new SimpleCompletionPolicy(
|
||||
((SimpleStepConfiguration) configuration).getCommitInterval()));
|
||||
|
||||
executor.setChunkOperations(repeatOperations);
|
||||
|
||||
return executor;
|
||||
|
||||
}
|
||||
|
||||
public void setJobRepository(JobRepository jobRepository) {
|
||||
this.jobRepository = jobRepository;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user