EMMA Coverage Report (generated Fri Aug 21 15:59:46 BST 2009)
[all classes][org.springframework.batch.core.partition.support]

COVERAGE SUMMARY FOR SOURCE FILE [TaskExecutorPartitionHandler.java]

nameclass, %method, %block, %line, %
TaskExecutorPartitionHandler.java100% (2/2)100% (9/9)100% (126/126)100% (30/30)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class TaskExecutorPartitionHandler100% (1/1)100% (7/7)100% (108/108)100% (27/27)
TaskExecutorPartitionHandler (): void 100% (1/1)100% (11/11)100% (3/3)
access$0 (TaskExecutorPartitionHandler): Step 100% (1/1)100% (3/3)100% (1/1)
afterPropertiesSet (): void 100% (1/1)100% (5/5)100% (2/2)
handle (StepExecutionSplitter, StepExecution): Collection 100% (1/1)100% (77/77)100% (15/15)
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% (4/4)
TaskExecutorPartitionHandler$1 (TaskExecutorPartitionHandler, StepExecution):... 100% (1/1)100% (9/9)100% (2/2)
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.beans.factory.InitializingBean;
34import org.springframework.core.task.SyncTaskExecutor;
35import org.springframework.core.task.TaskExecutor;
36import org.springframework.core.task.TaskRejectedException;
37import org.springframework.util.Assert;
38 
39/**
40 * A {@link PartitionHandler} that uses a {@link TaskExecutor} to execute the
41 * partitioned {@link Step} locally in multiple threads. This can be an effective
42 * approach for scaling batch steps that are IO intensive, like directory and
43 * filesystem scanning and copying.
44 * 
45 * @author Dave Syer
46 * @since 2.0
47 */
48public class TaskExecutorPartitionHandler implements PartitionHandler, InitializingBean {
49 
50        private int gridSize = 1;
51 
52        private TaskExecutor taskExecutor = new SyncTaskExecutor();
53 
54        private Step step;
55 
56        public void afterPropertiesSet() throws Exception {
57                Assert.notNull(step, "A Step must be provided.");
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 case of
65         * 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         * @see PartitionHandler#handle(StepExecutionSplitter, StepExecution)
96         */
97        public Collection<StepExecution> handle(StepExecutionSplitter stepExecutionSplitter,
98                        StepExecution masterStepExecution) throws Exception {
99 
100                Set<Future<StepExecution>> tasks = new HashSet<Future<StepExecution>>(gridSize);
101 
102                Collection<StepExecution> result = new ArrayList<StepExecution>();
103 
104                for (final StepExecution stepExecution : stepExecutionSplitter.split(masterStepExecution, gridSize)) {
105 
106                        final FutureTask<StepExecution> task = new FutureTask<StepExecution>(new Callable<StepExecution>() {
107                                public StepExecution call() throws Exception {
108                                        step.execute(stepExecution);
109                                        return stepExecution;
110                                }
111                        });
112 
113                        try {
114                                taskExecutor.execute(task);
115                                tasks.add(task);
116                        }
117                        catch (TaskRejectedException e) {
118                                // couldn't execute one of the tasks
119                                ExitStatus exitStatus = ExitStatus.FAILED
120                                                .addExitDescription("TaskExecutor rejected the task for this step.");
121                                /*
122                                 * Set the status in case the caller is tracking it through the
123                                 * JobExecution.
124                                 */
125                                stepExecution.setStatus(BatchStatus.FAILED);
126                                stepExecution.setExitStatus(exitStatus);
127                                result.add(stepExecution);
128                        }
129 
130                }
131 
132                for (Future<StepExecution> task : tasks) {
133                        result.add(task.get());
134                }
135                return result;
136 
137        }
138 
139}

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