1 | /* |
2 | * Copyright 2006-2013 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 | package org.springframework.batch.core.partition.support; |
17 | |
18 | import java.util.Collection; |
19 | import java.util.Set; |
20 | |
21 | import org.springframework.batch.core.StepExecution; |
22 | import org.springframework.batch.core.partition.PartitionHandler; |
23 | import org.springframework.batch.core.partition.StepExecutionSplitter; |
24 | |
25 | /** |
26 | * Base {@link PartitionHandler} implementation providing common base |
27 | * features. Subclasses are expected to implement only the |
28 | * {@link #doHandle(org.springframework.batch.core.StepExecution, java.util.Set)} |
29 | * method which returns with the result of the execution(s) or an exception if |
30 | * the step failed to process. |
31 | * |
32 | * @author Sebastien Gerard |
33 | * @author Dave Syer |
34 | */ |
35 | public abstract class AbstractPartitionHandler implements PartitionHandler { |
36 | |
37 | private int gridSize = 1; |
38 | |
39 | /** |
40 | * Executes the specified {@link StepExecution} instances and returns an updated |
41 | * view of them. Throws an {@link Exception} if anything goes wrong. |
42 | * |
43 | * @param masterStepExecution the whole partition execution |
44 | * @param partitionStepExecutions the {@link StepExecution} instances to execute |
45 | * @return an updated view of these completed {@link StepExecution} instances |
46 | * @throws Exception if anything goes wrong. This allows implementations to |
47 | * be liberal and rely on the caller to translate an exception into a step |
48 | * failure as necessary. |
49 | */ |
50 | protected abstract Set<StepExecution> doHandle(StepExecution masterStepExecution, |
51 | Set<StepExecution> partitionStepExecutions) throws Exception; |
52 | |
53 | /** |
54 | * @see PartitionHandler#handle(StepExecutionSplitter, StepExecution) |
55 | */ |
56 | @Override |
57 | public Collection<StepExecution> handle(final StepExecutionSplitter stepSplitter, |
58 | final StepExecution masterStepExecution) throws Exception { |
59 | final Set<StepExecution> stepExecutions = stepSplitter.split(masterStepExecution, gridSize); |
60 | |
61 | return doHandle(masterStepExecution, stepExecutions); |
62 | } |
63 | |
64 | /** |
65 | * Returns the number of step executions. |
66 | * |
67 | * @return the number of step executions |
68 | */ |
69 | public int getGridSize() { |
70 | return gridSize; |
71 | } |
72 | |
73 | /** |
74 | * Passed to the {@link StepExecutionSplitter} in the |
75 | * {@link #handle(StepExecutionSplitter, StepExecution)} method, instructing |
76 | * it how many {@link StepExecution} instances are required, ideally. The |
77 | * {@link StepExecutionSplitter} is allowed to ignore the grid size in the |
78 | * case of a restart, since the input data partitions must be preserved. |
79 | * |
80 | * @param gridSize the number of step executions that will be created |
81 | */ |
82 | public void setGridSize(int gridSize) { |
83 | this.gridSize = gridSize; |
84 | } |
85 | |
86 | } |
87 | |