IN PROGRESS - issue BATCH-546: pull duplicates from TaskletStep and ItemOrientedStep into AbstractStep
pulled up jobRepository property
This commit is contained in:
@@ -21,6 +21,9 @@ import org.springframework.batch.core.StepExecution;
|
||||
import org.springframework.batch.core.StepExecutionListener;
|
||||
import org.springframework.batch.core.UnexpectedJobExecutionException;
|
||||
import org.springframework.batch.core.listener.CompositeStepExecutionListener;
|
||||
import org.springframework.batch.core.repository.JobRepository;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* A {@link Step} implementation that provides common behavior to subclasses,
|
||||
@@ -30,7 +33,7 @@ import org.springframework.batch.core.listener.CompositeStepExecutionListener;
|
||||
* @author Ben Hale
|
||||
* @author Robert Kasanicky
|
||||
*/
|
||||
public abstract class AbstractStep implements Step {
|
||||
public abstract class AbstractStep implements Step, InitializingBean {
|
||||
|
||||
protected String name;
|
||||
|
||||
@@ -39,6 +42,8 @@ public abstract class AbstractStep implements Step {
|
||||
protected boolean allowStartIfComplete;
|
||||
|
||||
private CompositeStepExecutionListener listener = new CompositeStepExecutionListener();
|
||||
|
||||
private JobRepository jobRepository;
|
||||
|
||||
/**
|
||||
* Default constructor.
|
||||
@@ -46,6 +51,12 @@ public abstract class AbstractStep implements Step {
|
||||
public AbstractStep() {
|
||||
super();
|
||||
}
|
||||
|
||||
public void afterPropertiesSet() throws Exception {
|
||||
Assert.notNull(jobRepository, "JobRepository is mandatory");
|
||||
}
|
||||
|
||||
|
||||
|
||||
public String getName() {
|
||||
return this.name;
|
||||
@@ -126,5 +137,20 @@ public abstract class AbstractStep implements Step {
|
||||
protected StepExecutionListener getCompositeListener() {
|
||||
return listener;
|
||||
}
|
||||
|
||||
/**
|
||||
* Public setter for {@link JobRepository}.
|
||||
*
|
||||
* @param jobRepository is a mandatory dependence (no default).
|
||||
*/
|
||||
public void setJobRepository(JobRepository jobRepository) {
|
||||
this.jobRepository = jobRepository;
|
||||
}
|
||||
|
||||
protected JobRepository getJobRepository() {
|
||||
return jobRepository;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -87,8 +87,6 @@ public class ItemOrientedStep extends AbstractStep {
|
||||
|
||||
private CompositeItemStream stream = new CompositeItemStream();
|
||||
|
||||
private JobRepository jobRepository;
|
||||
|
||||
private PlatformTransactionManager transactionManager;
|
||||
|
||||
private ItemHandler itemHandler;
|
||||
@@ -103,15 +101,6 @@ public class ItemOrientedStep extends AbstractStep {
|
||||
synchronizer = new StepExecutionSynchronizerFactory().getStepExecutionSynchronizer();
|
||||
}
|
||||
|
||||
/**
|
||||
* Public setter for {@link JobRepository}.
|
||||
*
|
||||
* @param jobRepository is a mandatory dependence (no default).
|
||||
*/
|
||||
public void setJobRepository(JobRepository jobRepository) {
|
||||
this.jobRepository = jobRepository;
|
||||
}
|
||||
|
||||
/**
|
||||
* Public setter for the {@link PlatformTransactionManager}.
|
||||
*
|
||||
@@ -251,7 +240,7 @@ public class ItemOrientedStep extends AbstractStep {
|
||||
getCompositeListener().beforeStep(stepExecution);
|
||||
stream.open(stepExecution.getExecutionContext());
|
||||
stream.update(stepExecution.getExecutionContext());
|
||||
jobRepository.saveOrUpdateExecutionContext(stepExecution);
|
||||
getJobRepository().saveOrUpdateExecutionContext(stepExecution);
|
||||
itemHandler.mark();
|
||||
|
||||
status = stepOperations.iterate(new RepeatCallback() {
|
||||
@@ -297,7 +286,7 @@ public class ItemOrientedStep extends AbstractStep {
|
||||
|
||||
stream.update(stepExecution.getExecutionContext());
|
||||
try {
|
||||
jobRepository.saveOrUpdateExecutionContext(stepExecution);
|
||||
getJobRepository().saveOrUpdateExecutionContext(stepExecution);
|
||||
}
|
||||
catch (Exception e) {
|
||||
fatalException.setException(e);
|
||||
@@ -365,7 +354,7 @@ public class ItemOrientedStep extends AbstractStep {
|
||||
stepExecution.setEndTime(new Date(System.currentTimeMillis()));
|
||||
|
||||
try {
|
||||
jobRepository.saveOrUpdate(stepExecution);
|
||||
getJobRepository().saveOrUpdate(stepExecution);
|
||||
}
|
||||
catch (RuntimeException e) {
|
||||
String msg = "Fatal error detected during final save of meta data";
|
||||
@@ -496,7 +485,7 @@ public class ItemOrientedStep extends AbstractStep {
|
||||
private Exception updateStatus(StepExecution stepExecution, BatchStatus status) {
|
||||
stepExecution.setStatus(status);
|
||||
try {
|
||||
jobRepository.saveOrUpdate(stepExecution);
|
||||
getJobRepository().saveOrUpdate(stepExecution);
|
||||
return null;
|
||||
}
|
||||
catch (Exception e) {
|
||||
|
||||
@@ -40,7 +40,7 @@ import org.springframework.util.Assert;
|
||||
* If the {@link Tasklet} itself implements {@link StepExecutionListener} it
|
||||
* will be registered automatically, but its injected dependencies will not be.
|
||||
* This is a good way to get access to job parameters and execution context if
|
||||
* the tasklet is parameterised.
|
||||
* the tasklet is parameterized.
|
||||
*
|
||||
* @author Ben Hale
|
||||
* @author Robert Kasanicky
|
||||
@@ -51,8 +51,6 @@ public class TaskletStep extends AbstractStep implements Step, InitializingBean,
|
||||
|
||||
private Tasklet tasklet;
|
||||
|
||||
private JobRepository jobRepository;
|
||||
|
||||
/**
|
||||
* 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
|
||||
@@ -83,7 +81,7 @@ public class TaskletStep extends AbstractStep implements Step, InitializingBean,
|
||||
* @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
|
||||
*/
|
||||
public void afterPropertiesSet() throws Exception {
|
||||
Assert.notNull(jobRepository, "JobRepository is mandatory for TaskletStep");
|
||||
super.afterPropertiesSet();
|
||||
Assert.notNull(tasklet, "Tasklet is mandatory for TaskletStep");
|
||||
if (tasklet instanceof StepExecutionListener) {
|
||||
registerStepExecutionListener((StepExecutionListener) tasklet);
|
||||
@@ -107,7 +105,7 @@ public class TaskletStep extends AbstractStep implements Step, InitializingBean,
|
||||
public TaskletStep(Tasklet tasklet, JobRepository jobRepository) {
|
||||
this();
|
||||
this.tasklet = tasklet;
|
||||
this.jobRepository = jobRepository;
|
||||
setJobRepository(jobRepository);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -118,17 +116,9 @@ public class TaskletStep extends AbstractStep implements Step, InitializingBean,
|
||||
this.tasklet = tasklet;
|
||||
}
|
||||
|
||||
/**
|
||||
* Public setter for the {@link JobRepository}.
|
||||
* @param jobRepository the {@link JobRepository} to set
|
||||
*/
|
||||
public void setJobRepository(JobRepository jobRepository) {
|
||||
this.jobRepository = jobRepository;
|
||||
}
|
||||
|
||||
public void execute(StepExecution stepExecution) throws JobInterruptedException, UnexpectedJobExecutionException {
|
||||
stepExecution.setStartTime(new Date());
|
||||
updateStatus(stepExecution, BatchStatus.STARTED);
|
||||
stepExecution.setStatus(BatchStatus.STARTED);
|
||||
|
||||
ExitStatus exitStatus = ExitStatus.FAILED;
|
||||
Exception fatalException = null;
|
||||
@@ -139,18 +129,18 @@ public class TaskletStep extends AbstractStep implements Step, InitializingBean,
|
||||
exitStatus = exitStatus.and(getCompositeListener().afterStep(stepExecution));
|
||||
|
||||
try {
|
||||
jobRepository.saveOrUpdateExecutionContext(stepExecution);
|
||||
updateStatus(stepExecution, BatchStatus.COMPLETED);
|
||||
getJobRepository().saveOrUpdateExecutionContext(stepExecution);
|
||||
stepExecution.setStatus(BatchStatus.COMPLETED);
|
||||
}
|
||||
catch (Exception e) {
|
||||
fatalException = e;
|
||||
updateStatus(stepExecution, BatchStatus.UNKNOWN);
|
||||
stepExecution.setStatus(BatchStatus.UNKNOWN);
|
||||
}
|
||||
|
||||
}
|
||||
catch (Exception e) {
|
||||
logger.error("Encountered an error running the tasklet");
|
||||
updateStatus(stepExecution, BatchStatus.FAILED);
|
||||
stepExecution.setStatus(BatchStatus.FAILED);
|
||||
try {
|
||||
exitStatus = exitStatus.and(getCompositeListener().onErrorInStep(stepExecution, e));
|
||||
}
|
||||
@@ -169,7 +159,7 @@ public class TaskletStep extends AbstractStep implements Step, InitializingBean,
|
||||
stepExecution.setExitStatus(exitStatus);
|
||||
stepExecution.setEndTime(new Date());
|
||||
try {
|
||||
jobRepository.saveOrUpdate(stepExecution);
|
||||
getJobRepository().saveOrUpdate(stepExecution);
|
||||
}
|
||||
catch (Exception e) {
|
||||
fatalException = e;
|
||||
@@ -184,8 +174,4 @@ public class TaskletStep extends AbstractStep implements Step, InitializingBean,
|
||||
|
||||
}
|
||||
|
||||
private void updateStatus(StepExecution stepExecution, BatchStatus status) {
|
||||
stepExecution.setStatus(status);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user