Package com.fasterxml.uuid.ext
Class FileBasedTimestampSynchronizer
java.lang.Object
com.fasterxml.uuid.TimestampSynchronizer
com.fasterxml.uuid.ext.FileBasedTimestampSynchronizer
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 Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionConstructor 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).FileBasedTimestampSynchronizer(File lockFile1, File lockFile2) FileBasedTimestampSynchronizer(File lockFile1, File lockFile2, long interval) -
Method Summary
Modifier and TypeMethodDescriptionvoidMethodUUIDTimerwill call if this synchronizer object is being replaced by another synchronizer (or removed, that is, no more synchronization is to be done).protected static voiddoDeactivate(com.fasterxml.uuid.ext.LockedFile lf1, com.fasterxml.uuid.ext.LockedFile lf2) protected longThis method is to be called only once byUUIDTimer.voidsetUpdateInterval(long interval) longupdate(long now) Method called byUUIDTimerto indicate that it has generated a timestamp value that is beyond last legal timestamp value.
-
Field Details
-
DEFAULT_LOCK_FILE_NAME1
- See Also:
-
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
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
- Throws:
IOException
-
FileBasedTimestampSynchronizer
public FileBasedTimestampSynchronizer(File lockFile1, File lockFile2, long interval) throws IOException - Throws:
IOException
-
-
Method Details
-
setUpdateInterval
public void setUpdateInterval(long interval) -
initialize
This method is to be called only once byUUIDTimer. 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:
initializein classTimestampSynchronizer- Returns:
- First timestamp value that was NOT locked by lock files; 0L to indicate that no information was read.
- Throws:
IOException
-
deactivate
Description copied from class:TimestampSynchronizerMethodUUIDTimerwill 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:
deactivatein classTimestampSynchronizer- Throws:
IOException
-
update
Description copied from class:TimestampSynchronizerMethod called byUUIDTimerto 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:
updatein classTimestampSynchronizer- 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)
-