001/*
002 * Copyright 2006-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.step.builder;
017
018import org.springframework.batch.core.Job;
019import org.springframework.batch.core.Step;
020import org.springframework.batch.core.job.flow.Flow;
021import org.springframework.batch.core.partition.support.Partitioner;
022import org.springframework.batch.core.step.tasklet.Tasklet;
023import org.springframework.batch.repeat.CompletionPolicy;
024
025/**
026 * Convenient entry point for building all kinds of steps. Use this as a factory for fluent builders of any step.
027 *
028 * @author Dave Syer
029 *
030 * @since 2.2
031 */
032public class StepBuilder extends StepBuilderHelper<StepBuilder> {
033
034        /**
035         * Initialize a step builder for a step with the given name.
036         *
037         * @param name the name of the step
038         */
039        public StepBuilder(String name) {
040                super(name);
041        }
042
043        /**
044         * Build a step with a custom tasklet, not necessarily item processing.
045         *
046         * @param tasklet a tasklet
047         * @return a {@link TaskletStepBuilder}
048         */
049        public TaskletStepBuilder tasklet(Tasklet tasklet) {
050                return new TaskletStepBuilder(this).tasklet(tasklet);
051        }
052
053        /**
054         * Build a step that processes items in chunks with the size provided. To extend the step to being fault tolerant,
055         * call the {@link SimpleStepBuilder#faultTolerant()} method on the builder. In most cases you will want to
056         * parameterize your call to this method, to preserve the type safety of your readers and writers, e.g.
057         *
058         * <pre>
059         * new StepBuilder(&quot;step1&quot;).&lt;Order, Ledger&gt; chunk(100).reader(new OrderReader()).writer(new LedgerWriter())
060         * // ... etc.
061         * </pre>
062         *
063         * @param chunkSize the chunk size (commit interval)
064         * @return a {@link SimpleStepBuilder}
065         * @param <I> the type of item to be processed as input
066         * @param <O> the type of item to be output
067         */
068        public <I, O> SimpleStepBuilder<I, O> chunk(int chunkSize) {
069                return new SimpleStepBuilder<I, O>(this).chunk(chunkSize);
070        }
071
072        /**
073         * Build a step that processes items in chunks with the completion policy provided. To extend the step to being
074         * fault tolerant, call the {@link SimpleStepBuilder#faultTolerant()} method on the builder. In most cases you will
075         * want to parameterize your call to this method, to preserve the type safety of your readers and writers, e.g.
076         *
077         * <pre>
078         * new StepBuilder(&quot;step1&quot;).&lt;Order, Ledger&gt; chunk(100).reader(new OrderReader()).writer(new LedgerWriter())
079         * // ... etc.
080         * </pre>
081         *
082         * @param completionPolicy the completion policy to use to control chunk processing
083         * @return a {@link SimpleStepBuilder}
084         * @param <I> the type of item to be processed as input
085         * @param <O> the type of item to be output *
086         */
087        public <I, O> SimpleStepBuilder<I, O> chunk(CompletionPolicy completionPolicy) {
088                return new SimpleStepBuilder<I, O>(this).chunk(completionPolicy);
089        }
090
091        /**
092         * Create a partition step builder for a remote (or local) step.
093         *
094         * @param stepName the name of the remote or delegate step
095         * @param partitioner a partitioner to be used to construct new step executions
096         * @return a {@link PartitionStepBuilder}
097         */
098        public PartitionStepBuilder partitioner(String stepName, Partitioner partitioner) {
099                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}