001/* 002 * Copyright 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.jsr.step.builder; 017 018import javax.batch.api.partition.PartitionReducer; 019 020import org.springframework.batch.core.Step; 021import org.springframework.batch.core.jsr.step.PartitionStep; 022import org.springframework.batch.core.partition.support.SimpleStepExecutionSplitter; 023import org.springframework.batch.core.partition.support.TaskExecutorPartitionHandler; 024import org.springframework.batch.core.step.builder.PartitionStepBuilder; 025import org.springframework.batch.core.step.builder.StepBuilderException; 026import org.springframework.batch.core.step.builder.StepBuilderHelper; 027import org.springframework.core.task.SyncTaskExecutor; 028 029/** 030 * An extension of the {@link PartitionStepBuilder} that uses {@link PartitionStep} 031 * so that JSR-352 specific semantics are honored. 032 * 033 * @author Michael Minella 034 * @since 3.0 035 */ 036public class JsrPartitionStepBuilder extends PartitionStepBuilder { 037 038 private PartitionReducer reducer; 039 040 /** 041 * @param parent parent step builder for basic step properties 042 */ 043 public JsrPartitionStepBuilder(StepBuilderHelper<?> parent) { 044 super(parent); 045 } 046 047 /** 048 * @param reducer used to provide a single callback at the beginning and end 049 * of a partitioned step. 050 * 051 * @return this 052 */ 053 public JsrPartitionStepBuilder reducer(PartitionReducer reducer) { 054 this.reducer = reducer; 055 return this; 056 } 057 058 @Override 059 public JsrPartitionStepBuilder step(Step step) { 060 super.step(step); 061 return this; 062 } 063 064 @Override 065 public Step build() { 066 PartitionStep step = new PartitionStep(); 067 step.setName(getName()); 068 super.enhance(step); 069 070 if (getPartitionHandler() != null) { 071 step.setPartitionHandler(getPartitionHandler()); 072 } 073 else { 074 TaskExecutorPartitionHandler partitionHandler = new TaskExecutorPartitionHandler(); 075 partitionHandler.setStep(getStep()); 076 if (getTaskExecutor() == null) { 077 taskExecutor(new SyncTaskExecutor()); 078 } 079 partitionHandler.setGridSize(getGridSize()); 080 partitionHandler.setTaskExecutor(getTaskExecutor()); 081 step.setPartitionHandler(partitionHandler); 082 } 083 084 if (getSplitter() != null) { 085 step.setStepExecutionSplitter(getSplitter()); 086 } 087 else { 088 089 boolean allowStartIfComplete = isAllowStartIfComplete(); 090 String name = getStepName(); 091 if (getStep() != null) { 092 try { 093 allowStartIfComplete = getStep().isAllowStartIfComplete(); 094 name = getStep().getName(); 095 } 096 catch (Exception e) { 097 logger.info("Ignored exception from step asking for name and allowStartIfComplete flag. " 098 + "Using default from enclosing PartitionStep (" + name + "," + allowStartIfComplete + ")."); 099 } 100 } 101 SimpleStepExecutionSplitter splitter = new SimpleStepExecutionSplitter(); 102 splitter.setPartitioner(getPartitioner()); 103 splitter.setJobRepository(getJobRepository()); 104 splitter.setAllowStartIfComplete(allowStartIfComplete); 105 splitter.setStepName(name); 106 splitter(splitter); 107 step.setStepExecutionSplitter(splitter); 108 109 } 110 111 if (getAggregator() != null) { 112 step.setStepExecutionAggregator(getAggregator()); 113 } 114 115 if(reducer != null) { 116 step.setPartitionReducer(reducer); 117 } 118 119 try { 120 step.afterPropertiesSet(); 121 } 122 catch (Exception e) { 123 throw new StepBuilderException(e); 124 } 125 126 return step; 127 128 } 129}