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 | |
17 | package org.springframework.batch.core.partition.support; |
18 | |
19 | import java.util.ArrayList; |
20 | import java.util.Collection; |
21 | import java.util.HashSet; |
22 | import java.util.Set; |
23 | import java.util.concurrent.Callable; |
24 | import java.util.concurrent.Future; |
25 | import java.util.concurrent.FutureTask; |
26 | |
27 | import org.springframework.batch.core.BatchStatus; |
28 | import org.springframework.batch.core.ExitStatus; |
29 | import org.springframework.batch.core.Step; |
30 | import org.springframework.batch.core.StepExecution; |
31 | import org.springframework.batch.core.partition.PartitionHandler; |
32 | import org.springframework.batch.core.partition.StepExecutionSplitter; |
33 | import org.springframework.batch.core.step.StepHolder; |
34 | import org.springframework.beans.factory.InitializingBean; |
35 | import org.springframework.core.task.SyncTaskExecutor; |
36 | import org.springframework.core.task.TaskExecutor; |
37 | import org.springframework.core.task.TaskRejectedException; |
38 | import 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 | */ |
49 | public 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 | } |