21 lines
5.4 KiB
HTML
21 lines
5.4 KiB
HTML
<html><head>
|
|
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
|
|
<title>79. Spring Cloud Zookeeper Dependency Watcher</title><link rel="stylesheet" type="text/css" href="css/manual-multipage.css"><meta name="generator" content="DocBook XSL Stylesheets V1.79.1"><link rel="home" href="multi_spring-cloud.html" title="Spring Cloud"><link rel="up" href="multi__spring_cloud_zookeeper.html" title="Part X. Spring Cloud Zookeeper"><link rel="prev" href="multi_spring-cloud-zookeeper-dependencies.html" title="78. Zookeeper Dependencies"><link rel="next" href="multi_spring-cloud-zookeeper-config.html" title="80. Distributed Configuration with Zookeeper"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">79. Spring Cloud Zookeeper Dependency Watcher</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="multi_spring-cloud-zookeeper-dependencies.html">Prev</a> </td><th width="60%" align="center">Part X. Spring Cloud Zookeeper</th><td width="20%" align="right"> <a accesskey="n" href="multi_spring-cloud-zookeeper-config.html">Next</a></td></tr></table><hr></div><div class="chapter"><div class="titlepage"><div><div><h2 class="title"><a name="spring-cloud-zookeeper-dependency-watcher" href="#spring-cloud-zookeeper-dependency-watcher"></a>79. Spring Cloud Zookeeper Dependency Watcher</h2></div></div></div><p>The Dependency Watcher mechanism lets you register listeners to your dependencies. The
|
|
functionality is, in fact, an implementation of the <code class="literal">Observator</code> pattern. When a
|
|
dependency changes, its state (to either UP or DOWN), some custom logic can be applied.</p><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="_activating_2" href="#_activating_2"></a>79.1 Activating</h2></div></div></div><p>Spring Cloud Zookeeper Dependencies functionality needs to be enabled for you to use the
|
|
Dependency Watcher mechanism.</p></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="_registering_a_listener" href="#_registering_a_listener"></a>79.2 Registering a Listener</h2></div></div></div><p>To register a listener, you must implement an interface called
|
|
<code class="literal">org.springframework.cloud.zookeeper.discovery.watcher.DependencyWatcherListener</code> and
|
|
register it as a bean. The interface gives you one method:</p><pre class="programlisting"><span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">void</span> stateChanged(String dependencyName, DependencyState newState);</pre><p>If you want to register a listener for a particular dependency, the <code class="literal">dependencyName</code> would
|
|
be the discriminator for your concrete implementation. <code class="literal">newState</code> provides you with
|
|
information about whether your dependency has changed to <code class="literal">CONNECTED</code> or <code class="literal">DISCONNECTED</code>.</p></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="spring-cloud-zookeeper-dependency-watcher-presence-checker" href="#spring-cloud-zookeeper-dependency-watcher-presence-checker"></a>79.3 Using the Presence Checker</h2></div></div></div><p>Bound with the Dependency Watcher is the functionality called Presence Checker. It lets
|
|
you provide custom behavior when your application boots, to react according to the state
|
|
of your dependencies.</p><p>The default implementation of the abstract
|
|
<code class="literal">org.springframework.cloud.zookeeper.discovery.watcher.presence.DependencyPresenceOnStartupVerifier</code>
|
|
class is the
|
|
<code class="literal">org.springframework.cloud.zookeeper.discovery.watcher.presence.DefaultDependencyPresenceOnStartupVerifier</code>,
|
|
which works in the following way.</p><div class="orderedlist"><ol class="orderedlist" type="1"><li class="listitem">If the dependency is marked us <code class="literal">required</code> and is not in Zookeeper, when your application
|
|
boots, it throws an exception and shuts down.</li><li class="listitem">If the dependency is not <code class="literal">required</code>, the
|
|
<code class="literal">org.springframework.cloud.zookeeper.discovery.watcher.presence.LogMissingDependencyChecker</code>
|
|
logs that the dependency is missing at the <code class="literal">WARN</code> level.</li></ol></div><p>Because the <code class="literal">DefaultDependencyPresenceOnStartupVerifier</code> is registered only when there is
|
|
no bean of type <code class="literal">DependencyPresenceOnStartupVerifier</code>, this functionality can be
|
|
overridden.</p></div></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="multi_spring-cloud-zookeeper-dependencies.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="multi__spring_cloud_zookeeper.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="multi_spring-cloud-zookeeper-config.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">78. Zookeeper Dependencies </td><td width="20%" align="center"><a accesskey="h" href="multi_spring-cloud.html">Home</a></td><td width="40%" align="right" valign="top"> 80. Distributed Configuration with Zookeeper</td></tr></table></div></body></html> |