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 Boolean parallelProcessing; 051 @XmlAttribute 052 private String strategyRef; 053 @XmlAttribute 054 private String strategyMethodName; 055 @XmlAttribute 056 private Boolean strategyMethodAllowNull; 057 @XmlAttribute 058 private String executorServiceRef; 059 @XmlAttribute 060 private Boolean stopOnException; 061 @XmlAttribute 062 private Boolean ignoreInvalidEndpoints; 063 @XmlAttribute 064 private Boolean streaming; 065 @XmlAttribute 066 @Metadata(defaultValue = "0") 067 private Long timeout; 068 @XmlAttribute 069 private String onPrepareRef; 070 @XmlTransient 071 private Processor onPrepare; 072 @XmlAttribute 073 private Boolean shareUnitOfWork; 074 @XmlAttribute 075 private Integer cacheSize; 076 @XmlAttribute 077 private Boolean parallelAggregate; 078 @XmlAttribute 079 private Boolean 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(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(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(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(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(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(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(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(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 * Uses the {@link Processor} when preparing the 327 * {@link org.apache.camel.Exchange} to be used send. This can be used to 328 * deep-clone messages that should be send, or any custom logic needed 329 * before the exchange is send. 330 * 331 * @param onPrepare the processor 332 * @return the builder 333 */ 334 public RecipientListDefinition<Type> onPrepare(Supplier<Processor> onPrepare) { 335 setOnPrepare(onPrepare.get()); 336 return this; 337 } 338 339 /** 340 * Sets the {@link Processor} when preparing the 341 * {@link org.apache.camel.Exchange} to be used send using a fluent buidler. 342 */ 343 public ProcessClause<RecipientListDefinition<Type>> onPrepare() { 344 ProcessClause<RecipientListDefinition<Type>> clause = new ProcessClause<>(this); 345 setOnPrepare(clause); 346 return clause; 347 } 348 349 /** 350 * Uses the {@link Processor} when preparing the 351 * {@link org.apache.camel.Exchange} to be send. This can be used to 352 * deep-clone messages that should be send, or any custom logic needed 353 * before the exchange is send. 354 * 355 * @param onPrepareRef reference to the processor to lookup in the 356 * {@link org.apache.camel.spi.Registry} 357 * @return the builder 358 */ 359 public RecipientListDefinition<Type> onPrepareRef(String onPrepareRef) { 360 setOnPrepareRef(onPrepareRef); 361 return this; 362 } 363 364 /** 365 * Sets a total timeout specified in millis, when using parallel processing. 366 * If the Recipient List hasn't been able to send and process all replies 367 * within the given timeframe, then the timeout triggers and the Recipient 368 * List breaks out and continues. Notice if you provide a 369 * TimeoutAwareAggregationStrategy then the timeout method is invoked before 370 * breaking out. If the timeout is reached with running tasks still 371 * remaining, certain tasks for which it is difficult for Camel to shut down 372 * in a graceful manner may continue to run. So use this option with a bit 373 * of care. 374 * 375 * @param timeout timeout in millis 376 * @return the builder 377 */ 378 public RecipientListDefinition<Type> timeout(long timeout) { 379 setTimeout(timeout); 380 return this; 381 } 382 383 /** 384 * Shares the {@link org.apache.camel.spi.UnitOfWork} with the parent and 385 * each of the sub messages. Recipient List will by default not share unit 386 * of work between the parent exchange and each recipient exchange. This 387 * means each sub exchange has its own individual unit of work. 388 * 389 * @return the builder. 390 */ 391 public RecipientListDefinition<Type> shareUnitOfWork() { 392 setShareUnitOfWork(true); 393 return this; 394 } 395 396 /** 397 * Sets the maximum size used by the 398 * {@link org.apache.camel.spi.ProducerCache} which is used to cache and 399 * reuse producers when using this recipient list, when uris are reused. 400 * 401 * @param cacheSize the cache size, use <tt>0</tt> for default cache size, 402 * or <tt>-1</tt> to turn cache off. 403 * @return the builder 404 */ 405 public RecipientListDefinition<Type> cacheSize(int cacheSize) { 406 setCacheSize(cacheSize); 407 return this; 408 } 409 410 // Properties 411 // ------------------------------------------------------------------------- 412 413 /** 414 * Expression that returns which endpoints (url) to send the message to (the 415 * recipients). If the expression return an empty value then the message is 416 * not sent to any recipients. 417 */ 418 @Override 419 public void setExpression(ExpressionDefinition expression) { 420 // override to include javadoc what the expression is used for 421 super.setExpression(expression); 422 } 423 424 public String getDelimiter() { 425 return delimiter; 426 } 427 428 public void setDelimiter(String delimiter) { 429 this.delimiter = delimiter; 430 } 431 432 public Boolean getParallelProcessing() { 433 return parallelProcessing; 434 } 435 436 public void setParallelProcessing(Boolean parallelProcessing) { 437 this.parallelProcessing = parallelProcessing; 438 } 439 440 public String getStrategyRef() { 441 return strategyRef; 442 } 443 444 /** 445 * Sets a reference to the AggregationStrategy to be used to assemble the 446 * replies from the recipients, into a single outgoing message from the 447 * RecipientList. By default Camel will use the last reply as the outgoing 448 * message. You can also use a POJO as the AggregationStrategy 449 */ 450 public void setStrategyRef(String strategyRef) { 451 this.strategyRef = strategyRef; 452 } 453 454 public String getStrategyMethodName() { 455 return strategyMethodName; 456 } 457 458 /** 459 * This option can be used to explicit declare the method name to use, when 460 * using POJOs as the AggregationStrategy. 461 */ 462 public void setStrategyMethodName(String strategyMethodName) { 463 this.strategyMethodName = strategyMethodName; 464 } 465 466 public Boolean getStrategyMethodAllowNull() { 467 return strategyMethodAllowNull; 468 } 469 470 /** 471 * If this option is false then the aggregate method is not used if there 472 * was no data to enrich. If this option is true then null values is used as 473 * the oldExchange (when no data to enrich), when using POJOs as the 474 * AggregationStrategy 475 */ 476 public void setStrategyMethodAllowNull(Boolean strategyMethodAllowNull) { 477 this.strategyMethodAllowNull = strategyMethodAllowNull; 478 } 479 480 @Override 481 public String getExecutorServiceRef() { 482 return executorServiceRef; 483 } 484 485 @Override 486 public void setExecutorServiceRef(String executorServiceRef) { 487 this.executorServiceRef = executorServiceRef; 488 } 489 490 public Boolean getIgnoreInvalidEndpoints() { 491 return ignoreInvalidEndpoints; 492 } 493 494 public void setIgnoreInvalidEndpoints(Boolean ignoreInvalidEndpoints) { 495 this.ignoreInvalidEndpoints = ignoreInvalidEndpoints; 496 } 497 498 public Boolean getStopOnException() { 499 return stopOnException; 500 } 501 502 public void setStopOnException(Boolean stopOnException) { 503 this.stopOnException = stopOnException; 504 } 505 506 public AggregationStrategy getAggregationStrategy() { 507 return aggregationStrategy; 508 } 509 510 /** 511 * Sets the AggregationStrategy to be used to assemble the replies from the 512 * recipients, into a single outgoing message from the RecipientList. By 513 * default Camel will use the last reply as the outgoing message. You can 514 * also use a POJO as the AggregationStrategy 515 */ 516 public void setAggregationStrategy(AggregationStrategy aggregationStrategy) { 517 this.aggregationStrategy = aggregationStrategy; 518 } 519 520 @Override 521 public ExecutorService getExecutorService() { 522 return executorService; 523 } 524 525 @Override 526 public void setExecutorService(ExecutorService executorService) { 527 this.executorService = executorService; 528 } 529 530 public Boolean getStreaming() { 531 return streaming; 532 } 533 534 public void setStreaming(Boolean streaming) { 535 this.streaming = streaming; 536 } 537 538 public Long getTimeout() { 539 return timeout; 540 } 541 542 public void setTimeout(Long timeout) { 543 this.timeout = timeout; 544 } 545 546 public String getOnPrepareRef() { 547 return onPrepareRef; 548 } 549 550 public void setOnPrepareRef(String onPrepareRef) { 551 this.onPrepareRef = onPrepareRef; 552 } 553 554 public Processor getOnPrepare() { 555 return onPrepare; 556 } 557 558 public void setOnPrepare(Processor onPrepare) { 559 this.onPrepare = onPrepare; 560 } 561 562 public Boolean getShareUnitOfWork() { 563 return shareUnitOfWork; 564 } 565 566 public void setShareUnitOfWork(Boolean shareUnitOfWork) { 567 this.shareUnitOfWork = shareUnitOfWork; 568 } 569 570 public Integer getCacheSize() { 571 return cacheSize; 572 } 573 574 public void setCacheSize(Integer cacheSize) { 575 this.cacheSize = cacheSize; 576 } 577 578 public Boolean getParallelAggregate() { 579 return parallelAggregate; 580 } 581 582 public void setParallelAggregate(Boolean parallelAggregate) { 583 this.parallelAggregate = parallelAggregate; 584 } 585 586 public Boolean getStopOnAggregateException() { 587 return stopOnAggregateException; 588 } 589 590 public void setStopOnAggregateException(Boolean stopOnAggregateException) { 591 this.stopOnAggregateException = stopOnAggregateException; 592 } 593}