On this page
ZooKeeper Java 示例
一个简单的监视 Client 端
为了向您介绍 ZooKeeper Java API,我们在这里开发了一个非常简单的监视 Client 端。该 ZooKeeperClient 端监视 ZooKeeper 节点的更改并通过启动或停止程序来响应。
Requirements
Client 有四个要求:
它作为参数:
ZooKeeper 服务的地址
znode 的名称-要监视的 znode 的名称
要将输出写入的文件名
带有参数的可执行文件。
它获取与 znode 关联的数据并启动可执行文件。
如果 znode 更改,则 Client 端将重新获取内容并重新启动可执行文件。
如果 znode 消失,则 Client 端将杀死可执行文件。
Program Design
按照惯例,ZooKeeper 应用程序分为两个单元,一个单元维护连接,另一个单元监视数据。在此应用程序中,称为 Executor 的类维护 ZooKeeper 连接,而名为 DataMonitor 的类监视 ZooKeeper 树中的数据。另外,执行程序包含主线程并包含执行逻辑。它负责的是几乎没有用户交互,以及与作为参数传递的可执行程序之间的交互,以及根据 znode 的状态(根据要求)关闭并重新启动的示例。
Actuator 类
Executor 对象是示例应用程序的主要容器。如Program Design所述,它同时包含 ZooKeeper 对象和 DataMonitor 。
// from the Executor class...
public static void main(String[] args) {
if (args.length < 4) {
System.err
.println("USAGE: Executor hostPort znode filename program [args ...]");
System.exit(2);
}
String hostPort = args[0];
String znode = args[1];
String filename = args[2];
String exec[] = new String[args.length - 3];
System.arraycopy(args, 3, exec, 0, exec.length);
try {
new Executor(hostPort, znode, filename, exec).run();
} catch (Exception e) {
e.printStackTrace();
}
}
public Executor(String hostPort, String znode, String filename,
String exec[]) throws KeeperException, IOException {
this.filename = filename;
this.exec = exec;
zk = new ZooKeeper(hostPort, 3000, this);
dm = new DataMonitor(zk, znode, null, this);
}
public void run() {
try {
synchronized (this) {
while (!dm.dead) {
wait();
}
}
} catch (InterruptedException e) {
}
}
回忆 Actuator 的工作是启动和停止您在命令行中 Importing 名称的可执行文件。这样做是为了响应 ZooKeeper 对象触发的事件。如您在上面的代码中看到的,Actuator 将对自身的引用作为 ZooKeeper 构造函数中的 Watcher 参数传递。它还将对自身的引用作为 DataMonitorListener 参数传递给 DataMonitor 构造函数。根据执行程序的定义,它实现以下两个接口:
public class Executor implements Watcher, Runnable, DataMonitor.DataMonitorListener {
...
Watcher **接口由 ZooKeeper Java API 定义。 ZooKeeper 使用它来通讯回其容器。它仅支持一种方法process()
,并且 ZooKeeper 使用它来通信将插入主线程的一般事件,例如 ZooKeeper 连接或 ZooKeeper 会话的状态。在此示例中,执行程序将这些事件转发至 DataMonitor 决定如何处理它们。这样做只是为了说明这一点,按照惯例,Executor 或某些类似于 Executor 的对象“拥有” ZooKeeper 连接,但是可以将事件委托给其他事件给其他对象。它还将其用作触发监视事件的默认通道。 (稍后对此有更多介绍.)
public void process(WatchedEvent event) {
dm.process(event);
}
另一方面,“ DataMonitorListener”接口不是 ZooKeeper API 的一部分。这是一个完全自定义的界面,专门为此示例应用程序设计。 DataMonitor 对象使用它来通讯回其容器,该容器也是 Executor 对象。DataMonitorListener 接口如下所示:
public interface DataMonitorListener {
/**
* The existence status of the node has changed.
*/
void exists(byte data[]);
/**
* The ZooKeeper session is no longer valid.
*
* @param rc
* the ZooKeeper reason code
*/
void closing(int rc);
}
此接口在 DataMonitor 类中定义,并在 Executor 类中实现。调用Executor.exists()
时,执行程序将根据要求决定是启动还是关闭。回想一下,当 znode 不再* exist *存在时,require 要求杀死可执行文件。
当调用Executor.closing()
时,Actuator 决定是否关闭自身以响应 ZooKeeper 连接永久消失。
您可能已经猜到了,DataMonitor 是调用这些方法的对象,以响应 ZooKeeper 状态的变化。
这是 Actuator 对DataMonitorListener.exists()
和DataMonitorListener.closing
的实现:
public void exists( byte[] data ) {
if (data == null) {
if (child != null) {
System.out.println("Killing process");
child.destroy();
try {
child.waitFor();
} catch (InterruptedException e) {
}
}
child = null;
} else {
if (child != null) {
System.out.println("Stopping child");
child.destroy();
try {
child.waitFor();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
try {
FileOutputStream fos = new FileOutputStream(filename);
fos.write(data);
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
System.out.println("Starting child");
child = Runtime.getRuntime().exec(exec);
new StreamWriter(child.getInputStream(), System.out);
new StreamWriter(child.getErrorStream(), System.err);
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void closing(int rc) {
synchronized (this) {
notifyAll();
}
}
DataMonitor 类
DataMonitor 类具有 ZooKeeper 逻辑的内容。它主要是异步的和事件驱动的。 DataMonitor 通过以下方式在构造函数中启动:
public DataMonitor(ZooKeeper zk, String znode, Watcher chainedWatcher,
DataMonitorListener listener) {
this.zk = zk;
this.znode = znode;
this.chainedWatcher = chainedWatcher;
this.listener = listener;
// Get things started by checking if the node exists. We are going
// to be completely event driven
对ZooKeeper.exists()
的调用检查 znode 的存在,设置监视,并将对自身的引用(this
)传递为完成回调对象。从这个意义上说,它开始了,因为 true 的处理过程是在触发手表时发生的。
Note
Note
不要将完成回调与监视回调混淆。当在服务器上完成监视操作的异步设置(通过ZooKeeper.exists()
)时,将调用ZooKeeper.exists()
完成回调(恰好是在 DataMonitor 对象中实现的方法StatCallback.processResult()
)。
另一方面,手表的触发将事件发送到* Executor *对象,因为执行者已注册为 ZooKeeper 对象的观察者。
顺便说一句,您可能会注意到,DataMonitor 也可以将自己注册为该特定监视事件的监视者。这是 ZooKeeper 3.0.0(多个监视程序的支持)的新增功能。但是,在此示例中,DataMonitor 不会注册为观察者。
当服务器上的ZooKeeper.exists()
操作完成时,ZooKeeper API 会在 Client 端上调用以下完成回调:
public void processResult(int rc, String path, Object ctx, Stat stat) {
boolean exists;
switch (rc) {
case Code.Ok:
exists = true;
break;
case Code.NoNode:
exists = false;
break;
case Code.SessionExpired:
case Code.NoAuth:
dead = true;
listener.closing(rc);
return;
default:
// Retry errors
zk.exists(znode, true, this, null);
return;
}
byte b[] = null;
if (exists) {
try {
b = zk.getData(znode, false, null);
} catch (KeeperException e) {
// We don't need to worry about recovering now. The watch
// callbacks will kick off any exception handling
e.printStackTrace();
} catch (InterruptedException e) {
return;
}
}
if ((b == null && b != prevData)
|| (b != null && !Arrays.equals(prevData, b))) {
listener.exists(b);</emphasis>
prevData = b;
}
}
该代码首先检查错误代码中是否存在 znode,致命错误和可恢复错误。如果文件(或 znode)存在,它将从 znode 获取数据,然后在状态发生变化时调用 Executor 的 exist()回调。注意,它不必对 getData 调用进行任何异常处理,因为它可以监视任何可能导致错误的事件:如果节点在调用ZooKeeper.getData()
之前被删除,则由ZooKeeper.exists()
设置的 watch 事件触发回调;如果出现通信错误,则在恢复连接时将触发连接监视事件。
最后,请注意 DataMonitor 如何处理监视事件:
public void process(WatchedEvent event) {
String path = event.getPath();
if (event.getType() == Event.EventType.None) {
// We are are being told that the state of the
// connection has changed
switch (event.getState()) {
case SyncConnected:
// In this particular example we don't need to do anything
// here - watches are automatically re-registered with
// server and any watches triggered while the client was
// disconnected will be delivered (in order of course)
break;
case Expired:
// It's all over
dead = true;
listener.closing(KeeperException.Code.SessionExpired);
break;
}
} else {
if (path != null && path.equals(znode)) {
// Something has changed on the node, let's find out
zk.exists(znode, true, this, null);
}
}
if (chainedWatcher != null) {
chainedWatcher.process(event);
}
}
如果 Client 端 ZooKeeper 库可以在会话期满(Expired 事件)之前重新构建与 ZooKeeper 的通信通道(SyncConnected 事件),则所有会话的手表将自动与服务器重新构建(手表的自动重置是新的 ZooKeeper 3.0.0)。有关更多信息,请参见程序员指南中的ZooKeeper Watches。在此函数中,当 DataMonitor 获取 znode 事件时,它会调用ZooKeeper.exists()
找出发生了什么更改。
完整的源清单
Executor.java
/**
* A simple example program to use DataMonitor to start and
* stop executables based on a znode. The program watches the
* specified znode and saves the data that corresponds to the
* znode in the filesystem. It also starts the specified program
* with the specified arguments when the znode exists and kills
* the program if the znode goes away.
*/
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
public class Executor
implements Watcher, Runnable, DataMonitor.DataMonitorListener
{
String znode;
DataMonitor dm;
ZooKeeper zk;
String filename;
String exec[];
Process child;
public Executor(String hostPort, String znode, String filename,
String exec[]) throws KeeperException, IOException {
this.filename = filename;
this.exec = exec;
zk = new ZooKeeper(hostPort, 3000, this);
dm = new DataMonitor(zk, znode, null, this);
}
/**
* @param args
*/
public static void main(String[] args) {
if (args.length < 4) {
System.err
.println("USAGE: Executor hostPort znode filename program [args ...]");
System.exit(2);
}
String hostPort = args[0];
String znode = args[1];
String filename = args[2];
String exec[] = new String[args.length - 3];
System.arraycopy(args, 3, exec, 0, exec.length);
try {
new Executor(hostPort, znode, filename, exec).run();
} catch (Exception e) {
e.printStackTrace();
}
}
/***************************************************************************
* We do process any events ourselves, we just need to forward them on.
*
* @see org.apache.zookeeper.Watcher#process(org.apache.zookeeper.proto.WatcherEvent)
*/
public void process(WatchedEvent event) {
dm.process(event);
}
public void run() {
try {
synchronized (this) {
while (!dm.dead) {
wait();
}
}
} catch (InterruptedException e) {
}
}
public void closing(int rc) {
synchronized (this) {
notifyAll();
}
}
static class StreamWriter extends Thread {
OutputStream os;
InputStream is;
StreamWriter(InputStream is, OutputStream os) {
this.is = is;
this.os = os;
start();
}
public void run() {
byte b[] = new byte[80];
int rc;
try {
while ((rc = is.read(b)) > 0) {
os.write(b, 0, rc);
}
} catch (IOException e) {
}
}
}
public void exists(byte[] data) {
if (data == null) {
if (child != null) {
System.out.println("Killing process");
child.destroy();
try {
child.waitFor();
} catch (InterruptedException e) {
}
}
child = null;
} else {
if (child != null) {
System.out.println("Stopping child");
child.destroy();
try {
child.waitFor();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
try {
FileOutputStream fos = new FileOutputStream(filename);
fos.write(data);
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
System.out.println("Starting child");
child = Runtime.getRuntime().exec(exec);
new StreamWriter(child.getInputStream(), System.out);
new StreamWriter(child.getErrorStream(), System.err);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
DataMonitor.java
/**
* A simple class that monitors the data and existence of a ZooKeeper
* node. It uses asynchronous ZooKeeper APIs.
*/
import java.util.Arrays;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.AsyncCallback.StatCallback;
import org.apache.zookeeper.KeeperException.Code;
import org.apache.zookeeper.data.Stat;
public class DataMonitor implements Watcher, StatCallback {
ZooKeeper zk;
String znode;
Watcher chainedWatcher;
boolean dead;
DataMonitorListener listener;
byte prevData[];
public DataMonitor(ZooKeeper zk, String znode, Watcher chainedWatcher,
DataMonitorListener listener) {
this.zk = zk;
this.znode = znode;
this.chainedWatcher = chainedWatcher;
this.listener = listener;
// Get things started by checking if the node exists. We are going
// to be completely event driven
zk.exists(znode, true, this, null);
}
/**
* Other classes use the DataMonitor by implementing this method
*/
public interface DataMonitorListener {
/**
* The existence status of the node has changed.
*/
void exists(byte data[]);
/**
* The ZooKeeper session is no longer valid.
*
* @param rc
* the ZooKeeper reason code
*/
void closing(int rc);
}
public void process(WatchedEvent event) {
String path = event.getPath();
if (event.getType() == Event.EventType.None) {
// We are are being told that the state of the
// connection has changed
switch (event.getState()) {
case SyncConnected:
// In this particular example we don't need to do anything
// here - watches are automatically re-registered with
// server and any watches triggered while the client was
// disconnected will be delivered (in order of course)
break;
case Expired:
// It's all over
dead = true;
listener.closing(KeeperException.Code.SessionExpired);
break;
}
} else {
if (path != null && path.equals(znode)) {
// Something has changed on the node, let's find out
zk.exists(znode, true, this, null);
}
}
if (chainedWatcher != null) {
chainedWatcher.process(event);
}
}
public void processResult(int rc, String path, Object ctx, Stat stat) {
boolean exists;
switch (rc) {
case Code.Ok:
exists = true;
break;
case Code.NoNode:
exists = false;
break;
case Code.SessionExpired:
case Code.NoAuth:
dead = true;
listener.closing(rc);
return;
default:
// Retry errors
zk.exists(znode, true, this, null);
return;
}
byte b[] = null;
if (exists) {
try {
b = zk.getData(znode, false, null);
} catch (KeeperException e) {
// We don't need to worry about recovering now. The watch
// callbacks will kick off any exception handling
e.printStackTrace();
} catch (InterruptedException e) {
return;
}
}
if ((b == null && b != prevData)
|| (b != null && !Arrays.equals(prevData, b))) {
listener.exists(b);
prevData = b;
}
}
}