...
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 initialises.
...
Auto-configured Camel primitives
Camel CDI provides beans for common Camel primitives that can be injected in any CDI beans, e.g.:
Code Block | ||
---|---|---|
| ||
@Inject
@Uri("direct:inbound")
ProducerTemplate producerTemplate;
@Inject
MockEndpoint outbound; // URI defaults to the member name, i.e. mock:outbound
@Inject
@Uri("direct:inbound")
Endpoint endpoint;
@Inject
TypeConverter converter; |
Camel context configuration
If you just want to change the name of the default CamelContext
bean, you can used the @ContextName
qualifier provided by Camel CDI, e.g.:
...
Code Block | ||
---|---|---|
| ||
@ApplicationScoped class ManualStartupCamelContext extends DefaultCamelContext { @PostConstruct void manual() { setAutoStartup(false); } } |
Multiple Camel contexts
...
Any number of CamelContext
beans can actually be declared in the application as documented above. In that case, the CDI qualifiers declared on these CamelContext
beans are used to bind the Camel routes and other Camel primitives to the corresponding Camel contexts. From example, if the following beans get declared:
...
The RoutesBuilder
beans qualified with @ContextName
get are automatically added to the corresponding CamelContext
beans by Camel CDI. If no such CamelContext
bean exists, it gets automatically added by Camel CDIcreated, as for the RouteAdddedToBazCamelContext
RouteAddedToBazCamelContext
bean. Note this only happens for the @ContextName
qualifier provided by Camel CDI. Hence the RouteNotAddedToAnyCamelContext
bean qualified with the user-defined @MyOtherQualifier
qualifier does not get added to any Camel contexts. That may be useful, for example, for Camel routes that may be required to be added later during the application execution.
...
Code Block | ||
---|---|---|
| ||
@Produces @ApplicationScoped @Named("properties") PropertiesComponent properties(PropertiesParser parser) { PropertiesComponent component = new PropertiesComponent(); component.setPropertiesParser(parser); return component; } // PropertiesParser bean that uses DeltaSpike to resolve properties static class DeltaSpikeParser extends DefaultPropertiesParser { @Override public String parseProperty(String key, String value, Properties properties) { return ConfigResolver.getPropertyValue(key); } } |
You can see the camel-example-cdi-properties
example for a working example of a Camel CDI application using DeltaSpike configuration mechanism.
Auto-configured type converters
...
Code Block | ||
---|---|---|
| ||
@Named("foo") class MyBeanMyNamedBean { //... } from("direct:inbound").bean("foo"); |
...
Code Block | ||
---|---|---|
| ||
@Inject @ContextName("foo") CdiEventEndpoint<List<String>> cdiEventEndpoint; // Only observes / consumes events having the @ContextName("foo") qualifier from(cdiEventEndpoint).log("Camel context '(foo') > CDI event received: ${body}"); // Produces / fires events with the @ContextName("foo") qualifier from("...").to(cdiEventEndpoint); void observeCdiEvents(@Observes @ContextName("foo") List<String> event) { logger.info("Camel context '(foo') > CDI event: {}", event); } |
...
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 the Widget and Gadget use-case from the EIP book implemented in Java with CDI dependency Injection,camel-example-swagger-cdi
- an example using REST DSL and Swagger Java with CDI.
See Also
- CDI Web site
- CDI ecosystem
- Going further with CDI (See Camel CDI section)