001/* 002 * Copyright 2006-2011 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.step.builder; 017 018import org.springframework.batch.core.Job; 019import org.springframework.batch.core.Step; 020import org.springframework.batch.core.launch.JobLauncher; 021import org.springframework.batch.core.launch.support.SimpleJobLauncher; 022import org.springframework.batch.core.step.job.JobParametersExtractor; 023import org.springframework.batch.core.step.job.JobStep; 024 025/** 026 * A step builder for {@link JobStep} instances. A job step executes a nested {@link Job} with parameters taken from the 027 * parent job or from the step execution. 028 * 029 * @author Dave Syer 030 * 031 * @since 2.2 032 */ 033public class JobStepBuilder extends StepBuilderHelper<JobStepBuilder> { 034 035 private Job job; 036 037 private JobLauncher jobLauncher; 038 039 private JobParametersExtractor jobParametersExtractor; 040 041 /** 042 * Create a new builder initialized with any properties in the parent. The parent is copied, so it can be re-used. 043 * 044 * @param parent a parent helper containing common step properties 045 */ 046 public JobStepBuilder(StepBuilderHelper<?> parent) { 047 super(parent); 048 } 049 050 /** 051 * Provide a job to execute during the step. 052 * 053 * @param job the job to execute 054 * @return this for fluent chaining 055 */ 056 public JobStepBuilder job(Job job) { 057 this.job = job; 058 return this; 059 } 060 061 /** 062 * Add a job launcher. Defaults to a simple job launcher. 063 * 064 * @param jobLauncher the job launcher to use 065 * @return this for fluent chaining 066 */ 067 public JobStepBuilder launcher(JobLauncher jobLauncher) { 068 this.jobLauncher = jobLauncher; 069 return this; 070 } 071 072 /** 073 * Provide a job parameters extractor. Useful for extracting job parameters from the parent step execution context 074 * or job parameters. 075 * 076 * @param jobParametersExtractor the job parameters extractor to use 077 * @return this for fluent chaining 078 */ 079 public JobStepBuilder parametersExtractor(JobParametersExtractor jobParametersExtractor) { 080 this.jobParametersExtractor = jobParametersExtractor; 081 return this; 082 } 083 084 /** 085 * Build a step from the job provided. 086 * 087 * @return a new job step 088 */ 089 public Step build() { 090 091 JobStep step = new JobStep(); 092 step.setName(getName()); 093 super.enhance(step); 094 if (job != null) { 095 step.setJob(job); 096 } 097 if (jobParametersExtractor != null) { 098 step.setJobParametersExtractor(jobParametersExtractor); 099 } 100 if (jobLauncher == null) { 101 SimpleJobLauncher jobLauncher = new SimpleJobLauncher(); 102 jobLauncher.setJobRepository(getJobRepository()); 103 try { 104 jobLauncher.afterPropertiesSet(); 105 } 106 catch (Exception e) { 107 throw new StepBuilderException(e); 108 } 109 this.jobLauncher = jobLauncher; 110 } 111 step.setJobLauncher(jobLauncher); 112 try { 113 step.afterPropertiesSet(); 114 } 115 catch (Exception e) { 116 throw new StepBuilderException(e); 117 } 118 return step; 119 120 } 121 122}