'Apache camel route is not identified

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.



Solution 1:[1]

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!");
  }
}

Solution 2:[2]

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?.

Solution 3:[3]

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;
        }

Solution 4:[4]

I had the same issue the routes were not being picked up, it was showing Routes startup (total:0 started:0) Adding @ComponentScan() solved the issue, I am getting Routes startup (total:1 started:1)

Thanks for that, it helped

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Community
Solution 3
Solution 4 procrastinator