Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Injecting a CDI bean managed by Quarkus into Camel K routes leads to a null reference #362

Closed
astefanutti opened this issue Jun 16, 2020 · 9 comments
Assignees

Comments

@astefanutti
Copy link
Member

In this example https://github.com/astefanutti/camel-k-example-metrics, I've declared a CDI bean in an external beans archive, that I use in a Camel K route.

However, this leads to the following exception when the integration starts:

2020-06-15 16:00:35,124 ERROR [io.qua.application] (main) Failed to start application: java.lang.RuntimeException: org.apache.camel.FailedToCreateRouteException: Failed to create route unreliable-service at: >>> process[Processor@0x0] <<< in route: Route(unreliable-service)[From[timer:stream?period=1000] -> ... because of ref must be specified on: process[Processor@0x0]
	at org.apache.camel.quarkus.core.CamelMainRecorder.start(CamelMainRecorder.java:99)
	at io.quarkus.deployment.steps.Main$start317617795.deploy_0(Main$start317617795.zig:115)
	at io.quarkus.deployment.steps.Main$start317617795.deploy(Main$start317617795.zig:36)
	at io.quarkus.runner.ApplicationImpl.doStart(ApplicationImpl.zig:522)
	at io.quarkus.runtime.Application.start(Application.java:90)
	at io.quarkus.runtime.ApplicationLifecycleManager.run(ApplicationLifecycleManager.java:90)
	at io.quarkus.runtime.Quarkus.run(Quarkus.java:61)
	at io.quarkus.runtime.Quarkus.run(Quarkus.java:38)
	at io.quarkus.runtime.Quarkus.run(Quarkus.java:106)
	at io.quarkus.runner.GeneratedMain.main(GeneratedMain.zig:29)
Caused by: org.apache.camel.FailedToCreateRouteException: Failed to create route unreliable-service at: >>> process[Processor@0x0] <<< in route: Route(unreliable-service)[From[timer:stream?period=1000] -> ... because of ref must be specified on: process[Processor@0x0]
	at org.apache.camel.reifier.RouteReifier.doCreateRoute(RouteReifier.java:393)
	at org.apache.camel.reifier.RouteReifier.createRoute(RouteReifier.java:117)
	at org.apache.camel.quarkus.core.FastCamelContext.startRouteDefinitions(FastCamelContext.java:694)
	at org.apache.camel.quarkus.core.FastCamelContext.startRouteDefinitions(FastCamelContext.java:486)
	at org.apache.camel.impl.engine.AbstractCamelContext.doInit(AbstractCamelContext.java:2617)
	at org.apache.camel.quarkus.core.FastCamelContext.doInit(FastCamelContext.java:372)
	at org.apache.camel.support.service.BaseService.init(BaseService.java:83)
	at org.apache.camel.impl.engine.AbstractCamelContext.init(AbstractCamelContext.java:2450)
	at org.apache.camel.quarkus.core.CamelMain.doInit(CamelMain.java:47)
	at org.apache.camel.support.service.BaseService.init(BaseService.java:83)
	at org.apache.camel.support.service.BaseService.start(BaseService.java:111)
	at org.apache.camel.quarkus.core.CamelMainRecorder.start(CamelMainRecorder.java:97)
	... 9 more
Caused by: java.lang.IllegalArgumentException: ref must be specified on: process[Processor@0x0]
	at org.apache.camel.util.ObjectHelper.notNull(ObjectHelper.java:171)
	at org.apache.camel.reifier.ProcessReifier.createProcessor(ProcessReifier.java:39)
	at org.apache.camel.reifier.ProcessorReifier.makeProcessor(ProcessorReifier.java:766)
	at org.apache.camel.reifier.ProcessorReifier.addRoutes(ProcessorReifier.java:511)
	at org.apache.camel.reifier.RouteReifier.doCreateRoute(RouteReifier.java:391)
	... 20 more
@lburgazzoli
Copy link
Contributor

lburgazzoli commented Jun 17, 2020

The reason of the failure is because of a wrong ordering of loading routes and post process them in camel-quarkus so the call to configure() method happens before the post processor kicks in, thus the service bean is not yet injected when bound to the route.

However, if you change the processor invocation from .process(service) to process("service") (so by name) then the route should not fail to start and your processor will be invoked.

Opened an issue on camel-quarkus

@astefanutti
Copy link
Member Author

Thanks a lot! I confirm process("service") works.

@astefanutti
Copy link
Member Author

It seems however lookup by type does not work:

https://github.com/astefanutti/camel-k-example-metrics/commit/9837214baf28ded239171c48e5f91b8f1a616509

There is no error, but the bean instances are not the same so the method handler interceptor does not work.

@lburgazzoli
Copy link
Contributor

lburgazzoli commented Jun 18, 2020

yeah that is because under the hood the BeanReifier does

camelContext.getClassResolver().resolveMandatoryClass(beanType)

so it won't search for a bean of type Service but it will instantiate a new instance

@astefanutti
Copy link
Member Author

That is what I suspected, but I find it a bit confusing that bean("beanName") does lookup into the registry, while bean(Bean.class) does not.

@davsclaus
Copy link
Contributor

@astefanutti yeah we can make bean(class) lookup too and if there is a single bean of that type then use it. However Camel did not have lookup anonymous by type in the beginning, and hence why bean is like that.

Would you create a JIRA ticket at Camel 3 project for this improvement

@astefanutti
Copy link
Member Author

@lburgazzoli
Copy link
Contributor

@astefanutti the injection issue has been fixed in camel-quakrus and will be available in the next release

@astefanutti
Copy link
Member Author

@lburgazzoli awesome. I'll try it ASAP. Let me close this then.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants