Package 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
-
-
Constructor Summary
Constructors Constructor Description AbstractServiceReference(Vertx vertx, ServiceDiscovery discovery, Record record)Creates a new instance ofAbstractServiceReference.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description <X> Xcached()Returns the service object.<X> XcachedAs(Class<X> x)GGets the service object if already retrieved.voidclose()Same asrelease(), here to implementAutoCloseable.<X> Xget()Gets the service object.<X> XgetAs(Class<X> x)Gets the service object.booleanisHolding(Object object)Checks whether or not the service reference has the given service object.protected voidonClose()Callback that let you cleanup the service object.Recordrecord()voidrelease()If the service object has been retrieved, callsonClose()and release the reference.protected abstract Tretrieve()Method to implement to retrieve the service object.
-
-
-
Constructor Detail
-
AbstractServiceReference
public AbstractServiceReference(Vertx vertx, ServiceDiscovery discovery, Record record)
Creates a new instance ofAbstractServiceReference.- Parameters:
vertx- the vert.x instancerecord- the service record
-
-
Method Detail
-
cached
public <X> X cached()
Returns the service object. If not retrieved or released, it returnsnull.- Specified by:
cachedin interfaceServiceReference- Returns:
- the cached service object,
nullif none
-
get
public <X> X get()
Gets the service object. If not retrieved, callretrieve(), otherwise returned the cached value.- Specified by:
getin interfaceServiceReference- 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. Unlikeget()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:
getAsin interfaceServiceReference- 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. Unlikecached()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:
cachedAsin interfaceServiceReference- 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:
recordin interfaceServiceReference- Returns:
- the service record.
-
release
public void release()
If the service object has been retrieved, callsonClose()and release the reference. Otherwise, does nothing.- Specified by:
releasein interfaceServiceReference
-
close
public void close()
Same asrelease(), here to implementAutoCloseable.- Specified by:
closein interfaceAutoCloseable
-
isHolding
public boolean isHolding(Object object)
Description copied from interface:ServiceReferenceChecks whether or not the service reference has the given service object.- Specified by:
isHoldingin interfaceServiceReference- Parameters:
object- the service object, must not benull- Returns:
trueif the service reference service object is equal to the given object,falseotherwise.
-
-