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 org.springframework.batch.core.Step; |
19 | import org.springframework.batch.core.StepExecution; |
20 | import org.springframework.batch.core.StepExecutionListener; |
21 | import org.springframework.batch.core.repository.JobRepository; |
22 | import org.springframework.batch.core.step.AbstractStep; |
23 | import org.springframework.batch.item.ExecutionContext; |
24 | import org.springframework.batch.repeat.ExitStatus; |
25 | import org.springframework.util.Assert; |
26 | |
27 | /** |
28 | * A {@link Step} that executes a {@link Tasklet} directly. This step does not |
29 | * manage transactions or any looping functionality. The tasklet should do this |
30 | * on its own. |
31 | * |
32 | * If the {@link Tasklet} itself implements {@link StepExecutionListener} it |
33 | * will be registered automatically, but its injected dependencies will not be. |
34 | * This is a good way to get access to job parameters and execution context if |
35 | * the tasklet is parameterized. |
36 | * |
37 | * @author Ben Hale |
38 | * @author Robert Kasanicky |
39 | */ |
40 | public class TaskletStep extends AbstractStep { |
41 | |
42 | private Tasklet tasklet; |
43 | |
44 | /** |
45 | * Register each of the objects as listeners. |
46 | * |
47 | * @deprecated use |
48 | * {@link #setStepExecutionListeners(StepExecutionListener[])} instead |
49 | */ |
50 | public void setStepListeners(StepExecutionListener[] listeners) { |
51 | setStepExecutionListeners(listeners); |
52 | } |
53 | |
54 | /** |
55 | * Check mandatory properties. |
56 | * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet() |
57 | */ |
58 | public void afterPropertiesSet() throws Exception { |
59 | super.afterPropertiesSet(); |
60 | Assert.notNull(tasklet, "Tasklet is mandatory for TaskletStep"); |
61 | if (tasklet instanceof StepExecutionListener) { |
62 | registerStepExecutionListener((StepExecutionListener) tasklet); |
63 | } |
64 | } |
65 | |
66 | /** |
67 | * Default constructor is useful for XML configuration. |
68 | */ |
69 | public TaskletStep() { |
70 | super(); |
71 | } |
72 | |
73 | /** |
74 | * Creates a new <code>Step</code> for executing a <code>Tasklet</code> |
75 | * |
76 | * @param tasklet The <code>Tasklet</code> to execute |
77 | * @param jobRepository The <code>JobRepository</code> to use for |
78 | * persistence of incremental state |
79 | */ |
80 | public TaskletStep(Tasklet tasklet, JobRepository jobRepository) { |
81 | this(); |
82 | this.tasklet = tasklet; |
83 | setJobRepository(jobRepository); |
84 | } |
85 | |
86 | /** |
87 | * Public setter for the {@link Tasklet}. |
88 | * @param tasklet the {@link Tasklet} to set |
89 | */ |
90 | public void setTasklet(Tasklet tasklet) { |
91 | this.tasklet = tasklet; |
92 | } |
93 | |
94 | /** |
95 | * Delegate to tasklet. |
96 | */ |
97 | protected ExitStatus doExecute(StepExecution stepExecution) throws Exception { |
98 | return tasklet.execute(); |
99 | } |
100 | |
101 | protected void close(ExecutionContext ctx) throws Exception { |
102 | } |
103 | |
104 | protected void open(ExecutionContext ctx) throws Exception { |
105 | } |
106 | |
107 | |
108 | |
109 | |
110 | |
111 | } |