Package com.storedobject.core
Class DataDistributor<D>
java.lang.Object
com.storedobject.core.DataDistributor<D>
- Type Parameters:
D- The type of data being distributed.
- All Implemented Interfaces:
Closeable,AutoCloseable
A thread-safe utility class responsible for distributing data to registered consumers at a regular interval.
The data is distributed using a background scheduled task. Consumers are notified through their
accept method.
Any consumer that throws an exception during data distribution is automatically unregistered.- Author:
- Syam
-
Constructor Summary
ConstructorsConstructorDescriptionDataDistributor(int refreshRate) DataDistributor(D data) Constructs aDataDistributorwith the specified data and a default refresh rate.DataDistributor(D data, int refreshRate) Constructs aDataDistributorwith the specified data and refresh rate. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Closes theDataDistributor, releasing any resources held by it.voidUpdates the last recorded data update time if the current specified time is more recent than the previously recorded update time.getData()Retrieves the current data being managed by theDataDistributor.longGet the last time a ping was received (GMT).longgetTime()Get the last time data was updated (GMT).voidpingReceived(long time) Updates the last recorded ping time if the specified time is more recent than the previously recorded ping time.voidRegisters a new consumer to receive data updates.voidSets the data to be distributed by theDataDistributor.voidunregister(Consumer<D> consumer) Unregisters a previously registered consumer from receiving data updates.
-
Constructor Details
-
DataDistributor
public DataDistributor() -
DataDistributor
public DataDistributor(int refreshRate) -
DataDistributor
Constructs aDataDistributorwith the specified data and a default refresh rate. The data is distributed to registered consumers periodically based on the preset interval.- Parameters:
data- The data to be distributed to consumers.
-
DataDistributor
Constructs aDataDistributorwith the specified data and refresh rate. The data is distributed to registered consumers periodically based on the refresh rate.- Parameters:
data- The data to be distributed to consumers.refreshRate- The interval, in seconds, at which data is distributed. If the value is less than or equal to 0, a default interval of 5 seconds is used.
-
-
Method Details
-
setData
Sets the data to be distributed by theDataDistributor. This method updates the internal state of theDataDistributorto use the specified data during the next distribution cycle.- Parameters:
data- The data to set. This data will be distributed to registered consumers.
-
getData
Retrieves the current data being managed by theDataDistributor.- Returns:
- The data currently managed and distributed by this instance.
-
close
public void close()Closes theDataDistributor, releasing any resources held by it. This method cancels the timer used for periodic data distribution, stopping any ongoing or scheduled tasks. After calling this method, the instance is no longer usable for distributing data.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable
-
register
-
unregister
-
dataUpdated
public void dataUpdated()Updates the last recorded data update time if the current specified time is more recent than the previously recorded update time. This method ensures that thelastUpdatereflects the most recent time at which a data update occurred, allowing tracking of the update state within theDataDistributor. -
pingReceived
public void pingReceived(long time) Updates the last recorded ping time if the specified time is more recent than the previously recorded ping time. This method ensures that thelastPingfield reflects the most recent time at which a ping was received.- Parameters:
time- The timestamp of the received ping, in milliseconds.
-
getTime
public long getTime()Get the last time data was updated (GMT).- Returns:
- Time.
-
getPingTime
public long getPingTime()Get the last time a ping was received (GMT).- Returns:
- Ping-time.
-