public final class FileBasedTimestampSynchronizer extends TimestampSynchronizer
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.
Modifier and Type | Field and Description |
---|---|
protected static String |
DEFAULT_LOCK_FILE_NAME1 |
protected static String |
DEFAULT_LOCK_FILE_NAME2 |
protected long |
mInterval |
protected com.fasterxml.uuid.ext.LockedFile |
mLocked1 |
protected com.fasterxml.uuid.ext.LockedFile |
mLocked2 |
Constructor and Description |
---|
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).
|
FileBasedTimestampSynchronizer(File lockFile1,
File lockFile2) |
FileBasedTimestampSynchronizer(File lockFile1,
File lockFile2,
long interval) |
Modifier and Type | Method and Description |
---|---|
void |
deactivate()
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). |
protected static void |
doDeactivate(com.fasterxml.uuid.ext.LockedFile lf1,
com.fasterxml.uuid.ext.LockedFile lf2) |
protected long |
initialize()
This method is to be called only once by
UUIDTimer . |
void |
setUpdateInterval(long interval) |
long |
update(long now)
Method called by
UUIDTimer to indicate that it has generated
a timestamp value that is beyond last legal timestamp value. |
protected static final String DEFAULT_LOCK_FILE_NAME1
protected static final String DEFAULT_LOCK_FILE_NAME2
protected long mInterval
protected final com.fasterxml.uuid.ext.LockedFile mLocked1
protected final com.fasterxml.uuid.ext.LockedFile mLocked2
public FileBasedTimestampSynchronizer() throws IOException
IOException
public FileBasedTimestampSynchronizer(File lockFile1, File lockFile2) throws IOException
IOException
public FileBasedTimestampSynchronizer(File lockFile1, File lockFile2, long interval) throws IOException
IOException
public void setUpdateInterval(long interval)
protected long initialize() throws IOException
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.initialize
in class TimestampSynchronizer
IOException
public void deactivate() throws IOException
TimestampSynchronizer
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.deactivate
in class TimestampSynchronizer
IOException
public long update(long now) throws IOException
TimestampSynchronizer
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).update
in class TimestampSynchronizer
now
- Timestamp value caller wants to use, and that the
synchronizer is asked to protect.IOException
protected static void doDeactivate(com.fasterxml.uuid.ext.LockedFile lf1, com.fasterxml.uuid.ext.LockedFile lf2)
Copyright © 2023 FasterXML.com. All rights reserved.