@Test public void testUnexpectedRollback() throws Exception { taskletStep.setTransactionManager( new ResourcelessTransactionManager() { @Override protected void doCommit(DefaultTransactionStatus status) throws TransactionException { super.doRollback(status); throw new UnexpectedRollbackException("bar"); } }); taskletStep.setTasklet( new Tasklet() { public RepeatStatus execute(StepContribution contribution, ChunkContext attributes) throws Exception { attributes .getStepContext() .getStepExecution() .getExecutionContext() .putString("foo", "bar"); return RepeatStatus.FINISHED; } }); taskletStep.execute(stepExecution); assertEquals(FAILED, stepExecution.getStatus()); Throwable e = stepExecution.getFailureExceptions().get(0); assertEquals("bar", e.getMessage()); assertEquals(0, stepExecution.getCommitCount()); assertEquals(1, stepExecution.getRollbackCount()); // Failed transaction counts as rollback assertEquals(0, stepExecution.getExecutionContext().size()); }
/** * Test method for {@link * org.springframework.batch.core.step.AbstractStep#execute(org.springframework.batch.core.StepExecution)} * . */ @Test public void testExecuteSunnyDay() throws Exception { step.setJob( new JobSupport("child") { @Override public void execute(JobExecution execution) throws UnexpectedJobExecutionException { execution.setStatus(BatchStatus.COMPLETED); execution.setEndTime(new Date()); } }); step.afterPropertiesSet(); step.execute(stepExecution); assertEquals(BatchStatus.COMPLETED, stepExecution.getStatus()); assertTrue( "Missing job parameters in execution context: " + stepExecution.getExecutionContext(), stepExecution .getExecutionContext() .containsKey(JobStep.class.getName() + ".JOB_PARAMETERS")); }
@Override public A23 process(A23 item) throws Exception { ExecutionContext stepContext = stepExecution.getExecutionContext(); long processCount = stepContext.getLong("PROCESS_COUNT", 0); processCount++; stepContext.putLong("PROCESS_COUNT", processCount); if (processCount % writeSampleFrequency == 1) { item.setSample(true); } return item; }
@Test public void testExecuteRestart() throws Exception { DefaultJobParametersExtractor jobParametersExtractor = new DefaultJobParametersExtractor(); jobParametersExtractor.setKeys(new String[] {"foo"}); ExecutionContext executionContext = stepExecution.getExecutionContext(); executionContext.put("foo", "bar"); step.setJobParametersExtractor(jobParametersExtractor); step.setJob( new JobSupport("child") { @Override public void execute(JobExecution execution) throws UnexpectedJobExecutionException { assertEquals(1, execution.getJobParameters().getParameters().size()); execution.setStatus(BatchStatus.FAILED); execution.setEndTime(new Date()); jobRepository.update(execution); throw new RuntimeException("FOO"); } @Override public boolean isRestartable() { return true; } }); step.afterPropertiesSet(); step.execute(stepExecution); assertEquals("FOO", stepExecution.getFailureExceptions().get(0).getMessage()); JobExecution jobExecution = stepExecution.getJobExecution(); jobExecution.setEndTime(new Date()); jobRepository.update(jobExecution); jobExecution = jobRepository.createJobExecution("job", new JobParameters()); stepExecution = jobExecution.createStepExecution("step"); // In a restart the surrounding Job would set up the context like this... stepExecution.setExecutionContext(executionContext); jobRepository.add(stepExecution); step.execute(stepExecution); assertEquals("FOO", stepExecution.getFailureExceptions().get(0).getMessage()); }