2

I have an XML file (in question) is used by process across multiple JVM. At one point of time, multiple process access the same file and writes to it. And i want the updated information to be read after each write. So basically 'FileLock' is a way to ensure to get exclusive lock on the File. But since it is XML, I tried to load using JAXB.

JAXBContext context = JAXBContext.newInstance(rootClass);
Unmarshaller um = context.createUnmarshaller();
Object obj = um.unmarshal(new FileInputStream(sourceXml));

with JAXB, for once I have to read the file using FileInputStream and get exclusive lock on the channel.

JAXBContext context = JAXBContext.newInstance(rootClass);
Marshaller m = context.createMarshaller();      
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
m.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
m.marshal(rootObj, xmlFile);    

But later to write it, the file is already locked by InputStream.

RandomAccessFile to simultaneously read/write is not an option for the XML file.

any ideas to get around this with single lock mechanism?

Vizllx
  • 9,135
  • 1
  • 41
  • 79
ulab
  • 1,079
  • 3
  • 15
  • 45

0 Answers0