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.util; 018 019 import java.util.EventObject; 020 import java.util.List; 021 022 import org.apache.camel.CamelContext; 023 import org.apache.camel.Endpoint; 024 import org.apache.camel.Exchange; 025 import org.apache.camel.Processor; 026 import org.apache.camel.Route; 027 import org.apache.camel.StatefulService; 028 import org.apache.camel.spi.EventFactory; 029 import org.apache.camel.spi.EventNotifier; 030 import org.apache.camel.spi.ManagementStrategy; 031 import org.slf4j.Logger; 032 import org.slf4j.LoggerFactory; 033 034 /** 035 * Helper for easily sending event notifications in a single line of code 036 * 037 * @version 038 */ 039 public 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 notifyExchangeCreated(CamelContext context, Exchange exchange) { 327 if (exchange.getProperty(Exchange.NOTIFY_EVENT, false, Boolean.class)) { 328 // do not generate events for an notify event 329 return; 330 } 331 332 ManagementStrategy management = context.getManagementStrategy(); 333 if (management == null) { 334 return; 335 } 336 337 List<EventNotifier> notifiers = management.getEventNotifiers(); 338 if (notifiers == null || notifiers.isEmpty()) { 339 return; 340 } 341 342 for (EventNotifier notifier : notifiers) { 343 if (notifier.isIgnoreExchangeEvents() || notifier.isIgnoreExchangeCreatedEvent()) { 344 continue; 345 } 346 347 EventFactory factory = management.getEventFactory(); 348 if (factory == null) { 349 return; 350 } 351 EventObject event = factory.createExchangeCreatedEvent(exchange); 352 if (event == null) { 353 return; 354 } 355 doNotifyEvent(notifier, event); 356 } 357 } 358 359 public static void notifyExchangeDone(CamelContext context, Exchange exchange) { 360 if (exchange.getProperty(Exchange.NOTIFY_EVENT, false, Boolean.class)) { 361 // do not generate events for an notify event 362 return; 363 } 364 365 ManagementStrategy management = context.getManagementStrategy(); 366 if (management == null) { 367 return; 368 } 369 370 List<EventNotifier> notifiers = management.getEventNotifiers(); 371 if (notifiers == null || notifiers.isEmpty()) { 372 return; 373 } 374 375 for (EventNotifier notifier : notifiers) { 376 if (notifier.isIgnoreExchangeEvents() || notifier.isIgnoreExchangeCompletedEvent()) { 377 continue; 378 } 379 380 EventFactory factory = management.getEventFactory(); 381 if (factory == null) { 382 return; 383 } 384 EventObject event = factory.createExchangeCompletedEvent(exchange); 385 if (event == null) { 386 return; 387 } 388 doNotifyEvent(notifier, event); 389 } 390 } 391 392 public static void notifyExchangeFailed(CamelContext context, Exchange exchange) { 393 if (exchange.getProperty(Exchange.NOTIFY_EVENT, false, Boolean.class)) { 394 // do not generate events for an notify event 395 return; 396 } 397 398 ManagementStrategy management = context.getManagementStrategy(); 399 if (management == null) { 400 return; 401 } 402 403 List<EventNotifier> notifiers = management.getEventNotifiers(); 404 if (notifiers == null || notifiers.isEmpty()) { 405 return; 406 } 407 408 for (EventNotifier notifier : notifiers) { 409 if (notifier.isIgnoreExchangeEvents() || notifier.isIgnoreExchangeFailedEvents()) { 410 continue; 411 } 412 413 EventFactory factory = management.getEventFactory(); 414 if (factory == null) { 415 return; 416 } 417 EventObject event = factory.createExchangeFailedEvent(exchange); 418 if (event == null) { 419 return; 420 } 421 doNotifyEvent(notifier, event); 422 } 423 } 424 425 public static void notifyExchangeFailureHandled(CamelContext context, Exchange exchange, Processor failureHandler, 426 boolean deadLetterChannel) { 427 if (exchange.getProperty(Exchange.NOTIFY_EVENT, false, Boolean.class)) { 428 // do not generate events for an notify event 429 return; 430 } 431 432 ManagementStrategy management = context.getManagementStrategy(); 433 if (management == null) { 434 return; 435 } 436 437 List<EventNotifier> notifiers = management.getEventNotifiers(); 438 if (notifiers == null || notifiers.isEmpty()) { 439 return; 440 } 441 442 for (EventNotifier notifier : notifiers) { 443 if (notifier.isIgnoreExchangeEvents() || notifier.isIgnoreExchangeFailedEvents()) { 444 continue; 445 } 446 447 EventFactory factory = management.getEventFactory(); 448 if (factory == null) { 449 return; 450 } 451 EventObject event = factory.createExchangeFailureHandledEvent(exchange, failureHandler, deadLetterChannel); 452 if (event == null) { 453 return; 454 } 455 doNotifyEvent(notifier, event); 456 } 457 } 458 459 public static void notifyExchangeRedelivery(CamelContext context, Exchange exchange, int attempt) { 460 if (exchange.getProperty(Exchange.NOTIFY_EVENT, false, Boolean.class)) { 461 // do not generate events for an notify event 462 return; 463 } 464 465 ManagementStrategy management = context.getManagementStrategy(); 466 if (management == null) { 467 return; 468 } 469 470 List<EventNotifier> notifiers = management.getEventNotifiers(); 471 if (notifiers == null || notifiers.isEmpty()) { 472 return; 473 } 474 475 for (EventNotifier notifier : notifiers) { 476 if (notifier.isIgnoreExchangeEvents() || notifier.isIgnoreExchangeFailedEvents()) { 477 continue; 478 } 479 480 EventFactory factory = management.getEventFactory(); 481 if (factory == null) { 482 return; 483 } 484 EventObject event = factory.createExchangeRedeliveryEvent(exchange, attempt); 485 if (event == null) { 486 return; 487 } 488 doNotifyEvent(notifier, event); 489 } 490 } 491 492 public static void notifyExchangeSending(CamelContext context, Exchange exchange, Endpoint endpoint) { 493 if (exchange.getProperty(Exchange.NOTIFY_EVENT, false, Boolean.class)) { 494 // do not generate events for an notify event 495 return; 496 } 497 498 ManagementStrategy management = context.getManagementStrategy(); 499 if (management == null) { 500 return; 501 } 502 503 List<EventNotifier> notifiers = management.getEventNotifiers(); 504 if (notifiers == null || notifiers.isEmpty()) { 505 return; 506 } 507 508 for (EventNotifier notifier : notifiers) { 509 if (notifier.isIgnoreExchangeEvents() || notifier.isIgnoreExchangeSentEvents()) { 510 continue; 511 } 512 513 EventFactory factory = management.getEventFactory(); 514 if (factory == null) { 515 return; 516 } 517 EventObject event = factory.createExchangeSendingEvent(exchange, endpoint); 518 if (event == null) { 519 return; 520 } 521 doNotifyEvent(notifier, event); 522 } 523 } 524 525 public static void notifyExchangeSent(CamelContext context, Exchange exchange, Endpoint endpoint, long timeTaken) { 526 if (exchange.getProperty(Exchange.NOTIFY_EVENT, false, Boolean.class)) { 527 // do not generate events for an notify event 528 return; 529 } 530 531 ManagementStrategy management = context.getManagementStrategy(); 532 if (management == null) { 533 return; 534 } 535 536 List<EventNotifier> notifiers = management.getEventNotifiers(); 537 if (notifiers == null || notifiers.isEmpty()) { 538 return; 539 } 540 541 for (EventNotifier notifier : notifiers) { 542 if (notifier.isIgnoreExchangeEvents() || notifier.isIgnoreExchangeSentEvents()) { 543 continue; 544 } 545 546 EventFactory factory = management.getEventFactory(); 547 if (factory == null) { 548 return; 549 } 550 EventObject event = factory.createExchangeSentEvent(exchange, endpoint, timeTaken); 551 if (event == null) { 552 return; 553 } 554 doNotifyEvent(notifier, event); 555 } 556 } 557 558 public static void notifyCamelContextSuspending(CamelContext context) { 559 ManagementStrategy management = context.getManagementStrategy(); 560 if (management == null) { 561 return; 562 } 563 564 List<EventNotifier> notifiers = management.getEventNotifiers(); 565 if (notifiers == null || notifiers.isEmpty()) { 566 return; 567 } 568 569 for (EventNotifier notifier : notifiers) { 570 if (notifier.isIgnoreCamelContextEvents()) { 571 continue; 572 } 573 574 EventFactory factory = management.getEventFactory(); 575 if (factory == null) { 576 return; 577 } 578 EventObject event = factory.createCamelContextSuspendingEvent(context); 579 if (event == null) { 580 return; 581 } 582 doNotifyEvent(notifier, event); 583 } 584 } 585 586 public static void notifyCamelContextSuspended(CamelContext context) { 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.isIgnoreCamelContextEvents()) { 599 continue; 600 } 601 602 EventFactory factory = management.getEventFactory(); 603 if (factory == null) { 604 return; 605 } 606 EventObject event = factory.createCamelContextSuspendedEvent(context); 607 if (event == null) { 608 return; 609 } 610 doNotifyEvent(notifier, event); 611 } 612 } 613 614 public static void notifyCamelContextResuming(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.createCamelContextResumingEvent(context); 635 if (event == null) { 636 return; 637 } 638 doNotifyEvent(notifier, event); 639 } 640 } 641 642 public static void notifyCamelContextResumed(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.createCamelContextResumedEvent(context); 663 if (event == null) { 664 return; 665 } 666 doNotifyEvent(notifier, event); 667 } 668 } 669 670 public static void notifyCamelContextResumeFailed(CamelContext context, Throwable cause) { 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.createCamelContextResumeFailureEvent(context, cause); 691 if (event == null) { 692 return; 693 } 694 doNotifyEvent(notifier, event); 695 } 696 } 697 698 private static void doNotifyEvent(EventNotifier notifier, EventObject event) { 699 // only notify if notifier is started 700 boolean started = true; 701 if (notifier instanceof StatefulService) { 702 started = ((StatefulService) notifier).isStarted(); 703 } 704 if (!started) { 705 LOG.debug("Ignoring notifying event {}. The EventNotifier has not been started yet: {}", event, notifier); 706 return; 707 } 708 709 if (!notifier.isEnabled(event)) { 710 LOG.trace("Notification of event is disabled: {}", event); 711 return; 712 } 713 714 try { 715 notifier.notify(event); 716 } catch (Throwable e) { 717 LOG.warn("Error notifying event " + event + ". This exception will be ignored. ", e); 718 } 719 } 720 721 }