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

      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.
    `protected void` | `close()` Callback that let you cleanup the service object.
    `<X> X` | `get()` Gets the service object.
    `Record` | `record()`
    `void` | `release()` If the service object has been retrieved, calls `close()` 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`
      Type Parameters:
        `X` - the type of result.
      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`
      Type Parameters:
        `X` - the type of result
      Returns:
        the service object

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

    - #### close

          protected void close()

      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 `close()` and release the reference. Otherwise, does nothing.
      Specified by:
        `release` in interface `ServiceReference`

Copyright © 2016. All rights reserved.