I use Camel standalone.
I first thought that I maybe could set the bean with a string parameter
(similarly to setting a bean with an xml file):
Registry registry = context.getRegistry();
registry.bind("CurrentAggregateStrategy", "org.mypackage.AggregateStrategy");
But this method isn't an available. Then I looked at annotation based
solutions. As I understand it:
For runtimes like main and Quarkus one can set this on the bean:
@BindToRegistry("myBeanId")
And for Spring runtimes:
@Component("myBeanId")
But these both are probably not usable from standalone Camel?
I even tried putting camel-spring on the classpath and add the package to
the componentscan, but this didn't change the result.
I also tried to set it from an XML like this:
String beans = "<beans
xmlns=\"http://www.springframework.org/schema/beans\"\n" +
" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" +
" xsi:schemaLocation=\"\n" +
" http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd\n" +
" http://camel.apache.org/schema/spring
http://camel.apache.org/schema/spring/camel-spring.xsd\">\n" +
"\n" +
" <bean id=\"myBeanId\" class=\"org.package.AggregateStrategy\"/>\n" +
"\n" +
"</beans>";
RoutesLoader loader = extendedCamelContext.getRoutesLoader();
Resource resource = IntegrationUtil.setResource(beans);
loader.loadRoutes(resource);
This seems to load, but the bean is still not found.
I was able to set from a String through Jooq/Joor:
Reflect.compile(
"com.example.RegisterBean",
"package com.example;\n" +
"class RegisterBean implements
java.util.function.Supplier<String> {\n" +
" public String get() {\n" +
" return \"Hello World!\";\n" +
" }\n\n" +
" public String
register(org.apache.camel.support.SimpleRegistry registry) throws
Exception {\n" +
" registry.bind(\"myBeanId\", new
org.package.AggregateStrategy());\n" +
"return \"done\";\n" +
" }\n" +
"}\n").create().call("register",registry).get();
I haven't worked with Joor before, but surprisingly it worked. I also got
it to work with AspectJ that bind the bean to the registry after the
CamelContext/Registry is set. However, both solutions seem a bit of
workaround compared to the annotation based solutions.
I will check the code for the camel-debug and see how that works. Thanks
for the suggestions so far.
Raymond
On Sat, Oct 1, 2022 at 2:36 PM Claus Ibsen <[email protected]> wrote:
> Hi
>
> If you are using spring boot, quarkus, cdi etc then they have auto
> configuration and cdi startup annotations you can use to trigger custom
> code.
>
> If you talk about plain standalone camel then its a bit more work to do,
> but we do this with some classpath scanning such as what we can do when you
> have camel-debug JAR on classpath or not. You could have your own service
> factory class you active on startup that then scans for custom JARs with a
> specific @JdkService factory which then can trigger calling this on startup
> where you can do custom code.
>
>
>
>
>
> On Sat, Oct 1, 2022 at 4:36 AM ski n <[email protected]> wrote:
>
> > I can register a bean like this:
> >
> > Registry registry = context.getRegistry();
> > registry.bind("CurrentAggregateStrategy", new AggregateStrategy());
> >
> > But I want this dependency to be optional, so I am not sure that the
> class
> > (in this example AggregateStrategy) is on the classpath.
> >
> > Is it possible that the bean can autoregister itself?
> >
> > Raymond
> >
>
>
> --
> Claus Ibsen
> -----------------
> http://davsclaus.com @davsclaus
> Camel in Action 2: https://www.manning.com/ibsen2
>