EventNotifier to log details about all sent ExchangesPage edited by Claus IbsenChanges (6)
Full ContentEventNotifier to log details about all sent ExchangesIf you want to log information all time taken to send and receive replies when sending to external endpoints, then you can leverage the EventNotifier in Camel. It allows you to subscribe to events and react upon them. For example we want to log the time it takes to send to endpoints which can be done by implementing a class which this logic as follows: MyLoggingSentEventNotifier public class MyLoggingSentEventNotifer extends EventNotifierSupport {
public void notify(EventObject event) throws Exception {
if (event instanceof ExchangeSentEvent) {
ExchangeSentEvent sent = (ExchangeSentEvent) event;
log.info("Took " + sent.getTimeTaken() + " millis to send to: " + sent.getEndpoint());
}
}
public boolean isEnabled(EventObject event) {
// we only want the sent events
return event instanceof ExchangeSentEvent;
}
protected void doStart() throws Exception {
// noop
}
protected void doStop() throws Exception {
// noop
}
}
Then in Java DSL you can tell Camel to use your custom EventNotifier simply by adding it:
context.getManagementStrategy().addEventNotifier(new MyLoggingSentEventNotifer());
And in Spring XML you declare a Spring bean and Camel will automatic pick it up: <bean id="myLoggingEventNotifier" class="org.apache.camel.processor.MyLoggingSentEventNotifer"/> <camelContext id="camel" xmlns="http://camel.apache.org/schema/spring"> <route> <from uri="direct:start"/> <to uri="direct:bar"/> <to uri="mock:result"/> </route> <route> <from uri="direct:bar"/> <delay><constant>1000</constant></delay> </route> </camelContext> If you run this example you will notice how Camel logs this now: INFO CamelContextFactoryBean - Using custom EventNotifier with id: myLoggingEventNotifier and implementation: org.apache.camel.processor.myloggingsenteventnoti...@76bf9e INFO MyLoggingSentEventNotifer - Took 1001 millis to send to: Endpoint[direct://bar] INFO MyLoggingSentEventNotifer - Took 0 millis to send to: Endpoint[mock://result] INFO MyLoggingSentEventNotifer - Took 1013 millis to send to: Endpoint[direct://start] You can implement logic to filter which endpoints to log, and maybe also thresholds to skip logging if it was fast, e.g. < 1 sec. Tip you can use the EndpointHelper.matchEndpoint method to leverage the same filtering capabilities that for example the Intercept uses in Camel. You can see which EventObject Camel provides from the javadoc.
Change Notification Preferences
View Online
|
View Changes
|
Add Comment
|
- [CONF] Apache Camel > EventNotifier to log details about all... confluence
- [CONF] Apache Camel > EventNotifier to log details abou... confluence
