-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
ResequenceDefinition.java
334 lines (299 loc) · 10.5 KB
/
ResequenceDefinition.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.camel.model;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementRef;
import javax.xml.bind.annotation.XmlElements;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;
import org.apache.camel.Expression;
import org.apache.camel.model.config.BatchResequencerConfig;
import org.apache.camel.model.config.ResequencerConfig;
import org.apache.camel.model.config.StreamResequencerConfig;
import org.apache.camel.model.language.ExpressionDefinition;
import org.apache.camel.processor.resequencer.ExpressionResultComparator;
import org.apache.camel.spi.Metadata;
/**
* Resequences (re-order) messages based on an expression
*/
@Metadata(label = "eip,routing")
@XmlRootElement(name = "resequence")
@XmlAccessorType(XmlAccessType.FIELD)
public class ResequenceDefinition extends ProcessorDefinition<ResequenceDefinition> {
@Metadata(required = false)
@XmlElements({
@XmlElement(name = "batch-config", type = BatchResequencerConfig.class),
@XmlElement(name = "stream-config", type = StreamResequencerConfig.class)}
)
private ResequencerConfig resequencerConfig;
@XmlTransient
private BatchResequencerConfig batchConfig;
@XmlTransient
private StreamResequencerConfig streamConfig;
@XmlElementRef @Metadata(required = true)
private ExpressionDefinition expression;
@XmlElementRef
private List<ProcessorDefinition<?>> outputs = new ArrayList<>();
public ResequenceDefinition() {
}
public ResequenceDefinition(Expression expression) {
if (expression != null) {
setExpression(ExpressionNodeHelper.toExpressionDefinition(expression));
}
}
public List<ProcessorDefinition<?>> getOutputs() {
return outputs;
}
public void setOutputs(List<ProcessorDefinition<?>> outputs) {
this.outputs = outputs;
}
@Override
public boolean isOutputSupported() {
return true;
}
// Fluent API
// -------------------------------------------------------------------------
/**
* Configures the stream-based resequencing algorithm using the default
* configuration.
*
* @return the builder
*/
public ResequenceDefinition stream() {
return stream(StreamResequencerConfig.getDefault());
}
/**
* Configures the batch-based resequencing algorithm using the default
* configuration.
*
* @return the builder
*/
public ResequenceDefinition batch() {
return batch(BatchResequencerConfig.getDefault());
}
/**
* Configures the stream-based resequencing algorithm using the given
* {@link StreamResequencerConfig}.
*
* @param config the config
* @return the builder
*/
public ResequenceDefinition stream(StreamResequencerConfig config) {
this.streamConfig = config;
this.batchConfig = null;
return this;
}
/**
* Configures the batch-based resequencing algorithm using the given
* {@link BatchResequencerConfig}.
*
* @param config the config
* @return the builder
*/
public ResequenceDefinition batch(BatchResequencerConfig config) {
this.batchConfig = config;
this.streamConfig = null;
return this;
}
/**
* Sets the timeout
* @param timeout timeout in millis
* @return the builder
*/
public ResequenceDefinition timeout(long timeout) {
if (streamConfig != null) {
streamConfig.setTimeout(timeout);
} else {
// initialize batch mode as its default mode
if (batchConfig == null) {
batch();
}
batchConfig.setBatchTimeout(timeout);
}
return this;
}
/**
* Sets the interval in milli seconds the stream resequencer will at most wait
* while waiting for condition of being able to deliver.
*
* @param deliveryAttemptInterval interval in millis
* @return the builder
*/
public ResequenceDefinition deliveryAttemptInterval(long deliveryAttemptInterval) {
if (streamConfig == null) {
throw new IllegalStateException("deliveryAttemptInterval() only supported for stream resequencer");
}
streamConfig.setDeliveryAttemptInterval(deliveryAttemptInterval);
return this;
}
/**
* Sets the rejectOld flag to throw an error when a message older than the last delivered message is processed
* @return the builder
*/
public ResequenceDefinition rejectOld() {
if (streamConfig == null) {
throw new IllegalStateException("rejectOld() only supported for stream resequencer");
}
streamConfig.setRejectOld(true);
return this;
}
/**
* Sets the in batch size for number of exchanges received
* @param batchSize the batch size
* @return the builder
*/
public ResequenceDefinition size(int batchSize) {
if (streamConfig != null) {
throw new IllegalStateException("size() only supported for batch resequencer");
}
// initialize batch mode as its default mode
if (batchConfig == null) {
batch();
}
batchConfig.setBatchSize(batchSize);
return this;
}
/**
* Sets the capacity for the stream resequencer
*
* @param capacity the capacity
* @return the builder
*/
public ResequenceDefinition capacity(int capacity) {
if (streamConfig == null) {
throw new IllegalStateException("capacity() only supported for stream resequencer");
}
streamConfig.setCapacity(capacity);
return this;
}
/**
* Enables duplicates for the batch resequencer mode
* @return the builder
*/
public ResequenceDefinition allowDuplicates() {
if (streamConfig != null) {
throw new IllegalStateException("allowDuplicates() only supported for batch resequencer");
}
// initialize batch mode as its default mode
if (batchConfig == null) {
batch();
}
batchConfig.setAllowDuplicates(true);
return this;
}
/**
* Enables reverse mode for the batch resequencer mode.
* <p/>
* This means the expression for determine the sequence order will be reversed.
* Can be used for Z..A or 9..0 ordering.
*
* @return the builder
*/
public ResequenceDefinition reverse() {
if (streamConfig != null) {
throw new IllegalStateException("reverse() only supported for batch resequencer");
}
// initialize batch mode as its default mode
if (batchConfig == null) {
batch();
}
batchConfig.setReverse(true);
return this;
}
/**
* If an incoming {@link org.apache.camel.Exchange} is invalid, then it will be ignored.
*
* @return builder
*/
public ResequenceDefinition ignoreInvalidExchanges() {
if (streamConfig != null) {
streamConfig.setIgnoreInvalidExchanges(true);
} else {
// initialize batch mode as its default mode
if (batchConfig == null) {
batch();
}
batchConfig.setIgnoreInvalidExchanges(true);
}
return this;
}
/**
* Sets the comparator to use for stream resequencer
*
* @param comparator the comparator
* @return the builder
*/
public ResequenceDefinition comparator(ExpressionResultComparator comparator) {
if (streamConfig == null) {
throw new IllegalStateException("comparator() only supported for stream resequencer");
}
streamConfig.setComparator(comparator);
return this;
}
@Override
public String toString() {
return "Resequencer[" + getExpression() + " -> " + getOutputs() + "]";
}
@Override
public String getShortName() {
return "resequence";
}
@Override
public String getLabel() {
return "resequencer[" + (getExpression() != null ? getExpression().getLabel() : "") + "]";
}
public ResequencerConfig getResequencerConfig() {
return resequencerConfig;
}
/**
* To configure the resequencer in using either batch or stream configuration. Will by default use batch configuration.
*/
public void setResequencerConfig(ResequencerConfig resequencerConfig) {
this.resequencerConfig = resequencerConfig;
}
public BatchResequencerConfig getBatchConfig() {
if (batchConfig == null && resequencerConfig != null && resequencerConfig instanceof BatchResequencerConfig) {
return (BatchResequencerConfig) resequencerConfig;
}
return batchConfig;
}
public StreamResequencerConfig getStreamConfig() {
if (streamConfig == null && resequencerConfig != null && resequencerConfig instanceof StreamResequencerConfig) {
return (StreamResequencerConfig) resequencerConfig;
}
return streamConfig;
}
public void setBatchConfig(BatchResequencerConfig batchConfig) {
this.batchConfig = batchConfig;
}
public void setStreamConfig(StreamResequencerConfig streamConfig) {
this.streamConfig = streamConfig;
}
public ExpressionDefinition getExpression() {
return expression;
}
/**
* Expression to use for re-ordering the messages, such as a header with a sequence number
*/
public void setExpression(ExpressionDefinition expression) {
this.expression = expression;
}
}