EventNotifier to log details about all sent Exchanges
If 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:
EventNotifiersimply by adding it:
And in Spring XML you declare a Spring bean and Camel will automatically pick it up:Error rendering macro 'code': Invalid value specified for parameter 'java.lang.NullPointerException'
<bean id="myLoggingEventNotifier" class="org.apache.camel.processor.MyLoggingSentEventNotifer"/> <camelContext 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:
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. Then you have a logger which logs all slow events.
EndpointHelper.matchEndpointmethod to leverage the same filtering capabilities that for example the Intercept uses in Camel.
You can see which
EventObject Camel provides from the javadoc.
From Camel 2.10: there is also a
ExchangeSendingEvent which is an event triggered before sending the message to the endpoint. Then we have an even for both before and after sending the message.