protected RepeatOperations createChunkOperations() { RepeatOperations repeatOperations = chunkOperations; if (repeatOperations == null) { RepeatTemplate repeatTemplate = new RepeatTemplate(); repeatTemplate.setCompletionPolicy(getChunkCompletionPolicy()); repeatOperations = repeatTemplate; } return repeatOperations; }
private RepeatOperations createRepeatOperations() { RepeatTemplate repeatOperations = new RepeatTemplate(); repeatOperations.setCompletionPolicy(getChunkCompletionPolicy()); repeatOperations.setExceptionHandler(getExceptionHandler()); return repeatOperations; } }
public RepeatTemplate getRepeatTemplate() { template = new RepeatTemplate(); // default stop after more items than exist in dataset template.setCompletionPolicy(new SimpleCompletionPolicy(8)); return template; }
public void testExecute() throws Exception { NestedRepeatCallback callback = new NestedRepeatCallback(new RepeatTemplate(), new RepeatCallback() { @Override public RepeatStatus doInIteration(RepeatContext context) throws Exception { count++; return RepeatStatus.continueIf(count <= 1); } }); RepeatStatus result = callback.doInIteration(null); assertEquals(2, count); assertFalse(result.isContinuable()); // False because processing has finished } }
@Test public void testReceiveAndExecuteWithCallbackReturningNull() throws Exception { RepeatTemplate template = new RepeatTemplate(); template.setCompletionPolicy(new SimpleCompletionPolicy(2)); container = getContainer(template); Session session = mock(Session.class); MessageConsumer consumer = mock(MessageConsumer.class); Message message = null; // Expect one call to consumer (chunk size is 2 but terminates on // first)... when(consumer.receive(1000)).thenReturn(message); when(session.getTransacted()).thenReturn(false); boolean received = doExecute(session, consumer); assertFalse("Message not received", received); }
@Test public void testProvide() throws Exception { provider = new SimpleChunkProvider<>(new ListItemReader<>(Arrays.asList("foo", "bar")), new RepeatTemplate()); Chunk<String> chunk = provider.provide(contribution); assertNotNull(chunk); assertEquals(2, chunk.getItems().size()); }
@Test public void testProvide() throws Exception { provider = new FaultTolerantChunkProvider<>(new ListItemReader<>(Arrays.asList("foo", "bar")), new RepeatTemplate()); Chunk<String> chunk = provider.provide(contribution); assertNotNull(chunk); assertEquals(2, chunk.getItems().size()); }
@Test public void testProvideWithOverflow() throws Exception { provider = new SimpleChunkProvider<String>(new ListItemReader<>(Arrays.asList("foo", "bar")), new RepeatTemplate()) { @Override protected String read(StepContribution contribution, Chunk<String> chunk) throws SkipOverflowException, Exception { chunk.skip(new RuntimeException("Planned")); throw new SkipOverflowException("Overflow"); } }; Chunk<String> chunk = null; chunk = provider.provide(contribution); assertNotNull(chunk); assertEquals(0, chunk.getItems().size()); assertEquals(1, chunk.getErrors().size()); }
@Test public void testProvideWithOverflow() throws Exception { provider = new FaultTolerantChunkProvider<>(new ItemReader<String>() { @Override public String read() throws Exception, UnexpectedInputException, ParseException { throw new RuntimeException("Planned"); } }, new RepeatTemplate()); provider.setSkipPolicy(new LimitCheckingItemSkipPolicy(Integer.MAX_VALUE, Collections.<Class<? extends Throwable>,Boolean>singletonMap(Exception.class, Boolean.TRUE))); provider.setMaxSkipsOnRead(10); Chunk<String> chunk = null; chunk = provider.provide(contribution); assertNotNull(chunk); assertEquals(0, chunk.getItems().size()); assertEquals(10, chunk.getErrors().size()); } }
@Test public void testEmptyReader() throws Exception { JobExecution jobExecutionContext = new JobExecution(jobInstance, jobParameters); StepExecution stepExecution = new StepExecution(step.getName(), jobExecutionContext); step = getStep(new String[0]); step.setTasklet(new TestingChunkOrientedTasklet<>(getReader(new String[0]), itemWriter, new RepeatTemplate())); step.setStepOperations(new RepeatTemplate()); step.execute(stepExecution); assertEquals(0, processed.size()); assertEquals(0, stepExecution.getReadCount()); // Commit after end of data detected (this leads to the commit count // being one greater than people expect if the commit interval is // commensurate with the total number of items).h assertEquals(1, stepExecution.getCommitCount()); }
@Test public void testNonTransactionalReceiveAndExecuteWithCallbackThrowingException() throws Exception { RepeatTemplate template = new RepeatTemplate(); template.setCompletionPolicy(new SimpleCompletionPolicy(2)); container = getContainer(template); container.setSessionTransacted(false); boolean received = doTestWithException(new IllegalStateException("No way!"), false, 2); assertTrue("Message not received but listener not transactional so this should be true", received); }
@Test public void testTransactionalReceiveAndExecuteWithCallbackThrowingException() throws Exception { RepeatTemplate template = new RepeatTemplate(); template.setCompletionPolicy(new SimpleCompletionPolicy(2)); container = getContainer(template); container.setSessionTransacted(true); try { boolean received = doTestWithException(new IllegalStateException("No way!"), true, 2); assertFalse("Message received", received); fail("Expected IllegalStateException"); } catch (IllegalStateException e) { assertEquals("No way!", e.getMessage()); } }
@Test public void testNonTransactionalReceiveAndExecuteWithCallbackThrowingError() throws Exception { RepeatTemplate template = new RepeatTemplate(); template.setCompletionPolicy(new SimpleCompletionPolicy(2)); container = getContainer(template); container.setSessionTransacted(false); try { boolean received = doTestWithException(new RuntimeException("No way!"), false, 2); assertTrue("Message not received but listener not transactional so this should be true", received); } catch (RuntimeException e) { assertEquals("No way!", e.getMessage()); fail("Unexpected Error - should be swallowed"); } }
public void testVoidServiceSunnyDay() throws Exception { ((Advised) service).addAdvice(interceptor); RepeatTemplate template = new RepeatTemplate(); // N.B. the default completion policy results in an infinite loop, so we // need to set the chunk size. template.setCompletionPolicy(new SimpleCompletionPolicy(2)); interceptor.setRepeatOperations(template); service.alternate(); assertEquals(2, target.count); }
public void testCallbackWithBoolean() throws Exception { RepeatTemplate template = new RepeatTemplate(); // N.B. the default completion policy results in an infinite loop, so we // need to set the chunk size. template.setCompletionPolicy(new SimpleCompletionPolicy(2)); interceptor.setRepeatOperations(template); ((Advised) service).addAdvice(interceptor); assertTrue(service.isContinuable()); assertEquals(2, target.count); }
@Test public void testStepToCompletion() throws Exception { RepeatTemplate template = new RepeatTemplate(); // process all items: template.setCompletionPolicy(new DefaultResultCompletionPolicy()); step.setStepOperations(template); JobExecution jobExecutionContext = new JobExecution(jobInstance, jobParameters); StepExecution stepExecution = new StepExecution(step.getName(), jobExecutionContext); step.execute(stepExecution); assertEquals(3, processed.size()); assertEquals(3, stepExecution.getReadCount()); }
@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); }
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; }
/** * StepExecution should be updated after every chunk commit. */ @Test public void testStepExecutionUpdates() throws Exception { JobExecution jobExecution = new JobExecution(jobInstance, jobParameters); StepExecution stepExecution = new StepExecution(step.getName(), jobExecution); step.setStepOperations(new RepeatTemplate()); JobRepositoryStub jobRepository = new JobRepositoryStub(); step.setJobRepository(jobRepository); step.execute(stepExecution); assertEquals(3, processed.size()); assertEquals(3, stepExecution.getReadCount()); assertTrue(3 <= jobRepository.updateCount); }
@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); }