4
votes

I have a spring boot application to which I am adding a camel route. The class where the route is defined extends FatJarRouter and is annotated with @Component. When the application is run as spring boot application, the route doesn't get identified. But if i write the route in the main class with @SpringBootApplication annotation the route is identified. This is how it shows in logs as of now:

o.a.camel.spring.SpringCamelContext : Total 0 routes, of which 0 are started. o.a.camel.spring.SpringCamelContext : Apache Camel 2.17.2 (CamelContext: camel-4) started in 0.026 seconds

The method with route is also annotated with override as:

@Override
public void configure()  throws Exception{
from("file:\\input").to("file:\\output");
}

Please tell me how can I identify the route while writing it as a separate class but not in the main class. Is there anything missing.

3
can you show camelContext initialization? I think this article will help youAleksei Bulgak
if you have added a new dependency in POM, it might be possible that dependency has an impact. happened with me.emarshah

3 Answers

2
votes

My guess is that you're not running Spring Boot correctly or your FatJarRouter is not on Spring Boot's component scan path. Let's say you have a Spring Boot application class:

package com.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        ApplicationContext ctx = SpringApplication.run(DemoApplication.class, args);
        System.out.println("Printing out ");

        Map<String, RouteBuilder> routeBuilders = 
            ctx.getBeansOfType(RouteBuilder.class);
        System.out.println(routeBuilders);

        CamelSpringBootApplicationController applicationController =
            ctx.getBean(CamelSpringBootApplicationController.class);
        applicationController.run();
    }
}

Your FatJarRouter must be in the same package (in this case com.example) or in a sub-package, e.g. com.example.camel:

package com.example.camel;

import org.apache.camel.Exchange;
import org.apache.camel.spring.boot.FatJarRouter;
import org.springframework.stereotype.Component;

@Component
public class DemoRouteBuilder extends FatJarRouter {

  @Override
  public void configure() throws Exception {
      from("timer:sender?delay=3000&period=5000")
          .log("Ping!");
  }
}
0
votes

From the camel FAQ documentation:

http://camel.apache.org/how-do-i-name-my-routes.html

from("direct:start").routeId("myRoute").to("mock:bar");

As for the 0 routes, please refer to the answer provided by Miloš Milivojević.

0
votes

Faced the same issue today. Putting @ComponentScan and servlet registration in the configuration class worked for me.

@SpringBootApplication
@Configuration
@ComponentScan("com.camel.practice")
    public class ApplicationMain {
    
        public static void main(String[] args) {
            SpringApplication.run(ApplicationMain.class, args);
        }
        @Bean
        public ServletRegistrationBean<CamelHttpTransportServlet> camelServletRegistrationBean() {
            ServletRegistrationBean<CamelHttpTransportServlet> registration = 
                    new ServletRegistrationBean<CamelHttpTransportServlet>(new CamelHttpTransportServlet(), "/*");
            registration.setName("CamelServlet");
            return registration;
        }