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 */ 017 package org.apache.camel.model; 018 019 import javax.xml.bind.annotation.XmlAccessType; 020 import javax.xml.bind.annotation.XmlAccessorType; 021 import javax.xml.bind.annotation.XmlAttribute; 022 import javax.xml.bind.annotation.XmlRootElement; 023 024 import org.apache.camel.CamelContext; 025 import org.apache.camel.LoggingLevel; 026 import org.apache.camel.processor.RedeliveryPolicy; 027 import org.apache.camel.util.CamelContextHelper; 028 import org.apache.camel.util.ObjectHelper; 029 030 /** 031 * Represents an XML <redeliveryPolicy/> element 032 * 033 * @version 034 */ 035 @XmlRootElement(name = "redeliveryPolicy") 036 @XmlAccessorType(XmlAccessType.FIELD) 037 public class RedeliveryPolicyDefinition { 038 @XmlAttribute 039 private String maximumRedeliveries; 040 @XmlAttribute 041 private String redeliveryDelay; 042 @XmlAttribute 043 private String asyncDelayedRedelivery; 044 @XmlAttribute 045 private String backOffMultiplier; 046 @XmlAttribute 047 private String useExponentialBackOff; 048 @XmlAttribute 049 private String collisionAvoidanceFactor; 050 @XmlAttribute 051 private String useCollisionAvoidance; 052 @XmlAttribute 053 private String maximumRedeliveryDelay; 054 @XmlAttribute 055 private LoggingLevel retriesExhaustedLogLevel; 056 @XmlAttribute 057 private LoggingLevel retryAttemptedLogLevel; 058 @XmlAttribute 059 private String logRetryAttempted; 060 @XmlAttribute 061 private String logStackTrace; 062 @XmlAttribute 063 private String logRetryStackTrace; 064 @XmlAttribute 065 private String logHandled; 066 @XmlAttribute 067 private String logContinued; 068 @XmlAttribute 069 private String logExhausted; 070 @XmlAttribute 071 private String logExhaustedMessageHistory; 072 @XmlAttribute 073 private String disableRedelivery; 074 @XmlAttribute 075 private String delayPattern; 076 @XmlAttribute 077 private String allowRedeliveryWhileStopping; 078 079 public RedeliveryPolicy createRedeliveryPolicy(CamelContext context, RedeliveryPolicy parentPolicy) { 080 081 RedeliveryPolicy answer; 082 if (parentPolicy != null) { 083 answer = parentPolicy.copy(); 084 } else { 085 answer = new RedeliveryPolicy(); 086 } 087 088 try { 089 090 // copy across the properties - if they are set 091 if (maximumRedeliveries != null) { 092 answer.setMaximumRedeliveries(CamelContextHelper.parseInteger(context, maximumRedeliveries)); 093 } 094 if (redeliveryDelay != null) { 095 answer.setRedeliveryDelay(CamelContextHelper.parseLong(context, redeliveryDelay)); 096 } 097 if (asyncDelayedRedelivery != null) { 098 if (CamelContextHelper.parseBoolean(context, asyncDelayedRedelivery)) { 099 answer.asyncDelayedRedelivery(); 100 } 101 } 102 if (retriesExhaustedLogLevel != null) { 103 answer.setRetriesExhaustedLogLevel(retriesExhaustedLogLevel); 104 } 105 if (retryAttemptedLogLevel != null) { 106 answer.setRetryAttemptedLogLevel(retryAttemptedLogLevel); 107 } 108 if (backOffMultiplier != null) { 109 answer.setBackOffMultiplier(CamelContextHelper.parseDouble(context, backOffMultiplier)); 110 } 111 if (useExponentialBackOff != null) { 112 answer.setUseExponentialBackOff(CamelContextHelper.parseBoolean(context, useExponentialBackOff)); 113 } 114 if (collisionAvoidanceFactor != null) { 115 answer.setCollisionAvoidanceFactor(CamelContextHelper.parseDouble(context, collisionAvoidanceFactor)); 116 } 117 if (useCollisionAvoidance != null) { 118 answer.setUseCollisionAvoidance(CamelContextHelper.parseBoolean(context, useCollisionAvoidance)); 119 } 120 if (maximumRedeliveryDelay != null) { 121 answer.setMaximumRedeliveryDelay(CamelContextHelper.parseLong(context, maximumRedeliveryDelay)); 122 } 123 if (logStackTrace != null) { 124 answer.setLogStackTrace(CamelContextHelper.parseBoolean(context, logStackTrace)); 125 } 126 if (logRetryStackTrace != null) { 127 answer.setLogRetryStackTrace(CamelContextHelper.parseBoolean(context, logRetryStackTrace)); 128 } 129 if (logHandled != null) { 130 answer.setLogHandled(CamelContextHelper.parseBoolean(context, logHandled)); 131 } 132 if (logContinued != null) { 133 answer.setLogContinued(CamelContextHelper.parseBoolean(context, logContinued)); 134 } 135 if (logRetryAttempted != null) { 136 answer.setLogRetryAttempted(CamelContextHelper.parseBoolean(context, logRetryAttempted)); 137 } 138 if (logExhausted != null) { 139 answer.setLogExhausted(CamelContextHelper.parseBoolean(context, logExhausted)); 140 } 141 if (logExhaustedMessageHistory != null) { 142 answer.setLogExhaustedMessageHistory(CamelContextHelper.parseBoolean(context, logExhaustedMessageHistory)); 143 } 144 if (disableRedelivery != null) { 145 if (CamelContextHelper.parseBoolean(context, disableRedelivery)) { 146 answer.setMaximumRedeliveries(0); 147 } 148 } 149 if (delayPattern != null) { 150 answer.setDelayPattern(delayPattern); 151 } 152 if (allowRedeliveryWhileStopping != null) { 153 answer.setAllowRedeliveryWhileStopping(CamelContextHelper.parseBoolean(context, allowRedeliveryWhileStopping)); 154 } 155 } catch (Exception e) { 156 throw ObjectHelper.wrapRuntimeCamelException(e); 157 } 158 159 return answer; 160 } 161 162 @Override 163 public String toString() { 164 return "RedeliveryPolicy[maximumRedeliveries: " + maximumRedeliveries + "]"; 165 } 166 167 // Fluent API 168 //------------------------------------------------------------------------- 169 170 /** 171 * Allow synchronous delayed redelivery. 172 * 173 * @return the builder 174 */ 175 public RedeliveryPolicyDefinition asyncDelayedRedelivery() { 176 setAsyncDelayedRedelivery("true"); 177 return this; 178 } 179 180 /** 181 * Controls whether to allow redelivery while stopping/shutting down a route that uses error handling. 182 * 183 * @param allowRedeliveryWhileStopping <tt>true</tt> to allow redelivery, <tt>false</tt> to reject redeliveries 184 * @return the builder 185 */ 186 public RedeliveryPolicyDefinition allowRedeliveryWhileStopping(boolean allowRedeliveryWhileStopping) { 187 return allowRedeliveryWhileStopping(Boolean.toString(allowRedeliveryWhileStopping)); 188 } 189 190 /** 191 * Controls whether to allow redelivery while stopping/shutting down a route that uses error handling. 192 * 193 * @param allowRedeliveryWhileStopping <tt>true</tt> to allow redelivery, <tt>false</tt> to reject redeliveries 194 * @return the builder 195 */ 196 public RedeliveryPolicyDefinition allowRedeliveryWhileStopping(String allowRedeliveryWhileStopping) { 197 setAllowRedeliveryWhileStopping(allowRedeliveryWhileStopping); 198 return this; 199 } 200 201 /** 202 * Sets the back off multiplier 203 * 204 * @param backOffMultiplier the back off multiplier 205 * @return the builder 206 */ 207 public RedeliveryPolicyDefinition backOffMultiplier(double backOffMultiplier) { 208 return backOffMultiplier(Double.toString(backOffMultiplier)); 209 } 210 211 /** 212 * Sets the back off multiplier (supports property placeholders) 213 * 214 * @param backOffMultiplier the back off multiplier 215 * @return the builder 216 */ 217 public RedeliveryPolicyDefinition backOffMultiplier(String backOffMultiplier) { 218 setBackOffMultiplier(backOffMultiplier); 219 return this; 220 } 221 222 /** 223 * Sets the collision avoidance percentage 224 * 225 * @param collisionAvoidancePercent the percentage 226 * @return the builder 227 */ 228 public RedeliveryPolicyDefinition collisionAvoidancePercent(double collisionAvoidancePercent) { 229 setCollisionAvoidanceFactor(Double.toString(collisionAvoidancePercent * 0.01d)); 230 return this; 231 } 232 233 /** 234 * Sets the collision avoidance factor 235 * 236 * @param collisionAvoidanceFactor the factor 237 * @return the builder 238 */ 239 public RedeliveryPolicyDefinition collisionAvoidanceFactor(double collisionAvoidanceFactor) { 240 return collisionAvoidanceFactor(Double.toString(collisionAvoidanceFactor)); 241 } 242 243 /** 244 * Sets the collision avoidance factor (supports property placeholders) 245 * 246 * @param collisionAvoidanceFactor the factor 247 * @return the builder 248 */ 249 public RedeliveryPolicyDefinition collisionAvoidanceFactor(String collisionAvoidanceFactor) { 250 setCollisionAvoidanceFactor(collisionAvoidanceFactor); 251 return this; 252 } 253 254 /** 255 * Sets the initial redelivery delay 256 * 257 * @param delay delay in millis 258 * @return the builder 259 */ 260 public RedeliveryPolicyDefinition redeliveryDelay(long delay) { 261 return redeliveryDelay(Long.toString(delay)); 262 } 263 264 /** 265 * Sets the initial redelivery delay (supports property placeholders) 266 * 267 * @param delay delay in millis 268 * @return the builder 269 */ 270 public RedeliveryPolicyDefinition redeliveryDelay(String delay) { 271 setRedeliveryDelay(delay); 272 return this; 273 } 274 275 /** 276 * Sets the logging level to use when retries has exhausted 277 * 278 * @param retriesExhaustedLogLevel the logging level 279 * @return the builder 280 */ 281 public RedeliveryPolicyDefinition retriesExhaustedLogLevel(LoggingLevel retriesExhaustedLogLevel) { 282 setRetriesExhaustedLogLevel(retriesExhaustedLogLevel); 283 return this; 284 } 285 286 /** 287 * Sets the logging level to use for logging retry attempts 288 * 289 * @param retryAttemptedLogLevel the logging level 290 * @return the builder 291 */ 292 public RedeliveryPolicyDefinition retryAttemptedLogLevel(LoggingLevel retryAttemptedLogLevel) { 293 setRetryAttemptedLogLevel(retryAttemptedLogLevel); 294 return this; 295 } 296 297 /** 298 * Sets whether stack traces should be logged. 299 * Can be used to include or reduce verbose. 300 * 301 * @param logStackTrace whether stack traces should be logged or not 302 * @return the builder 303 */ 304 public RedeliveryPolicyDefinition logStackTrace(boolean logStackTrace) { 305 return logStackTrace(Boolean.toString(logStackTrace)); 306 } 307 308 /** 309 * Sets whether stack traces should be logged (supports property placeholders) 310 * Can be used to include or reduce verbose. 311 * 312 * @param logStackTrace whether stack traces should be logged or not 313 * @return the builder 314 */ 315 public RedeliveryPolicyDefinition logStackTrace(String logStackTrace) { 316 setLogStackTrace(logStackTrace); 317 return this; 318 } 319 320 /** 321 * Sets whether stack traces should be logged when an retry attempt failed. 322 * Can be used to include or reduce verbose. 323 * 324 * @param logRetryStackTrace whether stack traces should be logged or not 325 * @return the builder 326 */ 327 public RedeliveryPolicyDefinition logRetryStackTrace(boolean logRetryStackTrace) { 328 return logRetryStackTrace(Boolean.toString(logRetryStackTrace)); 329 } 330 331 /** 332 * Sets whether stack traces should be logged when an retry attempt failed (supports property placeholders). 333 * Can be used to include or reduce verbose. 334 * 335 * @param logRetryStackTrace whether stack traces should be logged or not 336 * @return the builder 337 */ 338 public RedeliveryPolicyDefinition logRetryStackTrace(String logRetryStackTrace) { 339 setLogRetryStackTrace(logRetryStackTrace); 340 return this; 341 } 342 343 /** 344 * Sets whether retry attempts should be logged or not. 345 * Can be used to include or reduce verbose. 346 * 347 * @param logRetryAttempted whether retry attempts should be logged or not 348 * @return the builder 349 */ 350 public RedeliveryPolicyDefinition logRetryAttempted(boolean logRetryAttempted) { 351 return logRetryAttempted(Boolean.toString(logRetryAttempted)); 352 } 353 354 /** 355 * Sets whether retry attempts should be logged or not (supports property placeholders). 356 * Can be used to include or reduce verbose. 357 * 358 * @param logRetryAttempted whether retry attempts should be logged or not 359 * @return the builder 360 */ 361 public RedeliveryPolicyDefinition logRetryAttempted(String logRetryAttempted) { 362 setLogRetryAttempted(logRetryAttempted); 363 return this; 364 } 365 366 /** 367 * Sets whether handled exceptions should be logged or not. 368 * Can be used to include or reduce verbose. 369 * 370 * @param logHandled whether handled exceptions should be logged or not 371 * @return the builder 372 */ 373 public RedeliveryPolicyDefinition logHandled(boolean logHandled) { 374 return logHandled(Boolean.toString(logHandled)); 375 } 376 377 /** 378 * Sets whether handled exceptions should be logged or not (supports property placeholders). 379 * Can be used to include or reduce verbose. 380 * 381 * @param logHandled whether handled exceptions should be logged or not 382 * @return the builder 383 */ 384 public RedeliveryPolicyDefinition logHandled(String logHandled) { 385 setLogHandled(logHandled); 386 return this; 387 } 388 389 /** 390 * Sets whether continued exceptions should be logged or not. 391 * Can be used to include or reduce verbose. 392 * 393 * @param logContinued whether continued exceptions should be logged or not 394 * @return the builder 395 */ 396 public RedeliveryPolicyDefinition logContinued(boolean logContinued) { 397 return logContinued(Boolean.toString(logContinued)); 398 } 399 400 /** 401 * Sets whether continued exceptions should be logged or not (supports property placeholders). 402 * Can be used to include or reduce verbose. 403 * 404 * @param logContinued whether continued exceptions should be logged or not 405 * @return the builder 406 */ 407 public RedeliveryPolicyDefinition logContinued(String logContinued) { 408 setLogContinued(logContinued); 409 return this; 410 } 411 412 /** 413 * Sets whether exhausted exceptions should be logged or not. 414 * Can be used to include or reduce verbose. 415 * 416 * @param logExhausted whether exhausted exceptions should be logged or not 417 * @return the builder 418 */ 419 public RedeliveryPolicyDefinition logExhausted(boolean logExhausted) { 420 return logExhausted(Boolean.toString(logExhausted)); 421 } 422 423 /** 424 * Sets whether exhausted exceptions should be logged or not (supports property placeholders). 425 * Can be used to include or reduce verbose. 426 * 427 * @param logExhausted whether exhausted exceptions should be logged or not 428 * @return the builder 429 */ 430 public RedeliveryPolicyDefinition logExhausted(String logExhausted) { 431 setLogExhausted(logExhausted); 432 return this; 433 } 434 435 /** 436 * Sets whether exhausted exceptions should be logged including message history or not (supports property placeholders). 437 * Can be used to include or reduce verbose. 438 * 439 * @param logExhaustedMessageHistory whether exhausted exceptions should be logged with message history 440 * @return the builder 441 */ 442 public RedeliveryPolicyDefinition logExhaustedMessageHistory(boolean logExhaustedMessageHistory) { 443 setLogExhaustedMessageHistory(Boolean.toString(logExhaustedMessageHistory)); 444 return this; 445 } 446 447 /** 448 * Sets whether exhausted exceptions should be logged including message history or not (supports property placeholders). 449 * Can be used to include or reduce verbose. 450 * 451 * @param logExhaustedMessageHistory whether exhausted exceptions should be logged with message history 452 * @return the builder 453 */ 454 public RedeliveryPolicyDefinition logExhaustedMessageHistory(String logExhaustedMessageHistory) { 455 setLogExhaustedMessageHistory(logExhaustedMessageHistory); 456 return this; 457 } 458 459 /** 460 * Sets the maximum redeliveries 461 * <ul> 462 * <li>x = redeliver at most x times</li> 463 * <li>0 = no redeliveries</li> 464 * <li>-1 = redeliver forever</li> 465 * </ul> 466 * 467 * @param maximumRedeliveries the value 468 * @return the builder 469 */ 470 public RedeliveryPolicyDefinition maximumRedeliveries(int maximumRedeliveries) { 471 return maximumRedeliveries(Integer.toString(maximumRedeliveries)); 472 } 473 474 /** 475 * Sets the maximum redeliveries (supports property placeholders) 476 * <ul> 477 * <li>x = redeliver at most x times</li> 478 * <li>0 = no redeliveries</li> 479 * <li>-1 = redeliver forever</li> 480 * </ul> 481 * 482 * @param maximumRedeliveries the value 483 * @return the builder 484 */ 485 public RedeliveryPolicyDefinition maximumRedeliveries(String maximumRedeliveries) { 486 setMaximumRedeliveries(maximumRedeliveries); 487 return this; 488 } 489 490 /** 491 * Turn on collision avoidance. 492 * 493 * @return the builder 494 */ 495 public RedeliveryPolicyDefinition useCollisionAvoidance() { 496 setUseCollisionAvoidance("true"); 497 return this; 498 } 499 500 /** 501 * Turn on exponential backk off 502 * 503 * @return the builder 504 */ 505 public RedeliveryPolicyDefinition useExponentialBackOff() { 506 setUseExponentialBackOff("true"); 507 return this; 508 } 509 510 /** 511 * Sets the maximum delay between redelivery 512 * 513 * @param maximumRedeliveryDelay the delay in millis 514 * @return the builder 515 */ 516 public RedeliveryPolicyDefinition maximumRedeliveryDelay(long maximumRedeliveryDelay) { 517 return maximumRedeliveryDelay(Long.toString(maximumRedeliveryDelay)); 518 } 519 520 /** 521 * Sets the maximum delay between redelivery (supports property placeholders) 522 * 523 * @param maximumRedeliveryDelay the delay in millis 524 * @return the builder 525 */ 526 public RedeliveryPolicyDefinition maximumRedeliveryDelay(String maximumRedeliveryDelay) { 527 setMaximumRedeliveryDelay(maximumRedeliveryDelay); 528 return this; 529 } 530 531 /** 532 * Sets the delay pattern with delay intervals. 533 * 534 * @param delayPattern the delay pattern 535 * @return the builder 536 */ 537 public RedeliveryPolicyDefinition delayPattern(String delayPattern) { 538 setDelayPattern(delayPattern); 539 return this; 540 } 541 542 // Properties 543 //------------------------------------------------------------------------- 544 545 public String getMaximumRedeliveries() { 546 return maximumRedeliveries; 547 } 548 549 public void setMaximumRedeliveries(String maximumRedeliveries) { 550 this.maximumRedeliveries = maximumRedeliveries; 551 } 552 553 public String getRedeliveryDelay() { 554 return redeliveryDelay; 555 } 556 557 public void setRedeliveryDelay(String redeliveryDelay) { 558 this.redeliveryDelay = redeliveryDelay; 559 } 560 561 public String getAsyncDelayedRedelivery() { 562 return asyncDelayedRedelivery; 563 } 564 565 public boolean isAsyncDelayedRedelivery(CamelContext context) { 566 if (getAsyncDelayedRedelivery() == null) { 567 return false; 568 } 569 570 try { 571 return CamelContextHelper.parseBoolean(context, getAsyncDelayedRedelivery()); 572 } catch (Exception e) { 573 throw ObjectHelper.wrapRuntimeCamelException(e); 574 } 575 } 576 577 public void setAsyncDelayedRedelivery(String asyncDelayedRedelivery) { 578 this.asyncDelayedRedelivery = asyncDelayedRedelivery; 579 } 580 581 public String getBackOffMultiplier() { 582 return backOffMultiplier; 583 } 584 585 public void setBackOffMultiplier(String backOffMultiplier) { 586 this.backOffMultiplier = backOffMultiplier; 587 } 588 589 public String getUseExponentialBackOff() { 590 return useExponentialBackOff; 591 } 592 593 public void setUseExponentialBackOff(String useExponentialBackOff) { 594 this.useExponentialBackOff = useExponentialBackOff; 595 } 596 597 public String getCollisionAvoidanceFactor() { 598 return collisionAvoidanceFactor; 599 } 600 601 public void setCollisionAvoidanceFactor(String collisionAvoidanceFactor) { 602 this.collisionAvoidanceFactor = collisionAvoidanceFactor; 603 } 604 605 public String getUseCollisionAvoidance() { 606 return useCollisionAvoidance; 607 } 608 609 public void setUseCollisionAvoidance(String useCollisionAvoidance) { 610 this.useCollisionAvoidance = useCollisionAvoidance; 611 } 612 613 public String getMaximumRedeliveryDelay() { 614 return maximumRedeliveryDelay; 615 } 616 617 public void setMaximumRedeliveryDelay(String maximumRedeliveryDelay) { 618 this.maximumRedeliveryDelay = maximumRedeliveryDelay; 619 } 620 621 public LoggingLevel getRetriesExhaustedLogLevel() { 622 return retriesExhaustedLogLevel; 623 } 624 625 public void setRetriesExhaustedLogLevel(LoggingLevel retriesExhaustedLogLevel) { 626 this.retriesExhaustedLogLevel = retriesExhaustedLogLevel; 627 } 628 629 public LoggingLevel getRetryAttemptedLogLevel() { 630 return retryAttemptedLogLevel; 631 } 632 633 public void setRetryAttemptedLogLevel(LoggingLevel retryAttemptedLogLevel) { 634 this.retryAttemptedLogLevel = retryAttemptedLogLevel; 635 } 636 637 public String getLogRetryAttempted() { 638 return logRetryAttempted; 639 } 640 641 public void setLogRetryAttempted(String logRetryAttempted) { 642 this.logRetryAttempted = logRetryAttempted; 643 } 644 645 public String getLogStackTrace() { 646 return logStackTrace; 647 } 648 649 public void setLogStackTrace(String logStackTrace) { 650 this.logStackTrace = logStackTrace; 651 } 652 653 public String getLogRetryStackTrace() { 654 return logRetryStackTrace; 655 } 656 657 public void setLogRetryStackTrace(String logRetryStackTrace) { 658 this.logRetryStackTrace = logRetryStackTrace; 659 } 660 661 public String getLogHandled() { 662 return logHandled; 663 } 664 665 public void setLogHandled(String logHandled) { 666 this.logHandled = logHandled; 667 } 668 669 public String getLogContinued() { 670 return logContinued; 671 } 672 673 public void setLogContinued(String logContinued) { 674 this.logContinued = logContinued; 675 } 676 677 public String getLogExhausted() { 678 return logExhausted; 679 } 680 681 public void setLogExhausted(String logExhausted) { 682 this.logExhausted = logExhausted; 683 } 684 685 public String getLogExhaustedMessageHistory() { 686 return logExhaustedMessageHistory; 687 } 688 689 public void setLogExhaustedMessageHistory(String logExhaustedMessageHistory) { 690 this.logExhaustedMessageHistory = logExhaustedMessageHistory; 691 } 692 693 public String getDisableRedelivery() { 694 return disableRedelivery; 695 } 696 697 public void setDisableRedelivery(String disableRedelivery) { 698 this.disableRedelivery = disableRedelivery; 699 } 700 701 public String getDelayPattern() { 702 return delayPattern; 703 } 704 705 public void setDelayPattern(String delayPattern) { 706 this.delayPattern = delayPattern; 707 } 708 709 public String getAllowRedeliveryWhileStopping() { 710 return allowRedeliveryWhileStopping; 711 } 712 713 public void setAllowRedeliveryWhileStopping(String allowRedeliveryWhileStopping) { 714 this.allowRedeliveryWhileStopping = allowRedeliveryWhileStopping; 715 } 716 }