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

...

As part of the Camel bean integration, Camel comes with a set of annotations that are seamlessly supported by Camel CDI. So you can use any of these annotations in your CDI beans, e.g.:

 

Camel annotation

CDI equivalent

Configuration property

Code Block
languagejava
@PropertyInject("key")
String value;

If using DeltaSpike configuration mechanism:

Code Block
languagejava
@Inject
@ConfigProperty(name = "key")
String value;

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) {
    //...
}
 

...

You can refer to CDI beans, either by type or name, From from the Camel DSL, e.g. with , using the Java Camel DSL:

Code Block
languagejava
class MyBean {
	//...
}
 
from("direct:inbound").bean(MyBean.class);

...

Code Block
languagejava
@Named("foo")
class MyNamedBean {
	//...
}
 
from("direct:inbound")
  .bean("foo");

Referring Beans From Endpoint URIs

When configuring endpoints using the URI syntax you can refer to beans in the Registry using the # notation. If the URI parameter value starts with a # sign sign then Camel CDI will lookup for a bean of the given type by name, e.g.:

Code Block
languagejava
from("jms:queue:{{destination}}?transacted=true&transactionManager=#jtaTransactionManager")
  .to("...");

Having the following CDI bean qualified with @Named("jtaTransactionManager"):

...

 

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.

...