The @ApplicationScoped annotation is required for @Inject and @ConfigProperty to work in a RouteBuilder. Note that the @ApplicationScoped beans are managed by the CDI container and their life cycle is thus a bit more complex than the one of the plain RouteBuilder. In other words, using @ApplicationScoped in RouteBuilder comes with some boot time penalty and you should therefore only annotate your RouteBuilder with @ApplicationScoped when you really need it.
If you are used to @org.apache.camel.EndpointInject and @org.apache.camel.Produce from plain Camel or from Camel on SpringBoot, you can continue using them on Quarkus too.
The following use cases are supported by org.apache.camel.quarkus:camel-quarkus-core:
To refer to a bean in a route definition by name, just annotate the bean with @Named("myNamedBean") and @ApplicationScoped (or some other supported scope). The @RegisterForReflection annotation is important for the native mode.
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Named;
import io.quarkus.runtime.annotations.RegisterForReflection;
@ApplicationScoped
@Named("myNamedBean")
@RegisterForReflection
public class NamedBean {
public String hello(String name) {
return "Hello " + name + " from the NamedBean";
}
}
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Named;
import io.quarkus.runtime.annotations.RegisterForReflection;
@ApplicationScoped
@Named("myNamedBean")
@RegisterForReflection
public class NamedBean {
public String hello(String name) {
return "Hello " + name + " from the NamedBean";
}
}
Copy to ClipboardCopied!Toggle word wrapToggle overflow
Then you can use the myNamedBean name in a route definition:
import org.apache.camel.builder.RouteBuilder;
public class CamelRoute extends RouteBuilder {
@Override
public void configure() {
from("direct:named")
.bean("myNamedBean", "hello");
/* ... which is an equivalent of the following: */
from("direct:named")
.to("bean:myNamedBean?method=hello");
}
}
import org.apache.camel.builder.RouteBuilder;
public class CamelRoute extends RouteBuilder {
@Override
public void configure() {
from("direct:named")
.bean("myNamedBean", "hello");
/* ... which is an equivalent of the following: */
from("direct:named")
.to("bean:myNamedBean?method=hello");
}
}
Copy to ClipboardCopied!Toggle word wrapToggle overflow
As an alternative to @Named, you may also use io.smallrye.common.annotation.Identifier to name and identify a bean.
import jakarta.enterprise.context.ApplicationScoped;
import io.quarkus.runtime.annotations.RegisterForReflection;
import io.smallrye.common.annotation.Identifier;
@ApplicationScoped
@Identifier("myBeanIdentifier")
@RegisterForReflection
public class MyBean {
public String hello(String name) {
return "Hello " + name + " from MyBean";
}
}
import jakarta.enterprise.context.ApplicationScoped;
import io.quarkus.runtime.annotations.RegisterForReflection;
import io.smallrye.common.annotation.Identifier;
@ApplicationScoped
@Identifier("myBeanIdentifier")
@RegisterForReflection
public class MyBean {
public String hello(String name) {
return "Hello " + name + " from MyBean";
}
}
Copy to ClipboardCopied!Toggle word wrapToggle overflow
Then refer to the identifier value within the Camel route:
import org.apache.camel.builder.RouteBuilder;
public class CamelRoute extends RouteBuilder {
@Override
public void configure() {
from("direct:start")
.bean("myBeanIdentifier", "Camel");
}
}
import org.apache.camel.builder.RouteBuilder;
public class CamelRoute extends RouteBuilder {
@Override
public void configure() {
from("direct:start")
.bean("myBeanIdentifier", "Camel");
}
}
Copy to ClipboardCopied!Toggle word wrapToggle overflow
Note
We aim at supporting all use cases listed in Bean binding section of Camel documentation. Do not hesitate to file an issue if some bean binding scenario does not work for you.
Since Camel Quarkus 2.0.0, the camel-quarkus-bean artifact brings support for @org.apache.camel.Consume - see the Pojo consuming section of Camel documentation.
Declaring a class like the following
import org.apache.camel.Consume;
public class Foo {
@Consume("activemq:cheese")
public void onCheese(String name) {
...
}
}
import org.apache.camel.Consume;
public class Foo {
@Consume("activemq:cheese")
public void onCheese(String name) {
...
}
}
Copy to ClipboardCopied!Toggle word wrapToggle overflow
will automatically create the following Camel route
Copy to ClipboardCopied!Toggle word wrapToggle overflow
for you. Note that Camel Quarkus will implicitly add @jakarta.inject.Singleton and jakarta.inject.Named("foo1234") to the bean class, where 1234 is a hash code obtained from the fully qualified class name. If your bean has some CDI scope (such as @ApplicationScoped) or @Named("someName") set already, those will be honored in the auto-created route.