javaapache-camelspring-camel

How to lock writing file so other consumer can't consume


I am working on replacing data in file (it's big file of 400 MB) using camel and I am facing issue where other consumer pickup file even though file is in use (it's in writing mode).

Is there a way in Camel to lock file which is in writing mode so another consumer/route can't consume. It should consume once the writing is done. I tried with readLocks but no luck so far.

from("file://A").split().tokenize("\n", 999).streaming()
.log("Spliting::::::::::").unmarshal(csv).bean("transferDate", "enrich")
.marshal(csv).to("file://B?fileExist=Append");

from("file://B?delete=true").to("file://A"); // this route pick up file even the first route haven't finished writing file completely

Solution

  • You should configure the Camel route with appropriate options for file reading and writing. In particular, you need to set the readLock and readLockTimeout options for the file endpoint.

     from("file:/input-directory")
            .routeId("fileRoute")
            .to("file:/output-directory?readLock=changed&readLockTimeout=5000")
            .log("File processed: ${file:name}")
            .end();