views:

40

answers:

2

I have looked at the source code of Apache Commons FileUtils.java class to see how they implement unix like touch functionality. But I wanted to confirm with the community here if my use case would be met by the implementation as it opens and closes a FileOutputStream to provide touch functionality

We have two webservers and one common server between them where a File is residing

For our application we need to use the time modified of this file to make some decisions. We actually don't want to modify the file but change its last modified date when some particular activity happens on one of the webservers.

Its important that last modified time set for the file is taken from the central server to avoid worrying about time differences between two web servers. Therefore changing file.setLastModfiied is not a good option as webserver would send its own time.

But I am wondering that even if I use Apache Commons FileUtils touch method to do this, would closing stream on one webserver set the last modified time of the file using time of the webserver or the central server.

Sorry for so much details but could not see any other way to explain the issue

+1  A: 

If you "touch" a file in the filesystem of one webserver, then the timestamp of the file will be set using the clock of that server. I don't think you can solve your problem that way.

I think you've got three options:

  • configure the servers to synchronize their clocks to the common timebase; e.g. using NTP,
  • put all files whose timestamps must be accurate to the common timebase on one server, or
  • change your system design so that it is immune to problems with different servers' clocks being out of sync.
Stephen C
Thanks for the response. In our case the file is lying a in a common server and both the webservers actually use a symbolic link to point to that location.With this setup which server's clock time would be picked up?
Fazal
It depends how how the web servers use the symbolic links. They need to resolve them using `File.getCanonicalFile()` or equivalent. Also, it may depend on how webservers access the shared file system. But you should be able to get an answer by looking at the timestamps the webservers are actually returning.
Stephen C
Thanks for the response. I will look at into it
Fazal
A: 

It would be much better to make use of a shared database if you have one so that you can avoid issues of concurrency and synchronisation. I can't recommend any simple and safe distributed file flag system.

jowierun