Class ServiceStateTracker.ServiceAvailabilityTracker
- java.lang.Object
-
- de.aristaflow.adept2.core.registry.cluster.ServiceStateTracker.ServiceAvailabilityTracker
-
- All Implemented Interfaces:
Runnable
- Enclosing class:
- ServiceStateTracker
public final class ServiceStateTracker.ServiceAvailabilityTracker extends Object implements Runnable
A tracker for the availability of a specificADEPT2Serviceidentified by itsURI[]. The service is polled regularly. If its availability changes (detected viaADEPT2Service.ping()) the registered listeners will be notified.
Note that polling will only take place if at least one listener is registered.
-
-
Field Summary
Fields Modifier and Type Field Description protected booleaninterruptibleWhether the executing thread may be interrupted.protected Collection<ClientService.ServiceStateListener>listenersThe listeners which are registered for changes in the service availability.protected LockrunLockA lock to prevent one tracker to run concurrently (for the very same service).protected URI[]serviceThe service of which to check its availability.protected Class<? extends ADEPT2Service>serviceClassThe interface of the service.
-
Constructor Summary
Constructors Modifier Constructor Description protectedServiceAvailabilityTracker(URI[] service, Class<? extends ADEPT2Service> serviceClass)Creates a new service availability tracker for the designated service.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanaddListener(ClientService.ServiceStateListener listener)Adds the designated listener for changes in the availability of ourADEPT2Service.booleaninterrupt()Stops the service availability tracking by cancelling the future and interrupting the executing thread if allowed.booleanremoveServiceStateListener(ClientService.ServiceStateListener listener)Removes the designated listener so that it is no longer notified for changes in the availability of ourADEPT2Service.voidrun()protected Triple<Long,Long,ADEPT2Service>serviceAvailable()Checks whether the platform and the service are available.
-
-
-
Field Detail
-
runLock
protected final Lock runLock
A lock to prevent one tracker to run concurrently (for the very same service). Tracking different services concurrently is fine.
We need a lock (instead ofsynchronized) to allow for interrupts.
-
service
protected final URI[] service
The service of which to check its availability.
-
serviceClass
protected final Class<? extends ADEPT2Service> serviceClass
The interface of the service.
-
listeners
protected final Collection<ClientService.ServiceStateListener> listeners
The listeners which are registered for changes in the service availability.
-
interruptible
protected boolean interruptible
Whether the executing thread may be interrupted.
To prevent inconsistent states, the current thread is not interruptible while notifying the listeners.
-
-
Constructor Detail
-
ServiceAvailabilityTracker
protected ServiceAvailabilityTracker(URI[] service, Class<? extends ADEPT2Service> serviceClass)
Creates a new service availability tracker for the designated service.- Parameters:
service- The service of which to check its availability.serviceClass- The interface of the service.
-
-
Method Detail
-
serviceAvailable
protected Triple<Long,Long,ADEPT2Service> serviceAvailable()
Checks whether the platform and the service are available. If so, the start time of (a service representing) the platform, the start time of the service and the service instance itself will be returned. The start time will beLong.MIN_VALUEif the platform or the service are unavailable. If the platform is unavailable, both start times will beLong.MIN_VALUEand the service instance will benull.- Returns:
- The start time of (a service representing) the platform, of the service and the service instance itself.
-
interrupt
public boolean interrupt()
Stops the service availability tracking by cancelling the future and interrupting the executing thread if allowed. While notifying the registered listeners, interrupts will be refused.- Returns:
- Whether cancelling the future was successful.
-
addListener
public boolean addListener(ClientService.ServiceStateListener listener)
Adds the designated listener for changes in the availability of ourADEPT2Service.- Parameters:
listener- The listener which to notify in case the availability of ourADEPT2Servicechanges.- Returns:
- Whether our
ADEPT2Serviceis currently available.
-
removeServiceStateListener
public boolean removeServiceStateListener(ClientService.ServiceStateListener listener)
Removes the designated listener so that it is no longer notified for changes in the availability of ourADEPT2Service. If this is the last listener, the availability check will be disabled.- Parameters:
listener- The listener which to remove.- Returns:
- Whether the designated listener was the last listener.
-
-