Class ServiceFactory


  • public class ServiceFactory
    extends java.lang.Object
    Class which encapsulates the creation of cluster service classes based on Curator Service Discovery.

    Cleanup

    Any factory, client, or service facility that uses the ServiceFactory can make sure its resources are closed by calling addCloseable(Closeable). All closeables will be called by the ServiceFactory during the reset(boolean) method.
    • Constructor Detail

      • ServiceFactory

        public ServiceFactory()
    • Method Detail

      • registerService

        @SafeVarargs
        public static <T> org.apache.curator.x.discovery.ServiceInstance<AieServiceInfo> registerService​(java.lang.Class<T> api,
                                                                                                         java.lang.String description,
                                                                                                         int port,
                                                                                                         java.lang.Class<? extends RemoteClientApiFactory> customFactory,
                                                                                                         java.lang.Class<? extends java.util.function.Predicate<java.lang.Throwable>>... predicates)
                                                                                                  throws AttivioException
        Throws:
        AttivioException
      • getService

        public static <T> T getService​(java.lang.Class<T> clazz,
                                       long timeout,
                                       boolean logErrorOnTimeout)
                                throws AttivioException
        Throws:
        AttivioException
      • getService

        public static <T> T getService​(java.lang.Class<T> clazz,
                                       java.util.Map<java.lang.String,​java.lang.String> attributes)
                                throws AttivioException
        Throws:
        AttivioException
      • getService

        public static <T> T getService​(java.lang.Class<T> clazz,
                                       java.lang.String nodeName,
                                       long timeout)
                                throws AttivioException
        Throws:
        AttivioException
      • getService

        public static <T> T getService​(java.lang.Class<T> clazz,
                                       java.util.Map<java.lang.String,​java.lang.String> attributes,
                                       long timeout)
                                throws AttivioException
        Throws:
        AttivioException
      • getService

        public static <T> T getService​(java.lang.Class<T> clazz,
                                       java.util.Map<java.lang.String,​java.lang.String> attributes,
                                       long timeout,
                                       boolean logErrorOnTimeout)
                                throws AttivioException
        Returns an implementation of the desired service API.

        Services come in two flavors. Some services are registered by a service provider and provide services through remote client implementations. Other services are built on an external platform such as a database. The latter are referred to as client-base services since from the AIE point of view, only a client is required. In either case, this method returns a (generally proxied) implementation that provides the service API.

        The timeout parameter is applicable only to registered services. This is because client-based services are provided externally and the ServiceFactory has no way of knowing if those services are up or not. For registered services, the timeout applies to the time for that service to be registered with the system. This lets callers wait a bit (say during a startup situation) for the service to come online but not wait indefinitely.

        When the timeout is in use, the service will be checked for registration every 100ms. A timeout < 0 will wait indefinitely. Regardless of the timeout, the availability of the service will always be tried at least once. That is, a low timeout or timeout of 0 will still succeed if the service is registered or is provided by a client-based API.

        Parameters:
        clazz - the class type of the desired API
        attributes - map of attributes that must match the service, or null or an empty map if any instance is fine.
        timeout - timeout in ms
        logErrorOnTimeout - if true an ERROR message will be logged in the event of a timeout
        Returns:
        an implementation of the API or null if unavailable within the timeout.
        Throws:
        AttivioException
      • getServiceInstance

        public static <T> org.apache.curator.x.discovery.ServiceInstance<AieServiceInfo> getServiceInstance​(java.lang.Class<T> clazz,
                                                                                                            java.lang.String nodeName)
                                                                                                     throws AttivioException
        Returns:
        an instance or throw an exception
        Throws:
        AttivioException
      • addServiceListener

        public static <T> void addServiceListener​(java.lang.Class<T> clazz,
                                                  ServiceListener listener)
                                           throws AttivioException
        Adds a listener that is called whenever the service availability changes. If the service is currently available, the listener will be called.
        Parameters:
        clazz -
        listener -
        Throws:
        AttivioException
      • getFactoryParams

        public static <T> T getFactoryParams​(java.lang.Class<T> clazz)
        Returns parameters of type T if previously set or null if not set. Note, only the exact class supplied is checked; any desired interfaces implemented by clazz must be individually checked.
        Parameters:
        clazz -
        Returns:
        the parameters
      • setFactoryParams

        public static void setFactoryParams​(java.lang.Object params)
        Sets the factory parameters for the type params.getClass() and all interfaces provided by the type for this client process. There can be only one set of parameters per type.
        Parameters:
        params -
      • reset

        public static void reset​(boolean clearServices)
        Resets all local caches of service registrations, mock services, curator and zookeeper connections. All Closeables added via the addCloseable(Closeable) method are closed.
        Parameters:
        clearServices - if true, registered services list is cleared otherwise they are re-registered
      • addCloseable

        public static void addCloseable​(java.io.Closeable closeable)
        Adds the closeable to a list that will be closed upon call to the reset(boolean) method. This call is thread-safe.
        Parameters:
        closeable -