In this post I show how to implement a message consumer FTP with Apache Camel FTP Component.This component extends the File Component and provides access to remote file systems over the FTP and SFTP protocols. In this example I make use of the Java DSL to set up the FTP route in the ConsumerRouteBuilder class. The CamelContext adds the FTP route to the application context and starts the message exchanges. I use a Thread.sleep of 30 seconds to simulate a runtime environment for consuming messages. This article comes with the source code which is available online on my github repository camel-ftp-consumer. I’ve also included the console output after a test execution with Maven where a file on the remote FTP server is downloaded and copied into a local directory.
Stack
Apache Camel FTP 2.17.3
JDK 1.8
Maven 3.2
FTP Component dependency in pom.xml
<dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-ftp</artifactId> <version>2.17.3</version> </dependency>
ConsumerRouteBuilder.java
public class ConsumerRouteBuilder extends RouteBuilder { final static String FTP_HOST = rb.getString("consumer.ftp.host"); final static String FTP_PORT = rb.getString("consumer.ftp.port"); final static String FTP_USER = rb.getString("consumer.ftp.user"); final static String FTP_PASSWORD = rb.getString("consumer.ftp.password"); final static String FTP_PROTOCOL = rb.getString("consumer.ftp.protocol"); final static String FTP_DIR = rb.getString("consumer.ftp.directoryName"); final static String FTP_OPTIONS = rb.getString("consumer.ftp.options"); public void configure() throws Exception { from(ftpUri+"&"+FTP_OPTIONS).to("file://"+DEST_FOLDER).log(LoggingLevel.INFO, "Processing ${id}"); System.out.println("Camel FTP route initialized. Ready to receive Invoices from: " + ftpUri); } }
CamelExecutor.java
public class CamelExecutor { private CamelContext camelCtx; public void init() throws Exception { camelCtx = new DefaultCamelContext(); try { camelCtx.addRoutes(new ConsumerRouteBuilder()); camelCtx.start(); // Simulating a runtime environment of 30 seconds Thread.sleep(30000); }catch (Exception e) { System.out.println("Unable to initialize CamelContext: " + e); return; } } }
Run test
$ mvn test