001/* 002 * Copyright 2013-2018 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 org.springframework.batch.core.ChunkListener; 019import org.springframework.batch.core.Step; 020import org.springframework.batch.core.jsr.configuration.support.BatchPropertyContext; 021import org.springframework.batch.core.jsr.step.BatchletStep; 022import org.springframework.batch.core.step.builder.StepBuilderException; 023import org.springframework.batch.core.step.builder.StepBuilderHelper; 024import org.springframework.batch.core.step.builder.TaskletStepBuilder; 025import org.springframework.batch.core.step.tasklet.TaskletStep; 026import org.springframework.batch.item.ItemStream; 027import org.springframework.batch.repeat.support.RepeatTemplate; 028import org.springframework.batch.repeat.support.TaskExecutorRepeatTemplate; 029 030/** 031 * Extension of the {@link TaskletStepBuilder} that uses a {@link BatchletStep} instead 032 * of a {@link TaskletStep}. 033 * 034 * @author Michael Minella 035 * @author Mahmoud Ben Hassine 036 * @since 3.0 037 */ 038public class JsrBatchletStepBuilder extends TaskletStepBuilder { 039 040 private BatchPropertyContext batchPropertyContext; 041 042 /** 043 * @param context used to resolve lazy bound properties 044 */ 045 public void setBatchPropertyContext(BatchPropertyContext context) { 046 this.batchPropertyContext = context; 047 } 048 049 public JsrBatchletStepBuilder(StepBuilderHelper<? extends StepBuilderHelper<?>> parent) { 050 super(parent); 051 } 052 053 /** 054 * Build the step from the components collected by the fluent setters. Delegates first to {@link #enhance(Step)} and 055 * then to {@link #createTasklet()} in subclasses to create the actual tasklet. 056 * 057 * @return a tasklet step fully configured and read to execute 058 */ 059 @Override 060 public TaskletStep build() { 061 062 registerStepListenerAsChunkListener(); 063 064 BatchletStep step = new BatchletStep(getName(), batchPropertyContext); 065 066 super.enhance(step); 067 068 step.setChunkListeners(chunkListeners.toArray(new ChunkListener[0])); 069 070 if (getTransactionAttribute() != null) { 071 step.setTransactionAttribute(getTransactionAttribute()); 072 } 073 074 if (getStepOperations() == null) { 075 076 stepOperations(new RepeatTemplate()); 077 078 if (getTaskExecutor() != null) { 079 TaskExecutorRepeatTemplate repeatTemplate = new TaskExecutorRepeatTemplate(); 080 repeatTemplate.setTaskExecutor(getTaskExecutor()); 081 repeatTemplate.setThrottleLimit(getThrottleLimit()); 082 stepOperations(repeatTemplate); 083 } 084 085 ((RepeatTemplate) getStepOperations()).setExceptionHandler(getExceptionHandler()); 086 087 } 088 step.setStepOperations(getStepOperations()); 089 step.setTasklet(createTasklet()); 090 091 step.setStreams(getStreams().toArray(new ItemStream[0])); 092 093 try { 094 step.afterPropertiesSet(); 095 } 096 catch (Exception e) { 097 throw new StepBuilderException(e); 098 } 099 100 return step; 101 102 } 103}