-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
ToDynamicDefinition.java
335 lines (290 loc) · 12 KB
/
ToDynamicDefinition.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
335
/*
* 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 jakarta.xml.bind.annotation.XmlAccessType;
import jakarta.xml.bind.annotation.XmlAccessorType;
import jakarta.xml.bind.annotation.XmlAttribute;
import jakarta.xml.bind.annotation.XmlRootElement;
import jakarta.xml.bind.annotation.XmlTransient;
import org.apache.camel.ExchangePattern;
import org.apache.camel.Message;
import org.apache.camel.builder.EndpointProducerBuilder;
import org.apache.camel.spi.AsEndpointUri;
import org.apache.camel.spi.Metadata;
/**
* Sends the message to a dynamic endpoint
*/
@Metadata(label = "eip,routing")
@XmlRootElement(name = "toD")
@XmlAccessorType(XmlAccessType.FIELD)
public class ToDynamicDefinition extends NoOutputDefinition<ToDynamicDefinition> implements CopyableProcessorDefinition {
@XmlTransient
protected EndpointProducerBuilder endpointProducerBuilder;
@XmlAttribute
@Metadata(required = true)
private String uri;
@XmlAttribute
private String variableSend;
@XmlAttribute
private String variableReceive;
@XmlAttribute
@Metadata(label = "advanced", javaType = "org.apache.camel.ExchangePattern", enums = "InOnly,InOut")
private String pattern;
@XmlAttribute
@Metadata(label = "advanced", javaType = "java.lang.Integer")
private String cacheSize;
@XmlAttribute
@Metadata(label = "advanced", javaType = "java.lang.Boolean")
private String ignoreInvalidEndpoint;
@XmlAttribute
@Metadata(label = "advanced", defaultValue = "true", javaType = "java.lang.Boolean")
private String allowOptimisedComponents;
@XmlAttribute
@Metadata(label = "advanced", defaultValue = "true", javaType = "java.lang.Boolean")
private String autoStartComponents;
public ToDynamicDefinition() {
}
public ToDynamicDefinition(String uri) {
this.uri = uri;
}
protected ToDynamicDefinition(ToDynamicDefinition source) {
super(source);
this.endpointProducerBuilder = source.endpointProducerBuilder;
this.uri = source.uri;
this.variableSend = source.variableSend;
this.variableReceive = source.variableReceive;
this.pattern = source.pattern;
this.cacheSize = source.cacheSize;
this.ignoreInvalidEndpoint = source.ignoreInvalidEndpoint;
this.allowOptimisedComponents = source.allowOptimisedComponents;
this.autoStartComponents = source.autoStartComponents;
}
@Override
public String getShortName() {
return "toD";
}
@Override
public String toString() {
return "DynamicTo[" + getLabel() + "]";
}
@Override
public String getLabel() {
return uri;
}
// Fluent API
// -------------------------------------------------------------------------
/**
* The uri of the endpoint to send to. The uri can be dynamic computed using the
* {@link org.apache.camel.language.simple.SimpleLanguage} expression.
*/
public ToDynamicDefinition uri(@AsEndpointUri String uri) {
setUri(uri);
return this;
}
/**
* The uri of the endpoint to send to.
*
* @param endpointProducerBuilder the dynamic endpoint to send to (resolved using simple language by default)
*/
public ToDynamicDefinition uri(@AsEndpointUri EndpointProducerBuilder endpointProducerBuilder) {
setEndpointProducerBuilder(endpointProducerBuilder);
return this;
}
/**
* To use a variable as the source for the message body to send. This makes it handy to use variables for user data
* and to easily control what data to use for sending and receiving.
*
* Important: When using send variable then the message body is taken from this variable instead of the current
* {@link Message}, however the headers from the {@link Message} will still be used as well. In other words, the
* variable is used instead of the message body, but everything else is as usual.
*/
public ToDynamicDefinition variableReceive(String variableReceive) {
setVariableReceive(variableReceive);
return this;
}
/**
* To use a variable to store the received message body (only body, not headers). This is handy for easy access to
* the received message body via variables.
*
* Important: When using receive variable then the received body is stored only in this variable and <b>not</b> on
* the current {@link org.apache.camel.Message}.
*/
public ToDynamicDefinition variableSend(String variableSend) {
setVariableSend(variableSend);
return this;
}
/**
* Sets the optional {@link ExchangePattern} used to invoke this endpoint
*/
public ToDynamicDefinition pattern(ExchangePattern pattern) {
return pattern(pattern.name());
}
/**
* Sets the optional {@link ExchangePattern} used to invoke this endpoint
*/
public ToDynamicDefinition pattern(String pattern) {
setPattern(pattern);
return this;
}
/**
* Sets the maximum size used by the {@link org.apache.camel.spi.ProducerCache} which is used to cache and reuse
* producers when using this recipient list, when uris are reused.
*
* Beware that when using dynamic endpoints then it affects how well the cache can be utilized. If each dynamic
* endpoint is unique then its best to turn off caching by setting this to -1, which allows Camel to not cache both
* the producers and endpoints; they are regarded as prototype scoped and will be stopped and discarded after use.
* This reduces memory usage as otherwise producers/endpoints are stored in memory in the caches.
*
* However if there are a high degree of dynamic endpoints that have been used before, then it can benefit to use
* the cache to reuse both producers and endpoints and therefore the cache size can be set accordingly or rely on
* the default size (1000).
*
* If there is a mix of unique and used before dynamic endpoints, then setting a reasonable cache size can help
* reduce memory usage to avoid storing too many non frequent used producers.
*
* @param cacheSize the cache size, use <tt>0</tt> for default cache size, or <tt>-1</tt> to turn cache off.
* @return the builder
*/
public ToDynamicDefinition cacheSize(int cacheSize) {
return cacheSize(Integer.toString(cacheSize));
}
/**
* Sets the maximum size used by the {@link org.apache.camel.spi.ProducerCache} which is used to cache and reuse
* producers when using this recipient list, when uris are reused.
*
* Beware that when using dynamic endpoints then it affects how well the cache can be utilized. If each dynamic
* endpoint is unique then its best to turn off caching by setting this to -1, which allows Camel to not cache both
* the producers and endpoints; they are regarded as prototype scoped and will be stopped and discarded after use.
* This reduces memory usage as otherwise producers/endpoints are stored in memory in the caches.
*
* However if there are a high degree of dynamic endpoints that have been used before, then it can benefit to use
* the cache to reuse both producers and endpoints and therefore the cache size can be set accordingly or rely on
* the default size (1000).
*
* If there is a mix of unique and used before dynamic endpoints, then setting a reasonable cache size can help
* reduce memory usage to avoid storing too many non frequent used producers.
*
* @param cacheSize the cache size, use <tt>0</tt> for default cache size, or <tt>-1</tt> to turn cache off.
* @return the builder
*/
public ToDynamicDefinition cacheSize(String cacheSize) {
setCacheSize(cacheSize);
return this;
}
/**
* Whether to ignore invalid endpoint URIs and skip sending the message.
*/
public ToDynamicDefinition ignoreInvalidEndpoint(boolean ignoreInvalidEndpoint) {
return ignoreInvalidEndpoint(Boolean.toString(ignoreInvalidEndpoint));
}
/**
* Whether to ignore invalid endpoint URIs and skip sending the message.
*/
public ToDynamicDefinition ignoreInvalidEndpoint(String ignoreInvalidEndpoint) {
setIgnoreInvalidEndpoint(ignoreInvalidEndpoint);
return this;
}
/**
* Whether to allow components to optimise toD if they are {@link org.apache.camel.spi.SendDynamicAware}.
*
* @return the builder
*/
public ToDynamicDefinition allowOptimisedComponents(boolean allowOptimisedComponents) {
return allowOptimisedComponents(Boolean.toString(allowOptimisedComponents));
}
/**
* Whether to allow components to optimise toD if they are {@link org.apache.camel.spi.SendDynamicAware}.
*
* @return the builder
*/
public ToDynamicDefinition allowOptimisedComponents(String allowOptimisedComponents) {
setAllowOptimisedComponents(allowOptimisedComponents);
return this;
}
/**
* Whether to auto startup components when toD is starting up.
*
* @return the builder
*/
public ToDynamicDefinition autoStartComponents(String autoStartComponents) {
setAutoStartComponents(autoStartComponents);
return this;
}
// Properties
// -------------------------------------------------------------------------
public String getUri() {
return uri;
}
/**
* The uri of the endpoint to send to. The uri can be dynamic computed using the
* {@link org.apache.camel.language.simple.SimpleLanguage} expression.
*/
public void setUri(String uri) {
this.uri = uri;
}
public EndpointProducerBuilder getEndpointProducerBuilder() {
return endpointProducerBuilder;
}
public void setEndpointProducerBuilder(EndpointProducerBuilder endpointProducerBuilder) {
this.endpointProducerBuilder = endpointProducerBuilder;
}
public String getVariableSend() {
return variableSend;
}
public void setVariableSend(String variableSend) {
this.variableSend = variableSend;
}
public String getVariableReceive() {
return variableReceive;
}
public void setVariableReceive(String variableReceive) {
this.variableReceive = variableReceive;
}
public String getPattern() {
return pattern;
}
public void setPattern(String pattern) {
this.pattern = pattern;
}
public String getCacheSize() {
return cacheSize;
}
public void setCacheSize(String cacheSize) {
this.cacheSize = cacheSize;
}
public String getIgnoreInvalidEndpoint() {
return ignoreInvalidEndpoint;
}
public void setIgnoreInvalidEndpoint(String ignoreInvalidEndpoint) {
this.ignoreInvalidEndpoint = ignoreInvalidEndpoint;
}
public String getAllowOptimisedComponents() {
return allowOptimisedComponents;
}
public void setAllowOptimisedComponents(String allowOptimisedComponents) {
this.allowOptimisedComponents = allowOptimisedComponents;
}
public String getAutoStartComponents() {
return autoStartComponents;
}
public void setAutoStartComponents(String autoStartComponents) {
this.autoStartComponents = autoStartComponents;
}
public ToDynamicDefinition copy() {
return new ToDynamicDefinition(this);
}
}