Class TroubleService
- All Implemented Interfaces:
CtxEventListener
,ContextHandleAware
,HandleAware
,Ready
,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 TypeMethodDescriptionboolean
Adds the specified trouble to the service.void
addListener
(TroubleListener listener) Add aTroubleListener
to the listener list.addListener
(TroubleMatcher matcher, TroubleAware troubleAware) Add aTroubleAware
as a listener with optional matcher.boolean
Returns true if the set contains the specified trouble.boolean
contains
(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.void
Logs a trouble.void
Removes the specified trouble form the list.void
removeImpacted
(Object... objs) Removes all troubles where any of the specified objects are in the impact list of the trouble.void
removeLinked
(Object... objs) Removes all troubles where any of the specified objects are in the linked list of the trouble.void
removeListener
(TroubleListener listener) Remove aTroubleListener
from the listener list.void
removeTagged
(String tag) Removes all troubles that are tagged with the specified tag.void
removeTroubles
(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, getHandlePrefix
Methods inherited from class com.tccc.kos.commons.util.ready.ReadyBean
getReady, onBeanReady, onDependenciesReady
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface com.tccc.kos.commons.core.context.CtxEventListener
onCtxAutowiringCompleted, onCtxDestroyed, onCtxPhaseCompleted
Methods inherited from interface com.tccc.kos.commons.core.service.handle.HandleAware
addHandleChild, getName, getPath
Methods 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 removeTroubleInfo
which 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 aTroubleAware
as a listener with optional matcher. If the matcher is specified, only troubles that match will be passed to the listener. This will return aTroubleListener
which combines theTroubleAware
andTroubleMatcher
. 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 aTroubleListener
to the listener list. Listeners are a combination ofTroubleAware
andTroubleMatcher
. It is typically more convenient to use anaddListener()
varient which handles the composition automatically. -
removeListener
Remove aTroubleListener
from the listener list. -
getListeners
-