SimpleStepExecutionSplitter splitter = new SimpleStepExecutionSplitter(); splitter.setPartitioner(partitioner); splitter.setJobRepository(getJobRepository()); splitter.setAllowStartIfComplete(allowStartIfComplete); splitter.setStepName(name); this.splitter = splitter; step.setStepExecutionSplitter(splitter);
/** * @see StepExecutionSplitter#split(StepExecution, int) */ @Override public Set<StepExecution> split(StepExecution stepExecution, int gridSize) throws JobExecutionException { JobExecution jobExecution = stepExecution.getJobExecution(); Map<String, ExecutionContext> contexts = getContexts(stepExecution, gridSize); Set<StepExecution> set = new HashSet<>(contexts.size()); for (Entry<String, ExecutionContext> context : contexts.entrySet()) { // Make the step execution name unique and repeatable String stepName = this.stepName + STEP_NAME_SEPARATOR + context.getKey(); StepExecution currentStepExecution = jobExecution.createStepExecution(stepName); boolean startable = isStartable(currentStepExecution, context.getValue()); if (startable) { set.add(currentStepExecution); } } jobRepository.addAll(set); Set<StepExecution> executions = new HashSet<>(set.size()); executions.addAll(set); return executions; }
/** * Check if a step execution is startable. * @param stepExecution the step execution to check * @param context the execution context of the step * @return true if the step execution is startable, false otherwise * @throws JobExecutionException if unable to check if the step execution is startable */ protected boolean isStartable(StepExecution stepExecution, ExecutionContext context) throws JobExecutionException { return getStartable(stepExecution, context); }
@Test public void testSimpleStepExecutionProviderJobRepositoryStepPartitioner() throws Exception { final Map<String, ExecutionContext> map = Collections.singletonMap("foo", new ExecutionContext()); SimpleStepExecutionSplitter splitter = new SimpleStepExecutionSplitter(jobRepository, true, step.getName(), new Partitioner() { @Override public Map<String, ExecutionContext> partition(int gridSize) { return map; } }); assertEquals(1, splitter.split(stepExecution, 2).size()); }
@Test public void testGetStepName() { SimpleStepExecutionSplitter provider = new SimpleStepExecutionSplitter(jobRepository, true, step.getName(), new SimplePartitioner()); assertEquals("step", provider.getStepName()); }
@Test public void testVanillaStepExecution() throws Exception { step.setStepExecutionSplitter(new SimpleStepExecutionSplitter(jobRepository, true, step.getName(), new SimplePartitioner())); step.setPartitionHandler(new PartitionHandler() { @Override public Collection<StepExecution> handle(StepExecutionSplitter stepSplitter, StepExecution stepExecution) throws Exception { Set<StepExecution> executions = stepSplitter.split(stepExecution, 2); for (StepExecution execution : executions) { execution.setStatus(BatchStatus.COMPLETED); execution.setExitStatus(ExitStatus.COMPLETED); } return executions; } }); step.afterPropertiesSet(); JobExecution jobExecution = jobRepository.createJobExecution("vanillaJob", new JobParameters()); StepExecution stepExecution = jobExecution.createStepExecution("foo"); jobRepository.add(stepExecution); step.execute(stepExecution); // one master and two workers assertEquals(3, stepExecution.getJobExecution().getStepExecutions().size()); assertEquals(BatchStatus.COMPLETED, stepExecution.getStatus()); }
/** * @see StepExecutionSplitter#split(StepExecution, int) */ public Set<StepExecution> split(StepExecution stepExecution, int gridSize) throws JobExecutionException { JobExecution jobExecution = stepExecution.getJobExecution(); Map<String, ExecutionContext> contexts = getContexts(stepExecution, gridSize); Set<StepExecution> set = new HashSet<StepExecution>(contexts.size()); for (Entry<String, ExecutionContext> context : contexts.entrySet()) { // Make the step execution name unique and repeatable String stepName = this.stepName + STEP_NAME_SEPARATOR + context.getKey(); StepExecution currentStepExecution = jobExecution.createStepExecution(stepName); boolean startable = getStartable(currentStepExecution, context.getValue()); if (startable) { jobRepository.add(currentStepExecution); set.add(currentStepExecution); } } return set; }
/** * Check if a step execution is startable. * @param stepExecution the step execution to check * @param context the execution context of the step * @return true if the step execution is startable, false otherwise * @throws JobExecutionException if unable to check if the step execution is startable * @deprecated This method is deprecated in favor of * {@link SimpleStepExecutionSplitter#isStartable} and will be removed in a * future version. */ @Deprecated protected boolean getStartable(StepExecution stepExecution, ExecutionContext context) throws JobExecutionException { JobInstance jobInstance = stepExecution.getJobExecution().getJobInstance(); String stepName = stepExecution.getStepName(); StepExecution lastStepExecution = jobRepository.getLastStepExecution(jobInstance, stepName); boolean isRestart = (lastStepExecution != null && lastStepExecution.getStatus() != BatchStatus.COMPLETED); if (isRestart) { stepExecution.setExecutionContext(lastStepExecution.getExecutionContext()); } else { stepExecution.setExecutionContext(context); } return shouldStart(allowStartIfComplete, stepExecution, lastStepExecution) || isRestart; }
if (isSameJobExecution(stepExecution, lastStepExecution)) {
@Test public void testCompleteStatusSameJobExecution() throws Exception { SimpleStepExecutionSplitter provider = new SimpleStepExecutionSplitter(jobRepository, false, step.getName(), new SimplePartitioner()); Set<StepExecution> split = provider.split(stepExecution, 2); assertEquals(2, split.size()); stepExecution = update(split, stepExecution, BatchStatus.COMPLETED); // If already complete in the same JobExecution we should execute again assertEquals(2, provider.split(stepExecution, 2).size()); }
@Test public void testFailedStepExecution() throws Exception { step.setStepExecutionSplitter(new SimpleStepExecutionSplitter(jobRepository, true, step.getName(), new SimplePartitioner())); step.setPartitionHandler(new PartitionHandler() { @Override public Collection<StepExecution> handle(StepExecutionSplitter stepSplitter, StepExecution stepExecution) throws Exception { Set<StepExecution> executions = stepSplitter.split(stepExecution, 2); for (StepExecution execution : executions) { execution.setStatus(BatchStatus.FAILED); execution.setExitStatus(ExitStatus.FAILED); } return executions; } }); step.afterPropertiesSet(); JobExecution jobExecution = jobRepository.createJobExecution("vanillaJob", new JobParameters()); StepExecution stepExecution = jobExecution.createStepExecution("foo"); jobRepository.add(stepExecution); step.execute(stepExecution); // one master and two workers assertEquals(3, stepExecution.getJobExecution().getStepExecutions().size()); assertEquals(BatchStatus.FAILED, stepExecution.getStatus()); }
/** * @see StepExecutionSplitter#split(StepExecution, int) */ @Override public Set<StepExecution> split(StepExecution stepExecution, int gridSize) throws JobExecutionException { JobExecution jobExecution = stepExecution.getJobExecution(); Map<String, ExecutionContext> contexts = getContexts(stepExecution, gridSize); Set<StepExecution> set = new HashSet<StepExecution>(contexts.size()); for (Entry<String, ExecutionContext> context : contexts.entrySet()) { // Make the step execution name unique and repeatable String stepName = this.stepName + STEP_NAME_SEPARATOR + context.getKey(); StepExecution currentStepExecution = jobExecution.createStepExecution(stepName); boolean startable = getStartable(currentStepExecution, context.getValue()); if (startable) { set.add(currentStepExecution); } } jobRepository.addAll(set); return set; }
/** * Check if a step execution is startable. * @param stepExecution the step execution to check * @param context the execution context of the step * @return true if the step execution is startable, false otherwise * @throws JobExecutionException if unable to check if the step execution is startable * @deprecated This method is deprecated in favor of * {@link SimpleStepExecutionSplitter#isStartable} and will be removed in a * future version. */ @Deprecated protected boolean getStartable(StepExecution stepExecution, ExecutionContext context) throws JobExecutionException { JobInstance jobInstance = stepExecution.getJobExecution().getJobInstance(); String stepName = stepExecution.getStepName(); StepExecution lastStepExecution = jobRepository.getLastStepExecution(jobInstance, stepName); boolean isRestart = (lastStepExecution != null && lastStepExecution.getStatus() != BatchStatus.COMPLETED); if (isRestart) { stepExecution.setExecutionContext(lastStepExecution.getExecutionContext()); } else { stepExecution.setExecutionContext(context); } return shouldStart(allowStartIfComplete, stepExecution, lastStepExecution) || isRestart; }
if (isSameJobExecution(stepExecution, lastStepExecution)) {
SimpleStepExecutionSplitter splitter = new SimpleStepExecutionSplitter(); splitter.setPartitioner(getPartitioner()); splitter.setJobRepository(getJobRepository()); splitter.setAllowStartIfComplete(allowStartIfComplete); splitter.setStepName(name); splitter(splitter); step.setStepExecutionSplitter(splitter);
@Test public void testCompleteStatusAfterFailure() throws Exception { SimpleStepExecutionSplitter provider = new SimpleStepExecutionSplitter(jobRepository, false, step.getName(), new SimplePartitioner()); Set<StepExecution> split = provider.split(stepExecution, 2); assertEquals(2, split.size()); StepExecution nextExecution = update(split, stepExecution, BatchStatus.COMPLETED, false); // If already complete in another JobExecution we don't execute again assertEquals(0, provider.split(nextExecution, 2).size()); }
@Test public void testStoppedStepExecution() throws Exception { step.setStepExecutionSplitter(new SimpleStepExecutionSplitter(jobRepository, true, step.getName(), new SimplePartitioner())); step.setPartitionHandler(new PartitionHandler() { @Override public Collection<StepExecution> handle(StepExecutionSplitter stepSplitter, StepExecution stepExecution) throws Exception { Set<StepExecution> executions = stepSplitter.split(stepExecution, 2); for (StepExecution execution : executions) { execution.setStatus(BatchStatus.STOPPED); execution.setExitStatus(ExitStatus.STOPPED); } return executions; } }); step.afterPropertiesSet(); JobExecution jobExecution = jobRepository.createJobExecution("vanillaJob", new JobParameters()); StepExecution stepExecution = jobExecution.createStepExecution("foo"); jobRepository.add(stepExecution); step.execute(stepExecution); // one master and two workers assertEquals(3, stepExecution.getJobExecution().getStepExecutions().size()); assertEquals(BatchStatus.STOPPED, stepExecution.getStatus()); }
/** * @see StepExecutionSplitter#split(StepExecution, int) */ @Override public Set<StepExecution> split(StepExecution stepExecution, int gridSize) throws JobExecutionException { JobExecution jobExecution = stepExecution.getJobExecution(); Map<String, ExecutionContext> contexts = getContexts(stepExecution, gridSize); Set<StepExecution> set = new HashSet<StepExecution>(contexts.size()); for (Entry<String, ExecutionContext> context : contexts.entrySet()) { // Make the step execution name unique and repeatable String stepName = this.stepName + STEP_NAME_SEPARATOR + context.getKey(); StepExecution currentStepExecution = jobExecution.createStepExecution(stepName); boolean startable = isStartable(currentStepExecution, context.getValue()); if (startable) { set.add(currentStepExecution); } } jobRepository.addAll(set); Set<StepExecution> executions = new HashSet<StepExecution>(set.size()); executions.addAll(set); return executions; }
/** * Check if a step execution is startable. * @param stepExecution the step execution to check * @param context the execution context of the step * @return true if the step execution is startable, false otherwise * @throws JobExecutionException if unable to check if the step execution is startable * @deprecated This method is deprecated in favor of * {@link SimpleStepExecutionSplitter#isStartable} and will be removed in a * future version. */ @Deprecated protected boolean getStartable(StepExecution stepExecution, ExecutionContext context) throws JobExecutionException { JobInstance jobInstance = stepExecution.getJobExecution().getJobInstance(); String stepName = stepExecution.getStepName(); StepExecution lastStepExecution = jobRepository.getLastStepExecution(jobInstance, stepName); boolean isRestart = (lastStepExecution != null && lastStepExecution.getStatus() != BatchStatus.COMPLETED); if (isRestart) { stepExecution.setExecutionContext(lastStepExecution.getExecutionContext()); } else { stepExecution.setExecutionContext(context); } return shouldStart(allowStartIfComplete, stepExecution, lastStepExecution) || isRestart; }
/** * Check if a step execution is startable. * @param stepExecution the step execution to check * @param context the execution context of the step * @return true if the step execution is startable, false otherwise * @throws JobExecutionException if unable to check if the step execution is startable */ protected boolean isStartable(StepExecution stepExecution, ExecutionContext context) throws JobExecutionException { return getStartable(stepExecution, context); }