View Javadoc

1   /*
2    * Copyright 2006-2013 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.springframework.batch.core.step.item;
18  
19  import org.apache.commons.logging.Log;
20  import org.apache.commons.logging.LogFactory;
21  import org.springframework.batch.core.StepContribution;
22  import org.springframework.batch.core.scope.context.ChunkContext;
23  import org.springframework.batch.core.step.tasklet.Tasklet;
24  import org.springframework.batch.repeat.RepeatStatus;
25  
26  /**
27   * A {@link Tasklet} implementing variations on read-process-write item
28   * handling.
29   *
30   * @author Dave Syer
31   *
32   * @param <I> input item type
33   */
34  public class ChunkOrientedTasklet<I> implements Tasklet {
35  
36  	private static final String INPUTS_KEY = "INPUTS";
37  
38  	private final ChunkProcessor<I> chunkProcessor;
39  
40  	private final ChunkProvider<I> chunkProvider;
41  
42  	private boolean buffering = true;
43  
44  	private static Log logger = LogFactory.getLog(ChunkOrientedTasklet.class);
45  
46  	public ChunkOrientedTasklet(ChunkProvider<I> chunkProvider, ChunkProcessor<I> chunkProcessor) {
47  		this.chunkProvider = chunkProvider;
48  		this.chunkProcessor = chunkProcessor;
49  	}
50  
51  	/**
52  	 * Flag to indicate that items should be buffered once read. Defaults to
53  	 * true, which is appropriate for forward-only, non-transactional item
54  	 * readers. Main (or only) use case for setting this flag to false is a
55  	 * transactional JMS item reader.
56  	 *
57  	 * @param buffering
58  	 */
59  	public void setBuffering(boolean buffering) {
60  		this.buffering = buffering;
61  	}
62  
63  	@Override
64  	public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
65  
66  		@SuppressWarnings("unchecked")
67  		Chunk<I> inputs = (Chunk<I>) chunkContext.getAttribute(INPUTS_KEY);
68  		if (inputs == null) {
69  			inputs = chunkProvider.provide(contribution);
70  			if (buffering) {
71  				chunkContext.setAttribute(INPUTS_KEY, inputs);
72  			}
73  		}
74  
75  		chunkProcessor.process(contribution, inputs);
76  		chunkProvider.postProcess(contribution, inputs);
77  
78  		// Allow a message coming back from the processor to say that we
79  		// are not done yet
80  		if (inputs.isBusy()) {
81  			logger.debug("Inputs still busy");
82  			return RepeatStatus.CONTINUABLE;
83  		}
84  
85  		chunkContext.removeAttribute(INPUTS_KEY);
86  		chunkContext.setComplete();
87  
88  		logger.debug("Inputs not busy, ended: " + inputs.isEnd());
89  		return RepeatStatus.continueIf(!inputs.isEnd());
90  
91  	}
92  
93  }