Is it possible to kick off a camel route using a java interface or bean?

Viewed 29062

I'd like to setup a spring bean (either via interface or bean class). that I can call to "start" a Route.

In this simple example when I call sayHello("world") from code I'd like it to route the return value of the sayHello method the endpoint that will write it out to a file.

Does anyone know if this is possible, or how to go about this? I know I can expose that same interface via CXF and make this work, but I really just want to call a method, not go through the trouble of sending a jms message or calling a webservice.

public interface Hello{
   public String sayHello(String value);
}

from("bean:helloBean").to("file:/data/outbox?fileName=hello.txt");
5 Answers

Since my routes were spring components using CamelConfiguration. I did following to use my interface in camel routes.

@Component
public class SomeRoute extends RouteBuilder {

    @Autowired
    private ApplicationContext applicationContext;

    @Override
    public void configure() throws Exception {
        from("direct:someroute")
        .bean(applicationContext.getBean(SomeInterface.class).getClass(), "someAbstractMethod")
        .to("direct:otherroute");
    }
}

This was very straight case, if you have multiple beans using same interface or abstract class you probably have to do some logic before using .getClass() on bean.

Related