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

COVERAGE SUMMARY FOR SOURCE FILE [TaskExecutorPartitionHandler.java]

nameclass, %method, %block, %line, %
TaskExecutorPartitionHandler.java100% (2/2)90%  (9/10)98%  (125/128)97%  (29/30)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class TaskExecutorPartitionHandler100% (1/1)88%  (7/8)97%  (107/110)96%  (27/28)
getStep (): Step 0%   (0/1)0%   (0/3)0%   (0/1)
TaskExecutorPartitionHandler (): void 100% (1/1)100% (11/11)100% (3/3)
access$000 (TaskExecutorPartitionHandler): Step 100% (1/1)100% (3/3)100% (1/1)
afterPropertiesSet (): void 100% (1/1)100% (1/1)100% (1/1)
handle (StepExecutionSplitter, StepExecution): Collection 100% (1/1)100% (80/80)100% (17/17)
setGridSize (int): void 100% (1/1)100% (4/4)100% (2/2)
setStep (Step): void 100% (1/1)100% (4/4)100% (2/2)
setTaskExecutor (TaskExecutor): void 100% (1/1)100% (4/4)100% (2/2)
     
class TaskExecutorPartitionHandler$1100% (1/1)100% (2/2)100% (18/18)100% (3/3)
TaskExecutorPartitionHandler$1 (TaskExecutorPartitionHandler, StepExecution):... 100% (1/1)100% (9/9)100% (1/1)
call (): StepExecution 100% (1/1)100% (9/9)100% (2/2)

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 */
16 
17package org.springframework.batch.core.partition.support;
18 
19import java.util.ArrayList;
20import java.util.Collection;
21import java.util.HashSet;
22import java.util.Set;
23import java.util.concurrent.Callable;
24import java.util.concurrent.Future;
25import java.util.concurrent.FutureTask;
26 
27import org.springframework.batch.core.BatchStatus;
28import org.springframework.batch.core.ExitStatus;
29import org.springframework.batch.core.Step;
30import org.springframework.batch.core.StepExecution;
31import org.springframework.batch.core.partition.PartitionHandler;
32import org.springframework.batch.core.partition.StepExecutionSplitter;
33import org.springframework.batch.core.step.StepHolder;
34import org.springframework.beans.factory.InitializingBean;
35import org.springframework.core.task.SyncTaskExecutor;
36import org.springframework.core.task.TaskExecutor;
37import org.springframework.core.task.TaskRejectedException;
38import org.springframework.util.Assert;
39 
40/**
41 * A {@link PartitionHandler} that uses a {@link TaskExecutor} to execute the
42 * partitioned {@link Step} locally in multiple threads. This can be an
43 * effective approach for scaling batch steps that are IO intensive, like
44 * directory and filesystem scanning and copying.
45 * 
46 * @author Dave Syer
47 * @since 2.0
48 */
49public class TaskExecutorPartitionHandler implements PartitionHandler, StepHolder, InitializingBean {
50 
51        private int gridSize = 1;
52 
53        private TaskExecutor taskExecutor = new SyncTaskExecutor();
54 
55        private Step step;
56 
57        public void afterPropertiesSet() throws Exception {
58        }
59 
60        /**
61         * Passed to the {@link StepExecutionSplitter} in the
62         * {@link #handle(StepExecutionSplitter, StepExecution)} method, instructing
63         * it how many {@link StepExecution} instances are required, ideally. The
64         * {@link StepExecutionSplitter} is allowed to ignore the grid size in the
65         * case of a restart, since the input data partitions must be preserved.
66         * 
67         * @param gridSize the number of step executions that will be created
68         */
69        public void setGridSize(int gridSize) {
70                this.gridSize = gridSize;
71        }
72 
73        /**
74         * Setter for the {@link TaskExecutor} that is used to farm out step
75         * executions to multiple threads.
76         * @param taskExecutor a {@link TaskExecutor}
77         */
78        public void setTaskExecutor(TaskExecutor taskExecutor) {
79                this.taskExecutor = taskExecutor;
80        }
81 
82        /**
83         * Setter for the {@link Step} that will be used to execute the partitioned
84         * {@link StepExecution}. This is a regular Spring Batch step, with all the
85         * business logic required to complete an execution based on the input
86         * parameters in its {@link StepExecution} context.
87         * 
88         * @param step the {@link Step} instance to use to execute business logic
89         */
90        public void setStep(Step step) {
91                this.step = step;
92        }
93        
94        /**
95         * The step instance that will be executed in parallel by this handler.
96         * 
97         * @return the step instance that will be used
98         * @see StepHolder#getStep()
99         */
100        public Step getStep() {
101                return this.step;
102        }
103 
104        /**
105         * @see PartitionHandler#handle(StepExecutionSplitter, StepExecution)
106         */
107        public Collection<StepExecution> handle(StepExecutionSplitter stepExecutionSplitter,
108                        StepExecution masterStepExecution) throws Exception {
109 
110                Assert.notNull(step, "A Step must be provided.");
111                
112                Set<Future<StepExecution>> tasks = new HashSet<Future<StepExecution>>(gridSize);
113 
114                Collection<StepExecution> result = new ArrayList<StepExecution>();
115 
116                for (final StepExecution stepExecution : stepExecutionSplitter.split(masterStepExecution, gridSize)) {
117 
118                        final FutureTask<StepExecution> task = new FutureTask<StepExecution>(new Callable<StepExecution>() {
119                                public StepExecution call() throws Exception {
120                                        step.execute(stepExecution);
121                                        return stepExecution;
122                                }
123                        });
124 
125                        try {
126                                taskExecutor.execute(task);
127                                tasks.add(task);
128                        }
129                        catch (TaskRejectedException e) {
130                                // couldn't execute one of the tasks
131                                ExitStatus exitStatus = ExitStatus.FAILED
132                                                .addExitDescription("TaskExecutor rejected the task for this step.");
133                                /*
134                                 * Set the status in case the caller is tracking it through the
135                                 * JobExecution.
136                                 */
137                                stepExecution.setStatus(BatchStatus.FAILED);
138                                stepExecution.setExitStatus(exitStatus);
139                                result.add(stepExecution);
140                        }
141 
142                }
143 
144                for (Future<StepExecution> task : tasks) {
145                        result.add(task.get());
146                }
147                return result;
148 
149        }
150 
151}

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