ZooKeeper Java示例
简单的手表客户端
为了向您介绍ZooKeeper Java API,我们在这里开发了一个非常简单的手表客户端。此ZooKeeper客户端监视ZooKeeper节点的更改并通过启动或停止程序来响应。
要求
客户有四个要求:
- 它需要作为参数:
- ZooKeeper服务的地址
- znode的名称 - 要观看的名称
- 要将输出写入的文件的名称
- 带参数的可执行文件。
- 它获取与znode关联的数据并启动可执行文件。
- 如果znode更改,则客户端将重新获取内容并重新启动可执行文件。
- 如果znode消失,客户端将终止可执行文件。
程序设计
传统上,ZooKeeper应用程序分为两个单元,一个维护连接,另一个监视数据。在此应用程序中,名为Executor的类 维护ZooKeeper连接,而名为DataMonitor的类 监视ZooKeeper树中的数据。此外,Executor包含主线程并包含执行逻辑。它负责与用户进行的少量交互,以及与作为参数传递的exectuable程序的交互,以及根据znode的状态,样本(根据要求)关闭并重新启动。
执行者类
Executor对象是示例应用程序的主要容器。它包含ZooKeeper对象DataMonitor,如上面Program Program中所述 。
// 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) {
}
}
回想一下,Executor的工作是启动和停止在命令行上传入其名称的可执行文件。它这是为了响应ZooKeeper对象触发的事件。正如您在上面的代码中所看到的,Executor将对自身的引用作为ZooKeeper构造函数中的Watcher参数传递。它还将对自身的引用传递给DataMonitor构造函数的DataMonitorListener参数。根据Executor的定义,它实现了这两个接口:
public class Executor implements Watcher, Runnable, DataMonitor.DataMonitorListener {
...
该看守接口由动物园管理员的Java API定义。ZooKeeper使用它来回传给它的容器。它只支持一种方法,process()
并且ZooKeeper使用它来传递主线程所在的通用事件,例如ZooKeeper连接或ZooKeeper会话的状态。本例中的Executor只是将这些事件转发到DataMonitor决定如何处理它们。它这样做只是为了说明按照惯例,Executor或类似Executor的对象“拥有”ZooKeeper连接,但可以将事件委托给其他事件到其他对象。它还使用此作为触发监视事件的默认通道。(稍后会详细介绍。)
public void process(WatchedEvent event) {
dm.process(event);
}
的DataMonitorListener 接口,在另一方面,是不是动物园管理员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不再存在时,需要说杀死可执行文件。
当Executor.closing()
被调用,执行器来决定是否自行关闭响应的ZooKeeper连接永久消失。
您可能已经猜到,DataMonitor是调用这些方法的对象,以响应ZooKeeper状态的变化。
这是Executor的实现 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,设置监视,并将对self(this
)的引用作为完成回调对象传递。从这个意义上说,它可以解决问题,因为真正的处理是在手表被触发时发生的。
注意
不要将完成回调与手表回调混淆。在
ZooKeeper.exists()
完成回调,这恰好是该方法StatCallback.processResult()
中在DataMonitor对象实现,当异步调用手表的设置(通过操作ZooKeeper.exists()
)在服务器上完成。
另一方面,触发监视会向Executor对象发送一个事件,因为Executor注册为ZooKeeper对象的Watcher。
另外,您可能会注意到DataMonitor也可以将自己注册为此特定监视事件的观察者。这是ZooKeeper 3.0.0的新功能(多个Watchers的支持)。但是,在此示例中,DataMonitor未注册为Watcher。
当ZooKeeper.exists()
操作在服务器上完成时,ZooKeeper API在客户端上调用此完成回调:
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的exists()回调。注意,它不必对getData调用执行任何异常处理,因为它有可能导致错误的任何ZooKeeper.getData()
事件的监视:如果节点在调用之前被删除,则ZooKeeper.exists()
触发器设置的监视事件是回调; 如果存在通信错误,则在连接恢复时会触发连接监视事件。
最后,请注意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);
}
}
如果客户端ZooKeeper库可以在会话到期(Expired事件)之前重新建立到ZooKeeper的通信通道(SyncConnected事件),则所有会话的监视将自动与服务器重新建立(手表的自动重置是新的ZooKeeper 3.0.0)。有关详细信息,请参阅 程序员指南中的ZooKeeper手表。在此函数中稍微低一点,当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;
}
}
}