Class AbstractManagedPendingService<O,S,R>
java.lang.Object
ru.progrm_jarvis.javacommons.service.AbstractManagedPendingService<O,S,R>
- Type Parameters:
O- type of owner of the serviceS- type of service owned while pendingR- type of value passed to hooks indicating that the service's pending has ended (the service became ready)
- All Implemented Interfaces:
ManagedPendingService<O,,S, R> PendingService<O,S, R>
public abstract class AbstractManagedPendingService<O,S,R>
extends Object
implements ManagedPendingService<O,S,R>
Abstract implementation of
ManagedPendingService.-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected classSafeowned serviceguaranteeing no undefined behaviour due to illegal state.protected static enumStates in which this service can be.Nested classes/interfaces inherited from interface ru.progrm_jarvis.javacommons.service.PendingService
PendingService.OwnedService<S,R> -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final @NonNull LockLock used for synchronizing lifecycle operationsprotected final @NonNull Map<O,PendingService.OwnedService<S, R>> Map of owned service instances by their ownersMap of callbacks to be called once the server gets started by their ownersprotected final @NonNull AtomicReference<AbstractManagedPendingService.State>Current state if this service -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected voidaddReadyCallback(O owner, @NonNull Consumer<R> readyCallback) Adds a ready-callback for the given owner.booleanChecks if this service has been successfully started.protected voidmarkAsReady(O owner) Marks the provided owner as ready.protected abstract SnewService(O owner) Creates a service for the provided owner.voidready()Marks this service as ready for starting.@NotNull PendingService.OwnedService<S,R> Gets an instance of a pending service which must be closed once the owner is ready with it.protected voidrunReadyCallbacks(R service) Runs allready-callbacks.protected abstract Rstart()Actually tarts this service returning it.protected voidtryStart()Attempts to start this service.
-
Field Details
-
state
Current state if this service -
lifecycleLock
Lock used for synchronizing lifecycle operations -
owners
Map of owned service instances by their owners -
readyCallbacks
Map of callbacks to be called once the server gets started by their owners
-
-
Constructor Details
-
AbstractManagedPendingService
protected AbstractManagedPendingService(@NonNull @NonNull Lock lifecycleLock, @NonNull @NonNull Map<O, PendingService.OwnedService<S, R>> owners, @NonNull @NonNull Map<O, Set<Consumer<R>>> readyCallbacks) Creates a new managed pending service.- Parameters:
lifecycleLock- lock used for synchronizing lifecycle operationsowners- empty map of owned service instances by their ownersreadyCallbacks- empty map of callbacks to be called once the server gets started by their owners- Throws:
IllegalArgumentException- ifownersorreadyCallbacksis not empty
-
-
Method Details
-
request
Description copied from interface:PendingServiceGets an instance of a pending service which must be closed once the owner is ready with it.- Specified by:
requestin interfacePendingService<O,S, R> - Parameters:
owner- owner requesting the service- Returns:
- owned service which must be closed once the owner is ready with it
-
newService
Creates a service for the provided owner.- Parameters:
owner- owner of the created service- Returns:
- creates service
-
markAsReady
Marks the provided owner as ready.- Parameters:
owner- owner which should now be considered ready
-
tryStart
protected void tryStart()Attempts to start this service. -
start
Actually tarts this service returning it.- Returns:
- started service
-
runReadyCallbacks
Runs allready-callbacks.- Parameters:
service- service to be passed to callbacks
-
addReadyCallback
Adds a ready-callback for the given owner.- Parameters:
owner- owner of the callbackreadyCallback- callback to be added to the given owner
-
ready
public void ready()Description copied from interface:ManagedPendingServiceMarks this service as ready for starting.- Specified by:
readyin interfaceManagedPendingService<O,S, R>
-
isSafelyStarted
public boolean isSafelyStarted()Description copied from interface:ManagedPendingServiceChecks if this service has been successfully started.- Specified by:
isSafelyStartedin interfaceManagedPendingService<O,S, R> - Returns:
trueif this service has been successfully started andfalseotherwise
-