protected RepeatOperations createChunkOperations() { RepeatOperations repeatOperations = chunkOperations; if (repeatOperations == null) { RepeatTemplate repeatTemplate = new RepeatTemplate(); repeatTemplate.setCompletionPolicy(getChunkCompletionPolicy()); repeatOperations = repeatTemplate; } return repeatOperations; }
stepOperations = new RepeatTemplate(); ((RepeatTemplate) stepOperations).setExceptionHandler(exceptionHandler);
@Test public void testNestedSession() throws Exception { RepeatTemplate outer = getRepeatTemplate(); RepeatTemplate inner = new RepeatTemplate(); outer.iterate(new NestedRepeatCallback(inner, new RepeatCallback() { @Override public RepeatStatus doInIteration(RepeatContext context) throws Exception { count++; assertNotNull(context); assertNotSame("Nested batch should have new session", context, context.getParent()); assertSame(context, RepeatSynchronizationManager.getContext()); return RepeatStatus.FINISHED; } }) { @Override public RepeatStatus doInIteration(RepeatContext context) throws Exception { count++; assertNotNull(context); assertSame(context, RepeatSynchronizationManager.getContext()); return super.doInIteration(context); } }); assertTrue("Too few attempts: " + count, count >= 1); assertTrue("Too many attempts: " + count, count <= 10); }
private RepeatOperations createRepeatOperations() { RepeatTemplate repeatOperations = new RepeatTemplate(); repeatOperations.setCompletionPolicy(getChunkCompletionPolicy()); repeatOperations.setExceptionHandler(getExceptionHandler()); return repeatOperations; } }
RepeatContext context = start(); boolean running = !isMarkedComplete(context); running = running && !isMarkedComplete(context); if (!running) break; RepeatInternalState state = createInternalState(context); running = running && !isMarkedComplete(context); result = getNextResult(context, callback, state); executeAfterInterceptors(context, result); doHandle(throwable, context, deferred); if (isComplete(context, result) || isMarkedComplete(context) || !deferred.isEmpty()) { running = false; result = result.and(waitForResults(state)); for (Throwable throwable : throwables) { doHandle(throwable, context, deferred); + throwable.getClass().getName() + ": " + throwable.getMessage()); rethrow(throwable);
public void testOpenInterceptors() throws Exception { RepeatTemplate template = new RepeatTemplate(); final List<Object> calls = new ArrayList<>(); template.setListeners(new RepeatListener[] { new RepeatListenerSupport() { @Override public void open(RepeatContext context) { calls.add("1"); } }, new RepeatListenerSupport() { @Override public void open(RepeatContext context) { calls.add("2"); context.setCompleteOnly(); } } }); template.iterate(new RepeatCallback() { @Override public RepeatStatus doInIteration(RepeatContext context) throws Exception { count++; return RepeatStatus.CONTINUABLE; } }); assertEquals(0, count); assertEquals("[1, 2]", calls.toString()); }
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 } }
/** * Chunking using a dedicated TerminationPolicy. Transactions would be laid * on at the level of chunkTemplate.execute() or the surrounding callback. */ @Test public void testChunkedBatchWithTerminationPolicy() throws Exception { RepeatTemplate repeatTemplate = new RepeatTemplate(); final RepeatCallback callback = new ItemReaderRepeatCallback<>(provider, processor); final RepeatTemplate chunkTemplate = new RepeatTemplate(); // The policy is resettable so we only have to resolve this dependency // once chunkTemplate.setCompletionPolicy(new SimpleCompletionPolicy(2)); RepeatStatus result = repeatTemplate.iterate(new NestedRepeatCallback(chunkTemplate, callback) { @Override public RepeatStatus doInIteration(RepeatContext context) throws Exception { count++; // for test assertion return super.doInIteration(context); } }); assertEquals(NUMBER_OF_ITEMS, processor.count); // The chunk executes 3 times because the last one // returns false. We terminate the main batch when // we encounter a partially empty chunk. assertEquals(3, count); assertFalse(result.isContinuable()); }
/** * 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); }
public void testBeforeInterceptorCanVeto() throws Exception { RepeatTemplate template = new RepeatTemplate(); final List<Object> calls = new ArrayList<>(); template.registerListener(new RepeatListenerSupport() { @Override public void before(RepeatContext context) { calls.add("1"); context.setCompleteOnly(); } }); template.iterate(new RepeatCallback() { @Override public RepeatStatus doInIteration(RepeatContext context) throws Exception { count++; return RepeatStatus.FINISHED; } }); assertEquals(0, count); // ... but the interceptor before() was called: assertEquals("[1]", calls.toString()); }
@Override public RepeatStatus doInIteration(RepeatContext context) throws Exception { stepTemplate.iterate(stepCallback); return RepeatStatus.FINISHED; } };
template.setExceptionHandler(exHandler); template.setListeners(new RepeatListener[] { listener }); template.iterate(new RepeatCallback() { @Override public RepeatStatus doInIteration(RepeatContext context) throws Exception {
/** * Check that the exception handler is called. * * @throws Exception */ @Test public void testExceptionHandlerCalledOnAbnormalCompletion() throws Exception { final List<Throwable> list = new ArrayList<>(); template.setExceptionHandler(new ExceptionHandler() { @Override public void handleException(RepeatContext context, Throwable throwable) throws RuntimeException { list.add(throwable); throw (RuntimeException) throwable; } }); try { template.iterate(new RepeatCallback() { @Override public RepeatStatus doInIteration(RepeatContext context) throws Exception { count++; throw new RuntimeException("foo"); } }); } catch (RuntimeException e) { assertEquals("foo", e.getMessage()); } assertEquals(1, count); assertEquals(1, list.size()); }
SimpleRetryExceptionHandler exceptionHandler = new SimpleRetryExceptionHandler(retryPolicyWrapper, getExceptionHandler(), nonRetryableExceptionClasses); ((RepeatTemplate) stepOperations).setExceptionHandler(exceptionHandler);
.<Class<? extends Throwable>, Boolean> singletonMap(Exception.class, true))); repeatTemplate.setCompletionPolicy(new SimpleCompletionPolicy(2));
private RepeatOperations createRepeatOperations() { RepeatTemplate repeatOperations = new RepeatTemplate(); repeatOperations.setCompletionPolicy(getChunkCompletionPolicy()); repeatOperations.setExceptionHandler(getExceptionHandler()); return repeatOperations; } }
public void testAfterInterceptors() throws Exception { RepeatTemplate template = new RepeatTemplate(); final List<Object> calls = new ArrayList<>(); template.setListeners(new RepeatListener[] { new RepeatListenerSupport() { @Override public void after(RepeatContext context, RepeatStatus result) { calls.add("1"); } }, new RepeatListenerSupport() { @Override public void after(RepeatContext context, RepeatStatus result) { calls.add("2"); } } }); template.iterate(new RepeatCallback() { @Override public RepeatStatus doInIteration(RepeatContext context) throws Exception { count++; return RepeatStatus.continueIf(count <= 1); } }); // 2 calls to the callback, and the second one had no processing... assertEquals(2, count); // ... so the interceptor after() is not called: assertEquals("[2, 1]", calls.toString()); }
@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()); }
template.setCompletionPolicy(new CompletionPolicySupport() { @Override public RepeatContext start(RepeatContext c) { template.iterate(new RepeatCallback() { @Override public RepeatStatus doInIteration(RepeatContext context) throws Exception {
RepeatContext context = start(); boolean running = !isMarkedComplete(context); running = running && !isMarkedComplete(context); if (!running) break; RepeatInternalState state = createInternalState(context); running = running && !isMarkedComplete(context); result = getNextResult(context, callback, state); executeAfterInterceptors(context, result); doHandle(throwable, context, deferred); if (isComplete(context, result) || isMarkedComplete(context) || !deferred.isEmpty()) { running = false; result = result.and(waitForResults(state)); for (Throwable throwable : throwables) { doHandle(throwable, context, deferred); + throwable.getClass().getName() + ": " + throwable.getMessage()); rethrow(throwable);