| 1 | /* |
| 2 | * Copyright 2012-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 java.util.LinkedHashSet; |
| 19 | import java.util.Set; |
| 20 | |
| 21 | import org.springframework.batch.core.ChunkListener; |
| 22 | import org.springframework.batch.core.Step; |
| 23 | import org.springframework.batch.core.StepExecutionListener; |
| 24 | import org.springframework.batch.core.step.tasklet.Tasklet; |
| 25 | import org.springframework.batch.core.step.tasklet.TaskletStep; |
| 26 | import org.springframework.batch.item.ItemStream; |
| 27 | import org.springframework.batch.repeat.RepeatOperations; |
| 28 | import org.springframework.batch.repeat.exception.DefaultExceptionHandler; |
| 29 | import org.springframework.batch.repeat.exception.ExceptionHandler; |
| 30 | import org.springframework.batch.repeat.support.RepeatTemplate; |
| 31 | import org.springframework.batch.repeat.support.TaskExecutorRepeatTemplate; |
| 32 | import org.springframework.core.task.SyncTaskExecutor; |
| 33 | import org.springframework.core.task.TaskExecutor; |
| 34 | import org.springframework.transaction.interceptor.TransactionAttribute; |
| 35 | |
| 36 | /** |
| 37 | * Base class for step builders that want to build a {@link TaskletStep}. Handles common concerns across all tasklet |
| 38 | * step variants, which are mostly to do with the type of tasklet they carry. |
| 39 | * |
| 40 | * @author Dave Syer |
| 41 | * |
| 42 | * @since 2.2 |
| 43 | * |
| 44 | * @param <B> the type of builder represented |
| 45 | */ |
| 46 | public abstract class AbstractTaskletStepBuilder<B extends AbstractTaskletStepBuilder<B>> extends |
| 47 | StepBuilderHelper<AbstractTaskletStepBuilder<B>> { |
| 48 | |
| 49 | protected Set<ChunkListener> chunkListeners = new LinkedHashSet<ChunkListener>(); |
| 50 | |
| 51 | private RepeatOperations stepOperations; |
| 52 | |
| 53 | private TransactionAttribute transactionAttribute; |
| 54 | |
| 55 | private Set<ItemStream> streams = new LinkedHashSet<ItemStream>(); |
| 56 | |
| 57 | private ExceptionHandler exceptionHandler = new DefaultExceptionHandler(); |
| 58 | |
| 59 | private int throttleLimit = TaskExecutorRepeatTemplate.DEFAULT_THROTTLE_LIMIT; |
| 60 | |
| 61 | private TaskExecutor taskExecutor; |
| 62 | |
| 63 | public AbstractTaskletStepBuilder(StepBuilderHelper<?> parent) { |
| 64 | super(parent); |
| 65 | } |
| 66 | |
| 67 | protected abstract Tasklet createTasklet(); |
| 68 | |
| 69 | /** |
| 70 | * Build the step from the components collected by the fluent setters. Delegates first to {@link #enhance(Step)} and |
| 71 | * then to {@link #createTasklet()} in subclasses to create the actual tasklet. |
| 72 | * |
| 73 | * @return a tasklet step fully configured and read to execute |
| 74 | */ |
| 75 | public TaskletStep build() { |
| 76 | |
| 77 | registerStepListenerAsChunkListener(); |
| 78 | |
| 79 | TaskletStep step = new TaskletStep(getName()); |
| 80 | |
| 81 | super.enhance(step); |
| 82 | |
| 83 | step.setChunkListeners(chunkListeners.toArray(new ChunkListener[0])); |
| 84 | |
| 85 | if (transactionAttribute != null) { |
| 86 | step.setTransactionAttribute(transactionAttribute); |
| 87 | } |
| 88 | |
| 89 | if (stepOperations == null) { |
| 90 | |
| 91 | stepOperations = new RepeatTemplate(); |
| 92 | |
| 93 | if (taskExecutor != null) { |
| 94 | TaskExecutorRepeatTemplate repeatTemplate = new TaskExecutorRepeatTemplate(); |
| 95 | repeatTemplate.setTaskExecutor(taskExecutor); |
| 96 | repeatTemplate.setThrottleLimit(throttleLimit); |
| 97 | stepOperations = repeatTemplate; |
| 98 | } |
| 99 | |
| 100 | ((RepeatTemplate) stepOperations).setExceptionHandler(exceptionHandler); |
| 101 | |
| 102 | } |
| 103 | step.setStepOperations(stepOperations); |
| 104 | step.setTasklet(createTasklet()); |
| 105 | |
| 106 | step.setStreams(streams.toArray(new ItemStream[0])); |
| 107 | |
| 108 | try { |
| 109 | step.afterPropertiesSet(); |
| 110 | } |
| 111 | catch (Exception e) { |
| 112 | throw new StepBuilderException(e); |
| 113 | } |
| 114 | |
| 115 | return step; |
| 116 | |
| 117 | } |
| 118 | |
| 119 | private void registerStepListenerAsChunkListener() { |
| 120 | for (StepExecutionListener stepExecutionListener: properties.getStepExecutionListeners()){ |
| 121 | if (stepExecutionListener instanceof ChunkListener){ |
| 122 | listener((ChunkListener)stepExecutionListener); |
| 123 | } |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Register a chunk listener. |
| 129 | * |
| 130 | * @param listener the listener to register |
| 131 | * @return this for fluent chaining |
| 132 | */ |
| 133 | public AbstractTaskletStepBuilder<B> listener(ChunkListener listener) { |
| 134 | chunkListeners.add(listener); |
| 135 | return this; |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Register a stream for callbacks that manage restart data. |
| 140 | * |
| 141 | * @param stream the stream to register |
| 142 | * @return this for fluent chaining |
| 143 | */ |
| 144 | public AbstractTaskletStepBuilder<B> stream(ItemStream stream) { |
| 145 | streams.add(stream); |
| 146 | return this; |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Provide a task executor to use when executing the tasklet. Default is to use a single-threaded (synchronous) |
| 151 | * executor. |
| 152 | * |
| 153 | * @param taskExecutor the task executor to register |
| 154 | * @return this for fluent chaining |
| 155 | */ |
| 156 | public AbstractTaskletStepBuilder<B> taskExecutor(TaskExecutor taskExecutor) { |
| 157 | this.taskExecutor = taskExecutor; |
| 158 | return this; |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * In the case of an asynchronous {@link #taskExecutor(TaskExecutor)} the number of concurrent tasklet executions |
| 163 | * can be throttled (beyond any throttling provided by a thread pool). The throttle limit should be less than the |
| 164 | * data source pool size used in the job repository for this step. |
| 165 | * |
| 166 | * @param throttleLimit maximium number of concurrent tasklet executions allowed |
| 167 | * @return this for fluent chaining |
| 168 | */ |
| 169 | public AbstractTaskletStepBuilder<B> throttleLimit(int throttleLimit) { |
| 170 | this.throttleLimit = throttleLimit; |
| 171 | return this; |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Sets the exception handler to use in the case of tasklet failures. Default is to rethrow everything. |
| 176 | * |
| 177 | * @param exceptionHandler the exception handler |
| 178 | * @return this for fluent chaining |
| 179 | */ |
| 180 | public AbstractTaskletStepBuilder<B> exceptionHandler(ExceptionHandler exceptionHandler) { |
| 181 | this.exceptionHandler = exceptionHandler; |
| 182 | return this; |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * Sets the repeat template used for iterating the tasklet execution. By default it will terminate only when the |
| 187 | * tasklet returns FINISHED (or null). |
| 188 | * |
| 189 | * @param repeatTemplate a repeat template with rules for iterating |
| 190 | * @return this for fluent chaining |
| 191 | */ |
| 192 | public AbstractTaskletStepBuilder<B> stepOperations(RepeatOperations repeatTemplate) { |
| 193 | this.stepOperations = repeatTemplate; |
| 194 | return this; |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * Sets the transaction attributes for the tasklet execution. Defaults to the default values for the transaction |
| 199 | * manager, but can be manipulated to provide longer timeouts for instance. |
| 200 | * |
| 201 | * @param transactionAttribute a transaction attribute set |
| 202 | * @return this for fluent chaining |
| 203 | */ |
| 204 | public AbstractTaskletStepBuilder<B> transactionAttribute(TransactionAttribute transactionAttribute) { |
| 205 | this.transactionAttribute = transactionAttribute; |
| 206 | return this; |
| 207 | } |
| 208 | |
| 209 | /** |
| 210 | * Convenience method for subclasses to access the step operations that were injected by user. |
| 211 | * |
| 212 | * @return the repeat operations used to iterate the tasklet executions |
| 213 | */ |
| 214 | protected RepeatOperations getStepOperations() { |
| 215 | return stepOperations; |
| 216 | } |
| 217 | |
| 218 | /** |
| 219 | * Convenience method for subclasses to access the exception handler that was injected by user. |
| 220 | * |
| 221 | * @return the exception handler |
| 222 | */ |
| 223 | protected ExceptionHandler getExceptionHandler() { |
| 224 | return exceptionHandler; |
| 225 | } |
| 226 | |
| 227 | /** |
| 228 | * Convenience method for subclasses to determine if the step is concurrent. |
| 229 | * |
| 230 | * @return true if the tasklet is going to be run in multiple threads |
| 231 | */ |
| 232 | protected boolean concurrent() { |
| 233 | boolean concurrent = taskExecutor != null && !(taskExecutor instanceof SyncTaskExecutor); |
| 234 | return concurrent; |
| 235 | } |
| 236 | |
| 237 | } |