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 service
S - type of service owned while pending
R - 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.
  • Field Details

    • state

      @NonNull protected final @NonNull AtomicReference<AbstractManagedPendingService.State> state
      Current state if this service
    • lifecycleLock

      @NonNull protected final @NonNull Lock lifecycleLock
      Lock used for synchronizing lifecycle operations
    • owners

      @NonNull protected final @NonNull Map<O,PendingService.OwnedService<S,R>> owners
      Map of owned service instances by their owners
    • readyCallbacks

      @NonNull protected final @NonNull Map<O,Set<Consumer<R>>> 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 operations
      owners - empty map of owned service instances by their owners
      readyCallbacks - empty map of callbacks to be called once the server gets started by their owners
      Throws:
      IllegalArgumentException - if owners or readyCallbacks is not empty
  • Method Details

    • request

      @NotNull public @NotNull PendingService.OwnedService<S,R> request(@NonNull O owner)
      Description copied from interface: PendingService
      Gets an instance of a pending service which must be closed once the owner is ready with it.
      Specified by:
      request in interface PendingService<O,S,R>
      Parameters:
      owner - owner requesting the service
      Returns:
      owned service which must be closed once the owner is ready with it
    • newService

      protected abstract S newService(@NonNull O owner)
      Creates a service for the provided owner.
      Parameters:
      owner - owner of the created service
      Returns:
      creates service
    • markAsReady

      protected void markAsReady(@NonNull O owner)
      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

      protected abstract R start()
      Actually tarts this service returning it.
      Returns:
      started service
    • runReadyCallbacks

      protected void runReadyCallbacks(R service)
      Runs all ready-callbacks.
      Parameters:
      service - service to be passed to callbacks
    • addReadyCallback

      protected void addReadyCallback(@NonNull O owner, @NonNull @NonNull Consumer<R> readyCallback)
      Adds a ready-callback for the given owner.
      Parameters:
      owner - owner of the callback
      readyCallback - callback to be added to the given owner
    • ready

      public void ready()
      Description copied from interface: ManagedPendingService
      Marks this service as ready for starting.
      Specified by:
      ready in interface ManagedPendingService<O,S,R>
    • isSafelyStarted

      public boolean isSafelyStarted()
      Description copied from interface: ManagedPendingService
      Checks if this service has been successfully started.
      Specified by:
      isSafelyStarted in interface ManagedPendingService<O,S,R>
      Returns:
      true if this service has been successfully started and false otherwise