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.item; |
17 | |
18 | import org.springframework.batch.core.StepListener; |
19 | import org.springframework.batch.core.Step; |
20 | import org.springframework.batch.core.StepExecutionListener; |
21 | import org.springframework.batch.item.ItemReader; |
22 | import org.springframework.batch.item.ItemStream; |
23 | import org.springframework.batch.item.ItemWriter; |
24 | import org.springframework.batch.repeat.RepeatOperations; |
25 | import org.springframework.batch.repeat.support.RepeatTemplate; |
26 | |
27 | /** |
28 | * Factory bean for {@link Step} implementations allowing registration of |
29 | * listeners and also direct injection of the {@link RepeatOperations} needed at |
30 | * step and chunk level. |
31 | * |
32 | * @author Dave Syer |
33 | * |
34 | */ |
35 | public class RepeatOperationsStepFactoryBean extends AbstractStepFactoryBean { |
36 | |
37 | private ItemStream[] streams = new ItemStream[0]; |
38 | |
39 | private StepListener[] listeners = new StepListener[0]; |
40 | |
41 | private RepeatOperations chunkOperations = new RepeatTemplate(); |
42 | |
43 | private RepeatOperations stepOperations = new RepeatTemplate(); |
44 | |
45 | /** |
46 | * The streams to inject into the {@link Step}. Any instance of |
47 | * {@link ItemStream} can be used, and will then receive callbacks at the |
48 | * appropriate stage in the step. |
49 | * |
50 | * @param streams an array of listeners |
51 | */ |
52 | public void setStreams(ItemStream[] streams) { |
53 | this.streams = streams; |
54 | } |
55 | |
56 | /** |
57 | * The listeners to inject into the {@link Step}. Any instance of |
58 | * {@link StepListener} can be used, and will then receive callbacks at the |
59 | * appropriate stage in the step. |
60 | * |
61 | * @param listeners an array of listeners |
62 | */ |
63 | public void setListeners(StepListener[] listeners) { |
64 | this.listeners = listeners; |
65 | } |
66 | |
67 | /** |
68 | * The {@link RepeatOperations} to use for the outer loop of the batch |
69 | * processing. Should be set up by the caller through a factory. Defaults to |
70 | * a plain {@link RepeatTemplate}. |
71 | * |
72 | * @param stepOperations a {@link RepeatOperations} instance. |
73 | */ |
74 | public void setStepOperations(RepeatOperations stepOperations) { |
75 | this.stepOperations = stepOperations; |
76 | } |
77 | |
78 | /** |
79 | * The {@link RepeatOperations} to use for the inner loop of the batch |
80 | * processing. should be set up by the caller through a factory. defaults to |
81 | * a plain {@link RepeatTemplate}. |
82 | * |
83 | * @param chunkOperations a {@link RepeatOperations} instance. |
84 | */ |
85 | public void setChunkOperations(RepeatOperations chunkOperations) { |
86 | this.chunkOperations = chunkOperations; |
87 | } |
88 | |
89 | /** |
90 | * @param step |
91 | * |
92 | */ |
93 | protected void applyConfiguration(ItemOrientedStep step) { |
94 | |
95 | super.applyConfiguration(step); |
96 | |
97 | step.setStreams(streams); |
98 | |
99 | ItemReader itemReader = getItemReader(); |
100 | ItemWriter itemWriter = getItemWriter(); |
101 | |
102 | /* |
103 | * Since we are going to wrap these things with listener callbacks we |
104 | * need to register them here because the step will not know we did |
105 | * that. |
106 | */ |
107 | if (itemReader instanceof ItemStream) { |
108 | step.registerStream((ItemStream) itemReader); |
109 | } |
110 | if (itemReader instanceof StepExecutionListener) { |
111 | step.registerStepExecutionListener((StepExecutionListener) itemReader); |
112 | } |
113 | if (itemWriter instanceof ItemStream) { |
114 | step.registerStream((ItemStream) itemWriter); |
115 | } |
116 | if (itemWriter instanceof StepExecutionListener) { |
117 | step.registerStepExecutionListener((StepExecutionListener) itemWriter); |
118 | } |
119 | |
120 | BatchListenerFactoryHelper helper = new BatchListenerFactoryHelper(); |
121 | |
122 | StepExecutionListener[] stepListeners = helper.getStepListeners(listeners); |
123 | itemReader = helper.getItemReader(itemReader, listeners); |
124 | itemWriter = helper.getItemWriter(itemWriter, listeners); |
125 | RepeatOperations stepOperations = helper.addChunkListeners(this.stepOperations, listeners); |
126 | |
127 | // In case they are used by subclasses: |
128 | setItemReader(itemReader); |
129 | setItemWriter(itemWriter); |
130 | |
131 | step.setStepExecutionListeners(stepListeners); |
132 | step.setItemHandler(new SimpleItemHandler(itemReader, itemWriter)); |
133 | |
134 | step.setChunkOperations(chunkOperations); |
135 | step.setStepOperations(stepOperations); |
136 | |
137 | } |
138 | |
139 | } |