1 | /* |
2 | * Copyright 2006-2013 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.step.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.partition.support.Partitioner; |
22 | import org.springframework.batch.core.step.tasklet.Tasklet; |
23 | import org.springframework.batch.repeat.CompletionPolicy; |
24 | |
25 | /** |
26 | * Convenient entry point for building all kinds of steps. Use this as a factory for fluent builders of any step. |
27 | * |
28 | * @author Dave Syer |
29 | * |
30 | * @since 2.2 |
31 | */ |
32 | public class StepBuilder extends StepBuilderHelper<StepBuilder> { |
33 | |
34 | /** |
35 | * Initialize a step builder for a step with the given name. |
36 | * |
37 | * @param name the name of the step |
38 | */ |
39 | public StepBuilder(String name) { |
40 | super(name); |
41 | } |
42 | |
43 | /** |
44 | * Build a step with a custom tasklet, not necessarily item processing. |
45 | * |
46 | * @param tasklet a tasklet |
47 | * @return a {@link TaskletStepBuilder} |
48 | */ |
49 | public TaskletStepBuilder tasklet(Tasklet tasklet) { |
50 | return new TaskletStepBuilder(this).tasklet(tasklet); |
51 | } |
52 | |
53 | /** |
54 | * Build a step that processes items in chunks with the size provided. To extend the step to being fault tolerant, |
55 | * call the {@link SimpleStepBuilder#faultTolerant()} method on the builder. In most cases you will want to |
56 | * parameterize your call to this method, to preserve the type safety of your readers and writers, e.g. |
57 | * |
58 | * <pre> |
59 | * new StepBuilder("step1").<Order, Ledger> chunk(100).reader(new OrderReader()).writer(new LedgerWriter()) |
60 | * // ... etc. |
61 | * </pre> |
62 | * |
63 | * @param chunkSize the chunk size (commit interval) |
64 | * @return a {@link SimpleStepBuilder} |
65 | * @param <I> the type of item to be processed as input |
66 | * @param <O> the type of item to be output |
67 | */ |
68 | public <I, O> SimpleStepBuilder<I, O> chunk(int chunkSize) { |
69 | return new SimpleStepBuilder<I, O>(this).chunk(chunkSize); |
70 | } |
71 | |
72 | /** |
73 | * Build a step that processes items in chunks with the completion policy provided. To extend the step to being |
74 | * fault tolerant, call the {@link SimpleStepBuilder#faultTolerant()} method on the builder. In most cases you will |
75 | * want to parameterize your call to this method, to preserve the type safety of your readers and writers, e.g. |
76 | * |
77 | * <pre> |
78 | * new StepBuilder("step1").<Order, Ledger> chunk(100).reader(new OrderReader()).writer(new LedgerWriter()) |
79 | * // ... etc. |
80 | * </pre> |
81 | * |
82 | * @param completionPolicy the completion policy to use to control chunk processing |
83 | * @return a {@link SimpleStepBuilder} |
84 | * @param <I> the type of item to be processed as input |
85 | * @param <O> the type of item to be output * |
86 | */ |
87 | public <I, O> SimpleStepBuilder<I, O> chunk(CompletionPolicy completionPolicy) { |
88 | return new SimpleStepBuilder<I, O>(this).chunk(completionPolicy); |
89 | } |
90 | |
91 | /** |
92 | * Create a partition step builder for a remote (or local) step. |
93 | * |
94 | * @param stepName the name of the remote or delegate step |
95 | * @param partitioner a partitioner to be used to construct new step executions |
96 | * @return a {@link PartitionStepBuilder} |
97 | */ |
98 | public PartitionStepBuilder partitioner(String stepName, Partitioner partitioner) { |
99 | return new PartitionStepBuilder(this).partitioner(stepName, partitioner); |
100 | } |
101 | |
102 | /** |
103 | * Create a partition step builder for a remote (or local) step. |
104 | * |
105 | * @param step the step to execute in parallel |
106 | * @return a PartitionStepBuilder |
107 | */ |
108 | public PartitionStepBuilder partitioner(Step step) { |
109 | return new PartitionStepBuilder(this).step(step); |
110 | } |
111 | |
112 | /** |
113 | * Create a new step builder that will execute a job. |
114 | * |
115 | * @param job a job to execute |
116 | * @return a {@link JobStepBuilder} |
117 | */ |
118 | public JobStepBuilder job(Job job) { |
119 | return new JobStepBuilder(this).job(job); |
120 | } |
121 | |
122 | /** |
123 | * Create a new step builder that will execute a flow. |
124 | * |
125 | * @param flow a flow to execute |
126 | * @return a {@link FlowStepBuilder} |
127 | */ |
128 | public FlowStepBuilder flow(Flow flow) { |
129 | return new FlowStepBuilder(this).flow(flow); |
130 | } |
131 | |
132 | } |