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

Can camel-k accept camel context definition? #814

Closed
rejimathews opened this issue Jul 12, 2019 · 14 comments
Closed

Can camel-k accept camel context definition? #814

rejimathews opened this issue Jul 12, 2019 · 14 comments

Comments

@rejimathews
Copy link

Does camel K understand if I pass the camel context definitions? Like

<camelContext xmlns="http://camel.apache.org/schema/spring">
  <route> 
    <from uri="activemq:Accounts.PurchaseOrders"/>
    <to uri="seda:A"/>
  </route>
  <route> 
    <from uri="seda:A"/>
    <to uri="activemq:UK.Accounts.Invoices"/>
  </route>
</camelContext>
@lburgazzoli
Copy link
Contributor

lburgazzoli commented Jul 12, 2019 via email

@WillemJiang
Copy link
Member

From my understanding camel-k-runtime can load the camel route and camel dsl fragment out of box. But what if there are some customer bean definition need to be load or bean instance need to be created. How can we load these beans inside of camel-k-runtime?

@lburgazzoli
Copy link
Contributor

you need to use java/groovy and put your bean in the registry

@rejimathews
Copy link
Author

Is there a thought of making camel-k capable of accepting one global resource file which contains all configurations including routes, beans, conduit elements etc

@lburgazzoli
Copy link
Contributor

you can use a java/groovy, if you want to use XML you need to add also a java file that adds the beans to the registry.

@rejimathews
Copy link
Author

you need to use java/groovy and put your bean in the registry

Do you have any examples for this? If so can you provide the link to the same?

@lburgazzoli
Copy link
Contributor

in the configure method just do something like getContext().getRegistry().bind(key, val)

@rejimathews
Copy link
Author

rejimathews commented Jul 19, 2019

in the configure method just do something like getContext().getRegistry().bind(key, val)

Sounds great!
I already have my project written as XML in route fragments. If I add a groovy or java here (to bring in my datasource bean binding), if I pass both xml and the java/groovy to "kamel run command", will it all get bound under the same camelContext?

Or rather, can i mix up routes , few from xml dsl and rest in groovy/java and have it all under single camelContext with common registry?

@lburgazzoli
Copy link
Contributor

lburgazzoli commented Jul 19, 2019 via email

@rejimathews
Copy link
Author

in the configure method just do something like getContext().getRegistry().bind(key, val)

I just realized I was blocked while implementing this way. The same issue mentioned at this thread. https://developer.jboss.org/thread/235103

Looks like it returns a read only instance of the registry. I don't see a bind() method available to use in the instance. Is there another way to get modifiable instance of Registry?

@lburgazzoli
Copy link
Contributor

Yeah works only in camel 3 as it has a writable registry

@rejimathews
Copy link
Author

rejimathews commented Jul 22, 2019

Yeah works only in camel 3 as it has a writable registry

Thanks !!
Something like this worked for me. Thanks @WillemJiang and @lburgazzoli for the clues!

import org.apache.camel.CamelContext;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.CompositeRegistry;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.impl.SimpleRegistry;
import org.apache.commons.dbcp.BasicDataSource;

public class EmbeddedRoute extends RouteBuilder {
    @Override
    public void configure() throws Exception {
        SimpleRegistry simpleRegistry = new SimpleRegistry();

        BasicDataSource mysqlDataSource = new BasicDataSource();
        mysqlDataSource.setUrl("jdbc:mysql://10.213.96.182/schemaname");
        mysqlDataSource.setUsername("********");
        mysqlDataSource.setPassword("***********");
        mysqlDataSource.setDriverClassName("com.mysql.jdbc.Driver");

        CamelContext camelContext = getContext();
        CompositeRegistry compositeRegistry = getCompositeRegistry();
        compositeRegistry.addRegistry(getContext().getRegistry());
        compositeRegistry.addRegistry(simpleRegistry);

        ((DefaultCamelContext)camelContext).setRegistry(compositeRegistry);
        simpleRegistry.put("mysqlDataSource", mysqlDataSource);
    }

    private CompositeRegistry getCompositeRegistry() {
        CompositeRegistry registry = new CompositeRegistry();
        return registry;
    }
}

@ogomezdi
Copy link

ogomezdi commented Jul 30, 2020

Notice that also in the examples came-k folder there is an example of using @BindToRegistry in conjunction of using multiple files for deploying one integration.

I want also to mention I wan't able to use "packages" for language "java" that will be helpful to have better organization of code supporting logical needed for each integration. I mean, what I'm trying to do is separe the logic from the route itself, so I've created two different java files, one containing only the route, and other implementing the utils (for example), the first one I want to place it in a "package org.something.integrations.route" and the other in a "package org.something.integrations.utils" if I did it in this way and execute this command:
kamel run ExampleRoute.java ExampleUtils.java --dev
I got this error:
[1] Monitoring pod example-route-57664f98f9-6pkkw [1] exec java -cp ./resources:/etc/camel/resources:/etc/camel/sources/i-source-000:/etc/camel/sources/i-source-001:dependencies/org.apache.camel.camel-api-3.4.0.jar:dependencies/org.apache.camel.camel-base-3.4.0.jar:dependencies/org.apache.camel.camel-bean-3.4.0.jar:dependencies/org.apache.camel.camel-core-engine-3.4.0.jar:dependencies/org.apache.camel.camel-core-languages-3.4.0.jar:dependencies/org.apache.camel.camel-endpointdsl-3.4.0.jar:dependencies/org.apache.camel.camel-log-3.4.0.jar:dependencies/org.apache.camel.camel-main-3.4.0.jar:dependencies/org.apache.camel.camel-management-api-3.4.0.jar:dependencies/org.apache.camel.camel-support-3.4.0.jar:dependencies/org.apache.camel.camel-timer-3.4.0.jar:dependencies/org.apache.camel.camel-util-3.4.0.jar:dependencies/org.apache.camel.k.camel-k-loader-java-1.4.1.jar:dependencies/org.apache.camel.k.camel-k-runtime-core-1.4.1.jar:dependencies/org.apache.camel.k.camel-k-runtime-main-1.4.1.jar:dependencies/org.apache.logging.log4j.log4j-api-2.13.3.jar:dependencies/org.apache.logging.log4j.log4j-core-2.13.3.jar:dependencies/org.apache.logging.log4j.log4j-slf4j-impl-2.13.3.jar:dependencies/org.jooq.joor-0.9.13.jar:dependencies/org.slf4j.slf4j-api-1.7.30.jar org.apache.camel.k.main.Application Condition "Ready" is "True" for Integration example-route [1] 2020-07-30 11:36:22.187 INFO [main] ApplicationRuntime - Add listener: org.apache.camel.k.listener.ContextConfigurer@503ecb24 [1] 2020-07-30 11:36:22.192 INFO [main] ApplicationRuntime - Add listener: org.apache.camel.k.listener.RoutesConfigurer@5d12a356 [1] 2020-07-30 11:36:22.195 INFO [main] ApplicationRuntime - Add listener: org.apache.camel.k.listener.PropertiesConfigurer@4fad9bb2 [1] 2020-07-30 11:36:22.216 INFO [main] ApplicationRuntime - Listener org.apache.camel.k.listener.PropertiesConfigurer@4fad9bb2 executed in phase ConfigureProperties [1] 2020-07-30 11:36:22.233 INFO [main] RuntimeSupport - Looking up loader for language: java [1] 2020-07-30 11:36:22.237 INFO [main] RuntimeSupport - Found loader org.apache.camel.k.loader.java.JavaSourceLoader@35f26e72 for language java from service definition [1] Exception in thread "main" org.apache.camel.RuntimeCamelException: org.joor.ReflectException: Compilation error: /org/something/integrations/route/ExampleRoute.java:6: error: package org.something.integrations.utils does not exist [1] import org.something.integrations.utils.ExampleUtils; [1] ^ [1] 1 error [1] [1] at org.apache.camel.RuntimeCamelException.wrapRuntimeCamelException(RuntimeCamelException.java:52) [1] at org.apache.camel.k.listener.RoutesConfigurer.load(RoutesConfigurer.java:102) [1] at org.apache.camel.k.listener.RoutesConfigurer.load(RoutesConfigurer.java:63) [1] at org.apache.camel.k.listener.RoutesConfigurer.accept(RoutesConfigurer.java:53) [1] at org.apache.camel.k.listener.AbstractPhaseListener.accept(AbstractPhaseListener.java:32) [1] at org.apache.camel.k.main.ApplicationRuntime$MainListenerAdapter.lambda$invokeListeners$0(ApplicationRuntime.java:178) [1] at java.base/java.util.stream.ForEachOps$ForEachOp$OfRef.accept(ForEachOps.java:183) [1] at java.base/java.util.stream.SortedOps$SizedRefSortingSink.end(SortedOps.java:357) [1] at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:485) [1] at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:474) [1] at java.base/java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:150) [1] at java.base/java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:173) [1] at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234) [1] at java.base/java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:497) [1] at org.apache.camel.k.main.ApplicationRuntime$MainListenerAdapter.invokeListeners(ApplicationRuntime.java:177) [1] at org.apache.camel.k.main.ApplicationRuntime$MainListenerAdapter.beforeConfigure(ApplicationRuntime.java:142) [1] at org.apache.camel.main.BaseMainSupport.postProcessCamelContext(BaseMainSupport.java:571) [1] at org.apache.camel.main.BaseMainSupport.initCamelContext(BaseMainSupport.java:379) [1] at org.apache.camel.k.main.ApplicationRuntime$MainAdapter.doInit(ApplicationRuntime.java:199) [1] at org.apache.camel.support.service.BaseService.init(BaseService.java:83) [1] at org.apache.camel.main.MainSupport.run(MainSupport.java:56) [1] at org.apache.camel.k.main.ApplicationRuntime.run(ApplicationRuntime.java:72) [1] at org.apache.camel.k.main.Application.main(Application.java:42) [1] Caused by: org.joor.ReflectException: Compilation error: /org/something/integrations/route/ExampleRoute.java:6: error: package org.something.integrations.utils does not exist [1] import org.something.integrations.utils.ExampleUtils; [1] ^ [1] 1 error [1] [1] at org.joor.Compile.compile(Compile.java:102) [1] at org.joor.Reflect.compile(Reflect.java:104) [1] at org.joor.Reflect.compile(Reflect.java:79) [1] at org.apache.camel.k.loader.java.JavaSourceLoader.load(JavaSourceLoader.java:48) [1] at org.apache.camel.k.listener.RoutesConfigurer.load(RoutesConfigurer.java:94) [1] ... 21 more Condition "Ready" is "False" for Integration example-route

The content of java files are:
ExampleRoute.java
`
// camel-k: language=java name=example-route

package org.something.integrations.route;

import org.apache.camel.builder.RouteBuilder;
import org.something.integrations.utils.ExampleUtils;

public class ExampleRoute extends RouteBuilder {

@OverRide
public void configure() throws Exception {

  // Write your routes here, for example:
  from("timer:java?period=5000")
    .routeId("java")
    .setBody(ExampleUtils.doSomething())
    .to("log:info?showAll=true&multiline=true");

}
}
`

ExampleUtils.java
`
// camel-k: language=java

package org.something.integrations.utils;

import org.apache.camel.builder.RouteBuilder;

public class ExampleUtils extends RouteBuilder {

@Override
public void configure() throws Exception {
}

public static String doSomething() {
    return "Hello World!!!";
    };
}

}
`

Done using:

  • Camel K Client 1.1.0
  • minikube version: v1.12.1 over Windows 10 Pro
  • VirtualBox 6.1.12

@lburgazzoli
Copy link
Contributor

I want also to mention I wan't able to use "packages" for language "java" that will be helpful to have better organization of code supporting logical needed for each integration.

can you rephrase it ? I don't get what you are trying to describe

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

4 participants