Class AbstractServiceReference<T>

  • Type Parameters:
    T - the type of service object
    All Implemented Interfaces:
    ServiceReference, AutoCloseable

    public abstract class AbstractServiceReference<T>
    extends Object
    implements ServiceReference
    A class to simplify the implementation of service reference. It stores the service object once retrieved. This class handles the synchronization, so callbacks are called with the monitor lock to avoid concurrent accesses.
    Author:
    Clement Escoffier
    • Field Detail

      • service

        protected T service
      • vertx

        protected final Vertx vertx
    • Method Detail

      • cached

        public <X> X cached()
        Returns the service object. If not retrieved or released, it returns null.
        Specified by:
        cached in interface ServiceReference
        Returns:
        the cached service object, null if none
      • get

        public <X> X get()
        Gets the service object. If not retrieved, call retrieve(), otherwise returned the cached value.
        Specified by:
        get in interface ServiceReference
        Returns:
        the service object
      • getAs

        public <X> X getAs​(Class<X> x)
        Gets the service object. It can be a proxy, a client or whatever object depending on the service type. Unlike get() this method let you configure the type of object you want to retrieve. This parameter must match the expected service type, and must pass the "polyglot" version of the class.
        Specified by:
        getAs in interface ServiceReference
        Type Parameters:
        X - the type of object
        Parameters:
        x - the
        Returns:
        the object to access the service
      • cachedAs

        public <X> X cachedAs​(Class<X> x)
        GGets the service object if already retrieved. It won't try to acquire the service object if not retrieved yet. Unlike cached() this method let you configure the type of object you want to retrieve. This parameter must match the expected service type, and must pass the "polyglot" version of the class.
        Specified by:
        cachedAs in interface ServiceReference
        Type Parameters:
        X - the type of object
        Parameters:
        x - the
        Returns:
        the object to access the service
      • retrieve

        protected abstract T retrieve()
        Method to implement to retrieve the service object. It can be a proxy creation, or a new client. This method is called once, then the return is cached.
        Returns:
        the service object
      • onClose

        protected void onClose()
        Callback that let you cleanup the service object. This callback is only called if the service objects has been retrieved.
      • release

        public void release()
        If the service object has been retrieved, calls onClose() and release the reference. Otherwise, does nothing.
        Specified by:
        release in interface ServiceReference
      • isHolding

        public boolean isHolding​(Object object)
        Description copied from interface: ServiceReference
        Checks whether or not the service reference has the given service object.
        Specified by:
        isHolding in interface ServiceReference
        Parameters:
        object - the service object, must not be null
        Returns:
        true if the service reference service object is equal to the given object, false otherwise.