...
Note that you can declare as many RoutesBuilder
beans as you want. Besides, RouteContainer
beans are also automatically collected, instantiated and added to the CamelContext
bean instance managed by Camel CDI when the container initializes.
Available as of Camel 2.19
In some situations, it may be necessary to disable the auto-configuration of the RouteBuilder
and RouteContainer
beans. That can be achieved by observing for the CdiCamelConfiguration
event, e.g.:
Code Block | ||
---|---|---|
| ||
static void configuration(@Observes CdiCamelConfiguration configuration) {
configuration.autoConfigureRoutes(false);
} |
Auto-Configured Camel Primitives
...
Else, if more customization 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 customization, 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.:
...
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.
...
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.:
...
For example, you can use the provided annotation literal for the @Uri
qualifier to lazily lookup for Camel primitives, e.g. for ProducerTemplate
beans:
...
Code Block | ||
---|---|---|
| ||
@Any @Inject Instance<ProducerTemplate> producers; ProducerTemplate inbound = producers .select(Uri.Literal.of("direct:inbound")) .get(); |
Endpoint
beans, e.g.:...
Code Block | ||
---|---|---|
| ||
@Any @Inject Instance<Endpoint> endpoints; MockEndpoint outbound = endpoints .select(MockEndpoint.class, Uri.Literal.of("mock:outbound")) .get(); |
Similarly, you can use the provided annotation literal for the @ContextName
qualifier to lazily lookup for CamelContext
beans, e.g.:
...
Code Block | ||
---|---|---|
| ||
@Any @Inject Instance<CamelContext> contexts; CamelContext context = contexts .select(ContextName.Literal.of("foo")) .get(); |
You can also refined the selection based on the Camel context type, e.g.:
Code Block | ||
---|---|---|
| ||
@Any @Inject Instance<CamelContext> contexts; // Refine the selection by type Instance<DefaultCamelContext> context = contexts.select(DefaultCamelContext.class); // Check if such a bean exists then retrieve a reference if (!context.isUnsatisfied()) context.get(); |
Or even iterate over a selection of Camel contexts, e.g.:
...
Code Block | ||
---|---|---|
| ||
@Any @Inject Instance<CamelContext> contexts; for (CamelContext context : contexts) context.setUseBreadcrumb(true); |
Maven Archetype
Among the available Camel Maven archetypes, you can use the provided camel-archetype-cdi
to generate a Camel CDI Maven project, e.g.:
...
Container | Version | Runtime |
---|---|---|
Weld SE |
| CDI 1.0 / Java SE 7 |
OpenWebBeans |
| CDI 1.0 / Java SE 7 |
Weld SE |
| CDI 1.2 / Java SE 7 |
OpenWebBeans |
| CDI 1.2 / Java SE 7 |
WildFly |
| CDI 1.2 / Java EE 7 |
WildFly |
| CDI 1.2 / Java EE 7 |
WildFly |
| CDI 1.2 / Java EE 7 |
Karaf |
| CDI 1.2 / OSGi 4 / PAX CDI |
Karaf |
| CDI 1.2 / OSGi 5 / PAX CDI |
Karaf |
| CDI 1.2 / OSGi 6 / PAX CDI |
...