/** * @return a {@link CompletionPolicy} consistent with the chunk size and injected policy (if present). */ protected CompletionPolicy getChunkCompletionPolicy() { Assert.state(!(completionPolicy != null && chunkSize > 0), "You must specify either a chunkCompletionPolicy or a commitInterval but not both."); Assert.state(chunkSize >= 0, "The commitInterval must be positive or zero (for default value)."); if (completionPolicy != null) { return completionPolicy; } if (chunkSize == 0) { logger.info("Setting commit interval to default value (" + DEFAULT_COMMIT_INTERVAL + ")"); chunkSize = DEFAULT_COMMIT_INTERVAL; } return new SimpleCompletionPolicy(chunkSize); }
public void testReset() throws Exception { policy.setChunkSize(2); policy.update(context); assertFalse(policy.isComplete(context, dummy)); policy.update(context); assertTrue(policy.isComplete(context, dummy)); context = policy.start(null); policy.update(context); assertFalse(policy.isComplete(context, dummy)); } }
public void testTerminationAfterExplicitChunkSize() throws Exception { int chunkSize = 2; policy.setChunkSize(chunkSize); for (int i = 0; i < chunkSize - 1; i++) { policy.update(context); assertFalse(policy.isComplete(context, dummy)); } policy.update(context); assertTrue(policy.isComplete(context, dummy)); }
public void testTerminationAfterNullResult() throws Exception { policy.update(context); assertFalse(policy.isComplete(context, dummy)); policy.update(context); assertTrue(policy.isComplete(context, null)); }
@Override protected void setUp() throws Exception { super.setUp(); context = policy.start(null); }
public void testTerminationAfterDefaultSize() throws Exception { for (int i = 0; i < SimpleCompletionPolicy.DEFAULT_CHUNK_SIZE - 1; i++) { policy.update(context); assertFalse(policy.isComplete(context, dummy)); } policy.update(context); assertTrue(policy.isComplete(context, dummy)); }
/** * Set up a {@link SimpleCompletionPolicy} with a commit interval taken from * the {@link JobParameters}. If there is a Long parameter with the given * key name, the intValue of this parameter is used. If not an exception * will be thrown. * * @see org.springframework.batch.core.listener.StepExecutionListenerSupport#beforeStep(org.springframework.batch.core.StepExecution) */ @Override public void beforeStep(StepExecution stepExecution) { JobParameters jobParameters = stepExecution.getJobParameters(); Assert.state(jobParameters.getParameters().containsKey(keyName), "JobParameters do not contain Long parameter with key=[" + keyName + "]"); delegate = new SimpleCompletionPolicy(jobParameters.getLong(keyName).intValue()); }
public RepeatTemplate getRepeatTemplate() { template = new RepeatTemplate(); // default stop after more items than exist in dataset template.setCompletionPolicy(new SimpleCompletionPolicy(8)); return template; }
@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 testReceiveAndExecuteWithCallback() throws Exception { RepeatTemplate template = new RepeatTemplate(); template.setCompletionPolicy(new SimpleCompletionPolicy(2)); container = getContainer(template); container.setMessageListener(new MessageListener() { @Override public void onMessage(Message arg0) { } }); Session session = mock(Session.class); MessageConsumer consumer = mock(MessageConsumer.class); Message message = mock(Message.class); // Expect two calls to consumer (chunk size)... when(session.getTransacted()).thenReturn(true); when(session.getTransacted()).thenReturn(true); when(consumer.receive(1000)).thenReturn(message); boolean received = doExecute(session, consumer); assertTrue("Message not received", received); }
public RepeatTemplate getRepeatTemplate() { TaskExecutorRepeatTemplate template = new TaskExecutorRepeatTemplate(); template.setTaskExecutor(new SimpleAsyncTaskExecutor()); // Set default completion above number of items in input file template.setCompletionPolicy(new SimpleCompletionPolicy(8)); return template; }
@Test public void testThrottleLimitWithEarlyCompletion() throws Exception { early = 2; template.setCompletionPolicy(new SimpleCompletionPolicy(10)); template.iterate(callback); int frequency = Collections.frequency(items, "null"); assertEquals(10, items.size() - frequency); // System.err.println("Frequency: " + frequency); assertEquals(0, frequency); }
@Test public void testExceptionThrownOnLastItem() throws Exception { template.setCompletionPolicy(new SimpleCompletionPolicy(2)); try { template.iterate(new RepeatCallback() { @Override public RepeatStatus doInIteration(RepeatContext context) throws Exception { count++; if (count < 2) { return RepeatStatus.CONTINUABLE; } throw new RuntimeException("Barf second try count=" + count); } }); fail("Expected exception on last item in batch"); } catch (Exception e) { // expected assertEquals("Barf second try count=2", e.getMessage()); } }
/** * Check that a dedicated TerminationPolicy can terminate the batch. * * @throws Exception */ @Test public void testEarlyCompletionWithPolicy() throws Exception { template.setCompletionPolicy(new SimpleCompletionPolicy(2)); template.iterate(new ItemReaderRepeatCallback<>(provider, processor)); assertEquals(2, processor.count); }
@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); }
public void testInterceptorChainWithRetry() throws Exception { ((Advised) service).addAdvice(interceptor); final List<Object> list = new ArrayList<>(); ((Advised) service).addAdvice(new MethodInterceptor() { @Override public Object invoke(MethodInvocation invocation) throws Throwable { list.add("chain"); return invocation.proceed(); } }); RepeatTemplate template = new RepeatTemplate(); template.setCompletionPolicy(new SimpleCompletionPolicy(2)); interceptor.setRepeatOperations(template); service.service(); assertEquals(2, target.count); assertEquals(2, list.size()); }
@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); }
@Test public void testErrorThrownByCallback() throws Exception { callback = new RepeatCallback() { private volatile AtomicInteger count = new AtomicInteger(0); @Override public RepeatStatus doInIteration(RepeatContext context) throws Exception { int position = count.incrementAndGet(); if(position == 4) { throw new OutOfMemoryError("Planned"); } else { return RepeatStatus.CONTINUABLE; } } }; template.setCompletionPolicy(new SimpleCompletionPolicy(10)); try { template.iterate(callback); fail("Expected planned exception"); } catch (OutOfMemoryError oome) { assertEquals("Planned", oome.getMessage()); } catch (Exception e) { e.printStackTrace(); fail("Wrong exception was thrown: " + e); } }