java - 未识别 Apache Camel 路线

标签 java spring-boot apache-camel

我有一个 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/

相关文章:

java - 使用 apache camel 的 camel-kafka 组件手动提交消费者偏移量

java - BufferedReader 的 readLine 方法中缺少行

java joption并将字符串转换为整数

java - 应用程序不启动 Spring Boot 1.2.1 + Spring Security + Servlet 2.5

java - Apache Camel 每天安排多次

java - Camel-Kafka 安全协议(protocol) SASL_PLAINTEXT 不支持

java - 无法在 Eclipse 3.6.1 中创建新的 java 项目

java - 解决描述资源路径位置类型错误

springboot + webpack 开发服务器,重建后不会更改 localhost 捆绑文件

java - 从 Spring Boot 持续使用 REST Web 服务