代码之家  ›  专栏  ›  技术社区  ›  Europa

Camel从FTP读取文件并存储在本地资源文件夹中

  •  0
  • Europa  · 技术社区  · 6 年前

    我试图构建一个简单的路由,从FTP文件夹读取数据,并将其存储在本地资源文件夹中。我可以连接到FTP端点,但之后什么也不会发生。

    要启动我的程序,我使用:mvn clean compile camel:run

    我真的不知道接下来该做什么来调试这个。

    终端输出:

    INFO | Apache Camel 2.20.0 (CamelContext: camel-1) is starting
     INFO | JMX is enabled
     INFO | Type converters loaded (core: 192, classpath: 4)
     INFO | 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
     INFO | Route: route1 started and consuming from: ftp://xx.xx.xx.xx/ftp/xx/xx?password=xxxxxx&username=xx
     INFO | Total 1 routes, of which 1 are started
     INFO | Apache Camel 2.20.0 (CamelContext: camel-1) started in 0.333 seconds
    

    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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>com.nettport</groupId>
        <artifactId>camel_download_file_from_ftp_and_get_name_and_content</artifactId>
        <version>1.0-SNAPSHOT</version>
    
    
    
    
        <dependencies>
            <!-- Camel -->
            <dependency>
                <groupId>org.apache.camel</groupId>
                <artifactId>camel-core</artifactId>
                <version>2.20.0</version>
            </dependency>
            <dependency>
                <groupId>org.apache.camel</groupId>
                <artifactId>spi-annotations</artifactId>
                <version>2.20.0</version>
            </dependency>
            <dependency>
                <groupId>org.apache.camel</groupId>
                <artifactId>camel-jms</artifactId>
                <version>2.20.0</version>
            </dependency>
    
            <!-- Spring -->
            <dependency>
                <groupId>org.apache.camel</groupId>
                <artifactId>camel-spring</artifactId>
                <version>2.20.0</version>
            </dependency>
            <!-- //Spring -->
    
            <!-- FTP -->
            <dependency>
                <groupId>org.apache.camel</groupId>
                <artifactId>camel-ftp</artifactId>
                <version>2.20.0</version>
            </dependency>
            <!-- //FTP -->
    
            <!-- Quartz -->
            <dependency>
                <groupId>org.apache.camel</groupId>
                <artifactId>camel-quartz</artifactId>
                <version>2.20.0</version>
            </dependency>
            <!-- //Quartz -->
    
            <!-- ActiveMq -->
            <dependency>
                <groupId>org.apache.activemq</groupId>
                <artifactId>activemq-all</artifactId>
                <version>5.15.1</version>
            </dependency>
    
    
            <!-- Logging -->
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-log4j12</artifactId>
                <version>1.8.0-alpha2</version>
            </dependency>
            <!-- //Logging -->
    
        </dependencies>
    
    
        <build>
            <plugins>
                <!-- Allows the routes to be run via 'mvn camel:run' -->
                <plugin>
                    <groupId>org.apache.camel</groupId>
                    <artifactId>camel-maven-plugin</artifactId>
                    <version>2.20.0</version>
                </plugin>
            </plugins>
        </build>
    
    </project>
    

    src/main/java/com/nettport/ReceiverRoute.java文件:

    package com.nettport;
    
    import org.apache.camel.builder.RouteBuilder;
    
    public class ReceiverRoute extends RouteBuilder {
    
        private String receiverFtpEndpoint;
    
        public void configure() throws Exception {
    
            // lets shutdown faster in case of in-flight messages stack up
            getContext().getShutdownStrategy().setTimeout(10);
    
            from(receiverFtpEndpoint)
            .log("### FTP Receiver Route started and consuming ###")
            .to("file:data/work_in_progress")
            .log("Downloaded file ${file:name} complete.");
        }
    
    
        public void setReceiverFtpEndpoint(String receiverFtpEndpoint) {
            this.receiverFtpEndpoint = receiverFtpEndpoint;
        }
    }
    

    META-INF/spring/camel-contxt.xml文件

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="
           http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
           http://camel.apache.org/schema/spring
           http://camel.apache.org/schema/spring/camel-spring.xsd">
    
    
        <bean id="receiverRoute" class="com.nettport.ReceiverRoute">
            <property name="receiverFtpEndpoint" value="ftp://xx.xx.xx.xx/ftp/xx/xx?username=xx&amp;password=xx"/>
        </bean>
    
    
        <camelContext xmlns="http://camel.apache.org/schema/spring">
            <routeBuilder ref="receiverRoute"/>
        </camelContext>
    
    </beans>
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   Lord Nighton    5 年前

    好的,同事们。万一有人遇到麻烦 I have set up a simple route that copies the files form FTP to my local disk folder and it does nothing 请尝试以下操作:

    • 确保你没有怀疑 WARN ERROR 在日志中分级消息。
    • 确保已指定 camel-ftp camel-ftp-starter 在你的 pom.xml . 我已经建立了一个Spring Boot应用程序,并且在我的 pom.xml文件

      <dependency>
          <groupId>org.apache.camel</groupId>
          <artifactId>camel-ftp-starter</artifactId>
          <version>3.0.0-M2</version>
      </dependency>
      
    • 请确保已阅读此文档:

    • 确保FTP服务器端的所有设置都正确:
      • 您的架构正确
      • 您已经设置了用户和密码
    • 确保要从(从FTP服务器)下载文件的目录是 NOT EMPTY
      • 在我的情况下(见下文),我用来复制文件的文件夹是 /home/lordnighton
      • 使用 FileZilla 客户端检查文件夹的状态(它还帮助您将文件复制到FTP服务器)
    • 确保FTP服务器已启动并正在运行

    逐个运行命令,将FTP服务器作为Docker容器

        $ docker pull stilliard/pure-ftpd:hardened
        $ docker run -e FTP_USER_NAME=lord -e FTP_USER_PASS=nighton -e FTP_USER_HOME=/home/lordnighton  -d --name ftpd_server -p 21:21 -p 30000-30009:30000-30009 -e "PUBLICHOST=localhost" stilliard/pure-ftpd:hardened
        $ ftp ftp://lord:nighton@localhost:21 # check the connection with user/password
    
    • 在类中指定扩展的路由 RouteBuilder :

      from("ftp://lord@localhost:21/home/lordnighton?password=nighton&passiveMode=true")
          .to("file:src/main/resources/data/from-ftp");
      
    • 您还可以切换 camel 的组件 TRACE 要查看更详细的日志:

      log4j.logger.org.apache.commons.net=TRACE
      log4j.logger.org.apache.camel.component.file=TRACE
      log4j.logger.org.apache.camel.component.ftp=TRACE
      
    • 有时切换 passiveMode true :

      from("ftp://lord@localhost:21/home/lordnighton?password=nighton&passiveMode=true")
      
    • 有时研究这个例子也是有帮助的-- https://github.com/apache/camel/tree/master/examples/camel-example-ftp

    祝你调试顺利!