public class MyLoggingSentEventNotifer extends EventNotifierSupport {
public void notify(EventObject event) throws Exception {
if (event instanceof ExchangeCompletedEvent) {;
ExchangeCompletedEvent completedEvent = (ExchangeCompletedEvent) event;
Exchange exchange = completedEvent.getExchange();
String routeId = exchange.getFromRouteId();
Date created = ((ExchangeCompletedEvent) event).getExchange()
.getProperty(Exchange.CREATED_TIMESTAMP, Date.class);
// calculate elapsed time
Date now = new Date();
long elapsed = now.getTime() - created.getTime();
log.info("Took " + elapsed + " millis on the route : " + routeId);
}
}
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
}
}
context.getManagementStrategy().addEventNotifier(new MyLoggingSentEventNotifer());
Reference
https://people.apache.org/~dkulp/camel/eventnotifier-to-log-details-about-all-sent-exchanges.html
Update
The Exchange.CREATED_TIMESTAMP
is no longer stored as exchange property, but you should use the getCreated
method on Exchange.