5

I have the following route definition:

@Component
public class CamelRoutes extends RouteBuilder {
    @Override
    public void configure() throws Exception {
           from("seda:second")                   
                .bean("helloWorld?method=smth")    
                .process(exchange -> {
                    System.out.println("Message:" + exchange.getIn().getBody());
                })
                .log("body:${body}");

and following bean:

public static class HelloWorld {
       public void execute(String str){
           System.out.println("HelloWorld#execute: " + str);
       }
        public void smth(String str){
            System.out.println("HelloWorld#smth: " + str);
        }
    }

But application doesn't start.
trace:

org.apache.camel.RuntimeCamelException: org.apache.camel.FailedToCreateRouteException: Failed to create route route4 at: >>> Bean[ref:helloWorld?method=smth] <<< in route: Route(route4)[[From[seda:second]] -> [Bean[ref:helloWorld?me... because of No bean could be found in the registry for: helloWorld?method=smth
    at org.apache.camel.util.ObjectHelper.wrapRuntimeCamelException(ObjectHelper.java:1831) ~[camel-core-2.20.0.jar:2.20.0]
    at org.apache.camel.spring.SpringCamelContext.start(SpringCamelContext.java:136) ~[camel-spring-2.20.0.jar:2.20.0]
    at org.apache.camel.spring.SpringCamelContext.onApplicationEvent(SpringCamelContext.java:174) ~[camel-spring-2.20.0.jar:2.20.0]
    at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:167) ~[spring-context-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139) ~[spring-context-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:383) ~[spring-context-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:337) ~[spring-context-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:882) ~[spring-context-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.finishRefresh(EmbeddedWebApplicationContext.java:144) ~[spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:545) ~[spring-context-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) ~[spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:737) [spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:370) [spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:314) [spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1162) [spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1151) [spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE]
    at SpringBootCamelIntegrationApplication.main(SpringBootCamelIntegrationApplication.java:9) [classes/:na]
Caused by: org.apache.camel.FailedToCreateRouteException: Failed to create route route4 at: >>> Bean[ref:helloWorld?method=smth] <<< in route: Route(route4)[[From[seda:second]] -> [Bean[ref:helloWorld?me... because of No bean could be found in the registry for: helloWorld?method=smth
    at org.apache.camel.model.RouteDefinition.addRoutes(RouteDefinition.java:1298) ~[camel-core-2.20.0.jar:2.20.0]
    at org.apache.camel.model.RouteDefinition.addRoutes(RouteDefinition.java:204) ~[camel-core-2.20.0.jar:2.20.0]
    at org.apache.camel.impl.DefaultCamelContext.startRoute(DefaultCamelContext.java:1135) ~[camel-core-2.20.0.jar:2.20.0]
    at org.apache.camel.impl.DefaultCamelContext.startRouteDefinitions(DefaultCamelContext.java:3714) ~[camel-core-2.20.0.jar:2.20.0]
    at org.apache.camel.impl.DefaultCamelContext.doStartCamel(DefaultCamelContext.java:3428) ~[camel-core-2.20.0.jar:2.20.0]
    at org.apache.camel.impl.DefaultCamelContext.access$000(DefaultCamelContext.java:208) ~[camel-core-2.20.0.jar:2.20.0]
    at org.apache.camel.impl.DefaultCamelContext$2.call(DefaultCamelContext.java:3236) ~[camel-core-2.20.0.jar:2.20.0]
    at org.apache.camel.impl.DefaultCamelContext$2.call(DefaultCamelContext.java:3232) ~[camel-core-2.20.0.jar:2.20.0]
    at org.apache.camel.impl.DefaultCamelContext.doWithDefinedClassLoader(DefaultCamelContext.java:3255) ~[camel-core-2.20.0.jar:2.20.0]
    at org.apache.camel.impl.DefaultCamelContext.doStart(DefaultCamelContext.java:3232) ~[camel-core-2.20.0.jar:2.20.0]
    at org.apache.camel.support.ServiceSupport.start(ServiceSupport.java:61) ~[camel-core-2.20.0.jar:2.20.0]
    at org.apache.camel.impl.DefaultCamelContext.start(DefaultCamelContext.java:3155) ~[camel-core-2.20.0.jar:2.20.0]
    at org.apache.camel.spring.SpringCamelContext.start(SpringCamelContext.java:133) ~[camel-spring-2.20.0.jar:2.20.0]
    ... 15 common frames omitted
Caused by: org.apache.camel.NoSuchBeanException: No bean could be found in the registry for: helloWorld?method=smth
    at org.apache.camel.component.bean.RegistryBean.getBean(RegistryBean.java:94) ~[camel-core-2.20.0.jar:2.20.0]
    at org.apache.camel.component.bean.RegistryBean.createCacheHolder(RegistryBean.java:69) ~[camel-core-2.20.0.jar:2.20.0]
    at org.apache.camel.model.BeanDefinition.createProcessor(BeanDefinition.java:251) ~[camel-core-2.20.0.jar:2.20.0]
    at org.apache.camel.model.ProcessorDefinition.makeProcessorImpl(ProcessorDefinition.java:549) ~[camel-core-2.20.0.jar:2.20.0]
    at org.apache.camel.model.ProcessorDefinition.makeProcessor(ProcessorDefinition.java:510) ~[camel-core-2.20.0.jar:2.20.0]
    at org.apache.camel.model.ProcessorDefinition.addRoutes(ProcessorDefinition.java:226) ~[camel-core-2.20.0.jar:2.20.0]
    at org.apache.camel.model.RouteDefinition.addRoutes(RouteDefinition.java:1295) ~[camel-core-2.20.0.jar:2.20.0]
    ... 27 common frames omitted

What wrong with my code base ?

update:

According Romat Vottnet advice I've wrote following:

@Component
public class CamelRoutes extends RouteBuilder {
    @Override
    public void configure() throws Exception {

        from("seda:second")                    
                .bean("helloWorld?method=smth()");

    }

    public static class HelloWorld {
        public void execute(String str) {
            System.out.println("HelloWorld#execute: " + str);
        }

        public void smth(String str) {
            System.out.println("HelloWorld#smth: " + str);
        }
    }

    @Bean(name = "helloWorld")
    public HelloWorld helloWorld() {
        return new HelloWorld();
    }
}

But I see:

org.apache.camel.RuntimeCamelException: org.apache.camel.FailedToCreateRouteException: Failed to create route route4 at: >>> Bean[ref:helloWorld?method=smth()] <<< in route: Route(route4)[[From[seda:second]] -> [Bean[ref:helloWorld?me... because of No bean could be found in the registry for: helloWorld?method=smth()
    at org.apache.camel.util.ObjectHelper.wrapRuntimeCamelException(ObjectHelper.java:1831) ~[camel-core-2.20.0.jar:2.20.0]
    at org.apache.camel.spring.SpringCamelContext.start(SpringCamelContext.java:136) ~[camel-spring-2.20.0.jar:2.20.0]
    at org.apache.camel.spring.SpringCamelContext.onApplicationEvent(SpringCamelContext.java:174) ~[camel-spring-2.20.0.jar:2.20.0]
    at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:167) ~[spring-context-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139) ~[spring-context-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:383) ~[spring-context-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:337) ~[spring-context-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:882) ~[spring-context-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.finishRefresh(EmbeddedWebApplicationContext.java:144) ~[spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:545) ~[spring-context-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) ~[spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:737) [spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:370) [spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:314) [spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1162) [spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1151) [spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE]
    at SpringBootCamelIntegrationApplication.main(SpringBootCamelIntegrationApplication.java:9) [classes/:na]
Caused by: org.apache.camel.FailedToCreateRouteException: Failed to create route route4 at: >>> Bean[ref:helloWorld?method=smth()] <<< in route: Route(route4)[[From[seda:second]] -> [Bean[ref:helloWorld?me... because of No bean could be found in the registry for: helloWorld?method=smth()
    at org.apache.camel.model.RouteDefinition.addRoutes(RouteDefinition.java:1298) ~[camel-core-2.20.0.jar:2.20.0]
    at org.apache.camel.model.RouteDefinition.addRoutes(RouteDefinition.java:204) ~[camel-core-2.20.0.jar:2.20.0]
    at org.apache.camel.impl.DefaultCamelContext.startRoute(DefaultCamelContext.java:1135) ~[camel-core-2.20.0.jar:2.20.0]
    at org.apache.camel.impl.DefaultCamelContext.startRouteDefinitions(DefaultCamelContext.java:3714) ~[camel-core-2.20.0.jar:2.20.0]
    at org.apache.camel.impl.DefaultCamelContext.doStartCamel(DefaultCamelContext.java:3428) ~[camel-core-2.20.0.jar:2.20.0]
    at org.apache.camel.impl.DefaultCamelContext.access$000(DefaultCamelContext.java:208) ~[camel-core-2.20.0.jar:2.20.0]
    at org.apache.camel.impl.DefaultCamelContext$2.call(DefaultCamelContext.java:3236) ~[camel-core-2.20.0.jar:2.20.0]
    at org.apache.camel.impl.DefaultCamelContext$2.call(DefaultCamelContext.java:3232) ~[camel-core-2.20.0.jar:2.20.0]
    at org.apache.camel.impl.DefaultCamelContext.doWithDefinedClassLoader(DefaultCamelContext.java:3255) ~[camel-core-2.20.0.jar:2.20.0]
    at org.apache.camel.impl.DefaultCamelContext.doStart(DefaultCamelContext.java:3232) ~[camel-core-2.20.0.jar:2.20.0]
    at org.apache.camel.support.ServiceSupport.start(ServiceSupport.java:61) ~[camel-core-2.20.0.jar:2.20.0]
    at org.apache.camel.impl.DefaultCamelContext.start(DefaultCamelContext.java:3155) ~[camel-core-2.20.0.jar:2.20.0]
    at org.apache.camel.spring.SpringCamelContext.start(SpringCamelContext.java:133) ~[camel-spring-2.20.0.jar:2.20.0]
    ... 15 common frames omitted
Caused by: org.apache.camel.NoSuchBeanException: No bean could be found in the registry for: helloWorld?method=smth()
    at org.apache.camel.component.bean.RegistryBean.getBean(RegistryBean.java:94) ~[camel-core-2.20.0.jar:2.20.0]
    at org.apache.camel.component.bean.RegistryBean.createCacheHolder(RegistryBean.java:69) ~[camel-core-2.20.0.jar:2.20.0]
    at org.apache.camel.model.BeanDefinition.createProcessor(BeanDefinition.java:251) ~[camel-core-2.20.0.jar:2.20.0]
    at org.apache.camel.model.ProcessorDefinition.makeProcessorImpl(ProcessorDefinition.java:549) ~[camel-core-2.20.0.jar:2.20.0]
    at org.apache.camel.model.ProcessorDefinition.makeProcessor(ProcessorDefinition.java:510) ~[camel-core-2.20.0.jar:2.20.0]
    at org.apache.camel.model.ProcessorDefinition.addRoutes(ProcessorDefinition.java:226) ~[camel-core-2.20.0.jar:2.20.0]
    at org.apache.camel.model.RouteDefinition.addRoutes(RouteDefinition.java:1295) ~[camel-core-2.20.0.jar:2.20.0]
    ... 27 common frames omitted

when application starts.

I checked - method marked as @Bean is invoked.

gstackoverflow
  • 36,709
  • 117
  • 359
  • 710
  • 1
    Actually my comment refered to using `.to("bean:nameOfBeanInRegistry(${body})` instead of using `.bean(Class, String)` if you want to stick with `?method=...` syntax. I'm not aware of `.bean(...)` supporting that syntax as it supports an additional parameters that is explicitly invoking a method that was not annotated with `@Handler`. This annotation is used in cases when the bean has multiple public methods available to invoke to tell Camel which one should be used by not specifying the name directly. Furthermore, you did not specify any argument for the method invoked, though smth requires 1 – Roman Vottner Oct 24 '17 at 10:53
  • @Roman Vottner, can you, please just write working code ? **to("bean:nameOfBeanInRegistry(${body})** - where should I write a **?method=...** and what should I write to provide argument for smth method? – gstackoverflow Oct 24 '17 at 12:12

4 Answers4

5

this works:

 from("seda:second")               
     .bean(HelloWorld.class, "smth")
gstackoverflow
  • 36,709
  • 117
  • 359
  • 710
0

As you're already using Spring, why not annotate your bean with @Component and use an autowired instance inside your route?

cristianoms
  • 3,456
  • 3
  • 26
  • 28
  • Actually I want to know how to use notation like *?option=value* – gstackoverflow Oct 24 '17 at 09:55
  • @gstackoverflow you mean something like `.to(bean:helloWorld?method=smth())`? Here, this bean needs to be available within Camel's registry. So either register the bean with Camel's registry or name the Spring bean via `@Bean(name="helloWorld")` which Camel will add to its registry automatically. For more information have a look at http://camel.apache.org/bean-binding.html – Roman Vottner Oct 24 '17 at 10:06
0

The way you give the class name and method to "bean" is wrong! If you wanna use bean then these are acceptable ways for how to work with it:

from("direct:a").bean(ClassName.class , "methodName").to("direct:b");
from("direct:a").bean("registeredClassName","methodName").to("direct:b"); 
// this registered named is the name that you give to spring annotations like
//@Component , @Configuration , @Service , @Controller , @Bean etc...
// If you are using Camel stand alone then registeredClassName is the name that 
// you gave to  camel's registry. 
//For example:

SimpleRegistry simpleRegistry = new SimpleRegistry(); // this class is just a mapper
simpleRegistry.put("registeredClassName",new ClassName());

CamelContext camelContext = new DefaultCamelContext(simpleRegistry);
// In Spring Boot 
@Autowired 
private ClassName instanceOfClassName;
from("direct:a").bean(instanceOfClassName , "methodName").to("direct:b");

And this is the right way for using class and method together like the way you went through:

from("direct:a").to("bean:registeredClassName?method=methodName").to("direct:b");
// remember no parenthesis () for methodName 

from("direct:a").to("bean:FullyQualifiedClassName?method=Name").to("direct:b");

Also this is another way of invoking bean and its' method although it is a little advance and you need to know what is Simple-Language.

from("direct:start").bean("registeredClassName","methodName(${body},${header.customerId},${header.customerType}");

That's all :))

Od Chan
  • 68
  • 8
0

I was trying to execute a method under another class using camel beans. so i had to use 'to' instead of bean. this is what worked for me.

        //add class instance and add it to the registry as a bean
    MyService myService = new MyService();

    SimpleRegistry registry = new SimpleRegistry();

    registry.bind("myServiceBean", myService);



    CamelContext context = new DefaultCamelContext(registry);

    context.addRoutes(new RouteBuilder() {
        @Override
        public void configure() throws Exception {
            from("direct:start")
                    .to("bean:myServiceBean?method=messageReciever");
        }
    });
context.start();
Indika_Nuwan95
  • 529
  • 5
  • 8