Class ServiceDiscoveryImpl<T>

  • All Implemented Interfaces:
    java.io.Closeable, java.lang.AutoCloseable, ServiceDiscovery<T>

    public class ServiceDiscoveryImpl<T>
    extends java.lang.Object
    implements ServiceDiscovery<T>
    A mechanism to register and query service instances using ZooKeeper
    • Constructor Detail

      • ServiceDiscoveryImpl

        public ServiceDiscoveryImpl​(org.apache.curator.framework.CuratorFramework client,
                                    java.lang.String basePath,
                                    InstanceSerializer<T> serializer,
                                    ServiceInstance<T> thisInstance,
                                    boolean watchInstances)
        Parameters:
        client - the client
        basePath - base path to store data
        serializer - serializer for instances (e.g. JsonInstanceSerializer)
        thisInstance - instance that represents the service that is running. The instance will get auto-registered
        watchInstances - if true, watches for changes to locally registered instances
    • Method Detail

      • start

        public void start()
                   throws java.lang.Exception
        The discovery must be started before use
        Specified by:
        start in interface ServiceDiscovery<T>
        Throws:
        java.lang.Exception - errors
      • close

        public void close()
                   throws java.io.IOException
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Throws:
        java.io.IOException
      • registerService

        public void registerService​(ServiceInstance<T> service)
                             throws java.lang.Exception
        Register/re-register/update a service instance
        Specified by:
        registerService in interface ServiceDiscovery<T>
        Parameters:
        service - service to add
        Throws:
        java.lang.Exception - errors
      • updateService

        public void updateService​(ServiceInstance<T> service)
                           throws java.lang.Exception
        Description copied from interface: ServiceDiscovery
        Update a service
        Specified by:
        updateService in interface ServiceDiscovery<T>
        Parameters:
        service - service to update
        Throws:
        java.lang.Exception - errors
      • internalRegisterService

        protected void internalRegisterService​(ServiceInstance<T> service)
                                        throws java.lang.Exception
        Throws:
        java.lang.Exception
      • unregisterService

        public void unregisterService​(ServiceInstance<T> service)
                               throws java.lang.Exception
        Unregister/remove a service instance
        Specified by:
        unregisterService in interface ServiceDiscovery<T>
        Parameters:
        service - the service
        Throws:
        java.lang.Exception - errors
      • queryForNames

        public java.util.Collection<java.lang.String> queryForNames()
                                                             throws java.lang.Exception
        Return the names of all known services
        Specified by:
        queryForNames in interface ServiceDiscovery<T>
        Returns:
        list of service names
        Throws:
        java.lang.Exception - errors
      • queryForInstances

        public java.util.Collection<ServiceInstance<T>> queryForInstances​(java.lang.String name)
                                                                   throws java.lang.Exception
        Return all known instances for the given service
        Specified by:
        queryForInstances in interface ServiceDiscovery<T>
        Parameters:
        name - name of the service
        Returns:
        list of instances (or an empty list)
        Throws:
        java.lang.Exception - errors
      • queryForInstance

        public ServiceInstance<T> queryForInstance​(java.lang.String name,
                                                   java.lang.String id)
                                            throws java.lang.Exception
        Return a service instance POJO
        Specified by:
        queryForInstance in interface ServiceDiscovery<T>
        Parameters:
        name - name of the service
        id - ID of the instance
        Returns:
        the instance or null if not found
        Throws:
        java.lang.Exception - errors