0
votes

I have just started to use Camel in one of my projects. I am trying to configure Camel with Spring, but having issues doing that.
I don't want to use xml configuration but rather go with Spring based Annotations for configuring Routes and Processors.

My App is a stand alone Spring Application, which will be run as Jar. To keep the app running, I've a empty scheduled method which runs every x min.

Below are the dependencies in my build.gralde

// Spring //
compile('org.springframework:spring-core:5.0.0.RC2')
compile('org.springframework:spring-context:5.0.0.RC2')
compile('org.springframework:spring-beans:5.0.0.RC2')
compile('org.springframework:spring-context-support:5.0.0.RC2')
// Apache //
// Camel //
compile('org.apache.camel:camel-core:2.19.1')
compile('org.apache.camel:camel-spring:2.19.1')

snapshot of beans.xml

<context:annotation-config/>
<tx:annotation-driven/>
<context:component-scan base-package="my.package" />

<camelContext id="aggregatorCamelContext" autoStartup="true" xmlns="http://camel.apache.org/schema/spring">
        <package>
            my.package.camel
        </package>        
</camelContext>

Sample RouteBuilder

@Component
public class SampleRoute extends RouteBuilder {

    @Autowired
    MyClass myObject;

    @Override
    public void configure() throws Exception {

        from("file:filelist")
            .process(myObject)
            .to("file:processedList");
    }

}

To keep the app alive ( I know bit hacky, but suffices for now )

@Component
@EnableScheduling
public class KeepitAlive {

    @Scheduled(fixedRate = 1000l)
    public void run(){
        System.out.println("KeepitAlive.run "+ Thread.currentThread().getName() );
    }
}

Main Class. I have tried both the methods, Initializing Spring context as well as Camel Main, but to no luck

public class MyApplication {

    public static void main(String[] args) throws Exception {
        /*AbstractXmlApplicationContext context =
                new ClassPathXmlApplicationContext("path/to/beans.xml");*/

        Main main = new Main();
        main.setApplicationContextUri("path/to/beans.xml");
        main.start();
    }

}

If I put my Route within camelContext declaration itself, it works absolutely fine,

    <route>
        <from uri="file:filelist"/>
        <to uri="file:processedlist"/>
    </route>

I've also looked into Camel Spring Integration documentation, but it also contains xml based configuration.
Could anybody please guide me in right direction.

2
Use the run method on the Main class if its from Apache Camel. See this FAQ: camel.apache.org/…Claus Ibsen

2 Answers

0
votes

Figured it out ultimately. Need to extend SpringRouteBuilder instead of RouteBuiler above in SampleRoute class.
Anybody struggling with issues, I suggest once go through Camel in Action book.
Somehow I missed it in the beginning which costed me lot of time figuring out trivial things that this book covers.

0
votes

You are using Camel's own package scanning via

<package> my.package.camel </package>

You should use <contextScan> if you want Camel to find the Spring @Component Camel routes.

See the Camel spring documentation for more: http://camel.apache.org/spring.html