|
16 | 16 |
|
17 | 17 | package org.springframework.integration.file.remote.aop;
|
18 | 18 |
|
19 |
| -import java.util.ArrayList; |
20 |
| -import java.util.Iterator; |
21 | 19 | import java.util.List;
|
22 | 20 |
|
23 |
| -import org.apache.commons.logging.Log; |
24 |
| -import org.apache.commons.logging.LogFactory; |
25 |
| - |
26 | 21 | import org.springframework.integration.aop.AbstractMessageSourceAdvice;
|
27 | 22 | import org.springframework.integration.core.MessageSource;
|
28 | 23 | import org.springframework.integration.file.remote.AbstractRemoteFileStreamingMessageSource;
|
|
37 | 32 | * @author Gary Russell
|
38 | 33 | * @author Michael Forstner
|
39 | 34 | * @author Artem Bilan
|
| 35 | + * @author David Turanski |
40 | 36 | *
|
41 | 37 | * @since 5.0.7
|
42 | 38 | *
|
@@ -88,167 +84,29 @@ public Message<?> afterReceive(Message<?> result, MessageSource<?> source) {
|
88 | 84 | return result;
|
89 | 85 | }
|
90 | 86 |
|
91 |
| - /** |
92 |
| - * Implementations can reconfigure the message source before and/or after |
93 |
| - * a poll. |
94 |
| - */ |
95 |
| - public interface RotationPolicy { |
96 |
| - |
97 |
| - /** |
98 |
| - * Invoked before the message source receive() method. |
99 |
| - * @param source the message source. |
100 |
| - */ |
101 |
| - void beforeReceive(MessageSource<?> source); |
102 |
| - |
103 |
| - /** |
104 |
| - * Invoked after the message source receive() method. |
105 |
| - * @param messageReceived true if a message was received. |
106 |
| - * @param source the message source. |
107 |
| - */ |
108 |
| - void afterReceive(boolean messageReceived, MessageSource<?> source); |
109 |
| - |
110 |
| - } |
111 |
| - |
112 |
| - /** |
113 |
| - * Standard rotation policy; iterates over key/directory pairs; when the end |
114 |
| - * is reached, starts again at the beginning. If the fair option is true |
115 |
| - * the rotation occurs on every poll, regardless of result. Otherwise rotation |
116 |
| - * occurs when the current pair returns no message. |
117 |
| - */ |
118 |
| - public static class StandardRotationPolicy implements RotationPolicy { |
119 |
| - |
120 |
| - protected final Log logger = LogFactory.getLog(getClass()); |
121 |
| - |
122 |
| - protected final DelegatingSessionFactory<?> factory; |
123 |
| - |
124 |
| - private final List<KeyDirectory> keyDirectories = new ArrayList<>(); |
125 |
| - |
126 |
| - private final boolean fair; |
127 |
| - |
128 |
| - private volatile Iterator<KeyDirectory> iterator; |
129 |
| - |
130 |
| - private volatile KeyDirectory current; |
| 87 | + public static class StandardRotationPolicy extends AbstractStandardRotationPolicy { |
131 | 88 |
|
132 |
| - private volatile boolean initialized; |
133 | 89 |
|
134 | 90 | public StandardRotationPolicy(DelegatingSessionFactory<?> factory, List<KeyDirectory> keyDirectories,
|
135 | 91 | boolean fair) {
|
136 |
| - |
137 |
| - Assert.notNull(factory, "factory cannot be null"); |
138 |
| - Assert.notNull(keyDirectories, "keyDirectories cannot be null"); |
139 |
| - Assert.isTrue(keyDirectories.size() > 0, "At least one KeyDirectory is required"); |
140 |
| - this.factory = factory; |
141 |
| - this.keyDirectories.addAll(keyDirectories); |
142 |
| - this.fair = fair; |
143 |
| - this.iterator = this.keyDirectories.iterator(); |
144 |
| - } |
145 |
| - |
146 |
| - protected Iterator<KeyDirectory> getIterator() { |
147 |
| - return this.iterator; |
148 |
| - } |
149 |
| - |
150 |
| - protected void setIterator(Iterator<KeyDirectory> iterator) { |
151 |
| - this.iterator = iterator; |
152 |
| - } |
153 |
| - |
154 |
| - protected boolean isInitialized() { |
155 |
| - return this.initialized; |
156 |
| - } |
157 |
| - |
158 |
| - protected void setInitialized(boolean initialized) { |
159 |
| - this.initialized = initialized; |
160 |
| - } |
161 |
| - |
162 |
| - protected DelegatingSessionFactory<?> getFactory() { |
163 |
| - return this.factory; |
164 |
| - } |
165 |
| - |
166 |
| - protected List<KeyDirectory> getKeyDirectories() { |
167 |
| - return this.keyDirectories; |
168 |
| - } |
169 |
| - |
170 |
| - protected boolean isFair() { |
171 |
| - return this.fair; |
172 |
| - } |
173 |
| - |
174 |
| - protected KeyDirectory getCurrent() { |
175 |
| - return this.current; |
176 |
| - } |
177 |
| - |
178 |
| - @Override |
179 |
| - public void beforeReceive(MessageSource<?> source) { |
180 |
| - if (this.fair || !this.initialized) { |
181 |
| - configureSource(source); |
182 |
| - this.initialized = true; |
183 |
| - } |
184 |
| - if (this.logger.isTraceEnabled()) { |
185 |
| - this.logger.trace("Next poll is for " + this.current); |
186 |
| - } |
187 |
| - this.factory.setThreadKey(this.current.getKey()); |
| 92 | + super(factory, keyDirectories, fair); |
188 | 93 | }
|
189 | 94 |
|
190 | 95 | @Override
|
191 |
| - public void afterReceive(boolean messageReceived, MessageSource<?> source) { |
192 |
| - if (this.logger.isTraceEnabled()) { |
193 |
| - this.logger.trace("Poll produced " |
194 |
| - + (messageReceived ? "a" : "no") |
195 |
| - + " message"); |
196 |
| - } |
197 |
| - this.factory.clearThreadKey(); |
198 |
| - if (!this.fair && !messageReceived) { |
199 |
| - configureSource(source); |
200 |
| - } |
201 |
| - } |
202 |
| - |
203 |
| - protected void configureSource(MessageSource<?> source) { |
| 96 | + protected void onRotation(MessageSource<?> source) { |
204 | 97 | Assert.isTrue(source instanceof AbstractInboundFileSynchronizingMessageSource
|
205 | 98 | || source instanceof AbstractRemoteFileStreamingMessageSource,
|
206 | 99 | "source must be an AbstractInboundFileSynchronizingMessageSource or a "
|
207 | 100 | + "AbstractRemoteFileStreamingMessageSource");
|
208 |
| - if (!this.iterator.hasNext()) { |
209 |
| - this.iterator = this.keyDirectories.iterator(); |
210 |
| - } |
211 |
| - this.current = this.iterator.next(); |
| 101 | + |
212 | 102 | if (source instanceof AbstractRemoteFileStreamingMessageSource) {
|
213 |
| - ((AbstractRemoteFileStreamingMessageSource<?>) source).setRemoteDirectory(this.current.getDirectory()); |
| 103 | + ((AbstractRemoteFileStreamingMessageSource<?>) source).setRemoteDirectory(getCurrent().getDirectory()); |
214 | 104 | }
|
215 | 105 | else {
|
216 | 106 | ((AbstractInboundFileSynchronizingMessageSource<?>) source).getSynchronizer()
|
217 |
| - .setRemoteDirectory(this.current.getDirectory()); |
| 107 | + .setRemoteDirectory(getCurrent().getDirectory()); |
218 | 108 | }
|
219 | 109 | }
|
220 | 110 |
|
221 | 111 | }
|
222 |
| - |
223 |
| - /** |
224 |
| - * A {@link DelegatingSessionFactory} key/directory pair. |
225 |
| - */ |
226 |
| - public static class KeyDirectory { |
227 |
| - |
228 |
| - private final Object key; |
229 |
| - |
230 |
| - private final String directory; |
231 |
| - |
232 |
| - public KeyDirectory(Object key, String directory) { |
233 |
| - Assert.notNull(key, "key cannot be null"); |
234 |
| - Assert.notNull(directory, "directory cannot be null"); |
235 |
| - this.key = key; |
236 |
| - this.directory = directory; |
237 |
| - } |
238 |
| - |
239 |
| - public Object getKey() { |
240 |
| - return this.key; |
241 |
| - } |
242 |
| - |
243 |
| - public String getDirectory() { |
244 |
| - return this.directory; |
245 |
| - } |
246 |
| - |
247 |
| - @Override |
248 |
| - public String toString() { |
249 |
| - return "KeyDirectory [key=" + this.key.toString() + ", directory=" + this.directory + "]"; |
250 |
| - } |
251 |
| - |
252 |
| - } |
253 |
| - |
254 | 112 | }
|
0 commit comments