...
Info |
---|
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. |
...
Else, if more customisation is needed, any CamelContext
class can be used to declare a custom Camel context bean. Then, the @PostConstruct
and @PreDestroy
lifecycle callbacks can be done to do the customisation, e.g.:
Code Block | ||
---|---|---|
| ||
@ApplicationScoped class CustomCamelContext extends DefaultCamelContext { @PostConstruct void customize() { // Set the Camel context name setName("custom"); // Disable JMX disableJMX(); } @PreDestroy void cleanUp() { // ... } } |
Producer and disposer methods can also be used as well to customize the Camel context bean, e.g.:
...
Camel annotation | CDI equivalent | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
Configuration property |
| If using DeltaSpike configuration mechanism:
See configuration properties for more details. | ||||||||||
Producer template injection (default Camel context) |
|
| ||||||||||
Endpoint injection (default Camel context) |
|
| ||||||||||
Endpoint injection (Camel context by name) |
|
| ||||||||||
Bean injection (by type) |
|
| ||||||||||
Bean injection (by name) |
|
| ||||||||||
POJO consuming |
|
...
Camel events to CDI events
Available as of Camel 2.17
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.:
...
Similarly, the @Default
qualifier can be used to observe Camel events for the default Camel context if multiples contexts exist, e.g.:
Code Block | ||
---|---|---|
| ||
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
Available as of Camel 2.17
The CDI event endpoint bridges the CDI events with the Camel routes so that CDI events can be seamlessly observed / consumed (resp. produced / fired) from Camel consumers (resp. by Camel producers).
...
This is equivalent to writing:
...
Code Block | ||
---|---|---|
| ||
@Inject Event<String> event; from("direct:event").process(new Processor() { @Override public void process(Exchange exchange) { event.fire(exchange.getBody(String.class)); } }).log("CDI event sent: ${body}"); |
Or using a Java 8 lambda expression:
Code Block | ||
---|---|---|
| ||
@Inject Event<String> event; from("direct:event") .process(exchange -> event.fire(exchange.getIn().getBody(String.class))) .log("CDI event sent: ${body}"); |
The type variable T
(resp. the qualifiers) of a particular CdiEventEndpoint<T>
injection point are automatically translated into the parameterized event type (resp. into the event qualifiers) e.g.:
...
camel-example-cdi-metrics
- illustrates the integration between Camel, Dropwizard Metrics and CDI,camel-example-cdi-properties
- illustrates the integration between Camel, DeltaSpike and CDI for configuration properties,camel-example-cdi-osgi
- a CDI application using the SJMS component that can be executed inside an OSGi container using PAX CDI,camel-example-cdi-rest-servlet
- illustrates the Camel REST DSL being used in a Web application that uses CDI as dependency injection framework,camel-example-widget-gadget-cdi
- The Widget and Gadget use-case from the EIP book implemented in Java with CDI dependency Injection.
See Also
- CDI Web site
- CDI ecosystem
- Going further with CDI (See Camel CDI section)