ZooKeeper Java Example

原链接http://zookeeper.apache.org/doc/trunk/javaExample.html

一个简单的监控客户端

为了向你介绍zookeeper的Java API,在这里我们开发了一个简单的监控客户端。zookeeper客户端通过监控一个zookeeper节点的状态改变来使一个程序的启动和关闭。

必要条件
客户端需要满足以下四点
    接受4个参数
        zookeeper服务的地址
        znode的名字-需要监控的那个节点
        写输出的文件名
        一个带参数的可执行程序

Program Design
一般的,zookeeper被分成两个模块,一个维持连接,另一个监控数据。在这个程序中,维持连接的类叫Executor,DataMonitor监控zookeeper树上的数据。同时,Executor包含主线程和执行逻辑。他事无巨细的负责用户交互。也负责你使用参数传入的可执行程序,还有根据znode的状态来关闭和重启样例程序。

Executor类

Executor对象是样例程序的原始容器。他包含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) {
        }
    }

回顾一下,Executor的工作是用来启动和关闭那个可执行程序(你使用命令行参数传入名称的那个)。它通过响应zookeeper对象激发的事件来完成这些工作。正像以上你看到的代码,Executor向zookeeper构造器传递了一个自身的引用作为监视器的参数。它同时也向DataMonitor构造器传递了一个自身的引用作为数据监视器的参数。每一个Executor的定义,都实现了所有这些接口。

Watcher接口定义在zookeeper Java API里。zookeeper使用它和它的拥有者(原文是container,作者本意应该就是持有Watcher接口的容器,也即实现Watcher接口的类)通讯。它只支持一个方法,process(),ZooKeeper 使用它来交互一些主线程感兴趣的一般事件,例如zookeeper连接状态或zookeeper会话。在这个例子中Executor只是简单将这些事件转发到DataMonitor并由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()方法被调用的时候,Executor根据需要决定是否启动或关闭。回想必要条件中所述当znode不存在时杀死那个可执行程序。


当Executor.closing()被调用的时候,Executor决定是否关闭自己来响应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 kicks things off in the constructor with。
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);
}
调用ZooKeeper.exists()来检查znode是否存在,设置一个监视器。同时传递一个他自身的引用作为完成时的回调对象。

注意
不要把完成回调和监控器回调弄混。ZooKeeper.exists()完成回调,它发生在实现在DataMonitor的方法StatCallback.processResult(),被调用当异步的设置监视器操作(由ZooKeeper.exists())由server完成时。

监视器的触发,在另一方面,向Executor发送一个事件,只要Executor向zookeeper对象注册一个监视器对象。

作为一个旁白,你可能注意到DataMonitor也可以将自己注册为监视器为了一个特别的监视事件。这是zooke3.0.0(支持多个触发器)的新特性;在这个例子中,然而,DataMonitor没有注册为一个监视器。

当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);
        prevData = b;
    }
}
代码首先检查znode存在性的错误码,致命错误,还有可恢复错误。如果节点存在,它就从znode上获取数据,如果状态改变就调用exists()回调Executor。注意不必为getData调用进行任何错误处理因为拥有任何可能导致一个错误的监视器。如果在调用ZooKeeper.getData()之前节点删除了,由ZooKeeper.exists()设置的一个监控事件触发一个回调;如果这里有一个通讯错误,当连接恢复时,连接监控事件激发。

最后,注意DataMonitor怎么处理监控事件。

如果在会话失效以前客户端zookeeper库能够和ZooKeeper建立连接通道。则所有的会话监控器会与服务自动重新建立(自动重置监控器是zookeeper3.0.0里的一个新特性)。欲了解更多请在开发者向导中查看zookeeper 监控器一节。在这个函数的底部,当DataMonitor想要获取一个节点的事件,它调用ZooKeeper.exists()来查找发生了什么。

源代码列表

Executor.java

    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);
        }
    }

完整代码列表

/**
 * 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;
        }
    }
}


你可能感兴趣的:(分布式,java)