EMMA Coverage Report (generated Thu Jan 24 13:37:04 CST 2013)
[all classes][org.springframework.batch.core.scope.context]

COVERAGE SUMMARY FOR SOURCE FILE [StepContextRepeatCallback.java]

nameclass, %method, %block, %line, %
StepContextRepeatCallback.java100% (1/1)100% (2/2)100% (79/79)100% (15/15)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class StepContextRepeatCallback100% (1/1)100% (2/2)100% (79/79)100% (15/15)
StepContextRepeatCallback (StepExecution): void 100% (1/1)100% (15/15)100% (5/5)
doInIteration (RepeatContext): RepeatStatus 100% (1/1)100% (64/64)100% (10/10)

1/*
2 * Copyright 2006-2007 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 */
16package org.springframework.batch.core.scope.context;
17 
18import java.util.Queue;
19import java.util.concurrent.LinkedBlockingQueue;
20 
21import org.apache.commons.logging.Log;
22import org.apache.commons.logging.LogFactory;
23import org.springframework.batch.core.Step;
24import org.springframework.batch.core.StepExecution;
25import org.springframework.batch.repeat.RepeatCallback;
26import org.springframework.batch.repeat.RepeatContext;
27import org.springframework.batch.repeat.RepeatStatus;
28import org.springframework.util.ObjectUtils;
29 
30/**
31 * Convenient base class for clients who need to do something in a repeat
32 * callback inside a {@link Step}.
33 * 
34 * @author Dave Syer
35 * 
36 */
37public abstract class StepContextRepeatCallback implements RepeatCallback {
38 
39        private final Queue<ChunkContext> attributeQueue = new LinkedBlockingQueue<ChunkContext>();
40        
41        private final StepExecution stepExecution;
42 
43        private final Log logger = LogFactory.getLog(StepContextRepeatCallback.class);
44 
45        /**
46         * @param stepExecution
47         */
48        public StepContextRepeatCallback(StepExecution stepExecution) {
49                this.stepExecution = stepExecution;
50        }
51 
52        /**
53         * Manage the {@link StepContext} lifecycle. Business processing should be
54         * delegated to {@link #doInChunkContext(RepeatContext, ChunkContext)}. This
55         * is to ensure that the current thread has a reference to the context, even
56         * if the callback is executed in a pooled thread. Handles the registration
57         * and de-registration of the step context, so clients should not duplicate
58         * those calls.
59         * 
60         * @see RepeatCallback#doInIteration(RepeatContext)
61         */
62        public RepeatStatus doInIteration(RepeatContext context) throws Exception {
63                
64                // The StepContext has to be the same for all chunks,
65                // otherwise step-scoped beans will be re-initialised for each chunk.
66                StepContext stepContext = StepSynchronizationManager.register(stepExecution);
67                logger.debug("Preparing chunk execution for StepContext: "+ObjectUtils.identityToString(stepContext));
68 
69                ChunkContext chunkContext = attributeQueue.poll();
70                if (chunkContext == null) {
71                        chunkContext = new ChunkContext(stepContext);
72                }
73 
74                try {
75                        logger.debug("Chunk execution starting: queue size="+attributeQueue.size());
76                        return doInChunkContext(context, chunkContext);
77                }
78                finally {
79                        // Still some stuff to do with the data in this chunk,
80                        // pass it back.
81                        if (!chunkContext.isComplete()) {
82                                attributeQueue.add(chunkContext);
83                        }
84                        StepSynchronizationManager.close();
85                }
86        }
87 
88        /**
89         * Do the work required for this chunk of the step. The {@link ChunkContext}
90         * provided is managed by the base class, so that if there is still work to
91         * do for the task in hand state can be stored here. In a multi-threaded
92         * client, the base class ensures that only one thread at a time can be
93         * working on each instance of {@link ChunkContext}. Workers should signal
94         * that they are finished with a context by removing all the attributes they
95         * have added. If a worker does not remove them another thread might see
96         * stale state.
97         * 
98         * @param context the current {@link RepeatContext}
99         * @param chunkContext the chunk context in which to carry out the work
100         * @return the repeat status from the execution
101         * @throws Exception implementations can throw an exception if anything goes
102         * wrong
103         */
104        public abstract RepeatStatus doInChunkContext(RepeatContext context, ChunkContext chunkContext) throws Exception;
105 
106}

[all classes][org.springframework.batch.core.scope.context]
EMMA 2.0.5312 (C) Vladimir Roubtsov