1 | /* |
2 | * Copyright 2006-2007 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.tasklet; |
17 | |
18 | import java.util.concurrent.Callable; |
19 | |
20 | import org.springframework.batch.core.StepContribution; |
21 | import org.springframework.batch.core.scope.context.ChunkContext; |
22 | import org.springframework.batch.repeat.RepeatStatus; |
23 | import org.springframework.beans.factory.InitializingBean; |
24 | import org.springframework.util.Assert; |
25 | |
26 | /** |
27 | * Adapts a {@link Callable}<{@link RepeatStatus}> to the {@link Tasklet} |
28 | * interface. |
29 | * |
30 | * @author Dave Syer |
31 | * |
32 | */ |
33 | public class CallableTaskletAdapter implements Tasklet, InitializingBean { |
34 | |
35 | private Callable<RepeatStatus> callable; |
36 | |
37 | /** |
38 | * Public setter for the {@link Callable}. |
39 | * @param callable the {@link Callable} to set |
40 | */ |
41 | public void setCallable(Callable<RepeatStatus> callable) { |
42 | this.callable = callable; |
43 | } |
44 | |
45 | /** |
46 | * Assert that the callable is set. |
47 | * |
48 | * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet() |
49 | */ |
50 | public void afterPropertiesSet() throws Exception { |
51 | Assert.notNull(callable); |
52 | } |
53 | |
54 | /** |
55 | * Execute the provided Callable and return its {@link RepeatStatus}. Ignores |
56 | * the {@link StepContribution} and the attributes. |
57 | * @see Tasklet#execute(StepContribution, ChunkContext) |
58 | */ |
59 | public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception { |
60 | return callable.call(); |
61 | } |
62 | |
63 | } |