当前位置: 首页 > 知识库问答 >
问题:

Apache Camel路由不调用处理器

黄昊
2023-03-14

我试图用ApacheCamel和Spring Boot编写我的第一个项目。它应该调用Restendpoint并处理数据,但从未调用我的处理器。我做错了什么?

日志显示路由已启动并且它从“Direct://httpRoute”中消耗。但在最后没有日志表明调用了MyProcess。

pplication.java

package de.importer.TssImport;

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

@SpringBootApplication
public class TssImportApplication {

    public static void main(String[] args) {
        SpringApplication.run(TssImportApplication.class, args);
    }

}

我的路线。JAVA

package de.importer.TssImport.routes;

import lombok.extern.slf4j.Slf4j;
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.builder.RouteBuilder;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class MyRoute extends RouteBuilder {
    @Override
    public void configure() throws Exception {
        log.error("TEST");
        from("direct:httpRoute").to("https://jsonplaceholder.typicode.com/todos/1").process(new MyProcessor());
    }

    class MyProcessor implements Processor {

        public void process(Exchange exchange) throws Exception {
            log.error("TEST 2");
            System.out.println(exchange.getIn().getBody(String.class));
        }
    }
}

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>de.importer</groupId>
    <artifactId>TssImport</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>TssImport</name>
    <description>Project to import data</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.apache.camel.springboot</groupId>
                <artifactId>camel-spring-boot-dependencies</artifactId>
                <version>3.3.0</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>org.apache.camel.springboot</groupId>
            <artifactId>camel-spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.apache.camel.springboot</groupId>
            <artifactId>camel-log-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.apache.camel.springboot</groupId>
            <artifactId>camel-http-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

日志

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.3.1.RELEASE)

2020-06-15 17:47:54.590  INFO 30161 --- [           main] de.importer.TssImport.TssImportApplication   : Starting TssImportApplication on importer-VirtualBox with PID 30161 (/home/importer/Documents/TssImport/tssimport/target/classes started by importer in /home/importer/Documents/TssImport/tssimport)
2020-06-15 17:47:54.593  INFO 30161 --- [           main] de.importer.TssImport.TssImportApplication   : No active profile set, falling back to default profiles: default
2020-06-15 17:47:55.732  INFO 30161 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.apache.camel.spring.boot.CamelAutoConfiguration' of type [org.apache.camel.spring.boot.CamelAutoConfiguration] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2020-06-15 17:47:55.912  INFO 30161 --- [           main] o.apache.camel.support.LRUCacheFactory   : Detected and using LRUCacheFactory: camel-caffeine-lrucache
2020-06-15 17:47:56.437 ERROR 30161 --- [           main] de.importer.TssImport.routes.MyRoute         : TEST
2020-06-15 17:47:56.494  INFO 30161 --- [           main] o.a.c.s.boot.SpringBootRoutesCollector   : Loading additional Camel XML routes from: classpath:camel/*.xml
2020-06-15 17:47:56.495  INFO 30161 --- [           main] o.a.c.s.boot.SpringBootRoutesCollector   : Loading additional Camel XML rests from: classpath:camel-rest/*.xml
2020-06-15 17:47:56.886  INFO 30161 --- [           main] o.a.camel.component.http.HttpComponent   : Created ClientConnectionManager org.apache.http.impl.conn.PoolingHttpClientConnectionManager@77bb0ab5
2020-06-15 17:47:56.918  INFO 30161 --- [           main] o.a.c.impl.engine.AbstractCamelContext   : Apache Camel 3.3.0 (CamelContext: camel-1) is starting
2020-06-15 17:47:56.919  INFO 30161 --- [           main] o.a.c.impl.engine.AbstractCamelContext   : StreamCaching is not in use. If using streams then its recommended to enable stream caching. See more details at http://camel.apache.org/stream-caching.html
2020-06-15 17:47:57.037  INFO 30161 --- [           main] o.a.c.impl.engine.AbstractCamelContext   : Route: route1 started and consuming from: direct://httpRoute
2020-06-15 17:47:57.046  INFO 30161 --- [           main] o.a.c.impl.engine.AbstractCamelContext   : Total 1 routes, of which 1 are started
2020-06-15 17:47:57.047  INFO 30161 --- [           main] o.a.c.impl.engine.AbstractCamelContext   : Apache Camel 3.3.0 (CamelContext: camel-1) started in 0.128 seconds
2020-06-15 17:47:57.052  INFO 30161 --- [           main] de.importer.TssImport.TssImportApplication   : Started TssImportApplication in 2.989 seconds (JVM running for 3.806)
2020-06-15 17:47:57.060  WARN 30161 --- [extShutdownHook] o.a.c.s.boot.SpringBootCamelContext      : CamelContext has only been running for less than a second. If you intend to run Camel for a longer time then you can set the property camel.springboot.main-run-controller=true in application.properties or add spring-boot-starter-web JAR to the classpath.
2020-06-15 17:47:57.061  INFO 30161 --- [extShutdownHook] o.a.c.impl.engine.AbstractCamelContext   : Apache Camel 3.3.0 (CamelContext: camel-1) is shutting down
2020-06-15 17:47:57.066  INFO 30161 --- [extShutdownHook] o.a.c.i.engine.DefaultShutdownStrategy   : Starting to graceful shutdown 1 routes (timeout 45 seconds)
2020-06-15 17:47:57.073  INFO 30161 --- [ - ShutdownTask] o.a.c.i.engine.DefaultShutdownStrategy   : Route: route1 shutdown complete, was consuming from: direct://httpRoute
2020-06-15 17:47:57.074  INFO 30161 --- [extShutdownHook] o.a.c.i.engine.DefaultShutdownStrategy   : Graceful shutdown of 1 routes completed in 0 seconds
2020-06-15 17:47:57.081  INFO 30161 --- [extShutdownHook] o.a.c.impl.engine.AbstractCamelContext   : Apache Camel 3.3.0 (CamelContext: camel-1) uptime 0.163 seconds
2020-06-15 17:47:57.081  INFO 30161 --- [extShutdownHook] o.a.c.impl.engine.AbstractCamelContext   : Apache Camel 3.3.0 (CamelContext: camel-1) is shutdown in 0.020 seconds

Process finished with exit code 0

共有1个答案

骆照
2023-03-14

首先,应用程序将很快关闭。要保持主线程被阻塞,以便Camel保持运行,可以包括spring boot starter web依赖项,或者添加Camel。春靴。对应用程序运行main run controller=true。属性应用程序。yml文件

https://cwiki.apache.org/confluence/display/CAMEL/Spring Boot

但是仍然不会有处理器日志,因为您没有向direct:httpRoute发送任何消息,因此不会调用处理器。

由于您无法从vm外部向direct:httpRoute发送任何消息,因此您将需要下面的内容来发送消息

在这里,我添加了一个rest控制器,当您在浏览器中转到send message to direct channelendpoint时,它将向您的驼峰路线发送消息。在添加rest控制器时,这将需要spring boot starter web

@RestController
public class DirectChannelController {
  
  @Autowired
  CamelContext camelContext;
  
  @Autowired
  ProducerTemplate producerTemplate;

  @RequestMapping(value = "/send-message-to-direct-channel")
  public void startCamel() {
      producerTemplate.sendBody("direct:httpRoute", "An example message everytime");
  }
}

或者你可以试试

@Component
public class MyRunner implements CommandLineRunner {

    @Autowired
    CamelContext camelContext;
    
    @Autowired
    ProducerTemplate producerTemp;


   @Override
   public void run(String... args) throws 
                         Exception {
   producerTemp.sendBody("direct:direct:httpRoute",
               "An example message everytime");
   }
}

如果您想通过计时器触发它,请尝试将以下路由添加到您的配置方法中

from("timer://simpleTimer?period=2000")
   .setBody()
   .simple("This is a test message")
      .to("direct:httpRoute");

添加camel。春靴。main run controller=true以保持应用程序正常运行

 类似资料:
  • 我的申请中有如下路线- 从("sftp: config").到("file: config") 我必须在生成endpoint时处理那些超出apache camel默认异常处理边界的异常。例如。编写文件时可能会出现异常。我如何处理那个异常? 在camel文档中,我了解到,对于文件消费者,他们提供了不同的选项,比如-consumer。errorHandler和consumer。bridgeErrorH

  • 问题内容: 我有一个带有服务的应用程序,该服务包装了我的API调用: 并且我正在使用UI-Router在状态之间进行转换: 我不再使用普通的AngularJS路由器,并且难以理解如何实现404。我可以看到抛出状态被拒绝,但是如何在状态路由器中捕获它呢? 问题答案: 仅当没有其他 路由 匹配时才调用该规则。您真正想要的是拦截事件,即在状态转换中出现问题(例如,解析失败)时触发的事件。您可以在状态更改

  • 要部署我的应用程序,我能想到的唯一方法是使用Tomcat。因此,我复制了项目的bundle.js和index.html文件,并将其放在eclipse IDE的WebContent中。下面是index.js文件: 因此,当我使用web pack dev server时,我的路由器工作正常,但当我将文件复制到eclipse项目的web content时(这将为我设置一切,并使项目可以使用这个Url h

  • 问题内容: 如何从处理程序内部正确引用路由名称? 应该全局分配而不是放在函数内部? 问题答案: 您具有返回给定请求的路由的方法。根据该请求,您可以创建一个子路由器并调用 示例:(播放:http : //play.golang.org/p/Lz10YUyP6e)

  • 问题内容: 我正在使用Angular2开发NodeJS应用程序。在我的应用程序中,我有一个主页和搜索页面。对于主页,我有一个将为 localhost:3000 /* 呈现的HTML页面,并且从主页用户导航到 搜索, 即 我由 angular2路由 处理的 localhost:3000 / search 页面。 * 我没有搜索页面的页面,其视图由angular2呈现。但是当我直接点击 localho

  • 试图使某些路由需要身份验证。 我有这个: 注意:是的,身份验证服务正常工作。 对于每个路由,我检查用户是否经过身份验证,如果没有,那么我想将他们重定向到登录页面,如果他们是,那么它将在第一个页面上着陆,路由为"/"。 我得到的只是: 我哪里做错了?