File has been edited by Claus Ibsen (Sep 16, 2008). Change summary: CAMEL-909 File ComponentThe File component provides access to file systems; allowing files to be processed by any other Camel Components or messages from other components can be saved to disk. URI formatfile:fileOrDirectoryName[?options] or
URI Options
By default the file is locked for the duration of the processing. Also when files are processed they are moved into the .camel subdirectory; so that they appear to be deleted. The File Consumer will always skip any file which name starts with a dot, such as ".", ".camel", ".m2" or ".groovy". The File Consumer stores internally the last poll time. This is used to avoid polling already polled files as it will compare the lastpolltime with the modification timestamp on the file. Beware that its not persistent in any way so restarting Camel will restart the lastpolltime variable and you can potentially consume the same file again. Therefore you should either delete or move consumed files to a different folder. By default Camel will move consumed files to the sub folder .camel relative where the file was consumed. Message HeadersThe following message headers can be used to affect the behavior of the component
Default Behavior Changed in Camel 1.5In Camel 1.5 the file consumer will avoid polling files that is currently in the progress of being written (see option consumer.exclusiveReadLock). However this requires Camel being able to rename the file for its testing. If the Camel user hasn't this rights on the file system, you can set this option to false to revert the change to the default behavior of Camel 1.4 or older. The recursive option has changed its default value from true to false in Camel 1.5. Common gotchas with folder and filenamesWhen Camel is producing files (writing files) there are a few gotchas how to set a filename of your choice. By default Camel will use the message id as the filename, and since the message id is normally a unique generated id you will end up with filenames such as: ID-MACHINENAME\2443-1211718892437\1-0. Such a filename is not desired and therefore best practice is to provide the filename in the message header "org.apache.camel.file.name". The sample code below produces files using the message id as the filename: from("direct:report").to("file:target/reports"); To use report.txt as the filename you have to do: from("direct:report").setHeader(FileComponent.HEADER_FILE_NAME, constant("report.txt")).to( "file:target/reports"); Canel will default try to auto create the folder if it does not exists, and this is a bad combination with the UUID filename from above. So if you have: from("direct:report").to("file:target/reports/report.txt"); And you want Camel to store in the file report.txt and autoCreate is true, then Camel will create the folder: target/reports/report.txt/. To fix this set the autoCreate=false and create the folder target/reports manually. from("direct:report").to("file:target/reports/report.txt?autoCreate=false"); With auto create disabled Camel will store the report in the report.txt as expected. File consumer, scanning for new files gotchaThe file consumer scans for new files by keeping an internal modified timestamp of the last consumed file. So if you copy a new file that has an older modified timestamp, then Camel will not pickup this file. This can happen if you are testing and you copy the same file back to the folder that has just been consumed. To remedy this modify the timestamp before copying the file back. 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. SamplesRead from a directory and write to another directoryfrom("file://inputdir/?delete=true").to("file://outputdir")
Listen on a directory and create a message for each file dropped there. Copy the contents to the outputdir and delete the file in the inputdir. Read from a directory and process the message in javafrom("file://inputdir/").process(new Processor() { public void process(Exchange exchange) throws Exception { Object body = exchange.getIn().getBody(); // do some business logic with the input body } }); Body will be File object pointing to the file that was just dropped to the inputdir directory. Read files from a directory and send the content to a jms queuefrom("file://inputdir/").convertBodyTo(String.class).to("jms:test.queue")
By default the file endpoint sends a FileMessage which contains a File as body. If you send this directly to the jms component the jms message will only contain the File object but not the content. By converting the File to a String the message will contain the file contents what is probably what you want to do. Writing to filesCamel is of course also able to write files, eg. producing files. In the sample below we receive some reports on the SEDA queue that we processes before they are written to a directory. public void testToFile() throws Exception { template.sendBody("seda:reports", "This is a great report"); // give time for the file to be written before assertions Thread.sleep(1000); // assert the file exists File file = new File("target/test-reports/report.txt"); file = file.getAbsoluteFile(); assertTrue("The file should have been written", file.exists()); } protected JndiRegistry createRegistry() throws Exception { // bind our processor in the registry with the given id JndiRegistry reg = super.createRegistry(); reg.bind("processReport", new ProcessReport()); return reg; } protected RouteBuilder createRouteBuilder() throws Exception { return new RouteBuilder() { public void configure() throws Exception { // the reports from the seda queue is processed by our processor // before they are written to files in the target/reports directory from("seda:reports").processRef("processReport").to("file://target/test-reports"); } }; } private class ProcessReport implements Processor { public void process(Exchange exchange) throws Exception { String body = exchange.getIn().getBody(String.class); // do some business logic here // set the output to the file exchange.getOut().setBody(body); // set the output filename using java code logic, notice that this is done by setting // a special header property of the out exchange exchange.getOut().setHeader(FileComponent.HEADER_FILE_NAME, "report.txt"); } } FileProducer filename gotchasThis unit test demonstrates some of the gotchas with filenames for the File Producer. public void testProducerWithMessageIdAsFileName() throws Exception { Endpoint endpoint = context.getEndpoint("direct:report"); Exchange exchange = endpoint.createExchange(); exchange.getIn().setBody("This is a good report"); FileEndpoint fileEndpoint = resolveMandatoryEndpoint("file:target/reports/report.txt", FileEndpoint.class); String id = fileEndpoint.getGeneratedFileName(exchange.getIn()); template.send("direct:report", exchange); File file = new File("target/reports/report.txt/" + id); assertEquals("File should exists", true, file.exists()); } public void testProducerWithConfiguedFileNameInEndpointURI() throws Exception { template.sendBody("direct:report2", "This is another good report"); File file = new File("target/report2.txt"); assertEquals("File should exists", true, file.exists()); } public void testProducerWithHeaderFileName() throws Exception { template.sendBody("direct:report3", "This is super good report"); File file = new File("target/report-super.txt"); assertEquals("File should exists", true, file.exists()); } protected RouteBuilder createRouteBuilder() throws Exception { return new RouteBuilder() { public void configure() throws Exception { from("direct:report").to("file:target/reports/report.txt"); from("direct:report2").to("file:target/report2.txt?autoCreate=false"); from("direct:report3").setHeader(FileComponent.HEADER_FILE_NAME, constant("report-super.txt")).to("file:target/"); } }; } Using _expression_ for filenamesIn this sample we want to move consumed files to a backup folder using todays date as a sub foldername: from("file://inbox?_expression_=backup/${date:now:yyyyMMdd}/${file:name}").to("...");
See File Language for more samples. See Also |
Unsubscribe or edit your notifications preferences