List of usage examples for org.springframework.batch.repeat RepeatCallback RepeatCallback
RepeatCallback
From source file:org.springframework.batch.core.jsr.step.item.JsrChunkProcessor.java
/** * Loops through reading (via {@link #provide(StepContribution, Chunk)} and * processing (via {@link #transform(StepContribution, Object)}) until the chunk * is complete. Once the chunk is complete, the results are written (via * {@link #persist(StepContribution, Chunk)}. * * @see ChunkProcessor#process(StepContribution, Chunk) * @param contribution a {@link StepContribution} * @param chunk a {@link Chunk}//w ww . jav a 2 s. co m */ @Override public void process(final StepContribution contribution, final Chunk<I> chunk) throws Exception { final AtomicInteger filterCount = new AtomicInteger(0); final Chunk<O> output = new Chunk<O>(); repeatTemplate.iterate(new RepeatCallback() { @Override public RepeatStatus doInIteration(RepeatContext context) throws Exception { I item = provide(contribution, chunk); if (item != null) { contribution.incrementReadCount(); } else { return RepeatStatus.FINISHED; } O processedItem = transform(contribution, item); if (processedItem == null) { filterCount.incrementAndGet(); } else { output.add(processedItem); } return RepeatStatus.CONTINUABLE; } }); contribution.incrementFilterCount(filterCount.get()); if (output.size() > 0) { persist(contribution, output); } }
From source file:org.springframework.batch.core.step.item.SimpleChunkProvider.java
@Override public Chunk<I> provide(final StepContribution contribution) throws Exception { final Chunk<I> inputs = new Chunk<I>(); repeatOperations.iterate(new RepeatCallback() { @Override//from w w w.j av a 2s . c o m public RepeatStatus doInIteration(final RepeatContext context) throws Exception { I item = null; try { item = read(contribution, inputs); } catch (SkipOverflowException e) { // read() tells us about an excess of skips by throwing an // exception return RepeatStatus.FINISHED; } if (item == null) { inputs.setEnd(); return RepeatStatus.FINISHED; } inputs.add(item); contribution.incrementReadCount(); return RepeatStatus.CONTINUABLE; } }); return inputs; }
From source file:org.springframework.batch.repeat.support.TaskExecutorRepeatTemplateBulkAsynchronousTests.java
@Before public void setUp() { template = new TaskExecutorRepeatTemplate(); TaskExecutor taskExecutor = new SimpleAsyncTaskExecutor(); threadPool.setMaxPoolSize(300);// ww w.j a va 2 s. c o m threadPool.setCorePoolSize(10); threadPool.setQueueCapacity(0); threadPool.afterPropertiesSet(); taskExecutor = threadPool; template.setTaskExecutor(taskExecutor); template.setThrottleLimit(throttleLimit); items = Collections.synchronizedList(new ArrayList<String>()); callback = new RepeatCallback() { private volatile AtomicInteger count = new AtomicInteger(0); @Override public RepeatStatus doInIteration(RepeatContext context) throws Exception { int position = count.incrementAndGet(); String item = position <= total ? "" + position : null; items.add("" + item); if (item != null) { beBusy(); } /* * In a multi-threaded task, one of the callbacks can call * FINISHED early, while other threads are still working, and * would do more work if the callback was called again. (This * happens for instance if there is a failure and you want to * retry the work.) */ RepeatStatus result = RepeatStatus.continueIf(position != early && item != null); if (position == error) { throw new RuntimeException("Planned"); } if (!result.isContinuable()) { logger.debug("Returning " + result + " for count=" + position); } return result; } }; }
From source file:org.springframework.batch.repeat.support.TaskExecutorRepeatTemplateBulkAsynchronousTests.java
@Test public void testErrorThrownByCallback() throws Exception { callback = new RepeatCallback() { private volatile AtomicInteger count = new AtomicInteger(0); @Override//from ww w . j ava 2 s .c om 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); } }