| 1 | /* |
| 2 | * Copyright 2006-2011 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.job.builder; |
| 17 | |
| 18 | import org.springframework.batch.core.Job; |
| 19 | import org.springframework.batch.core.Step; |
| 20 | import org.springframework.batch.core.job.flow.Flow; |
| 21 | import org.springframework.batch.core.job.flow.FlowJob; |
| 22 | import org.springframework.batch.core.step.builder.StepBuilderException; |
| 23 | |
| 24 | /** |
| 25 | * A job builder for {@link FlowJob} instances. A flow job delegates processing to a nested flow composed of steps and |
| 26 | * conditional transitions between steps. |
| 27 | * |
| 28 | * @author Dave Syer |
| 29 | * |
| 30 | * @since 2.2 |
| 31 | */ |
| 32 | public class FlowJobBuilder extends JobBuilderHelper<FlowJobBuilder> { |
| 33 | |
| 34 | private Flow flow; |
| 35 | |
| 36 | /** |
| 37 | * Create a new builder initialized with any properties in the parent. The parent is copied, so it can be re-used. |
| 38 | * |
| 39 | * @param parent a parent helper containing common job properties |
| 40 | */ |
| 41 | public FlowJobBuilder(JobBuilderHelper<?> parent) { |
| 42 | super(parent); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Start a job with this flow, but expect to transition from there to other flows or steps. |
| 47 | * |
| 48 | * @param flow the flow to start with |
| 49 | * @return a builder to enable fluent chaining |
| 50 | */ |
| 51 | public JobFlowBuilder start(Flow flow) { |
| 52 | return new JobFlowBuilder(this, flow); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Start a job with this step, but expect to transition from there to other flows or steps. |
| 57 | * |
| 58 | * @param step the step to start with |
| 59 | * @return a builder to enable fluent chaining |
| 60 | */ |
| 61 | public JobFlowBuilder start(Step step) { |
| 62 | return new JobFlowBuilder(this, step); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Provide a single flow to execute as the job. |
| 67 | * |
| 68 | * @param flow the flow to execute |
| 69 | * @return this for fluent chaining |
| 70 | */ |
| 71 | protected FlowJobBuilder flow(Flow flow) { |
| 72 | this.flow = flow; |
| 73 | return this; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Build a job that executes the flow provided, normally composed of other steps. |
| 78 | * |
| 79 | * @return a flow job |
| 80 | */ |
| 81 | public Job build() { |
| 82 | FlowJob job = new FlowJob(); |
| 83 | job.setName(getName()); |
| 84 | job.setFlow(flow); |
| 85 | super.enhance(job); |
| 86 | try { |
| 87 | job.afterPropertiesSet(); |
| 88 | } |
| 89 | catch (Exception e) { |
| 90 | throw new StepBuilderException(e); |
| 91 | } |
| 92 | return job; |
| 93 | } |
| 94 | |
| 95 | } |