Camel CDI
The Camel CDI component provides auto-configuration for Apache Camel using CDI as dependency injection framework based on the convention-over-configuration principle. It auto-detects Camel routes available in the application and provides beans for common Camel primitives like Endpoint
, ProducerTemplate
or TypeConverter
. It implements standard Camel bean integration so that Camel annotations like @Consume
, @Produce
and @PropertyInject
can be used seamlessly in CDI beans. Besides, it bridges Camel events (e.g. RouteAddedEvent
, CamelContextStartedEvent
or ExchangeCompletedEvent
) as CDI events and provides a CDI events endpoint that can be used to consume / produce CDI events from / to Camel routes.
While the Camel CDI component is available as of Camel 2.10, it's been rewritten in Camel 2.17 to better fit into the CDI programming model. Hence some of the features like the Camel events to CDI events bridge and the CDI events endpoint only apply starting Camel 2.17.
Auto-configured Camel context
Camel CDI automatically deploys and configures a CamelContext
bean. That CamelContext
bean is automatically instantiated, configured and started (resp. stopped) when the CDI container initialises (resp. shuts down). It can be injected in the application, e.g.:
@Inject CamelContext context;
That default CamelContext
bean is qualified with the built-in @Default
qualifier, is scoped @ApplicationScoped
and is of type DefaultCamelContext
.
Note that this bean can be customised programmatically and other Camel context beans can be deployed in the application as well.
Auto-detecting Camel routes
Camel CDI automatically collects all the RoutesBuilder
beans in the application, instantiates and add them to the CamelContext
bean instance when the CDI container initialises. For example, adding a Camel route is as simple as declaring a class, e.g.:
class MyRouteBean extends RoutesBuilder { @Override public void configure() { from("direct:inbound") .to("mock:outbound"); } }
Custom Camel context customisation
Multiple Camel contexts support
Camel events to CDI events
Camel provides a set of management events that can be subscribed to for listening to Camel context, service, route and exchange events. Camel CDI seamlessly translates these Camel events into CDI events that can be observed using CDI observer methods, e.g.:
void onContextStarting(@Observes CamelContextStartingEvent event) { // Called before the default Camel context is about to start }
When multiple Camel contexts exist in the CDI container, the Camel context bean qualifiers, like @ContextName
, can be used to refine the observer method resolution to a particular Camel context as specified in observer resolution, e.g.:
void onRouteStarted(@Observes @ContextName("foo") RouteStartedEvent event) { // Called after the route 'event.getRoute()' for the Camel context 'foo' has started } void onContextStarted(@Observes @Manual CamelContextStartedEvent event) { // Called after the the Camel context qualified with '@Manual' has started }
Similarly, the @Default
qualifier can be used to observe Camel events for the default Camel context if multiples contexts exist, e.g.:
void onExchangeCompleted(@Observes @Default ExchangeCompletedEvent event) { // Called after the exchange 'event.getExchange()' processing has completed }
In that example, if no qualifier is specified, the @Any
qualifier is implicitly assumed, so that corresponding events for all the Camel contexts get received.
Note that the support for Camel events translation into CDI events is only activated if observer methods listening for Camel events are detected in the deployment, and that per Camel context.
CDI events endpoint
Auto-configured type converters
CDI beans annotated with the @Converter
annotation are automatically registered into the deployed Camel contexts, e.g.:
@Converter public class MyTypeConverter { @Converter public Output convert(Input input) { //... } }
Note that CDI injection is supported within the type converters.
Configuration properties
Camel bean integration
Supported containers