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

时间:2018-07-24 09:11:06

标签: java apache-camel

我正在尝试构建一个从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 个答案:

答案 0 :(得分:1)

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

  • 确保您的日志中没有可疑的WARNERROR级消息。
  • 确保已在camel-ftp中指定了camel-ftp-starterpom.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
    • 就我而言(请参见下文),我用来复制FROM文件的文件夹为/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

祝您调试顺利!