001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.camel.model; 018 019import java.util.concurrent.ExecutorService; 020import java.util.function.Supplier; 021 022import javax.xml.bind.annotation.XmlAccessType; 023import javax.xml.bind.annotation.XmlAccessorType; 024import javax.xml.bind.annotation.XmlAttribute; 025import javax.xml.bind.annotation.XmlRootElement; 026import javax.xml.bind.annotation.XmlTransient; 027 028import org.apache.camel.AggregationStrategy; 029import org.apache.camel.Expression; 030import org.apache.camel.Processor; 031import org.apache.camel.builder.ProcessClause; 032import org.apache.camel.model.language.ExpressionDefinition; 033import org.apache.camel.spi.Metadata; 034 035/** 036 * Routes messages to a number of dynamically specified recipients (dynamic to) 037 */ 038@Metadata(label = "eip,endpoint,routing") 039@XmlRootElement(name = "recipientList") 040@XmlAccessorType(XmlAccessType.FIELD) 041public class RecipientListDefinition<Type extends ProcessorDefinition<Type>> extends ExpressionNode implements ExecutorServiceAwareDefinition<RecipientListDefinition<Type>> { 042 @XmlTransient 043 private AggregationStrategy aggregationStrategy; 044 @XmlTransient 045 private ExecutorService executorService; 046 @XmlAttribute 047 @Metadata(defaultValue = ",") 048 private String delimiter; 049 @XmlAttribute 050 private String parallelProcessing; 051 @XmlAttribute 052 private String strategyRef; 053 @XmlAttribute 054 private String strategyMethodName; 055 @XmlAttribute 056 private String strategyMethodAllowNull; 057 @XmlAttribute 058 private String executorServiceRef; 059 @XmlAttribute 060 private String stopOnException; 061 @XmlAttribute 062 private String ignoreInvalidEndpoints; 063 @XmlAttribute 064 private String streaming; 065 @XmlAttribute 066 @Metadata(defaultValue = "0") 067 private String timeout; 068 @XmlAttribute 069 private String onPrepareRef; 070 @XmlTransient 071 private Processor onPrepare; 072 @XmlAttribute 073 private String shareUnitOfWork; 074 @XmlAttribute 075 private String cacheSize; 076 @XmlAttribute 077 private String parallelAggregate; 078 @XmlAttribute 079 private String stopOnAggregateException; 080 081 public RecipientListDefinition() { 082 } 083 084 public RecipientListDefinition(ExpressionDefinition expression) { 085 super(expression); 086 } 087 088 public RecipientListDefinition(Expression expression) { 089 super(expression); 090 } 091 092 @Override 093 public String toString() { 094 return "RecipientList[" + getExpression() + "]"; 095 } 096 097 @Override 098 public String getShortName() { 099 return "recipientList"; 100 } 101 102 @Override 103 public String getLabel() { 104 return "recipientList[" + getExpression() + "]"; 105 } 106 107 // Fluent API 108 // ------------------------------------------------------------------------- 109 110 @Override 111 @SuppressWarnings("unchecked") 112 public Type end() { 113 // allow end() to return to previous type so you can continue in the DSL 114 return (Type)super.end(); 115 } 116 117 /** 118 * Delimiter used if the Expression returned multiple endpoints. Can be 119 * turned off using the value <tt>false</tt>. 120 * <p/> 121 * The default value is , 122 * 123 * @param delimiter the delimiter 124 * @return the builder 125 */ 126 public RecipientListDefinition<Type> delimiter(String delimiter) { 127 setDelimiter(delimiter); 128 return this; 129 } 130 131 /** 132 * Sets the AggregationStrategy to be used to assemble the replies from the 133 * recipients, into a single outgoing message from the RecipientList. By 134 * default Camel will use the last reply as the outgoing message. You can 135 * also use a POJO as the AggregationStrategy 136 */ 137 public RecipientListDefinition<Type> aggregationStrategy(AggregationStrategy aggregationStrategy) { 138 setAggregationStrategy(aggregationStrategy); 139 return this; 140 } 141 142 /** 143 * Sets the AggregationStrategy to be used to assemble the replies from the 144 * recipients, into a single outgoing message from the RecipientList. By 145 * default Camel will use the last reply as the outgoing message. You can 146 * also use a POJO as the AggregationStrategy 147 */ 148 public RecipientListDefinition<Type> aggregationStrategy(Supplier<AggregationStrategy> aggregationStrategy) { 149 setAggregationStrategy(aggregationStrategy.get()); 150 return this; 151 } 152 153 /** 154 * Sets a reference to the AggregationStrategy to be used to assemble the 155 * replies from the recipients, into a single outgoing message from the 156 * RecipientList. By default Camel will use the last reply as the outgoing 157 * message. You can also use a POJO as the AggregationStrategy 158 */ 159 public RecipientListDefinition<Type> aggregationStrategyRef(String aggregationStrategyRef) { 160 setStrategyRef(aggregationStrategyRef); 161 return this; 162 } 163 164 /** 165 * This option can be used to explicit declare the method name to use, when 166 * using POJOs as the AggregationStrategy. 167 * 168 * @param methodName the method name to call 169 * @return the builder 170 */ 171 public RecipientListDefinition<Type> aggregationStrategyMethodName(String methodName) { 172 setStrategyMethodName(methodName); 173 return this; 174 } 175 176 /** 177 * If this option is false then the aggregate method is not used if there 178 * was no data to enrich. If this option is true then null values is used as 179 * the oldExchange (when no data to enrich), when using POJOs as the 180 * AggregationStrategy 181 * 182 * @return the builder 183 */ 184 public RecipientListDefinition<Type> aggregationStrategyMethodAllowNull() { 185 setStrategyMethodAllowNull(Boolean.toString(true)); 186 return this; 187 } 188 189 /** 190 * Ignore the invalidate endpoint exception when try to create a producer 191 * with that endpoint 192 * 193 * @return the builder 194 */ 195 public RecipientListDefinition<Type> ignoreInvalidEndpoints() { 196 setIgnoreInvalidEndpoints(Boolean.toString(true)); 197 return this; 198 } 199 200 /** 201 * If enabled then sending messages to the recipients occurs concurrently. 202 * Note the caller thread will still wait until all messages has been fully 203 * processed, before it continues. Its only the sending and processing the 204 * replies from the recipients which happens concurrently. 205 * 206 * @return the builder 207 */ 208 public RecipientListDefinition<Type> parallelProcessing() { 209 setParallelProcessing(Boolean.toString(true)); 210 return this; 211 } 212 213 /** 214 * If enabled then sending messages to the recipients occurs concurrently. 215 * Note the caller thread will still wait until all messages has been fully 216 * processed, before it continues. Its only the sending and processing the 217 * replies from the recipients which happens concurrently. 218 * 219 * @return the builder 220 */ 221 public RecipientListDefinition<Type> parallelProcessing(boolean parallelProcessing) { 222 setParallelProcessing(Boolean.toString(parallelProcessing)); 223 return this; 224 } 225 226 /** 227 * If enabled then the aggregate method on AggregationStrategy can be called 228 * concurrently. Notice that this would require the implementation of 229 * AggregationStrategy to be implemented as thread-safe. By default this is 230 * false meaning that Camel synchronizes the call to the aggregate method. 231 * Though in some use-cases this can be used to archive higher performance 232 * when the AggregationStrategy is implemented as thread-safe. 233 * 234 * @return the builder 235 */ 236 public RecipientListDefinition<Type> parallelAggregate() { 237 setParallelAggregate(Boolean.toString(true)); 238 return this; 239 } 240 241 /** 242 * If enabled, unwind exceptions occurring at aggregation time to the error 243 * handler when parallelProcessing is used. Currently, aggregation time 244 * exceptions do not stop the route processing when parallelProcessing is 245 * used. Enabling this option allows to work around this behavior. The 246 * default value is <code>false</code> for the sake of backward 247 * compatibility. 248 * 249 * @return the builder 250 */ 251 public RecipientListDefinition<Type> stopOnAggregateException() { 252 setStopOnAggregateException(Boolean.toString(true)); 253 return this; 254 } 255 256 /** 257 * If enabled then Camel will process replies out-of-order, eg in the order 258 * they come back. If disabled, Camel will process replies in the same order 259 * as defined by the recipient list. 260 * 261 * @return the builder 262 */ 263 public RecipientListDefinition<Type> streaming() { 264 setStreaming(Boolean.toString(true)); 265 return this; 266 } 267 268 /** 269 * Will now stop further processing if an exception or failure occurred 270 * during processing of an {@link org.apache.camel.Exchange} and the caused 271 * exception will be thrown. 272 * <p/> 273 * Will also stop if processing the exchange failed (has a fault message) or 274 * an exception was thrown and handled by the error handler (such as using 275 * onException). In all situations the recipient list will stop further 276 * processing. This is the same behavior as in pipeline, which is used by 277 * the routing engine. 278 * <p/> 279 * The default behavior is to <b>not</b> stop but continue processing till 280 * the end 281 * 282 * @return the builder 283 */ 284 public RecipientListDefinition<Type> stopOnException() { 285 setStopOnException(Boolean.toString(true)); 286 return this; 287 } 288 289 /** 290 * To use a custom Thread Pool to be used for parallel processing. Notice if 291 * you set this option, then parallel processing is automatic implied, and 292 * you do not have to enable that option as well. 293 */ 294 @Override 295 public RecipientListDefinition<Type> executorService(ExecutorService executorService) { 296 setExecutorService(executorService); 297 return this; 298 } 299 300 /** 301 * Refers to a custom Thread Pool to be used for parallel processing. Notice 302 * if you set this option, then parallel processing is automatic implied, 303 * and you do not have to enable that option as well. 304 */ 305 @Override 306 public RecipientListDefinition<Type> executorServiceRef(String executorServiceRef) { 307 setExecutorServiceRef(executorServiceRef); 308 return this; 309 } 310 311 /** 312 * Uses the {@link Processor} when preparing the 313 * {@link org.apache.camel.Exchange} to be used send. This can be used to 314 * deep-clone messages that should be send, or any custom logic needed 315 * before the exchange is send. 316 * 317 * @param onPrepare the processor 318 * @return the builder 319 */ 320 public RecipientListDefinition<Type> onPrepare(Processor onPrepare) { 321 setOnPrepare(onPrepare); 322 return this; 323 } 324 325 /** 326 * Sets the {@link Processor} when preparing the 327 * {@link org.apache.camel.Exchange} to be used send using a fluent buidler. 328 */ 329 public ProcessClause<RecipientListDefinition<Type>> onPrepare() { 330 ProcessClause<RecipientListDefinition<Type>> clause = new ProcessClause<>(this); 331 setOnPrepare(clause); 332 return clause; 333 } 334 335 /** 336 * Uses the {@link Processor} when preparing the 337 * {@link org.apache.camel.Exchange} to be send. This can be used to 338 * deep-clone messages that should be send, or any custom logic needed 339 * before the exchange is send. 340 * 341 * @param onPrepareRef reference to the processor to lookup in the 342 * {@link org.apache.camel.spi.Registry} 343 * @return the builder 344 */ 345 public RecipientListDefinition<Type> onPrepareRef(String onPrepareRef) { 346 setOnPrepareRef(onPrepareRef); 347 return this; 348 } 349 350 /** 351 * Sets a total timeout specified in millis, when using parallel processing. 352 * If the Recipient List hasn't been able to send and process all replies 353 * within the given timeframe, then the timeout triggers and the Recipient 354 * List breaks out and continues. Notice if you provide a 355 * TimeoutAwareAggregationStrategy then the timeout method is invoked before 356 * breaking out. If the timeout is reached with running tasks still 357 * remaining, certain tasks for which it is difficult for Camel to shut down 358 * in a graceful manner may continue to run. So use this option with a bit 359 * of care. 360 * 361 * @param timeout timeout in millis 362 * @return the builder 363 */ 364 public RecipientListDefinition<Type> timeout(long timeout) { 365 setTimeout(Long.toString(timeout)); 366 return this; 367 } 368 369 /** 370 * Shares the {@link org.apache.camel.spi.UnitOfWork} with the parent and 371 * each of the sub messages. Recipient List will by default not share unit 372 * of work between the parent exchange and each recipient exchange. This 373 * means each sub exchange has its own individual unit of work. 374 * 375 * @return the builder. 376 */ 377 public RecipientListDefinition<Type> shareUnitOfWork() { 378 setShareUnitOfWork(Boolean.toString(true)); 379 return this; 380 } 381 382 /** 383 * Sets the maximum size used by the 384 * {@link org.apache.camel.spi.ProducerCache} which is used to cache and 385 * reuse producers when using this recipient list, when uris are reused. 386 * 387 * @param cacheSize the cache size, use <tt>0</tt> for default cache size, 388 * or <tt>-1</tt> to turn cache off. 389 * @return the builder 390 */ 391 public RecipientListDefinition<Type> cacheSize(int cacheSize) { 392 setCacheSize(Integer.toString(cacheSize)); 393 return this; 394 } 395 396 // Properties 397 // ------------------------------------------------------------------------- 398 399 /** 400 * Expression that returns which endpoints (url) to send the message to (the 401 * recipients). If the expression return an empty value then the message is 402 * not sent to any recipients. 403 */ 404 @Override 405 public void setExpression(ExpressionDefinition expression) { 406 // override to include javadoc what the expression is used for 407 super.setExpression(expression); 408 } 409 410 public String getDelimiter() { 411 return delimiter; 412 } 413 414 public void setDelimiter(String delimiter) { 415 this.delimiter = delimiter; 416 } 417 418 public String getParallelProcessing() { 419 return parallelProcessing; 420 } 421 422 public void setParallelProcessing(String parallelProcessing) { 423 this.parallelProcessing = parallelProcessing; 424 } 425 426 public String getStrategyRef() { 427 return strategyRef; 428 } 429 430 /** 431 * Sets a reference to the AggregationStrategy to be used to assemble the 432 * replies from the recipients, into a single outgoing message from the 433 * RecipientList. By default Camel will use the last reply as the outgoing 434 * message. You can also use a POJO as the AggregationStrategy 435 */ 436 public void setStrategyRef(String strategyRef) { 437 this.strategyRef = strategyRef; 438 } 439 440 public String getStrategyMethodName() { 441 return strategyMethodName; 442 } 443 444 /** 445 * This option can be used to explicit declare the method name to use, when 446 * using POJOs as the AggregationStrategy. 447 */ 448 public void setStrategyMethodName(String strategyMethodName) { 449 this.strategyMethodName = strategyMethodName; 450 } 451 452 public String getStrategyMethodAllowNull() { 453 return strategyMethodAllowNull; 454 } 455 456 /** 457 * If this option is false then the aggregate method is not used if there 458 * was no data to enrich. If this option is true then null values is used as 459 * the oldExchange (when no data to enrich), when using POJOs as the 460 * AggregationStrategy 461 */ 462 public void setStrategyMethodAllowNull(String strategyMethodAllowNull) { 463 this.strategyMethodAllowNull = strategyMethodAllowNull; 464 } 465 466 @Override 467 public String getExecutorServiceRef() { 468 return executorServiceRef; 469 } 470 471 @Override 472 public void setExecutorServiceRef(String executorServiceRef) { 473 this.executorServiceRef = executorServiceRef; 474 } 475 476 public String getIgnoreInvalidEndpoints() { 477 return ignoreInvalidEndpoints; 478 } 479 480 public void setIgnoreInvalidEndpoints(String ignoreInvalidEndpoints) { 481 this.ignoreInvalidEndpoints = ignoreInvalidEndpoints; 482 } 483 484 public String getStopOnException() { 485 return stopOnException; 486 } 487 488 public void setStopOnException(String stopOnException) { 489 this.stopOnException = stopOnException; 490 } 491 492 public AggregationStrategy getAggregationStrategy() { 493 return aggregationStrategy; 494 } 495 496 /** 497 * Sets the AggregationStrategy to be used to assemble the replies from the 498 * recipients, into a single outgoing message from the RecipientList. By 499 * default Camel will use the last reply as the outgoing message. You can 500 * also use a POJO as the AggregationStrategy 501 */ 502 public void setAggregationStrategy(AggregationStrategy aggregationStrategy) { 503 this.aggregationStrategy = aggregationStrategy; 504 } 505 506 @Override 507 public ExecutorService getExecutorService() { 508 return executorService; 509 } 510 511 @Override 512 public void setExecutorService(ExecutorService executorService) { 513 this.executorService = executorService; 514 } 515 516 public String getStreaming() { 517 return streaming; 518 } 519 520 public void setStreaming(String streaming) { 521 this.streaming = streaming; 522 } 523 524 public String getTimeout() { 525 return timeout; 526 } 527 528 public void setTimeout(String timeout) { 529 this.timeout = timeout; 530 } 531 532 public String getOnPrepareRef() { 533 return onPrepareRef; 534 } 535 536 public void setOnPrepareRef(String onPrepareRef) { 537 this.onPrepareRef = onPrepareRef; 538 } 539 540 public Processor getOnPrepare() { 541 return onPrepare; 542 } 543 544 public void setOnPrepare(Processor onPrepare) { 545 this.onPrepare = onPrepare; 546 } 547 548 public String getShareUnitOfWork() { 549 return shareUnitOfWork; 550 } 551 552 public void setShareUnitOfWork(String shareUnitOfWork) { 553 this.shareUnitOfWork = shareUnitOfWork; 554 } 555 556 public String getCacheSize() { 557 return cacheSize; 558 } 559 560 public void setCacheSize(String cacheSize) { 561 this.cacheSize = cacheSize; 562 } 563 564 public String getParallelAggregate() { 565 return parallelAggregate; 566 } 567 568 public void setParallelAggregate(String parallelAggregate) { 569 this.parallelAggregate = parallelAggregate; 570 } 571 572 public String getStopOnAggregateException() { 573 return stopOnAggregateException; 574 } 575 576 public void setStopOnAggregateException(String stopOnAggregateException) { 577 this.stopOnAggregateException = stopOnAggregateException; 578 } 579}