Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

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
languagejava
@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 annotationCDI equivalent
Configuration property
Code Block
languagejava
@PropertyInject("propertykey")
String propertyvalue;

If using DeltaSpike configuration mechanism:

Code Block
languagejava
@Inject
@ConfigProperty(name = "messagekey")
String propertyvalue;

See configuration properties for more details.

Producer template injection (default Camel context)
Code Block
languagejava
@Produce(uri = "mock:outbound")
ProducerTemplate producer;
Code Block
languagejava
@Inject
@Uri("direct:outbound")
ProducerTemplate producer;
Endpoint injection (default Camel context)
Code Block
languagejava
@EndpointInject(uri = "direct:inbound")
Endpoint endpoint;
Code Block
languagejava
@Inject
@Uri("direct:inbound")
Endpoint endpoint;
Endpoint injection (Camel context by name)
Code Block
languagejava
@EndpointInject(uri = "direct:inbound", context = "foo")
Endpoint contextEndpoint;
Code Block
languagejava
@Inject
@ContextName("foo")
@Uri("direct:inbound")
Endpoint contextEndpoint;
Bean injection (by type)
Code Block
languagejava
@BeanInject
MyBean bean;
Code Block
languagejava
@Inject
MyBean bean;
Bean injection (by name)
Code Block
languagejava
@BeanInject("foo")
MyBean bean;
Code Block
languagejava
@Inject
@Named("foo")
MyBean bean;
POJO consuming
Code Block
languagejava
@Consume(uri = "seda:inbound")
void consume(@Body String body) {
    //...
}
 

...

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
languagejava
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
languagejava
@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
languagejava
@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