protected void enhance(Step target) { if (target instanceof AbstractStep) { AbstractStep step = (AbstractStep) target; step.setJobRepository(properties.getJobRepository()); Boolean allowStartIfComplete = properties.allowStartIfComplete; if (allowStartIfComplete != null) { step.setAllowStartIfComplete(allowStartIfComplete); } step.setStartLimit(properties.startLimit); List<StepExecutionListener> listeners = properties.stepExecutionListeners; if (!listeners.isEmpty()) { step.setStepExecutionListeners(listeners.toArray(new StepExecutionListener[0])); } } if (target instanceof TaskletStep) { TaskletStep step = (TaskletStep) target; step.setTransactionManager(properties.transactionManager); } }
@Before public void onSetUp() throws Exception { step = new TaskletStep("stepName"); step.setJobRepository(jobRepository); step.setTransactionManager(transactionManager); // Only process one item: chunkOperations = new RepeatTemplate(); chunkOperations.setCompletionPolicy(new SimpleCompletionPolicy(1)); job = new JobSupport("FOO"); step.setTransactionManager(transactionManager); }
@Test public void testRepositoryErrorOnUpdateStepExecutionInTransactionRollbackFailed() throws Exception { taskletStep.setTasklet(new Tasklet() { @Override public RepeatStatus execute(StepContribution contribution, ChunkContext attributes) throws Exception { return RepeatStatus.FINISHED; } }); taskletStep.setTransactionManager(new FailingRollbackTransactionManager()); jobRepository.setFailOnUpdateStepExecution(1); jobRepository.setFailInTransaction(true); taskletStep.execute(stepExecution); assertEquals(UNKNOWN, stepExecution.getStatus()); Throwable e = stepExecution.getFailureExceptions().get(0); assertEquals("Expected exception in rollback", e.getMessage()); }
@Test public void testRepositoryErrorOnExecutionContextInTransactionRollbackFailed() throws Exception { taskletStep.setTasklet(new Tasklet() { @Override public RepeatStatus execute(StepContribution contribution, ChunkContext attributes) throws Exception { return RepeatStatus.FINISHED; } }); taskletStep.setTransactionManager(new FailingRollbackTransactionManager()); jobRepository.setFailOnUpdateExecutionContext(true); jobRepository.setFailInTransaction(true); taskletStep.execute(stepExecution); assertEquals(UNKNOWN, stepExecution.getStatus()); Throwable e = stepExecution.getFailureExceptions().get(0); assertEquals("Expected exception in rollback", e.getMessage()); }
@Test public void testCommitCount_Even() throws Exception { JobExecution jobExecutionContext = new JobExecution(jobInstance, jobParameters); step = getStep(new String[] { "foo", "bar", "spam", "eggs" }, 2); step.setTransactionManager(transactionManager); StepExecution stepExecution = new StepExecution(step.getName(), jobExecutionContext); step.execute(stepExecution); assertEquals(4, processed.size()); assertEquals(4, stepExecution.getReadCount()); assertEquals(4, stepExecution.getWriteCount()); assertEquals(3, stepExecution.getCommitCount()); //the empty chunk is the 3rd commit }
@SuppressWarnings("serial") @Test public void testStatusForCommitFailedException() throws Exception { step.setTransactionManager(new ResourcelessTransactionManager() { @Override protected void doCommit(DefaultTransactionStatus status) throws TransactionException { // Simulate failure on commit throw new RuntimeException("Foo"); } @Override protected void doRollback(DefaultTransactionStatus status) throws TransactionException { throw new RuntimeException("Bar"); } }); JobExecution jobExecutionContext = new JobExecution(jobInstance, jobParameters); StepExecution stepExecution = new StepExecution(step.getName(), jobExecutionContext); stepExecution.setExecutionContext(foobarEc); // step.setLastExecution(stepExecution); step.execute(stepExecution); assertEquals(BatchStatus.UNKNOWN, stepExecution.getStatus()); Throwable ex = stepExecution.getFailureExceptions().get(0); // The original rollback failed because of this one: assertEquals("Bar", ex.getMessage()); }
private TaskletStep getStep(String[] strings, int commitInterval) throws Exception { TaskletStep step = new TaskletStep("stepName"); // Only process one item: RepeatTemplate template = new RepeatTemplate(); template.setCompletionPolicy(new SimpleCompletionPolicy(commitInterval)); step.setTasklet(new TestingChunkOrientedTasklet<>(getReader(strings), itemWriter, template)); step.setJobRepository(new JobRepositorySupport()); step.setTransactionManager(transactionManager); return step; }
@Test public void testCommitCount_Uneven() throws Exception { JobExecution jobExecutionContext = new JobExecution(jobInstance, jobParameters); step = getStep(new String[] { "foo", "bar", "spam" }, 2); step.setTransactionManager(transactionManager); StepExecution stepExecution = new StepExecution(step.getName(), jobExecutionContext); step.execute(stepExecution); assertEquals(3, processed.size()); assertEquals(3, stepExecution.getReadCount()); assertEquals(3, stepExecution.getWriteCount()); assertEquals(2, stepExecution.getCommitCount()); }
@Before public void init() throws Exception { maxActive = dataSource.getMaxTotal(); maxIdle = dataSource.getMaxIdle(); // Force deadlock with batch waiting for DB pool and vice versa dataSource.setMaxTotal(1); dataSource.setMaxIdle(1); step = new TaskletStep("stepName"); step.setJobRepository(jobRepository); step.setTransactionManager(transactionManager); // Only process one item: chunkOperations = new RepeatTemplate(); chunkOperations.setCompletionPolicy(new SimpleCompletionPolicy(1)); job = new JobSupport("FOO"); TaskExecutorRepeatTemplate repeatTemplate = new TaskExecutorRepeatTemplate(); repeatTemplate.setThrottleLimit(2); repeatTemplate.setTaskExecutor(new SimpleAsyncTaskExecutor()); step.setStepOperations(repeatTemplate); step.setTransactionManager(transactionManager); }
@SuppressWarnings("serial") @Test public void testStatusForResetFailedException() throws Exception { ItemReader<String> itemReader = new ItemReader<String>() { @Override public String read() throws Exception { // Trigger a rollback throw new RuntimeException("Foo"); } }; step.setTasklet(new TestingChunkOrientedTasklet<>(itemReader, itemWriter)); step.setTransactionManager(new ResourcelessTransactionManager() { @Override protected void doRollback(DefaultTransactionStatus status) throws TransactionException { // Simulate failure on rollback when stream resets throw new RuntimeException("Bar"); } }); JobExecution jobExecutionContext = new JobExecution(jobInstance, jobParameters); StepExecution stepExecution = new StepExecution(step.getName(), jobExecutionContext); stepExecution.setExecutionContext(foobarEc); // step.setLastExecution(stepExecution); step.execute(stepExecution); assertEquals(BatchStatus.UNKNOWN, stepExecution.getStatus()); String msg = stepExecution.getExitStatus().getExitDescription(); assertTrue("Message does not contain ResetFailedException: " + msg, msg.contains("ResetFailedException")); // The original rollback was caused by this one: assertEquals("Bar", stepExecution.getFailureExceptions().get(0).getMessage()); }
@Before public void setUp() throws Exception { transactionManager = new ResourcelessTransactionManager(); RepeatTemplate template = new RepeatTemplate(); template.setCompletionPolicy(new SimpleCompletionPolicy(1)); step = getStep(new String[] { "foo", "bar", "spam" }); step.setStepOperations(template); job = new JobSupport("FOO"); jobInstance = new JobInstance(0L, job.getName()); jobParameters = new JobParameters(); step.setTransactionManager(transactionManager); }
step.setTransactionManager(repositoryFactory.getTransactionManager()); step.setJobRepository(jobRepository); step.afterPropertiesSet();
private void configureStep(TaskletStep step) throws JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException { this.step = step; JobSupport job = new JobSupport(); job.addStep(step); job.setBeanName("testJob"); jobExecution = jobRepository.createJobExecution(job.getName(), new JobParameters()); step.setJobRepository(jobRepository); step.setTransactionManager(new ResourcelessTransactionManager()); itemWriter = new ItemWriter<Object>() { @Override public void write(List<? extends Object> item) throws Exception { } }; stepExecution = new StepExecution(step.getName(), jobExecution); }
@Before public void init() { taskletStep = new TaskletStep(); taskletStep.setTasklet(new ExceptionTasklet()); jobRepository = new UpdateCountingJobRepository(); taskletStep.setJobRepository(jobRepository); taskletStep.setTransactionManager(new ResourcelessTransactionManager()); JobInstance jobInstance = new JobInstance(1L, "testJob"); JobExecution jobExecution = new JobExecution(jobInstance, new JobParameters()); stepExecution = new StepExecution("testStep", jobExecution); }
@SuppressWarnings("serial") @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() { @Override 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(2, stepExecution.getExecutionContext().size()); assertTrue(stepExecution.getExecutionContext().containsKey(Step.STEP_TYPE_KEY)); assertTrue(stepExecution.getExecutionContext().containsKey(TaskletStep.TASKLET_TYPE_KEY)); }
public void testCommitError() throws Exception { taskletStep.setTransactionManager(new ResourcelessTransactionManager() { @Override protected void doCommit(DefaultTransactionStatus status) throws TransactionException {
private void setUp() throws Exception { step = new TaskletStep("stepName"); ResourcelessTransactionManager transactionManager = new ResourcelessTransactionManager(); step.setTransactionManager(transactionManager); RepeatTemplate chunkTemplate = new RepeatTemplate(); chunkTemplate.setCompletionPolicy(new SimpleCompletionPolicy(2)); step.setTasklet(new TestingChunkOrientedTasklet<>(new ListItemReader<>(items), itemProcessor, itemWriter, chunkTemplate)); jobRepository = new JobRepositorySupport(); step.setJobRepository(jobRepository); TaskExecutorRepeatTemplate template = new TaskExecutorRepeatTemplate(); template.setThrottleLimit(throttleLimit); SimpleAsyncTaskExecutor taskExecutor = new SimpleAsyncTaskExecutor(); taskExecutor.setConcurrencyLimit(concurrencyLimit); template.setTaskExecutor(taskExecutor); step.setStepOperations(template); step.registerStream(new ItemStreamSupport() { private int count = 0; @Override public void update(ExecutionContext executionContext) { super.update(executionContext); executionContext.putInt("counter", count++); } }); }
protected void enhance(Step target) { if (target instanceof AbstractStep) { AbstractStep step = (AbstractStep) target; step.setJobRepository(properties.getJobRepository()); Boolean allowStartIfComplete = properties.allowStartIfComplete; if (allowStartIfComplete != null) { step.setAllowStartIfComplete(allowStartIfComplete); } step.setStartLimit(properties.startLimit); List<StepExecutionListener> listeners = properties.stepExecutionListeners; if (!listeners.isEmpty()) { step.setStepExecutionListeners(listeners.toArray(new StepExecutionListener[0])); } } if (target instanceof TaskletStep) { TaskletStep step = (TaskletStep) target; step.setTransactionManager(properties.transactionManager); } }
protected void enhance(Step target) { if (target instanceof AbstractStep) { AbstractStep step = (AbstractStep) target; step.setJobRepository(properties.getJobRepository()); Boolean allowStartIfComplete = properties.allowStartIfComplete; if (allowStartIfComplete != null) { step.setAllowStartIfComplete(allowStartIfComplete); } step.setStartLimit(properties.startLimit); List<StepExecutionListener> listeners = properties.stepExecutionListeners; if (!listeners.isEmpty()) { step.setStepExecutionListeners(listeners.toArray(new StepExecutionListener[0])); } } if (target instanceof TaskletStep) { TaskletStep step = (TaskletStep) target; step.setTransactionManager(properties.transactionManager); } }
protected void enhance(Step target) { if (target instanceof AbstractStep) { AbstractStep step = (AbstractStep) target; step.setJobRepository(properties.getJobRepository()); Boolean allowStartIfComplete = properties.allowStartIfComplete; if (allowStartIfComplete != null) { step.setAllowStartIfComplete(allowStartIfComplete); } step.setStartLimit(properties.startLimit); List<StepExecutionListener> listeners = properties.stepExecutionListeners; if (!listeners.isEmpty()) { step.setStepExecutionListeners(listeners.toArray(new StepExecutionListener[0])); } } if (target instanceof TaskletStep) { TaskletStep step = (TaskletStep) target; step.setTransactionManager(properties.transactionManager); } }