java - Concurrently write to XML file -


I have several processes running on different machines that need to be read / written in the shared XML file, I use it I'm doing DOM with Java and FileLocks (although I know that the database will be a more effective approach, this project is not viable due to constraints).

To make changes to the XML file, the relevant process first creates a specially locked channel which is used to read the file, then write the new version before closing the channel. An attempt is made to reuse the same channel; The lock is never below like this though the issue is that I'm getting a java.nio.channels.ClosedChannelException while trying to write results, even if I do not explicitly close the channel Does I suspect that the line of code:

  doctor = debugger. Pars (channel.Newinputstream (channel));  

Turns off the channel if so, how can I force the channel to remain open? My code can be seen below:

[Code removed after update]

Update: System.out.println (channel.isOpen ()) Confirming the suspicious line code before and after it confirms that this is where the channel is closed.

Update: By asking the code given below, parsing prevents the channel from shutting down during operation. The issue is now that instead of changing the original XML file, the transformer adds the converted document to the original. In the documentation I have searched for Transformer.transform (I searched for transformer / transformer / transformer factory / ) No related option can be found to specify the output: StreamResult ). Am I forgetting something? Do I have to clean any channel before writing? Thank you.

Update: Finally, the solution of the epine issue by shortening the channel to 0. Thanks to JL Riche for advice. The job code is posted as an answer.

Instead of trying this design:

  1. A new service Create (a process) that opens a socket and hears "update commands".
  2. All other processes do not write the file directly, but instead send "update command" new service

In this way, you do not have to worry about locking To make the whole thing more believable, you may want to add buffers to the process of sending them so that they can continue the service for a while.

With this approach, you'll never get the file lock (which can be incredible on the basis of your OS) socket, also ensure that you can not start the service twice.


Comments