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

Trying to use bean method call #class:com.mongodb.client.MongoClients#create("mongodb://foo") throws ClassNotFoundException #2444

Closed
claudio4j opened this issue Jun 24, 2021 · 8 comments
Assignees

Comments

@claudio4j
Copy link
Contributor

claudio4j commented Jun 24, 2021

To create a custom mongodb connection using MongoClient in a kamelet throws a ClassNotFoundException

The MongoClient is created like:

MongoClient mongoClient = MongoClients.create("mongodb://myuser:mypasswd@172.17.0.9:27017");

Setting the bean in a kamelet

  flow:
    beans:
       - name: mongoDBConnection
         type: '#class:com.mongodb.client.MongoClients#create("mongodb://myuser:mypasswd@172.17.0.9:27017")'

Also tried with #type in place of #class, but same error occurs.

Causes

2021-06-24 17:36:36,231 ERROR [org.apa.cam.qua.mai.CamelMainRuntime] (main) Failed to start application: org.apache.camel.RuntimeCamelException: org.apache.camel.VetoCamelContextStartException: Failure creating route from template: mongodb-sink
        at org.apache.camel.RuntimeCamelException.wrapRuntimeException(RuntimeCamelException.java:66)
        at org.apache.camel.support.service.BaseService.doFail(BaseService.java:413)
        at org.apache.camel.impl.engine.AbstractCamelContext.doFail(AbstractCamelContext.java:3349)
        at org.apache.camel.support.service.BaseService.fail(BaseService.java:342)
        at org.apache.camel.impl.engine.AbstractCamelContext.failOnStartup(AbstractCamelContext.java:4846)
        at org.apache.camel.impl.engine.AbstractCamelContext.init(AbstractCamelContext.java:2487)
        at org.apache.camel.support.service.BaseService.start(BaseService.java:111)
        at org.apache.camel.impl.engine.AbstractCamelContext.start(AbstractCamelContext.java:2494)
        at org.apache.camel.quarkus.main.CamelMain.doStart(CamelMain.java:94)
        at org.apache.camel.support.service.BaseService.start(BaseService.java:119)
        at org.apache.camel.quarkus.main.CamelMain.startEngine(CamelMain.java:139)
        at org.apache.camel.quarkus.main.CamelMainRuntime.start(CamelMainRuntime.java:49)
        at org.apache.camel.quarkus.core.CamelBootstrapRecorder.start(CamelBootstrapRecorder.java:45)
        at io.quarkus.deployment.steps.CamelBootstrapProcessor$boot-173480958.deploy_0(CamelBootstrapProcessor$boot-173480958.zig:101)
        at io.quarkus.deployment.steps.CamelBootstrapProcessor$boot-173480958.deploy(CamelBootstrapProcessor$boot-173480958.zig:40)
        at io.quarkus.runner.ApplicationImpl.doStart(ApplicationImpl.zig:722)
        at io.quarkus.runtime.Application.start(Application.java:101)
        at io.quarkus.runtime.ApplicationLifecycleManager.run(ApplicationLifecycleManager.java:101)
        at io.quarkus.runtime.Quarkus.run(Quarkus.java:66)
        at io.quarkus.runtime.Quarkus.run(Quarkus.java:42)
        at io.quarkus.runtime.Quarkus.run(Quarkus.java:119)
        at io.quarkus.runner.GeneratedMain.main(GeneratedMain.zig:29)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.base/java.lang.reflect.Method.invoke(Method.java:566)
        at io.quarkus.bootstrap.runner.QuarkusEntryPoint.doRun(QuarkusEntryPoint.java:48)
        at io.quarkus.bootstrap.runner.QuarkusEntryPoint.main(QuarkusEntryPoint.java:25)
Caused by: org.apache.camel.VetoCamelContextStartException: Failure creating route from template: mongodb-sink                                                                                                                                                
        at org.apache.camel.component.kamelet.KameletComponent$LifecycleHandler.onContextInitialized(KameletComponent.java:405)
        at org.apache.camel.impl.engine.AbstractCamelContext.doInit(AbstractCamelContext.java:2765)
        at org.apache.camel.quarkus.core.FastCamelContext.doInit(FastCamelContext.java:568)
        at org.apache.camel.support.service.BaseService.init(BaseService.java:83)
        at org.apache.camel.impl.engine.AbstractCamelContext.init(AbstractCamelContext.java:2475)
Caused by: java.lang.ClassNotFoundException: com.mongodb.client.MongoClients#create("mongodb://myuser:mypasswd@172.17.0.9:27017")                                                                                                                             
        at org.apache.camel.quarkus.core.CamelQuarkusClassResolver.resolveMandatoryClass(CamelQuarkusClassResolver.java:59)
        at org.apache.camel.quarkus.core.BaseModel.addTemplateBeans(BaseModel.java:380)
        at org.apache.camel.quarkus.core.BaseModel.addRouteFromTemplate(BaseModel.java:300)
        at org.apache.camel.quarkus.core.BaseModel.addRouteFromTemplate(BaseModel.java:220)
        at org.apache.camel.quarkus.core.FastCamelContext.addRouteFromTemplate(FastCamelContext.java:235)
        at org.apache.camel.component.kamelet.KameletComponent$LifecycleHandler.createRouteForEndpoint(KameletComponent.java:387)
        at org.apache.camel.component.kamelet.KameletComponent$LifecycleHandler.onContextInitialized(KameletComponent.java:402)
@davsclaus davsclaus self-assigned this Jun 25, 2021
@davsclaus
Copy link
Contributor

Yeah unfortunately using factory methods is not supported yet when using #class: syntax

@davsclaus
Copy link
Contributor

Created a ticket
https://issues.apache.org/jira/browse/CAMEL-16759

@davsclaus
Copy link
Contributor

Instead you can use a script to create the bean

 flow:
    beans:
       - name: mongoDBConnection
         type: 'groovy'
         bean-type 'com.mongodb.client.MongoClient`
         script: 'com.mongodb.client.MongoClients.create("mongodb://myuser:mypasswd@172.17.0.9:27017")'

@davsclaus
Copy link
Contributor

@lburgazzoli did we get rid of the duplicate code in camel-quarkus that setup local beans, eg I think in the past we had some code from camel-core copied over to camel-quarkus.

I am asking because I update the camel-core to add support for the #class with factory method in the DefaultModel class
apache/camel@fa51da1

The stacktrace in the top refers to a BaseModel in camel-quarkus, and I cannot find that anymore.

@davsclaus
Copy link
Contributor

Camel 3.12 now supports the factory method (must be public static). You can include the return type class of the created bean (if you want) as that allows Camel to know its type for dependency injection, otherwise it becomes a Object type. You do this with the bean-type as shown:

 flow:
    beans:
       - name: mongoDBConnection
         type: '#class:com.mongodb.client.MongoClients#create("mongodb://myuser:mypasswd@172.17.0.9:27017")'
         bean-type 'com.mongodb.client.MongoClient`

@lburgazzoli
Copy link
Contributor

As far as I remember yes we don't hvae the duplication anymore, but @jamesnetherton knows better

@jamesnetherton
Copy link
Contributor

As far as I remember yes we don't hvae the duplication anymore, but @jamesnetherton knows better

Yeah, there is no duplication anymore. I don't think that change made it into CQ 2.0.0-M2, so it'll be there from 2.0.0 onwards.

@github-actions
Copy link
Contributor

This issue has been automatically marked as stale due to 90 days of inactivity.
It will be closed if no further activity occurs within 15 days.
If you think that’s incorrect or the issue should never stale, please simply write any comment.
Thanks for your contributions!

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

No branches or pull requests

4 participants