Class FileBasedTimestampSynchronizer

java.lang.Object
com.fasterxml.uuid.TimestampSynchronizer
com.fasterxml.uuid.ext.FileBasedTimestampSynchronizer

public final class FileBasedTimestampSynchronizer extends TimestampSynchronizer
Implementation of TimestampSynchronizer, which uses file system as the storage and locking mechanism.

Synchronization is achieved by obtaining an exclusive file locks on two specified lock files, and by using the files to store first "safe" timestamp value that the generator can use; alternating between one to use to ensure one of them always contains a valid timestamp. Latter is needed to guard against system clock moving backwards after UUID generator restart.

Note: this class will only work on JDK 1.4 and above, since it requires NIO package to do proper file locking (as well as new opening mode for RandomAccessFile).

Also note that it is assumed that the caller has taken care to synchronize access to method to be single-threaded. As such, none of the methods is explicitly synchronized here.

  • Field Details

    • DEFAULT_LOCK_FILE_NAME1

      protected static final String DEFAULT_LOCK_FILE_NAME1
      See Also:
    • DEFAULT_LOCK_FILE_NAME2

      protected static final String DEFAULT_LOCK_FILE_NAME2
      See Also:
    • mInterval

      protected long mInterval
    • mLocked1

      protected final com.fasterxml.uuid.ext.LockedFile mLocked1
    • mLocked2

      protected final com.fasterxml.uuid.ext.LockedFile mLocked2
  • Constructor Details

    • FileBasedTimestampSynchronizer

      public FileBasedTimestampSynchronizer() throws IOException
      Constructor that uses default values for names of files to use (files will get created in the current working directory), as well as for the update frequency value (10 seconds).
      Throws:
      IOException
    • FileBasedTimestampSynchronizer

      public FileBasedTimestampSynchronizer(File lockFile1, File lockFile2) throws IOException
      Throws:
      IOException
    • FileBasedTimestampSynchronizer

      public FileBasedTimestampSynchronizer(File lockFile1, File lockFile2, long interval) throws IOException
      Throws:
      IOException
  • Method Details

    • setUpdateInterval

      public void setUpdateInterval(long interval)
    • initialize

      protected long initialize() throws IOException
      This method is to be called only once by UUIDTimer. It should fetch the persisted timestamp value, which indicates first timestamp value that is guaranteed NOT to have used by a previous incarnation. If it can not determine such value, it is to return 0L as a marker.
      Specified by:
      initialize in class TimestampSynchronizer
      Returns:
      First timestamp value that was NOT locked by lock files; 0L to indicate that no information was read.
      Throws:
      IOException
    • deactivate

      public void deactivate() throws IOException
      Description copied from class: TimestampSynchronizer
      Method UUIDTimer will call if this synchronizer object is being replaced by another synchronizer (or removed, that is, no more synchronization is to be done). It will not be called if JVM terminates.
      Specified by:
      deactivate in class TimestampSynchronizer
      Throws:
      IOException
    • update

      public long update(long now) throws IOException
      Description copied from class: TimestampSynchronizer
      Method called by UUIDTimer to indicate that it has generated a timestamp value that is beyond last legal timestamp value. The method should only return once it has "locked" specified timestamp value (and possible additional ones).
      Specified by:
      update in class TimestampSynchronizer
      Parameters:
      now - Timestamp value caller wants to use, and that the synchronizer is asked to protect.
      Returns:
      Timestamp value that the caller can NOT use. That is, all timestamp values prior to (less than) this value can be used ok, but this value and ones after can only be used by first calling update.
      Throws:
      IOException
    • doDeactivate

      protected static void doDeactivate(com.fasterxml.uuid.ext.LockedFile lf1, com.fasterxml.uuid.ext.LockedFile lf2)