FTPPage edited by Arjan MoraalFTP/SFTP Component - Camel 1.x onlyThis component provides access to remote file systems over the FTP and SFTP protocols. Maven users will need to add the following dependency to their pom.xml for this component: <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-cometd</artifactId> <version>x.x.x</version> <!-- use the same version as your Camel core version --> </dependency>
Consumer propertiesWhen using FTPConsumer (downloading files from a FTP Server) the consumer specific properties from the File component should be prefixed with "consumer.". For example the delay option from File Component should be specified as "consumer.delay=30000" in the URI. See the samples or some of the unit tests of this component. Filename _expression_In Camel 1.5 we have support for setting the filename using an _expression_. This can be set either using the _expression_ option or as a string based File Language _expression_ in the org.apache.camel.file.name header. See the File Language for some samples. Camel 1.x Known issuesSee the timestamp warning. When consuming files (downloading) you must use type conversation to either String or to InputStream for ASCII and BINARY file types. In Camel 1.4 or below Camel FTPConsumer will poll files regardless if the file is currently being written. See the consumer.exclusiveReadLock option. Also in Camel 1.3 since setNames is default false then you must explicitly set the filename using the setHeader _expression_ when consuming from FTP directly to File. private String ftpUrl = "ftp://camelri...@localhost:21/public/downloads?password=admin&binary=false"; private String fileUrl = "file:myfolder/?append=false&noop=true"; return new RouteBuilder() { public void configure() throws Exception { from(ftpUrl).setHeader(FileComponent.HEADER_FILE_NAME, constant("downloaded.txt")).convertBodyTo(String.class).to(fileUrl); } }; Or you can set the option to true as illustrated below: private String ftpUrl = "ftp://camelri...@localhost:21/public/downloads?password=admin&binary=false&consumer.setNames=true"; private String fileUrl = "file:myfolder/?append=false&noop=true"; return new RouteBuilder() { public void configure() throws Exception { from(ftpUrl).convertBodyTo(String.class).to(fileUrl); } }; SampleIn the sample below we setup Camel to download all the reports from the FTP server once every hour (60 min) as BINARY content and store it as files on the local file system. protected RouteBuilder createRouteBuilder() throws Exception { return new RouteBuilder() { public void configure() throws Exception { // we use a delay of 60 minutes (eg. once pr. hour we poll the FTP server long delay = 60 * 60 * 1000L; // from the given FTP server we poll (= download) all the files // from the public/reports folder as BINARY types and store this as files // in a local directory. Camel will use the filenames from the FTPServer // notice that the FTPConsumer properties must be prefixed with "consumer." in the URL // the delay parameter is from the FileConsumer component so we should use consumer.delay as // the URI parameter name. The FTP Component is an extension of the File Component. from("ftp://sc...@localhost/public/reports?password=tiger&binary=true&consumer.delay=" + delay). to("file://target/test-reports"); } }; } And the route using Spring DSL: <route> <from uri="ftp://sc...@localhost/public/reports?password=tiger&binary=true&consumer.delay=60000"/> <to uri="file://target/test-reports"/> </route> Using _expression_ for filenamesIn this sample we want to move consumed files to a backup folder using today's date as a sub foldername. Notice that the move happens on the remote FTP server. If you want to store the downloaded file on your local disk then route it to the File component as the sample above illustrates. from(ftpUrl + "&_expression_=backup/${date:now:yyyyMMdd}/${file:name}").to("..."); See File Language for more samples. Consuming a remote FTP server triggered by a routeThe FTP consumer is built as a scheduled consumer to be used in the from route. However if you want to start consuming from a FTP server triggered within a route it's a bit cumbersome to do this in Camel 1.x (we plan to improve this in Camel 2.x). However it's possible as this code below demonstrates. In the sample we have a SEDA queue where a message arrives that holds a message containing a filename to poll from a remote FTP server. So we setup a basic FTP url as: // we use directory=false to indicate we only want to consume a single file // we use delay=5000 to use 5 sec delay between pools to avoid polling a second time before we stop the consumer // this is because we only want to run a single poll and get the file // file=getme/ is the path to the folder where the file is private String getUrl = "ftp://ad...@localhost:" + port + "?password=admin&binary=false&directory=false&consumer.delay=5000&file=getme/"; And then we have the route where we use Processor within the route so we can use Java code. In this Java code we create the ftp consumer that downloads the file we want. And after the download we can get the content of the file and put it in the original exchange that continues being routed. As this is based on an unit test it routes to a Mock endpoint. from("seda:start").process(new Processor() { public void process(final Exchange exchange) throws Exception { // get the filename from our custome header we want to get from a remote server String filename = exchange.getIn().getHeader("myfile", String.class); // construct the total url for the ftp consumer String url = "" + filename; // create a ftp endpoint Endpoint ftp = context.getEndpoint(url); // create a polling consumer so we can poll the remote ftp file PollingConsumer consumer = ftp.createPollingConsumer(); consumer.start(); // receive the remote ftp without timeout Exchange result = consumer.receive(); // we must stop the consumer consumer.stop(); // the result is the response from the FTP consumer (the downloaded file) // replace the outher exchange with the content from the downloaded file exchange.getIn().setBody(result.getIn().getBody()); } }).to("mock:result"); Debug loggingThis component has log level TRACE that can be helpful if you have problems. See Also
Change Notification Preferences
View Online
|
View Change
|
Add Comment
|
- [CONF] Apache Camel > FTP confluence
- [CONF] Apache Camel > FTP confluence
- [CONF] Apache Camel > FTP confluence
- [CONF] Apache Camel > FTP confluence
- [CONF] Apache Camel > FTP confluence
- [CONF] Apache Camel > FTP confluence