我有一个 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 as:
@Override
public void configure() throws Exception{
from("file:\\input").to("file:\\output");
}
请告诉我如何在将路由编写为单独的类而不是在主类中时识别路由。有什么遗漏吗。
最佳答案
我的猜测是您没有正确运行 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!");
}
}
关于java - 未识别 Apache Camel 路线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38949006/