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
FieldsModifier and TypeFieldDescriptionprotected static final Stringprotected static final String(package private) static final longThe default update interval is 10 seconds, meaning that the synchronizer "reserves" next 10 seconds for generation.private static final org.slf4j.Logger(package private) booleanFlag used to indicate which of timestamp files has the most recently succesfully updated timestamp value.protected longprotected final LockedFileprotected final LockedFile -
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(LockedFile lf1, 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
-
logger
private static final org.slf4j.Logger logger -
DEFAULT_UPDATE_INTERVAL
static final long DEFAULT_UPDATE_INTERVALThe default update interval is 10 seconds, meaning that the synchronizer "reserves" next 10 seconds for generation. This also means that the lock files need to be accessed at most once every ten second.- See Also:
-
DEFAULT_LOCK_FILE_NAME1
- See Also:
-
DEFAULT_LOCK_FILE_NAME2
- See Also:
-
mInterval
protected long mInterval -
mLocked1
-
mLocked2
-
mFirstActive
boolean mFirstActiveFlag used to indicate which of timestamp files has the most recently succesfully updated timestamp value. True means thatmFile1is more recent; false thatmFile2is.
-
-
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
-