Apache Camel FTP 无法捕获远程 FTP 文件夹上的文件创建事件

Apache Camel FTP can not catch file created events on remote FTP folder

我正在尝试使用 Apache Camel FTP 来使用被推送到 FTP 文件夹的文件。用于 Camel 上下文的 FTP 服务器的路由和连接是可以的,但是当文件被推送到 FTP 文件夹时,我无法处理文件创建事件。 这是我的代码和日志: 首先是我的 pom.xml for camel 的一部分(我使用的是 camel 3.0.1 版本):

<!-- Listen FTP folder -->
        <!-- https://mvnrepository.com/artifact/org.apache.camel.springboot/camel-spring-boot-starter -->
        <dependency>
            <groupId>org.apache.camel.springboot</groupId>
            <artifactId>camel-spring-boot-starter</artifactId>
            <version>${camel.version}</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.camel/camel-core-starter -->
        <dependency>
            <groupId>org.apache.camel.springboot</groupId>
            <artifactId>camel-core-starter</artifactId>
            <version>${camel.version}</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.camel.springboot/camel-ftp-starter -->
        <dependency>
            <groupId>org.apache.camel.springboot</groupId>
            <artifactId>camel-ftp-starter</artifactId>
            <version>${camel.version}</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.camel.springboot/camel-jacksonxml-starter -->
        <dependency>
            <groupId>org.apache.camel.springboot</groupId>
            <artifactId>camel-jacksonxml-starter</artifactId>
            <version>3.0.0</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.camel.springboot/camel-jackson-starter -->
        <dependency>
            <groupId>org.apache.camel.springboot</groupId>
            <artifactId>camel-jackson-starter</artifactId>
            <version>${camel.version}</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.camel.springboot/camel-rabbitmq-starter -->
        <dependency>
            <groupId>org.apache.camel.springboot</groupId>
            <artifactId>camel-rabbitmq-starter</artifactId>
            <version>${camel.version}</version>
        </dependency>

初始化骆驼消耗的RouteBuilder:

package xx.listener.route;

import org.apache.camel.LoggingLevel;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.jackson.JacksonDataFormat;
import org.springframework.stereotype.Component;

import xx.listener.common.model.ddXMLModel;

@Component
public class DataRouter extends RouteBuilder {
    @Override
    public void configure() throws Exception {
        System.out.println("Initialize camel route...");
        JacksonDataFormat jacksonDataFormat = new JacksonDataFormat();
        jacksonDataFormat.setInclude("NON_NULL");
        jacksonDataFormat.setPrettyPrint(true);

        from("ftp://user@localhost:21/ftp/files?password=abc@123&move=.done&moveFailed=.error")
                .log("${body}")
                .unmarshal().jacksonxml(ddXMLModel.class)
                .marshal(jacksonDataFormat)
                .log("${body}")
                .to("rabbitmq://localhost:5672/test_camel?username=root&password=123456&queue=camel.queue&autoDelete=false").end();
    }
}

启动应用程序时我的日志:

Initialize camel route...
[INFO ] 2020-12-22 14:21:24.522 [main] SpringBootRoutesCollector - Loading additional Camel XML routes from: classpath:camel/*.xml
[INFO ] 2020-12-22 14:21:24.522 [main] SpringBootRoutesCollector - Loading additional Camel XML rests from: classpath:camel-rest/*.xml
[INFO ] 2020-12-22 14:21:24.530 [main] SpringBootCamelContext - Apache Camel 3.0.1 (CamelContext: camel-1) is starting
[INFO ] 2020-12-22 14:21:24.531 [main] JmxManagementStrategy - JMX is enabled
[WARN ] 2020-12-22 14:21:24.747 [main] RabbitMQComponent - The old syntax rabbitmq://hostname:port/exchangeName is deprecated. You should configure the hostname on the component or ConnectionFactory
[INFO ] 2020-12-22 14:21:24.755 [main] SpringBootCamelContext - 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
[WARN ] 2020-12-22 14:21:24.772 [main] JacksonDataFormat - The option autoDiscoverObjectMapper is set to false, Camel won't search in the registry
[INFO ] 2020-12-22 14:21:24.897 [main] SpringBootCamelContext - Route: route1 started and consuming from: ftp://user@localhost:21/ftp/files?move=.done&moveFailed=.error&password=xxxxxx
[INFO ] 2020-12-22 14:21:24.902 [main] SpringBootCamelContext - Total 1 routes, of which 1 are started
[INFO ] 2020-12-22 14:21:24.904 [main] SpringBootCamelContext - Apache Camel 3.0.1 (CamelContext: camel-1) started in 0.372 seconds
[INFO ] 2020-12-22 14:21:24.908 [main] ListenerApplication - Started ListenerApplication in 2.979 seconds (JVM running for 4.169)

我连接到我的 FTP 服务器,切换到文件目录并将示例 xml 文件推送到此文件夹,但我的应用程序控制台中没有任何反应。我没有更多日志了。

我还尝试在应用程序主 class 中添加到 Camel 上下文的路由。但是我得到了相同的结果。

CamelContext camelContext = new DefaultCamelContext();
        try {
            camelContext.addRoutes(new DataRouter());
            camelContext.start();
        }
        catch (Exception e){
            e.printStackTrace();
            camelContext.stop();
        }

我的代码有什么问题。谢谢

只有 2 个想法,因为没有任何反应(没有读取文件,没有错误)

  • 用户有查看文件的权限吗?
  • [userHome]/ftp/files下的文件吗?

第二点是因为Camel FTP路径总是相对的。所以用户的home路径,即ftp登录后用户登陆的目录,就是路径的起点。

最后:尝试将您的日志级别提高到 DEBUG

通过将 passiveMode=true 添加到 ftp uri 以允许登录到 ftp 服务器

来解决此问题