camel-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Arjan Moraal <nab...@ajmoraal.fastmail.net>
Subject Problem with concurrent dir/file access?
Date Wed, 31 Oct 2007 11:59:06 GMT

Hi all,

I have a problem with the FileComponent endpoint. In our application, a lot
of files are written at about the same time to a directory that might not
exist yet. This often causes a FileNotFoundException. Or the directory is
created as file instead of directory.

I managed to extract it into a unit test that fails with a
FileNotFoundException on the directory (See bottom of this message).

I think the problem is in the FileProducer.process() method where it first
tries to build the filename String, and then creates the parent directories:
        File file = createFileName(exchange.getIn());
        buildDirectory(file);
However, the createFileName() method already checks whether the directory
exists (if (endpointFile.isDirectory()), which might not be the case since
buildDirectory() is called after createFileName()...

I don't have a solution/patch yet, but I thought I just let you know anyway
:)

Thanks,
Arjan

package com.bbc.newsi.feeds.sport.renderservice.camel;

import static org.apache.camel.language.juel.JuelExpression.el;

import java.io.File;

import junit.framework.TestCase;

import org.apache.camel.CamelContext;
import org.apache.camel.CamelTemplate;
import org.apache.camel.Exchange;
import org.apache.camel.Message;
import org.apache.camel.Processor;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.file.FileComponent;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.commons.io.FileUtils;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;

/**
 * 
 * @author Albert Moraal
 * @version $Revision$ $Date$ $Author$
 */
public class FileAsDirBugTest extends TestCase {
    private static final Logger log =
Logger.getLogger(FileAsDirBugTest.class);

    private CamelContext context;
    private CamelTemplate template;

    @Override
    protected void setUp() throws Exception {
        super.setUp();
        BasicConfigurator.configure();
        Logger.getRootLogger().setLevel(Level.DEBUG);
        context = new DefaultCamelContext();
        context.start();
        template = new CamelTemplate(context);
    }
    
    @Override
    protected void tearDown() throws Exception {
        context.stop();
        super.tearDown();
    }

    public void testFileCreatedAsDir() throws Exception {
        log.debug("testFileCreatedAsDir");
        final String path = "a/b/c/d/e/f/g/h";
        final int numFiles = 10;
        
        context.addRoutes(
            new RouteBuilder() {
                @Override
                public void configure() {
                    String[] destinations = new String[numFiles];
                    for (int i=0; i<numFiles; i++) {
                        destinations[i] = "seda:file"+i;
                        
                        from("seda:file"+i)
                            .setHeader(FileComponent.HEADER_FILE_NAME,
el("file"+i+".txt"))
                            .to("file://"+path+"/?append=false&noop=true");
                    }
                    
                    from("seda:testFileCreatedAsDir")
                        .to(destinations);
                }
            }
        );

        FileUtils.deleteDirectory(new File("a"));
        
        template.send("seda:testFileCreatedAsDir", new Processor() {
            public void process(Exchange exchange) {
                Message in = exchange.getIn();
                in.setBody("Contents of test file");
            }
        });

        Thread.sleep(2*1000);
        for (int i=0; i<numFiles; i++) {
            assertTrue((new File(path+"/file"+i+".txt")).isFile());
        }
    }

}

-- 
View this message in context: http://www.nabble.com/Problem-with-concurrent-dir-file-access--tf4724448s22882.html#a13507397
Sent from the Camel - Users mailing list archive at Nabble.com.


Mime
View raw message