ChunkProcessorChunkHandler<T> handler = new ChunkProcessorChunkHandler<>(); setNonBuffering(chunkProcessor); handler.setChunkProcessor(chunkProcessor); handler.afterPropertiesSet();
/** * * @see ChunkHandler#handleChunk(ChunkRequest) */ @ServiceActivator public ChunkResponse handleChunk(ChunkRequest<S> chunkRequest) throws Exception { if (logger.isDebugEnabled()) { logger.debug("Handling chunk: " + chunkRequest); } StepContribution stepContribution = chunkRequest.getStepContribution(); Throwable failure = process(chunkRequest, stepContribution); if (failure != null) { logger.debug("Failed chunk", failure); return new ChunkResponse(false, chunkRequest.getSequence(), chunkRequest.getJobId(), stepContribution, failure.getClass().getName() + ": " + failure.getMessage()); } if (logger.isDebugEnabled()) { logger.debug("Completed chunk handling with " + stepContribution); } return new ChunkResponse(true, chunkRequest.getSequence(), chunkRequest.getJobId(), stepContribution); }
/** * Create an {@link IntegrationFlow} with a {@link ChunkProcessorChunkHandler} * configured as a service activator listening to the input channel and replying * on the output channel. * * @return the integration flow */ @SuppressWarnings({"unchecked", "rawtypes"}) public IntegrationFlow build() { Assert.notNull(this.itemWriter, "An ItemWriter must be provided"); Assert.notNull(this.inputChannel, "An InputChannel must be provided"); Assert.notNull(this.outputChannel, "An OutputChannel must be provided"); if(this.itemProcessor == null) { this.itemProcessor = new PassThroughItemProcessor(); } SimpleChunkProcessor<I, O> chunkProcessor = new SimpleChunkProcessor<>(this.itemProcessor, this.itemWriter); ChunkProcessorChunkHandler<I> chunkProcessorChunkHandler = new ChunkProcessorChunkHandler<>(); chunkProcessorChunkHandler.setChunkProcessor(chunkProcessor); return IntegrationFlows .from(this.inputChannel) .handle(chunkProcessorChunkHandler, SERVICE_ACTIVATOR_METHOD_NAME) .channel(this.outputChannel) .get(); }
@Test public void testVanillaHandleChunk() throws Exception { handler.setChunkProcessor(new ChunkProcessor<Object>() { public void process(StepContribution contribution, Chunk<Object> chunk) throws Exception { count += chunk.size(); } }); StepContribution stepContribution = MetaDataInstanceFactory.createStepExecution().createStepContribution(); ChunkResponse response = handler.handleChunk(new ChunkRequest<>(0, StringUtils .commaDelimitedListToSet("foo,bar"), 12L, stepContribution)); assertEquals(stepContribution, response.getStepContribution()); assertEquals(12, response.getJobId().longValue()); assertTrue(response.isSuccessful()); assertEquals(2, count); }
@Bean @Profile("slave") @ServiceActivator(inputChannel = "inboundRequests", outputChannel = "outboundReplies") public ChunkProcessorChunkHandler chunkProcessorChunkHandler(ItemProcessor<Customer, Customer> itemProcessor, ItemWriter<Customer> itemWriter) throws Exception { SimpleChunkProcessor chunkProcessor = new SimpleChunkProcessor<>(itemProcessor, itemWriter); chunkProcessor.afterPropertiesSet(); ChunkProcessorChunkHandler<Customer> chunkHandler = new ChunkProcessorChunkHandler<>(); chunkHandler.setChunkProcessor(chunkProcessor); chunkHandler.afterPropertiesSet(); return chunkHandler; }