...
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.:
...
Code Block | ||
---|---|---|
| ||
class MyRouteBean extends RouteBuilder {
@Override
public void configure() {
from("activemq:queue:foo")
.transacted("PROPAGATION_REQUIRED")
.bean("transformer")
.to("jpa:my.application.entity.Bar")
.log("${body.id} inserted");
}
}
|
This would be equivalent to:
Code Block | ||
---|---|---|
| ||
class MyRouteBean extends RouteBuilder { @Inject @Named("PROPAGATION_REQUIRED") Policy required; @Override public void configure() { from("activemq:queue:foo") .policy(required) .bean("transformer") .to("jpa:my.application.entity.Bar") .log("${body.id} inserted"); } } |
The list of supported transaction policy names is: PROPAGATION_NEVER
, PROPAGATION_NOT_SUPPORTED
, PROPAGATION_SUPPORTS
, PROPAGATION_REQUIRED
, PROPAGATION_REQUIRES_NEW
, PROPAGATION_NESTED
, PROPAGATION_MANDATORY
.
Transactional error handler
...
Auto-configured OSGi integration
Available as of Camel 2.17CDI provides a transactional error handler that extends the redelivery error handler, forces a rollback whenever an exception occurs and creates a new transaction for each redelivery. Camel CDI provides the CdiRouteBuilder
class that exposes the transactionErrorHandler
helper method to enable quick access to the configuration, e.g.:
Code Block | ||
---|---|---|
| ||
class MyRouteBean extends CdiRouteBuilder {
@Override
public void configure() {
errorHandler(transactionErrorHandler()
.setTransactionPolicy("PROPAGATION_SUPPORTS")
.maximumRedeliveries(5)
.maximumRedeliveryDelay(5000)
.collisionAvoidancePercent(10)
.backOffMultiplier(1.5));
}
} |
Auto-configured OSGi integration
Available as of Camel 2.17
The Camel context beans are automatically adapted by Camel CDI so that The Camel context beans are automatically adapted by Camel CDI so that they are registered as OSGi services and the various resolvers (like ComponentResolver
and DataFormatResolver
) integrate with the OSGi registry. That means that the Karaf Camel commands can be used to operate the Camel contexts auto-configured by Camel CDI, 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.:
...