Camel Event Logging With Spring Java Configuration

Here's how to set up a Camel EventNotifier which is managed by Spring Java Configuration. You may wish to do this if you EventNotifier implementation has further Spring-based dependencies of its own.

We can do this by having our Spring config extend CamelConfiguration:

@Configuration
public class MyConfig extends CamelConfiguration {
...
}

Then we can wire in our custom EventNotifier (and any other beans it requires):

@Bean
public EventNotifier newEventNotifier() {
    CustomEventNotifier eventNotifier = new CustomEventNotifier();
    eventNotifier.setIgnoreCamelContextEvents(true);
    eventNotifier.setIgnoreRouteEvents(true);
    eventNotifier.setIgnoreServiceEvents(true);
    eventNotifier.setIgnoreExchangeCreatedEvent(true);
    // etc.
    return eventNotifier;
}

Finally, and here's the critical part for Camel, we need to inject it into the CamelContext. We can do this by overriding CamelConfiguration's setupCamelContext method, which will do our work at the right time in the Spring+Camel lifecycle:

@Override
protected void setupCamelContext(CamelContext camelContext) {
    ApplicationContext ctx = getApplicationContext();
    ManagementStrategy mgtStrategy = camelContext.getManagementStrategy()  ;      
    mgtStrategy.addEventNotifier(ctx.getBean(EventNotifier.class));
}