Class CriticalDataService
Critical data is encoded as json and compressed with a CRC32 header for actual
storage. This allows arbitrary json data to be stored in critical data while
still attempting to minimize the space required. The CRC32 also allows junk
data to be detected and flagged as invalid. Individual CriticalDataSource
implementations are expected to know the length of critical data and return
the correct number of bytes. A typical solution for this is to write the length
of the array before writing the array itself.
Critical data is namespaced and is effectively a map with the key being the name of the critical data to operate on. This allows multiple services to share critical data without requiring a global format for all data.
Care should be taken to minimize the size of json data where possible. Critical data is often a very limited resource, sometimes providing as little as 100 bytes of storage depending on the hardware backing the source. All critical data is stored on every source so the upper limit for data is the smallest source capacity.
As critical data is inherently distributed across different hardware, it can take
some time for critical data to become available, if it becomes available at all.
The criticalDataReady indicator can be used to determine when critical
data becomes ready and even get a callback when ready. The
CriticalDataListener interface provides additional events such as when
critical data is changed or if critical data is unavailable.
- Since:
- 1
- Version:
- 1
-
Nested Class Summary
Nested Classes -
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionaddInternalSource(double weight) Add the local host as a critical data source.addInternalSource(int maxLength, double weight) Add the local host as a critical data source.voidaddSource(CriticalDataSource source) Add a new source to the service with the default weight for the source.voidaddSource(CriticalDataSource source, Double weight) Add a new source to the service with the specified weight.<T> TgetCriticalData(String name, Class<T> clazz) Return the named critical data as an instance of the specified class.Return theReadyIndicatorthat can be used to detect if critical data is already ready, or can generate a callback when critical data is ready for use.booleanReturn true if the critical data is ready for use.voidready(CriticalDataSource source) Called when aCriticalDataSourcebecomes ready for use.voidremoveCriticalData(String name) Remove the named critical data and write back to the source.voidremoveSource(CriticalDataSource source) Remove a source from the service.voidsetAllCriticalData(JsonNode json) Set all of critical data with the specified json data.voidsetCriticalData(String name, Object data, Class<?> view) Set the new critical data and write back to all the sources.voidUpdate all the sources with critical data if needed.
-
Field Details
-
ALL_DATA
- See Also:
-
-
Method Details
-
getCriticalDataReady
Return theReadyIndicatorthat can be used to detect if critical data is already ready, or can generate a callback when critical data is ready for use.- Since:
- 1
-
isCriticalDataReady
public boolean isCriticalDataReady()Return true if the critical data is ready for use. This is simply a wrapper forgetCriticalDataReady().isReady().- Returns:
- true if critical data is ready for use
- Since:
- 1
-
getCriticalData
Return the named critical data as an instance of the specified class. This will deserialize the named section from json format back to the specified class.- Parameters:
name- the name of the data to return- Returns:
- the named critical data
- Throws:
CriticalDataException- if critical data not available or can't be parsed- Since:
- 1
-
setCriticalData
Set the new critical data and write back to all the sources.- Parameters:
name- the name of the data to setdata- the new critical data (null will remove the name entirely)view- optional view to json conversion- Throws:
CriticalDataException- if critical data not available or can't be set- Since:
- 1
-
removeCriticalData
Remove the named critical data and write back to the source.- Parameters:
name- the name of the data to remove- Since:
- 1
-
setAllCriticalData
Set all of critical data with the specified json data. This is typically used during testing or manufacturing to set critical data when none already exists. This will replace the current critical data if any exists.- Parameters:
json- json representation of all critical data- Throws:
CriticalDataException- if critical data can't be set- Since:
- 1
-
updateAllSourcesIfNeeded
public void updateAllSourcesIfNeeded()Update all the sources with critical data if needed.- Since:
- 1
-
ready
Called when aCriticalDataSourcebecomes ready for use. Normally a source will be checked when it is added, but if not ready at that point, this should be called to indicate that it is ready and should be checked again. Even if critical data has been selected, this will force the data to be replicated to the source.- Since:
- 1
-
addSource
Add a new source to the service with the default weight for the source. A source can only be added once. Subsequent calls for the same source will be ignored.- Parameters:
source- the new source- Since:
- 1
-
addSource
Add a new source to the service with the specified weight. This will only add the source and set the weight if the source has not already been added.- Parameters:
source- the new sourceweight- the weight for the source or null to leave source unchanged- Since:
- 1
-
removeSource
Remove a source from the service. This only needs to be called if the source is not in aBeanContextas it will be automatically removed when the context is destroyed.- Parameters:
source- the source to remove- Since:
- 1
-
addInternalSource
Add the local host as a critical data source. This will use the local disk to store critical data for this source. This source can only be added once. Subsequent calls will simply return the same source. If this this call creates the source, the max size will be set to 1kb.- Parameters:
weight- the weight of the source (only used on first call)- Returns:
- the internal source
- Since:
- 1
-
addInternalSource
Add the local host as a critical data source. This will use the local disk to store critical data for this source. This source can only be added once. Subsequent calls will simply return the same source.- Parameters:
maxLength- the max bytes of critical data this source can storeweight- the weight of the source (only used on first call)- Returns:
- the internal source
- Since:
- 1
-