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.util; 018 019import java.util.EventObject; 020import java.util.List; 021 022import org.apache.camel.CamelContext; 023import org.apache.camel.Endpoint; 024import org.apache.camel.Exchange; 025import org.apache.camel.Processor; 026import org.apache.camel.Route; 027import org.apache.camel.StatefulService; 028import org.apache.camel.spi.EventFactory; 029import org.apache.camel.spi.EventNotifier; 030import org.apache.camel.spi.ManagementStrategy; 031import org.slf4j.Logger; 032import org.slf4j.LoggerFactory; 033 034/** 035 * Helper for easily sending event notifications in a single line of code 036 * 037 * @version 038 */ 039public final class EventHelper { 040 041 private static final Logger LOG = LoggerFactory.getLogger(EventHelper.class); 042 043 private EventHelper() { 044 } 045 046 public static void notifyCamelContextStarting(CamelContext context) { 047 ManagementStrategy management = context.getManagementStrategy(); 048 if (management == null) { 049 return; 050 } 051 052 List<EventNotifier> notifiers = management.getEventNotifiers(); 053 if (notifiers == null || notifiers.isEmpty()) { 054 return; 055 } 056 057 for (EventNotifier notifier : notifiers) { 058 if (notifier.isIgnoreCamelContextEvents()) { 059 continue; 060 } 061 062 EventFactory factory = management.getEventFactory(); 063 if (factory == null) { 064 return; 065 } 066 EventObject event = factory.createCamelContextStartingEvent(context); 067 if (event == null) { 068 return; 069 } 070 doNotifyEvent(notifier, event); 071 } 072 } 073 074 public static void notifyCamelContextStarted(CamelContext context) { 075 ManagementStrategy management = context.getManagementStrategy(); 076 if (management == null) { 077 return; 078 } 079 080 List<EventNotifier> notifiers = management.getEventNotifiers(); 081 if (notifiers == null || notifiers.isEmpty()) { 082 return; 083 } 084 085 for (EventNotifier notifier : notifiers) { 086 if (notifier.isIgnoreCamelContextEvents()) { 087 continue; 088 } 089 090 EventFactory factory = management.getEventFactory(); 091 if (factory == null) { 092 return; 093 } 094 EventObject event = factory.createCamelContextStartedEvent(context); 095 if (event == null) { 096 return; 097 } 098 doNotifyEvent(notifier, event); 099 } 100 } 101 102 public static void notifyCamelContextStartupFailed(CamelContext context, Throwable cause) { 103 ManagementStrategy management = context.getManagementStrategy(); 104 if (management == null) { 105 return; 106 } 107 108 List<EventNotifier> notifiers = management.getEventNotifiers(); 109 if (notifiers == null || notifiers.isEmpty()) { 110 return; 111 } 112 113 for (EventNotifier notifier : notifiers) { 114 if (notifier.isIgnoreCamelContextEvents()) { 115 continue; 116 } 117 118 EventFactory factory = management.getEventFactory(); 119 if (factory == null) { 120 return; 121 } 122 EventObject event = factory.createCamelContextStartupFailureEvent(context, cause); 123 if (event == null) { 124 return; 125 } 126 doNotifyEvent(notifier, event); 127 } 128 } 129 130 public static void notifyCamelContextStopping(CamelContext context) { 131 ManagementStrategy management = context.getManagementStrategy(); 132 if (management == null) { 133 return; 134 } 135 136 List<EventNotifier> notifiers = management.getEventNotifiers(); 137 if (notifiers == null || notifiers.isEmpty()) { 138 return; 139 } 140 141 for (EventNotifier notifier : notifiers) { 142 if (notifier.isIgnoreCamelContextEvents()) { 143 continue; 144 } 145 146 EventFactory factory = management.getEventFactory(); 147 if (factory == null) { 148 return; 149 } 150 EventObject event = factory.createCamelContextStoppingEvent(context); 151 if (event == null) { 152 return; 153 } 154 doNotifyEvent(notifier, event); 155 } 156 } 157 158 public static void notifyCamelContextStopped(CamelContext context) { 159 ManagementStrategy management = context.getManagementStrategy(); 160 if (management == null) { 161 return; 162 } 163 164 List<EventNotifier> notifiers = management.getEventNotifiers(); 165 if (notifiers == null || notifiers.isEmpty()) { 166 return; 167 } 168 169 for (EventNotifier notifier : notifiers) { 170 if (notifier.isIgnoreCamelContextEvents()) { 171 continue; 172 } 173 174 EventFactory factory = management.getEventFactory(); 175 if (factory == null) { 176 return; 177 } 178 EventObject event = factory.createCamelContextStoppedEvent(context); 179 if (event == null) { 180 return; 181 } 182 doNotifyEvent(notifier, event); 183 } 184 } 185 186 public static void notifyCamelContextStopFailed(CamelContext context, Throwable cause) { 187 ManagementStrategy management = context.getManagementStrategy(); 188 if (management == null) { 189 return; 190 } 191 192 List<EventNotifier> notifiers = management.getEventNotifiers(); 193 if (notifiers == null || notifiers.isEmpty()) { 194 return; 195 } 196 197 for (EventNotifier notifier : notifiers) { 198 if (notifier.isIgnoreCamelContextEvents()) { 199 continue; 200 } 201 202 EventFactory factory = management.getEventFactory(); 203 if (factory == null) { 204 return; 205 } 206 EventObject event = factory.createCamelContextStopFailureEvent(context, cause); 207 if (event == null) { 208 return; 209 } 210 doNotifyEvent(notifier, event); 211 } 212 } 213 214 public static void notifyServiceStopFailure(CamelContext context, Object service, Throwable cause) { 215 ManagementStrategy management = context.getManagementStrategy(); 216 if (management == null) { 217 return; 218 } 219 220 List<EventNotifier> notifiers = management.getEventNotifiers(); 221 if (notifiers == null || notifiers.isEmpty()) { 222 return; 223 } 224 225 for (EventNotifier notifier : notifiers) { 226 if (notifier.isIgnoreServiceEvents()) { 227 continue; 228 } 229 230 EventFactory factory = management.getEventFactory(); 231 if (factory == null) { 232 return; 233 } 234 EventObject event = factory.createServiceStopFailureEvent(context, service, cause); 235 if (event == null) { 236 return; 237 } 238 doNotifyEvent(notifier, event); 239 } 240 } 241 242 public static void notifyServiceStartupFailure(CamelContext context, Object service, Throwable cause) { 243 ManagementStrategy management = context.getManagementStrategy(); 244 if (management == null) { 245 return; 246 } 247 248 List<EventNotifier> notifiers = management.getEventNotifiers(); 249 if (notifiers == null || notifiers.isEmpty()) { 250 return; 251 } 252 253 for (EventNotifier notifier : notifiers) { 254 if (notifier.isIgnoreServiceEvents()) { 255 continue; 256 } 257 258 EventFactory factory = management.getEventFactory(); 259 if (factory == null) { 260 return; 261 } 262 EventObject event = factory.createServiceStartupFailureEvent(context, service, cause); 263 if (event == null) { 264 return; 265 } 266 doNotifyEvent(notifier, event); 267 } 268 } 269 270 public static void notifyRouteStarted(CamelContext context, Route route) { 271 ManagementStrategy management = context.getManagementStrategy(); 272 if (management == null) { 273 return; 274 } 275 276 List<EventNotifier> notifiers = management.getEventNotifiers(); 277 if (notifiers == null || notifiers.isEmpty()) { 278 return; 279 } 280 281 for (EventNotifier notifier : notifiers) { 282 if (notifier.isIgnoreRouteEvents()) { 283 continue; 284 } 285 286 EventFactory factory = management.getEventFactory(); 287 if (factory == null) { 288 return; 289 } 290 EventObject event = factory.createRouteStartedEvent(route); 291 if (event == null) { 292 return; 293 } 294 doNotifyEvent(notifier, event); 295 } 296 } 297 298 public static void notifyRouteStopped(CamelContext context, Route route) { 299 ManagementStrategy management = context.getManagementStrategy(); 300 if (management == null) { 301 return; 302 } 303 304 List<EventNotifier> notifiers = management.getEventNotifiers(); 305 if (notifiers == null || notifiers.isEmpty()) { 306 return; 307 } 308 309 for (EventNotifier notifier : notifiers) { 310 if (notifier.isIgnoreRouteEvents()) { 311 continue; 312 } 313 314 EventFactory factory = management.getEventFactory(); 315 if (factory == null) { 316 return; 317 } 318 EventObject event = factory.createRouteStoppedEvent(route); 319 if (event == null) { 320 return; 321 } 322 doNotifyEvent(notifier, event); 323 } 324 } 325 326 public static void notifyRouteAdded(CamelContext context, Route route) { 327 ManagementStrategy management = context.getManagementStrategy(); 328 if (management == null) { 329 return; 330 } 331 332 List<EventNotifier> notifiers = management.getEventNotifiers(); 333 if (notifiers == null || notifiers.isEmpty()) { 334 return; 335 } 336 337 for (EventNotifier notifier : notifiers) { 338 if (notifier.isIgnoreRouteEvents()) { 339 continue; 340 } 341 342 EventFactory factory = management.getEventFactory(); 343 if (factory == null) { 344 return; 345 } 346 EventObject event = factory.createRouteAddedEvent(route); 347 if (event == null) { 348 return; 349 } 350 doNotifyEvent(notifier, event); 351 } 352 } 353 354 public static void notifyRouteRemoved(CamelContext context, Route route) { 355 ManagementStrategy management = context.getManagementStrategy(); 356 if (management == null) { 357 return; 358 } 359 360 List<EventNotifier> notifiers = management.getEventNotifiers(); 361 if (notifiers == null || notifiers.isEmpty()) { 362 return; 363 } 364 365 for (EventNotifier notifier : notifiers) { 366 if (notifier.isIgnoreRouteEvents()) { 367 continue; 368 } 369 370 EventFactory factory = management.getEventFactory(); 371 if (factory == null) { 372 return; 373 } 374 EventObject event = factory.createRouteRemovedEvent(route); 375 if (event == null) { 376 return; 377 } 378 doNotifyEvent(notifier, event); 379 } 380 } 381 382 public static void notifyExchangeCreated(CamelContext context, Exchange exchange) { 383 if (exchange.getProperty(Exchange.NOTIFY_EVENT, false, Boolean.class)) { 384 // do not generate events for an notify event 385 return; 386 } 387 388 ManagementStrategy management = context.getManagementStrategy(); 389 if (management == null) { 390 return; 391 } 392 393 List<EventNotifier> notifiers = management.getEventNotifiers(); 394 if (notifiers == null || notifiers.isEmpty()) { 395 return; 396 } 397 398 for (EventNotifier notifier : notifiers) { 399 if (notifier.isIgnoreExchangeEvents() || notifier.isIgnoreExchangeCreatedEvent()) { 400 continue; 401 } 402 403 EventFactory factory = management.getEventFactory(); 404 if (factory == null) { 405 return; 406 } 407 EventObject event = factory.createExchangeCreatedEvent(exchange); 408 if (event == null) { 409 return; 410 } 411 doNotifyEvent(notifier, event); 412 } 413 } 414 415 public static void notifyExchangeDone(CamelContext context, Exchange exchange) { 416 if (exchange.getProperty(Exchange.NOTIFY_EVENT, false, Boolean.class)) { 417 // do not generate events for an notify event 418 return; 419 } 420 421 ManagementStrategy management = context.getManagementStrategy(); 422 if (management == null) { 423 return; 424 } 425 426 List<EventNotifier> notifiers = management.getEventNotifiers(); 427 if (notifiers == null || notifiers.isEmpty()) { 428 return; 429 } 430 431 for (EventNotifier notifier : notifiers) { 432 if (notifier.isIgnoreExchangeEvents() || notifier.isIgnoreExchangeCompletedEvent()) { 433 continue; 434 } 435 436 EventFactory factory = management.getEventFactory(); 437 if (factory == null) { 438 return; 439 } 440 EventObject event = factory.createExchangeCompletedEvent(exchange); 441 if (event == null) { 442 return; 443 } 444 doNotifyEvent(notifier, event); 445 } 446 } 447 448 public static void notifyExchangeFailed(CamelContext context, Exchange exchange) { 449 if (exchange.getProperty(Exchange.NOTIFY_EVENT, false, Boolean.class)) { 450 // do not generate events for an notify event 451 return; 452 } 453 454 ManagementStrategy management = context.getManagementStrategy(); 455 if (management == null) { 456 return; 457 } 458 459 List<EventNotifier> notifiers = management.getEventNotifiers(); 460 if (notifiers == null || notifiers.isEmpty()) { 461 return; 462 } 463 464 for (EventNotifier notifier : notifiers) { 465 if (notifier.isIgnoreExchangeEvents() || notifier.isIgnoreExchangeFailedEvents()) { 466 continue; 467 } 468 469 EventFactory factory = management.getEventFactory(); 470 if (factory == null) { 471 return; 472 } 473 EventObject event = factory.createExchangeFailedEvent(exchange); 474 if (event == null) { 475 return; 476 } 477 doNotifyEvent(notifier, event); 478 } 479 } 480 481 public static void notifyExchangeFailureHandled(CamelContext context, Exchange exchange, Processor failureHandler, 482 boolean deadLetterChannel, String deadLetterUri) { 483 if (exchange.getProperty(Exchange.NOTIFY_EVENT, false, Boolean.class)) { 484 // do not generate events for an notify event 485 return; 486 } 487 488 ManagementStrategy management = context.getManagementStrategy(); 489 if (management == null) { 490 return; 491 } 492 493 List<EventNotifier> notifiers = management.getEventNotifiers(); 494 if (notifiers == null || notifiers.isEmpty()) { 495 return; 496 } 497 498 for (EventNotifier notifier : notifiers) { 499 if (notifier.isIgnoreExchangeEvents() || notifier.isIgnoreExchangeFailedEvents()) { 500 continue; 501 } 502 503 EventFactory factory = management.getEventFactory(); 504 if (factory == null) { 505 return; 506 } 507 EventObject event = factory.createExchangeFailureHandledEvent(exchange, failureHandler, deadLetterChannel, deadLetterUri); 508 if (event == null) { 509 return; 510 } 511 doNotifyEvent(notifier, event); 512 } 513 } 514 515 public static void notifyExchangeRedelivery(CamelContext context, Exchange exchange, int attempt) { 516 if (exchange.getProperty(Exchange.NOTIFY_EVENT, false, Boolean.class)) { 517 // do not generate events for an notify event 518 return; 519 } 520 521 ManagementStrategy management = context.getManagementStrategy(); 522 if (management == null) { 523 return; 524 } 525 526 List<EventNotifier> notifiers = management.getEventNotifiers(); 527 if (notifiers == null || notifiers.isEmpty()) { 528 return; 529 } 530 531 for (EventNotifier notifier : notifiers) { 532 if (notifier.isIgnoreExchangeEvents() || notifier.isIgnoreExchangeFailedEvents()) { 533 continue; 534 } 535 536 EventFactory factory = management.getEventFactory(); 537 if (factory == null) { 538 return; 539 } 540 EventObject event = factory.createExchangeRedeliveryEvent(exchange, attempt); 541 if (event == null) { 542 return; 543 } 544 doNotifyEvent(notifier, event); 545 } 546 } 547 548 public static void notifyExchangeSending(CamelContext context, Exchange exchange, Endpoint endpoint) { 549 if (exchange.getProperty(Exchange.NOTIFY_EVENT, false, Boolean.class)) { 550 // do not generate events for an notify event 551 return; 552 } 553 554 ManagementStrategy management = context.getManagementStrategy(); 555 if (management == null) { 556 return; 557 } 558 559 List<EventNotifier> notifiers = management.getEventNotifiers(); 560 if (notifiers == null || notifiers.isEmpty()) { 561 return; 562 } 563 564 for (EventNotifier notifier : notifiers) { 565 if (notifier.isIgnoreExchangeEvents() || notifier.isIgnoreExchangeSentEvents()) { 566 continue; 567 } 568 569 EventFactory factory = management.getEventFactory(); 570 if (factory == null) { 571 return; 572 } 573 EventObject event = factory.createExchangeSendingEvent(exchange, endpoint); 574 if (event == null) { 575 return; 576 } 577 doNotifyEvent(notifier, event); 578 } 579 } 580 581 public static void notifyExchangeSent(CamelContext context, Exchange exchange, Endpoint endpoint, long timeTaken) { 582 if (exchange.getProperty(Exchange.NOTIFY_EVENT, false, Boolean.class)) { 583 // do not generate events for an notify event 584 return; 585 } 586 587 ManagementStrategy management = context.getManagementStrategy(); 588 if (management == null) { 589 return; 590 } 591 592 List<EventNotifier> notifiers = management.getEventNotifiers(); 593 if (notifiers == null || notifiers.isEmpty()) { 594 return; 595 } 596 597 for (EventNotifier notifier : notifiers) { 598 if (notifier.isIgnoreExchangeEvents() || notifier.isIgnoreExchangeSentEvents()) { 599 continue; 600 } 601 602 EventFactory factory = management.getEventFactory(); 603 if (factory == null) { 604 return; 605 } 606 EventObject event = factory.createExchangeSentEvent(exchange, endpoint, timeTaken); 607 if (event == null) { 608 return; 609 } 610 doNotifyEvent(notifier, event); 611 } 612 } 613 614 public static void notifyCamelContextSuspending(CamelContext context) { 615 ManagementStrategy management = context.getManagementStrategy(); 616 if (management == null) { 617 return; 618 } 619 620 List<EventNotifier> notifiers = management.getEventNotifiers(); 621 if (notifiers == null || notifiers.isEmpty()) { 622 return; 623 } 624 625 for (EventNotifier notifier : notifiers) { 626 if (notifier.isIgnoreCamelContextEvents()) { 627 continue; 628 } 629 630 EventFactory factory = management.getEventFactory(); 631 if (factory == null) { 632 return; 633 } 634 EventObject event = factory.createCamelContextSuspendingEvent(context); 635 if (event == null) { 636 return; 637 } 638 doNotifyEvent(notifier, event); 639 } 640 } 641 642 public static void notifyCamelContextSuspended(CamelContext context) { 643 ManagementStrategy management = context.getManagementStrategy(); 644 if (management == null) { 645 return; 646 } 647 648 List<EventNotifier> notifiers = management.getEventNotifiers(); 649 if (notifiers == null || notifiers.isEmpty()) { 650 return; 651 } 652 653 for (EventNotifier notifier : notifiers) { 654 if (notifier.isIgnoreCamelContextEvents()) { 655 continue; 656 } 657 658 EventFactory factory = management.getEventFactory(); 659 if (factory == null) { 660 return; 661 } 662 EventObject event = factory.createCamelContextSuspendedEvent(context); 663 if (event == null) { 664 return; 665 } 666 doNotifyEvent(notifier, event); 667 } 668 } 669 670 public static void notifyCamelContextResuming(CamelContext context) { 671 ManagementStrategy management = context.getManagementStrategy(); 672 if (management == null) { 673 return; 674 } 675 676 List<EventNotifier> notifiers = management.getEventNotifiers(); 677 if (notifiers == null || notifiers.isEmpty()) { 678 return; 679 } 680 681 for (EventNotifier notifier : notifiers) { 682 if (notifier.isIgnoreCamelContextEvents()) { 683 continue; 684 } 685 686 EventFactory factory = management.getEventFactory(); 687 if (factory == null) { 688 return; 689 } 690 EventObject event = factory.createCamelContextResumingEvent(context); 691 if (event == null) { 692 return; 693 } 694 doNotifyEvent(notifier, event); 695 } 696 } 697 698 public static void notifyCamelContextResumed(CamelContext context) { 699 ManagementStrategy management = context.getManagementStrategy(); 700 if (management == null) { 701 return; 702 } 703 704 List<EventNotifier> notifiers = management.getEventNotifiers(); 705 if (notifiers == null || notifiers.isEmpty()) { 706 return; 707 } 708 709 for (EventNotifier notifier : notifiers) { 710 if (notifier.isIgnoreCamelContextEvents()) { 711 continue; 712 } 713 714 EventFactory factory = management.getEventFactory(); 715 if (factory == null) { 716 return; 717 } 718 EventObject event = factory.createCamelContextResumedEvent(context); 719 if (event == null) { 720 return; 721 } 722 doNotifyEvent(notifier, event); 723 } 724 } 725 726 public static void notifyCamelContextResumeFailed(CamelContext context, Throwable cause) { 727 ManagementStrategy management = context.getManagementStrategy(); 728 if (management == null) { 729 return; 730 } 731 732 List<EventNotifier> notifiers = management.getEventNotifiers(); 733 if (notifiers == null || notifiers.isEmpty()) { 734 return; 735 } 736 737 for (EventNotifier notifier : notifiers) { 738 if (notifier.isIgnoreCamelContextEvents()) { 739 continue; 740 } 741 742 EventFactory factory = management.getEventFactory(); 743 if (factory == null) { 744 return; 745 } 746 EventObject event = factory.createCamelContextResumeFailureEvent(context, cause); 747 if (event == null) { 748 return; 749 } 750 doNotifyEvent(notifier, event); 751 } 752 } 753 754 private static void doNotifyEvent(EventNotifier notifier, EventObject event) { 755 // only notify if notifier is started 756 boolean started = true; 757 if (notifier instanceof StatefulService) { 758 started = ((StatefulService) notifier).isStarted(); 759 } 760 if (!started) { 761 LOG.debug("Ignoring notifying event {}. The EventNotifier has not been started yet: {}", event, notifier); 762 return; 763 } 764 765 if (!notifier.isEnabled(event)) { 766 LOG.trace("Notifier: {} is not enabled for the event: {}", notifier, event); 767 return; 768 } 769 770 try { 771 notifier.notify(event); 772 } catch (Throwable e) { 773 LOG.warn("Error notifying event " + event + ". This exception will be ignored. ", e); 774 } 775 } 776 777}