001/*
002 * Copyright 2006-2013 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.springframework.batch.core.partition.support;
017
018import java.util.Collection;
019import java.util.Set;
020
021import org.springframework.batch.core.StepExecution;
022import org.springframework.batch.core.partition.PartitionHandler;
023import org.springframework.batch.core.partition.StepExecutionSplitter;
024
025/**
026 * Base {@link PartitionHandler} implementation providing common base
027 * features. Subclasses are expected to implement only the
028 * {@link #doHandle(org.springframework.batch.core.StepExecution, java.util.Set)}
029 * method which returns with the result of the execution(s) or an exception if
030 * the step failed to process.
031 *
032 * @author Sebastien Gerard
033 * @author Dave Syer
034 */
035public abstract class AbstractPartitionHandler implements PartitionHandler {
036
037        private int gridSize = 1;
038
039        /**
040         * Executes the specified {@link StepExecution} instances and returns an updated
041         * view of them. Throws an {@link Exception} if anything goes wrong.
042         *
043         * @param masterStepExecution the whole partition execution
044         * @param partitionStepExecutions the {@link StepExecution} instances to execute
045         * @return an updated view of these completed {@link StepExecution} instances
046         * @throws Exception if anything goes wrong. This allows implementations to
047         * be liberal and rely on the caller to translate an exception into a step
048         * failure as necessary.
049         */
050        protected abstract Set<StepExecution> doHandle(StepExecution masterStepExecution,
051                        Set<StepExecution> partitionStepExecutions) throws Exception;
052
053        /**
054         * @see PartitionHandler#handle(StepExecutionSplitter, StepExecution)
055         */
056        @Override
057        public Collection<StepExecution> handle(final StepExecutionSplitter stepSplitter,
058                        final StepExecution masterStepExecution) throws Exception {
059                final Set<StepExecution> stepExecutions = stepSplitter.split(masterStepExecution, gridSize);
060
061                return doHandle(masterStepExecution, stepExecutions);
062        }
063
064        /**
065         * Returns the number of step executions.
066         *
067         * @return the number of step executions
068         */
069        public int getGridSize() {
070                return gridSize;
071        }
072
073        /**
074         * Passed to the {@link StepExecutionSplitter} in the
075         * {@link #handle(StepExecutionSplitter, StepExecution)} method, instructing
076         * it how many {@link StepExecution} instances are required, ideally. The
077         * {@link StepExecutionSplitter} is allowed to ignore the grid size in the
078         * case of a restart, since the input data partitions must be preserved.
079         *
080         * @param gridSize the number of step executions that will be created
081         */
082        public void setGridSize(int gridSize) {
083                this.gridSize = gridSize;
084        }
085
086}
087