Class AbstractService<A extends ServiceInstance,B,C extends TrafficPolicy<A,B>>

java.lang.Object
io.inverno.mod.discovery.AbstractService<A,B,C>
Type Parameters:
A - the type of service instance
B - the type of service request
C - the type of traffic policy
All Implemented Interfaces:
ManageableService<A,B,C>, Service<A,B,C>

public abstract class AbstractService<A extends ServiceInstance,B,C extends TrafficPolicy<A,B>> extends Object implements ManageableService<A,B,C>

Base Service implementation.

Since:
1.12
Author:
Jeremy Kuhn
  • Field Details

    • serviceId

      protected final ServiceID serviceId
      The service ID.
  • Constructor Details

    • AbstractService

      public AbstractService(ServiceID serviceId)

      Creates a service.

      Parameters:
      serviceId - the service ID
  • Method Details

    • getID

      public ServiceID getID()
      Description copied from interface: Service

      Returns the service ID.

      Specified by:
      getID in interface Service<A extends ServiceInstance,B,C extends TrafficPolicy<A,B>>
      Returns:
      the service ID
    • getTrafficPolicy

      public C getTrafficPolicy()
      Description copied from interface: Service

      Returns the traffic policy.

      Specified by:
      getTrafficPolicy in interface Service<A extends ServiceInstance,B,C extends TrafficPolicy<A,B>>
      Returns:
      the traffic policy
    • getInstance

      public Mono<? extends A> getInstance(B serviceRequest)
      Description copied from interface: Service

      Returns a service instance for processing the specified service request.

      Implementations should typically rely on the service traffic policy to determine which instance should be assigned to process a particular request. A typical use case is service load balancing where a service is deployed on multiple servers and requests must be distributed among these servers in a random or round robin fashion, by taking the server load into account...

      Implementations can also assign instances based on the content of the request in which case it is possible that no service instance could match the request and an empty Mono shall then be returned.

      Specified by:
      getInstance in interface Service<A extends ServiceInstance,B,C extends TrafficPolicy<A,B>>
      Parameters:
      serviceRequest - a service request
      Returns:
      a Mono emitting the instance to use to process the service request or an empty Mono if no instance matching the request could be found
    • getInstances

      public List<A> getInstances()
      Description copied from interface: ManageableService

      Returns the service instances.

      Specified by:
      getInstances in interface ManageableService<A extends ServiceInstance,B,C extends TrafficPolicy<A,B>>
      Returns:
      a list of service instances
    • refresh

      public Mono<? extends Service<A,B,C>> refresh(C trafficPolicy)

      Refreshes the service instances keeping the pre-existing instances, creating added instances and closing removed instances.

      The load balancer is eventually recreated with the refreshed list of instances.

      Specified by:
      refresh in interface Service<A extends ServiceInstance,B,C extends TrafficPolicy<A,B>>
      Parameters:
      trafficPolicy - a traffic policy
      Returns:
      a Mono emitting a refreshed service or an empty Mono if no service instance could be resolved
      See Also:
    • resolveInstances

      protected abstract Mono<Map<Integer,Supplier<A>>> resolveInstances(C trafficPolicy)

      Resolves and returns the service instances using the specified traffic policy.

      The traffic policy must be considered to uniquely identify a service instance as it might impact how a service instance is eventually created. For instance, considering a server instance, it is uniquely identified by an Inet Socket Address and the traffic policy which specifies how to connect to the server. Such identifier is typically obtained with Objects.hash(address, trafficPolicy).

      Parameters:
      trafficPolicy - a traffic policy
      Returns:
      a map with unique service instance identifiers as key and service instance provider as value
    • getLastRefreshed

      public long getLastRefreshed()
      Description copied from interface: Service

      Returns the refreshed time stamp.

      Specified by:
      getLastRefreshed in interface Service<A extends ServiceInstance,B,C extends TrafficPolicy<A,B>>
      Returns:
      an epoch time in milliseconds
    • shutdown

      public Mono<Void> shutdown()
      Description copied from interface: Service

      Shutdowns the service.

      This basically shutdowns all service instances and free resources.

      Specified by:
      shutdown in interface Service<A extends ServiceInstance,B,C extends TrafficPolicy<A,B>>
      Returns:
      a Mono which completes once the service is shutdown
    • shutdownGracefully

      public Mono<Void> shutdownGracefully()
      Description copied from interface: Service

      Gracefully shutdowns the service.

      This basically gracefully shutdowns all service instances and free resources.

      Specified by:
      shutdownGracefully in interface Service<A extends ServiceInstance,B,C extends TrafficPolicy<A,B>>
      Returns:
      a Mono which completes once the service is shutdown