This Confluence has been LDAP enabled, if you are an ASF Committer, please use your LDAP Credentials to login. Any problems file an INFRA jira ticket please.

Child pages
  • CDI

Versions Compared

Key

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

...

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
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.:

...

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.

...

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.:

...

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
languagejava
@Any
@Inject
Instance<ProducerTemplate> producers;
 
ProducerTemplate inbound = producers
    .select(Uri.Literal.of("direct:inbound"))
    .get();

 

Or for Endpoint beans, e.g.:

 

Code Block
languagejava
@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
languagejava
@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
languagejava
@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
languagejava
@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.:

...