io.vertx.servicediscovery.types

## Class AbstractServiceReference<T>

- java.lang.Object
- - io.vertx.servicediscovery.types.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 Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `protected T` | `service`
    `protected Vertx` | `vertx`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `AbstractServiceReference(Vertx vertx, ServiceDiscovery discovery, Record record)` Creates a new instance of `AbstractServiceReference`.

  - ### Method Summary

    All Methods Instance Methods Abstract Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `<X> X` | `cached()` Returns the service object.
    `<X> X` | `cachedAs(Class<X> x)` GGets the service object if already retrieved.
    `void` | `close()` Same as `release()`, here to implement `AutoCloseable`.
    `<X> X` | `get()` Gets the service object.
    `<X> X` | `getAs(Class<X> x)` Gets the service object.
    `boolean` | `isHolding(Object object)` Checks whether or not the service reference has the given service object.
    `protected void` | `onClose()` Callback that let you cleanup the service object.
    `Record` | `record()`
    `void` | `release()` If the service object has been retrieved, calls `onClose()` and release the reference.
    `protected abstract T` | `retrieve()` Method to implement to retrieve the service object.

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Field Detail

    - #### service

          protected T service

    - #### vertx

          protected final Vertx vertx

  - ### Constructor Detail

    - #### AbstractServiceReference

          public AbstractServiceReference(Vertx vertx,
                                          ServiceDiscovery discovery,
                                          Record record)

      Creates a new instance of `AbstractServiceReference`.
      Parameters:
        `vertx` - the vert.x instance
        `record` - the service record

  - ### 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.

    - #### record

          public Record record()

      Specified by:
        `record` in interface `ServiceReference`
      Returns:
        the service record.

    - #### 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`

    - #### close

          public void close()

      Same as `release()`, here to implement `AutoCloseable`.
      Specified by:
        `close` in interface `AutoCloseable`

    - #### 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.

Copyright © 2017. All rights reserved.