Apache Camel路由未被识别。

3
我有一个Spring Boot应用程序,我正在添加一个camel路由。定义路由的类扩展了FatJarRouter,并带有@Component注释。当应用程序作为Spring Boot应用程序运行时,路由未被识别。但是,如果我在带有@SpringBootApplication注释的主类中编写路由,则路由将被识别。目前,日志显示如下:

o.a.camel.spring.SpringCamelContext:共计0条路由,其中0条已启动。 o.a.camel.spring.SpringCamelContext:Apache Camel 2.17.2(CamelContext:camel-4)在0.026秒内启动。

具有路由的方法还使用override进行了注释:
@Override
public void configure()  throws Exception{
from("file:\\input").to("file:\\output");
}

请告诉我如何在单独的类中编写路由并标识它,而不是在主类中。是否有什么遗漏?

你能展示一下camelContext的初始化吗?我认为这篇文章会对你有所帮助 - Aliaksei Bulhak
如果您在POM中添加了新的依赖项,那么这个依赖项可能会产生影响。我曾经遇到过这种情况。 - emarshah
4个回答

2

我猜测您未能正确运行Spring Boot或者您的 FatJarRouter 不在Spring Boot的组件扫描路径上。假设您有一个Spring Boot应用程序类:

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

您的FatJarRouter必须与同一包(在本例中为com.example)或子包中的文件位于同一目录下,例如: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!");
  }
}

路由在主Spring Boot应用程序类的子包中。我如何检查Spring Boot的类路径上是否存在子包或任何其他包。所有包都是src文件夹的一部分。 - Megan
1
那么它肯定必须被捡起来。除非您有自己的@ComponentScan覆盖默认值。您能分享一下您的@SpringBootApplication类和路由器吗? - Miloš Milivojević

0

我曾经遇到过同样的问题,路由没有被识别,显示为Routes startup (total:0 started:0)。添加@ComponentScan()解决了这个问题,现在我的路由显示为Routes startup (total:1 started:1)。

非常感谢你的帮助。


0

0
今天面临了同样的问题。在配置类中放置 @ComponentScan 和 Servlet 注册对我有用。
@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;
        }

1
除了@Elletlar所说的之外:图像无法被互联网搜索引擎索引。索引使得更容易找到适当的问题和答案来满足互联网搜索的需求。 - TT.
我的错,我是新手回答 stack overflow 的问题。 - Mukul Panchakarla

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接