|
File has been edited by Claus Ibsen (May 25, 2008). Content: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 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 directory; so that they appear to be deleted. Message HeadersThe following message headers can be used to affect the behavior of the component
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, "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. 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"); 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"); } } See Also |
Unsubscribe or edit your notifications preferences
