CDI with Camel K runtime 1.7.
CDI does not work with Camel K runtime 1.7. The route is deployed, but when using the injected bean I get either a "nullpointerexception" or "bean not found". I tried it on an example from your project https://github.com/apache/camel-k/tree/aff450bfcccfa35ac0496336b1ea3801e4f8118f/e2e/yaks/openshift/monitoring/app as well as on examples that work on Camel in combination with Quarkus (quarkus Di, spring DI). I enclose in the attachment. I ask for verification / advice. It seems like https://github.com/apache/camel-k-runtime/issues/362 issue. Thank you. CDI_pokus.zip
CDI in camel-k does not work at the moment, i.e. it is not possible to use @Inject on a camel-k integration.
when do you plan to add there?
We don't have any concrete ETA at the moment, we may have a look at this when working on camel-k 2.0 but again, no concrete ETA.
If you have any spare time to look at implementing it, then it would be very appreciated
Can you please refer me to target design, current state, documentation? I'll take a look at it. What actually solved https://github.com/apache/camel-k-runtime/issues/362?
Can you please refer me to target design, current state, documentation? I'll take a look at it. What actually solved #362?
Any update?
@ogomezdi the plan is to work on supporting directly Camel Quarkus out of the box, hence supporting CDI and any other Camel Quarkus feature. Maybe in Camel K 2.6 or 2.7 this should be already available. Keep it tuned.
I've seen in Camel K 2.6 there is a new feature for plain-quarkus
traits: camel: runtimeProvider: plain-quarkus
But I was trying to use @Inject with resource created but not working. Are we still waiting for CDI to work? or Is there any option to add?
I'm trying with:
`// camel-k: name=hello-world
// camel-k: dependency=camel-quarkus-fhir
// camel-k: trait=camel.runtime-provider=plain-quarkus
import org.apache.camel.builder.RouteBuilder;
import ca.uhn.fhir.context.FhirContext;
import jakarta.enterprise.context.ApplicationScoped; import jakarta.inject.Inject;
@ApplicationScoped public class HelloWorld extends RouteBuilder {
@Inject
FhirContext fhircontext;
@Override
public void configure() throws Exception {
System.out.println("¡¡¡¡¡¡Bean: " + fhircontext + " !!!!");
from("timer:hello?period=1000")
.routeId("Hello")
.log("Hello World!");
}
}` But still getting null
The FhirContext is a named bean, so try:
@Inject
@Named("R4")
FhirContext fhircontext;
The
FhirContextis a named bean, so try:@Inject @Named("R4") FhirContext fhircontext;
Still null 😢
In theory everything working with plain Camel Quarkus should be working the same in Camel K with this new approach. Can you try doing exactly the same with a local execution (ie, via Camel JBang run with Quarkus runtime)?
In theory everything working with plain Camel Quarkus should be working the same in Camel K with this new approach. Can you try doing exactly the same with a local execution (ie, via Camel JBang run with Quarkus runtime)?
I've tried with Quarkus runtime, and there is working properly, let me show you output logs:
-
For Camel k Execution
[1] 2025-02-24 09:06:15,413 INFO [ca.uhn.fhi.uti.VersionUtil] (main) HAPI FHIR version 7.4.0 - Rev 71e9af61cf [1] 2025-02-24 09:06:15,415 INFO [ca.uhn.fhi.con.FhirContext] (main) Creating new FHIR context for FHIR version [R4] [1] 2025-02-24 09:06:21,399 INFO [org.apa.cam.qua.cor.CamelBootstrapRecorder] (main) Apache Camel Quarkus 3.15.1 is starting [1] 2025-02-24 09:06:21,400 INFO [org.apa.cam.mai.MainSupport] (main) Apache Camel (Main) 4.8.1 is starting [1] 2025-02-24 09:06:24,604 INFO [org.apa.cam.mai.BaseMainSupport] (main) Auto-configuration summary [1] 2025-02-24 09:06:24,605 INFO [org.apa.cam.mai.BaseMainSupport] (main) [MicroProfilePropertiesSource] camel.main.routesIncludePattern = file:/etc/camel/sources/** [1] 2025-02-24 09:06:24,605 INFO [org.apa.cam.mai.BaseMainSupport] (main) [MicroProfilePropertiesSource] camel.main.sourceLocationEnabled = true [1] ¡¡¡¡¡¡Bean: -null-!!!! [1] 2025-02-24 09:06:25,011 INFO [org.apa.cam.imp.eng.AbstractCamelContext] (main) Apache Camel 4.8.1 (camel-1) is starting [1] 2025-02-24 09:06:25,019 INFO [org.apa.cam.imp.eng.AbstractCamelContext] (main) Routes startup (total:1) [1] 2025-02-24 09:06:25,019 INFO [org.apa.cam.imp.eng.AbstractCamelContext] (main) Started Hello (timer://hello) [1] 2025-02-24 09:06:25,094 INFO [org.apa.cam.imp.eng.AbstractCamelContext] (main) Apache Camel 4.8.1 (camel-1) started in 7ms (build:0ms init:0ms start:7ms) [1] 2025-02-24 09:06:25,100 INFO [io.quarkus] (main) camel-k-integration 2.6.0 on JVM (powered by Quarkus 3.15.2) started in 11.091s. [1] 2025-02-24 09:06:25,100 INFO [io.quarkus] (main) Profile prod activated. [1] 2025-02-24 09:06:25,100 INFO [io.quarkus] (main) Installed features: [camel-core, camel-fhir, camel-java-joor-dsl, camel-timer, cdi] [1] 2025-02-24 09:06:26,023 INFO [Hel.java:28] (Camel (camel-1) thread #1 - timer://hello) Hello World! [1] 2025-02-24 09:06:27,018 INFO [Hel.java:28] (Camel (camel-1) thread #1 - timer://hello) Hello World!Log line at 2025-02-24 09:06:24,605 showing ¡¡¡¡¡¡Bean: -null-!!!! -
From Quarkus runtime:
2025-02-24 10:10:35,362 INFO [ca.uhn.fhi.uti.VersionUtil] (main) HAPI FHIR version 7.4.0 - Rev 71e9af61cf 2025-02-24 10:10:35,367 INFO [ca.uhn.fhi.con.FhirContext] (main) Creating new FHIR context for FHIR version [R4] 2025-02-24 10:10:37,205 INFO [org.apa.cam.qua.cor.CamelBootstrapRecorder] (main) Apache Camel Quarkus 3.15.1 is starting 2025-02-24 10:10:37,207 INFO [org.apa.cam.mai.MainSupport] (main) Apache Camel (Main) 4.8.1 is starting ¡¡¡¡¡¡Bean: -FhirContext[R4]-!!!! 2025-02-24 10:10:37,399 INFO [org.apa.cam.imp.eng.AbstractCamelContext] (main) Apache Camel 4.8.1 (camel-1) is starting 2025-02-24 10:10:37,410 INFO [org.apa.cam.imp.eng.AbstractCamelContext] (main) Routes startup (total:1) 2025-02-24 10:10:37,410 INFO [org.apa.cam.imp.eng.AbstractCamelContext] (main) Started Hello (timer://hello) 2025-02-24 10:10:37,410 INFO [org.apa.cam.imp.eng.AbstractCamelContext] (main) Apache Camel 4.8.1 (camel-1) started in 10ms (build:0ms init:0ms start:10ms) 2025-02-24 10:10:37,417 INFO [io.quarkus] (main) hello-world 1.0.0 on JVM (powered by Quarkus 3.15.2) started in 2.359s. 2025-02-24 10:10:37,417 INFO [io.quarkus] (main) Profile prod activated. 2025-02-24 10:10:37,418 INFO [io.quarkus] (main) Installed features: [camel-core, camel-fhir, camel-timer, cdi] 2025-02-24 10:10:38,444 INFO [Hello] (Camel (camel-1) thread #1 - timer://hello) Hello World! 2025-02-24 10:10:39,409 INFO [Hello] (Camel (camel-1) thread #1 - timer://hello) Hello World! 2025-02-24 10:10:40,411 INFO [Hello] (Camel (camel-1) thread #1 - timer://hello) Hello World!Log line at 2025-02-24 10:10:37,207 showing ¡¡¡¡¡¡Bean: -FhirContext[R4]-!!!!
The route it's soo simple, in both cases I've used: ` // camel-k: name=hello-world // camel-k: dependency=camel-quarkus-fhir // camel-k: trait=camel.runtime-provider=plain-quarkus
package org.acme.osgomez;
import org.apache.camel.builder.RouteBuilder;
import ca.uhn.fhir.context.FhirContext; import jakarta.enterprise.context.ApplicationScoped; import jakarta.inject.Inject; import jakarta.inject.Named;
@ApplicationScoped public class HelloWorld extends RouteBuilder {
@Inject
@Named("R4")
FhirContext fhircontext;
@Override
public void configure() throws Exception {
System.out.println("¡¡¡¡¡¡Bean: -" + fhircontext + "-!!!!");
from("timer:hello?period=1000")
.routeId("Hello")
.log("Hello World!");
}
}
Finally de pom used for Quarkus runtime was:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.acme.osgomez</groupId>
<artifactId>hello-world</artifactId>
<properties>
<compiler-plugin.version>3.13.0</compiler-plugin.version>
<maven.compiler.release>21</maven.compiler.release>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<quarkus.platform.artifact-id>quarkus-bom</quarkus.platform.artifact-id>
<quarkus.platform.group-id>io.quarkus.platform</quarkus.platform.group-id>
<quarkus.platform.version>3.15.2</quarkus.platform.version>
<skipITs>true</skipITs>
<surefire-plugin.version>3.3.1</surefire-plugin.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>${quarkus.platform.group-id}</groupId>
<artifactId>${quarkus.platform.artifact-id}</artifactId>
<version>${quarkus.platform.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>${quarkus.platform.group-id}</groupId>
<artifactId>quarkus-camel-bom</artifactId>
<version>${quarkus.platform.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-fhir</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-core</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-timer</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-arc</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-junit5</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>${quarkus.platform.group-id}</groupId>
<artifactId>quarkus-maven-plugin</artifactId>
<version>${quarkus.platform.version}</version>
<extensions>true</extensions>
<executions>
<execution>
<goals>
<goal>build</goal>
<goal>generate-code</goal>
<goal>generate-code-tests</goal>
<goal>native-image-agent</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>${compiler-plugin.version}</version>
<configuration>
<parameters>true</parameters>
</configuration>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>${surefire-plugin.version}</version>
<configuration>
<systemPropertyVariables>
<java.util.logging.manager>org.jboss.logmanager.LogManager</java.util.logging.manager>
<maven.home>${maven.home}</maven.home>
</systemPropertyVariables>
</configuration>
</plugin>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<version>${surefire-plugin.version}</version>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
<configuration>
<systemPropertyVariables>
<native.image.path>${project.build.directory}/${project.build.finalName}-runner</native.image.path>
<java.util.logging.manager>org.jboss.logmanager.LogManager</java.util.logging.manager>
<maven.home>${maven.home}</maven.home>
</systemPropertyVariables>
</configuration>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>native</id>
<activation>
<property>
<name>native</name>
</property>
</activation>
<properties>
<skipITs>false</skipITs>
<quarkus.native.enabled>true</quarkus.native.enabled>
</properties>
</profile>
</profiles>
`
Maybe the difference relies on how the Route it's been loaded, camel k loads the route at runtime, but I'm not sure.
` Maybe the difference relies on how the Route it's been loaded, camel k loads the route at runtime, but I'm not sure.
Yep, likely this is the reason as it may have different classloaders involved, at least in java. It would be interesting to try with other DSL such as YAML as I think in that case we'd be in a similar situation in local execution of Camel Quarkus as well. IIRC, during a build of a Java route, the class is packaged as part of the dependency, which is something not happening in Camel K.
May be related to https://github.com/apache/camel-k/issues/5539
May be related to apache/camel-k#5539
Absolutely, I'll also follow this issue to see if finally we got it. Thks!!!