Class TroubleService
- All Implemented Interfaces:
CtxEventListener,ContextHandleAware,HandleAware,Ready,ReadyAndReadyListener,ReadyListener
Troubles do not persist across reboots. Generally speaking this tends not to be a problem but if persistence is required for specific troubles then application code can perform that function and simply recreate the troubles at startup.
This service implements TroubleSet which is essentially a container for troubles. This allows troubles to be added to either the service directly, or in specific use cases, added to another set and then merged into the service.
The equality of troubles is used to determine whether to add a trouble to the service. If a trouble is added but is equal to an existing trouble in the service, the new trouble will not be added. A vast majority of the time this greatly simplifies application code as it doesn't need to check if a trouble already exists before adding another instance. Care must be taken with the definition of equality when writing troubles to make sure that information in a new trouble is not lost because it is considered equal to an existing trouble without that information and thus the new trouble is discarded.
Usage
public class MyClass {
@Autowired
private TroubleService troubleService;
// . . .
}
- Since:
- 1.0
- Version:
- 2023-03-06
-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionbooleanAdds the specified trouble to the service.voidaddListener(TroubleListener listener) Add aTroubleListenerto the listener list.addListener(TroubleMatcher matcher, TroubleAware troubleAware) Add aTroubleAwareas a listener with optional matcher.booleanReturns true if the set contains the specified trouble.booleancontains(TroubleMatcher matcher) Returns true if any troubles match the specified matcher.getFirstTrouble(TroubleMatcher matcher) Returns the first trouble that matches the matcher.Returns theTroubleInfoFactory.getTroubleById(int id) Gets a trouble by ID.Returns the list of all troubles.getTroubles(TroubleMatcher matcher) Returns a list of troubles that match the specified matcher.voidLogs a trouble.voidRemoves the specified trouble form the list.voidremoveImpacted(Object... objs) Removes all troubles where any of the specified objects are in the impact list of the trouble.voidremoveLinked(Object... objs) Removes all troubles where any of the specified objects are in the linked list of the trouble.voidremoveListener(TroubleListener listener) Remove aTroubleListenerfrom the listener list.voidremoveTagged(String tag) Removes all troubles that are tagged with the specified tag.voidremoveTroubles(TroubleMatcher matcher) Removes all troubles that match the specified matcher.Resolves a trouble.resolve(Collection<Trouble> troubles) Resolves a list of troubles and wrap it all up in a single future.Methods inherited from class com.tccc.kos.commons.core.service.AbstractService
getHandle, getHandlePrefixMethods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface com.tccc.kos.commons.core.context.CtxEventListener
onCtxAutowiringCompleted, onCtxDestroyed, onCtxPhaseCompletedMethods inherited from interface com.tccc.kos.commons.core.service.handle.HandleAware
addHandleChild, getName, getPathMethods inherited from interface com.tccc.kos.commons.util.ready.ReadyAndReadyListener
onBeanReady, onDependenciesReadyMethods inherited from interface com.tccc.kos.commons.util.ready.ReadyListener
onDependenciesGroupReady
-
Field Details
-
REASON_errTroubleRemoved
- See Also:
-
-
Method Details
-
getInfoFactory
Returns theTroubleInfoFactory. This can be used to add or removeTroubleInfowhich decorates troubles. -
log
Logs a trouble. This does not add the trouble to the service, no listeners are notified and no references to the trouble are kept. This simply logs the trouble in the standard log stream. Useful for troubles that don't really do anything but are of interest from a data analytics perspective as it allows for consistent data in the stream.- Parameters:
trouble- the trouble to log
-
add
Adds the specified trouble to the service. If an equal trouble exists then this trouble will not be added.- Parameters:
trouble- the trouble to add- Returns:
- true if added
-
remove
Removes the specified trouble form the list.- Parameters:
trouble- the trouble to remove
-
removeImpacted
Removes all troubles where any of the specified objects are in the impact list of the trouble.- Parameters:
objs- the objects to match with impact lists
-
removeLinked
Removes all troubles where any of the specified objects are in the linked list of the trouble.- Parameters:
objs- the objects to match with impact lists
-
removeTagged
Removes all troubles that are tagged with the specified tag.- Parameters:
tag- the tag to check
-
resolve
Resolves a trouble. A resolvable trouble can generally fix the underlying problem and this method is the way of triggering that action. This returns a future that needs to be started in order for the resolve process to run. If the future is successful then the trouble will automatically be removed, otherwise it will remain in the list of troubles.If the trouble is not resolvable this will throw
NotResolvableTroubleException.- Parameters:
trouble- the trouble to resolve- Returns:
- future to resolve the trouble
- Throws:
TroubleException- if an error is detected
-
resolve
Resolves a list of troubles and wrap it all up in a single future. Any troubles that are not resolvable or are already in progress will be left out of the resulting future.Any troubles that are successful will be removed from the list while any troubles that don't resolve successfully will remain in the list.
All troubles will be resolved in parallel and a failure of a single trouble will not impact the rest of the troubles being resolved.
- Parameters:
troubles- the list of troubles to resolve- Returns:
- parallel future for all the troubles
-
contains
Returns true if any troubles match the specified matcher.- Parameters:
matcher- the matcher to use- Returns:
- true if any troubles match
-
getFirstTrouble
Returns the first trouble that matches the matcher.- Parameters:
matcher- the matcher to use- Returns:
- the first matching trouble or null
-
getTroubles
Returns a list of troubles that match the specified matcher.- Parameters:
matcher- the matcher to use- Returns:
- the list of matching troubles
-
removeTroubles
Removes all troubles that match the specified matcher.- Parameters:
matcher- the matcher to use
-
getTroubles
Returns the list of all troubles. This is a copy of the active list of troubles.- Returns:
- list of all troubles
-
contains
Returns true if the set contains the specified trouble.- Parameters:
trouble- the trouble to check- Returns:
- true if the trouble is in the list
-
getTroubleById
Gets a trouble by ID.- Parameters:
id- the id of the trouble to return- Returns:
- the trouble with the specified id or null
-
addListener
Add aTroubleAwareas a listener with optional matcher. If the matcher is specified, only troubles that match will be passed to the listener. This will return aTroubleListenerwhich combines theTroubleAwareandTroubleMatcher. This listener can be used to remove the listener at a later time.- Parameters:
matcher- optional matcher to filter which troubles are passedtroubleAware- receives the trouble events- Returns:
- the constructed trouble listenser
-
addListener
Add aTroubleListenerto the listener list. Listeners are a combination ofTroubleAwareandTroubleMatcher. It is typically more convenient to use anaddListener()varient which handles the composition automatically. -
removeListener
Remove aTroubleListenerfrom the listener list. -
getListeners
-