You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2024/06/26 08:26:55 UTC
(camel) branch main updated: Patch 5 (Add documentation for EventNotifier) (#14650)
This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/main by this push:
new 3ee6e9ffd1c Patch 5 (Add documentation for EventNotifier) (#14650)
3ee6e9ffd1c is described below
commit 3ee6e9ffd1c8044da3228d9bc1e8235964296130
Author: Raymond Meester <ra...@gmail.com>
AuthorDate: Wed Jun 26 10:26:49 2024 +0200
Patch 5 (Add documentation for EventNotifier) (#14650)
* Update index.adoc
Add event notifier documentation
* Create event-notifier.adoc
Add documentation about the use of the EventNotifier
* Update index.adoc
---
.../advanced-camel-core-guides/pages/index.adoc | 1 +
.../modules/ROOT/pages/event-notifier.adoc | 138 +++++++++++++++++++++
docs/user-manual/modules/ROOT/pages/index.adoc | 1 +
3 files changed, 140 insertions(+)
diff --git a/docs/main/modules/advanced-camel-core-guides/pages/index.adoc b/docs/main/modules/advanced-camel-core-guides/pages/index.adoc
index c47b2f82908..7aec83366c4 100644
--- a/docs/main/modules/advanced-camel-core-guides/pages/index.adoc
+++ b/docs/main/modules/advanced-camel-core-guides/pages/index.adoc
@@ -30,6 +30,7 @@ In these page you can find guides and documents that can help you to discover ho
* xref:manual::http-session-handling.adoc[HTTP Session Handling]
* xref:manual::browsable-endpoint.adoc[Browsable Endpoint]
* xref:manual::camel-configuration-utilities.adoc[Configuration Utilities]
+* xref:manual::event-notifier.adoc[Event Notifier]
If you are looking at contributing to Apache Camel, these guides may also help:
diff --git a/docs/user-manual/modules/ROOT/pages/event-notifier.adoc b/docs/user-manual/modules/ROOT/pages/event-notifier.adoc
new file mode 100644
index 00000000000..fc19134a8b3
--- /dev/null
+++ b/docs/user-manual/modules/ROOT/pages/event-notifier.adoc
@@ -0,0 +1,138 @@
+= EventNotifier
+
+The event notifier `org.apache.camel.spi.EventNotifier` is used to get notified about events within the CamelContext. For example, when a route is started or stopped.
+The events are collected independently from the CamelContext (routes, services, exchanges, and so on). This means that the notifier doesn't interfere with the rest of Camel, and
+the events can be processed independently for logging, data offloading or other type of actions.
+
+== Type of Events
+
+The following CamelEvents are available:
+
+* CamelEvent
+** CamelContextEvent
+*** CamelContextInitializedEvent
+*** CamelContextInitializingEvent
+*** CamelContextReloadedEvent
+*** CamelContextReloadFailureEvent
+*** CamelContextReloadingEvent
+*** CamelContextResumedEvent
+*** CamelContextResumeFailureEvent
+*** CamelContextResumingEvent
+*** CamelContextRoutesStartedEvent
+*** CamelContextRoutesStartingEvent
+*** CamelContextRoutesStoppedEvent
+*** CamelContextRoutesStoppingEvent
+*** CamelContextStartedEvent
+*** CamelContextStartingEvent
+*** CamelContextStartupFailureEvent
+*** CamelContextStopFailureEvent
+*** CamelContextStoppedEvent
+*** CamelContextStoppingEvent
+*** CamelContextSuspendedEvent
+*** CamelContextSuspendingEvent
+** ExchangeEvent
+*** ExchangeAsyncProcessingStartedEvent
+*** ExchangeCompletedEvent
+*** ExchangeCreatedEvent
+*** ExchangeFailedEvent
+*** ExchangeFailureEvent
+*** ExchangeFailureHandledEvent
+*** ExchangeFailureHandlingEvent
+*** ExchangeRedeliveryEvent
+*** ExchangeSendingEvent
+*** ExchangeSentEvent
+** FailureEvent
+** RouteEvent
+*** RouteAddedEvent
+*** RouteReloadedEvent
+*** RouteRemovedEvent
+*** RouteStartedEvent
+*** RouteStartingEvent
+*** RouteStoppedEvent
+*** RouteStoppingEvent
+** ServiceEvent
+*** ServiceStartupFailureEvent
+*** ServiceStopFailureEvent
+** StepEvent
+*** StepCompletedEvent
+*** StepFailedEvent
+*** StepStartedEvent
+
+See the Javadoc of the `org.apache.camel.spi.CamelEvent` for more details.
+
+== Collect Events
+
+To collect events a class is needed that extends the EventNotifierSupport class.
+
+[source,java]
+----
+public class MyCollector extends EventNotifierSupport {
+
+ protected Logger log = LoggerFactory.getLogger(getClass());
+
+ @Override
+ public void notify(CamelEvent event) throws Exception {
+
+ log.info("Event: " event.getType().name());
+
+ }
+
+}
+----
+
+Then you can enable the notifier:
+
+[source,java]
+----
+context.getManagementStrategy().addEventNotifier(new MyCollector());
+----
+
+
+== Collect Specific Events
+
+By default you get notified by all events. It's however possible to exclude certain groups of events to get
+only the group of event you are interest in.
+
+In the following example we collect only StepEvents:
+
+[source,java]
+----
+public class StepCollector extends EventNotifierSupport {
+ protected Logger log = LoggerFactory.getLogger(getClass());
+
+ @Override
+ public void notify(CamelEvent event) throws Exception {
+
+ // Cast CamelEvent to StepEvent
+ CamelEvent.StepEvent stepEvent = (CamelEvent.StepEvent) event;
+
+ // Get detailed information from the StepEvent
+ String eventType = stepEvent.getType().name();
+ String stepId = stepEvent.getStepId();
+ String body = stepEvent.getExchange().getMessage().getBody(String.class);
+
+ log.info("Event Type:\t\t" + eventType);
+ log.info("Step ID:\t\t\t" + stepId);
+ log.info("Message Body:\t\t " + body);
+
+ }
+
+}
+----
+
+Then you can enable the notifier for steps by excluding other groups:
+
+[source,java]
+----
+//Create eventNotifier that only collects stepEvents
+StepCollector stepCollector = new StepCollector();
+stepCollector.setIgnoreCamelContextEvents(true);
+stepCollector.setIgnoreCamelContextInitEvents(true);
+stepCollector.setIgnoreExchangeEvents(true);
+stepCollector.setIgnoreRouteEvents(true);
+stepCollector.setIgnoreServiceEvents(true);
+stepCollector.setIgnoreStepEvents(false);
+
+//Add the Event Notifier to the Camel Context
+context.getManagementStrategy().addEventNotifier(stepCollector);
+----
diff --git a/docs/user-manual/modules/ROOT/pages/index.adoc b/docs/user-manual/modules/ROOT/pages/index.adoc
index 7bc73e2fb4d..03dfe4533e7 100644
--- a/docs/user-manual/modules/ROOT/pages/index.adoc
+++ b/docs/user-manual/modules/ROOT/pages/index.adoc
@@ -85,6 +85,7 @@ For a deeper and better understanding of Apache Camel, an xref:faq:what-is-camel
* xref:exchange-pattern.adoc[Exchange Pattern]
* xref:exchange-pooling.adoc[Exchange Pooling]
* xref:expression.adoc[Expression]
+* xref:event-notifier.adoc[Event Notifier]
* xref:http-session-handling.adoc[HTTP-Session Handling]
* xref:injector.adoc[Injector]
* xref:components:eips:intercept.adoc[Intercept]