The Dependency Watcher mechanism allows you to register listeners to your dependencies. The functionality is in fact an implementation of the Observator
pattern. When a dependency changes
its state (UP or DOWN) then some custom logic can be applied.
Spring Cloud Zookeeper Dependencies functionality needs to be enabled to profit from Dependency Watcher mechanism.
In order to register a listener you have to implement an interface org.springframework.cloud.zookeeper.discovery.watcher.DependencyWatcherListener
and register it as a bean.
The interface gives you one method:
void stateChanged(String dependencyName, DependencyState newState);
If you want to register a listener for a particular dependency then the dependencyName
would be the discriminator for your concrete implementation. newState
will provide you with information
whether your dependency has changed to CONNECTED
or DISCONNECTED
.
Bound with Dependency Watcher is the functionality called Presence Checker. It allows you to provide custom behaviour upon booting of your application to react accordingly to the state of your dependencies.
The default implementation of the abstract org.springframework.cloud.zookeeper.discovery.watcher.presence.DependencyPresenceOnStartupVerifier
class is the
org.springframework.cloud.zookeeper.discovery.watcher.presence.DefaultDependencyPresenceOnStartupVerifier
which works in the following way.
required
and it’s not in Zookeeper then upon booting your application will throw an exception and shutdownrequired
the org.springframework.cloud.zookeeper.discovery.watcher.presence.LogMissingDependencyChecker
will log that application is missing at WARN
levelThe functionality can be overriden since the DefaultDependencyPresenceOnStartupVerifier
is registered only when there is no bean of DependencyPresenceOnStartupVerifier
.