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> X
cached()
Returns the service object.<X> X
cachedAs(Class<X> x)
GGets the service object if already retrieved.void
close()
Same asrelease()
, here to implementAutoCloseable
.<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, callsonClose()
and release the reference.protected abstract T
retrieve()
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:
cached
in interfaceServiceReference
- Returns:
- the cached service object,
null
if none
-
get
public <X> X get()
Gets the service object. If not retrieved, callretrieve()
, otherwise returned the cached value.- Specified by:
get
in 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:
getAs
in 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:
cachedAs
in 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:
record
in 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:
release
in interfaceServiceReference
-
close
public void close()
Same asrelease()
, here to implementAutoCloseable
.- Specified by:
close
in interfaceAutoCloseable
-
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 interfaceServiceReference
- Parameters:
object
- the service object, must not benull
- Returns:
true
if the service reference service object is equal to the given object,false
otherwise.
-
-