| 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.beans.factory.InitializingBean; |
| 34 | import org.springframework.core.task.SyncTaskExecutor; |
| 35 | import org.springframework.core.task.TaskExecutor; |
| 36 | import org.springframework.core.task.TaskRejectedException; |
| 37 | import 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 | */ |
| 48 | public 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 | } |