ソースを参照

ZOOKEEPER-193. update java example doc to compile with latest zookeeper

git-svn-id: https://svn.apache.org/repos/asf/hadoop/zookeeper/trunk@705346 13f79535-47bb-0310-9956-ffa450edef68
Patrick D. Hunt 16 年 前
コミット
b8c2b82127

+ 3 - 0
CHANGES.txt

@@ -35,6 +35,9 @@ Backward compatibile changes:
 
   BUGFIXES: 
 
+  ZOOKEEPER-193. update java example doc to compile with latest zookeeper
+  (phunt)
+
   ZOOKEEPER-187. CreateMode api docs missing (phunt)
 
   ZOOKEEPER-186. add new "releasenotes.xml" to forrest documentation

+ 83 - 99
docs/javaExample.html

@@ -271,47 +271,44 @@ document.write("Last Published: " + document.lastModified);
     both the <strong>ZooKeeper</strong> object, <strong>DataMonitor</strong>, as described above in 
     <a href="#sc_design">Program Design</a>.  </p>
 <pre class="code">
-// from the Executor class...
+    // from the Executor class...
     
-public static void main(String[] args) {
-    if (args.length &lt; 4) {
-        System.err
-                .println("USAGE: Executor hostPort znode filename program [args ...]");
-        System.exit(2);
+    public static void main(String[] args) {
+        if (args.length &lt; 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();
+        }
     }
-    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 {
-        Executor theExectutor = new Executor(hostPort, znode, filename, exec);
-	theExectutor.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 Executor(String hostPort, String znode, String filename,
-    String exec[]) throws KeeperException, IOException {
-    this.filename = filename;
-    this.exec = exec;
-    
-    //create a new zookeeper object, passing a self-reference in a the Watcher
-    zk = new ZooKeeper(hostPort, 3000, this);
-    dm = new DataMonitor(zk, znode, null, this);
-}
-   
-public void run() {
-    try {
-        synchronized (this) {
-            while (!dm.dead) {
-                wait();
+
+    public void run() {
+        try {
+            synchronized (this) {
+                while (!dm.dead) {
+                    wait();
+                }
             }
+        } catch (InterruptedException e) {
         }
-    } catch (InterruptedException e) {    
     }
-}
 </pre>
 <p>
     Recall that the Executor's job is to starts and stop the executable whose name you pass in on the command line. 
@@ -321,7 +318,7 @@ public void run() {
     interfaces:
     </p>
 <pre class="code">
-public class Executor implements Watcher, Runnable, DataMonitorListener {
+public class Executor implements Watcher, Runnable, DataMonitor.DataMonitorListener {
 ...
     </pre>
 <p>The <strong>Watcher</strong> interface is defined by the ZooKeeper Java API.
@@ -331,9 +328,9 @@ public class Executor implements Watcher, Runnable, DataMonitorListener {
     the point that, by convention, the Executor or some Executor-like object "owns" the ZooKeeper connection, but it is free to delegate the events to other
     events to other objects. It also uses this as the default channel on which to fire watch events. (More on this later.)</p>
 <pre class="code">
-public void process(WatcherEvent event) {
-    dm.process(event);
-}
+    public void process(WatchedEvent event) {
+        dm.process(event);
+    }
 </pre>
 <p>The <strong>DataMonitorListener</strong> 
     interface, on the other hand, is not part of the the ZooKeeper API. It is a completely custom interface, 
@@ -512,33 +509,33 @@ the connection comes back up.
 </p>
 <p>Finally, notice how DataMonitor processes watch events: </p>
 <pre class="code">
-public void process(WatcherEvent event) {
-    String path = event.getPath();
-    if (event.getType() == Watcher.Event.EventNone) {
-        // We are are being told that the state of the
-        // connection has changed
-        switch (event.getState()) {
-        case Event.KeeperStateSyncConnected:
-            // Everything is happy. Lets kick things off
-            // again by checking the existence of the znode
-            zk.exists(znode, true, this, null);
-            break;
-        case Event.KeeperStateExpired:
-            // It's all over
-            dead = true;
-            listener.closing(KeeperException.Code.SessionExpired);
-            break;
+    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:
+                // Everything is happy. Lets kick things off
+                // again by checking the existence of the znode
+                zk.exists(znode, true, this, null);
+                break;
+            case Expired:
+                // It's all over
+                dead = true;
+                listener.closing(KeeperException.Code.SessionExpired);
+                break;
+            }
+        } else {
+            if (path != null &amp;&amp; path.equals(znode)) {
+                // Something has changed on the node, let's find out
+                zk.exists(znode, true, this, null);
+            }
         }
-    } else {
-        if (path != null &amp;&amp; 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);
         }
-   }
-   if (chainedWatcher != null) {
-        chainedWatcher.process(event);
-   }
-}
+    }
 </pre>
 <p>
 If the client-side ZooKeeper libraries can reestablish the communication channel to ZooKeeper, DataMonitor simply kicks
@@ -564,20 +561,19 @@ If it gets an event for a znode, it calls <span class="codefrag command">ZooKeep
  * with the specified arguments when the znode exists and kills
  * the program if the znode goes away.
  */
-package com.yahoo.zk.executor;
-
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
 
-import com.yahoo.zk.executor.DataMonitor.DataMonitorListener;
-import com.yahoo.zookeeper.KeeperException;
-import com.yahoo.zookeeper.Watcher;
-import com.yahoo.zookeeper.ZooKeeper;
-import com.yahoo.zookeeper.proto.WatcherEvent;
+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, DataMonitorListener {
+public class Executor
+    implements Watcher, Runnable, DataMonitor.DataMonitorListener
+{
     String znode;
 
     DataMonitor dm;
@@ -621,15 +617,13 @@ public class Executor implements Watcher, Runnable, DataMonitorListener {
 
     /***************************************************************************
      * We do process any events ourselves, we just need to forward them on.
-     * 
-     * @see com.yahoo.zookeeper.Watcher#process(com.yahoo.zookeeper.proto.WatcherEvent)
+     *
+     * @see org.apache.zookeeper.Watcher#process(org.apache.zookeeper.proto.WatcherEvent)
      */
-    @Override
-    public void process(WatcherEvent event) {
+    public void process(WatchedEvent event) {
         dm.process(event);
     }
 
-    @Override
     public void run() {
         try {
             synchronized (this) {
@@ -641,7 +635,6 @@ public class Executor implements Watcher, Runnable, DataMonitorListener {
         }
     }
 
-    @Override
     public void closing(int rc) {
         synchronized (this) {
             notifyAll();
@@ -672,7 +665,6 @@ public class Executor implements Watcher, Runnable, DataMonitorListener {
         }
     }
 
-    @Override
     public void exists(byte[] data) {
         if (data == null) {
             if (child != null) {
@@ -728,17 +720,15 @@ public class Executor implements Watcher, Runnable, DataMonitorListener {
  * A simple class that monitors the data and existence of a ZooKeeper
  * node. It uses asynchronous ZooKeeper APIs.
  */
-package com.yahoo.zk.executor;
-
 import java.util.Arrays;
 
-import com.yahoo.zookeeper.KeeperException;
-import com.yahoo.zookeeper.Watcher;
-import com.yahoo.zookeeper.ZooKeeper;
-import com.yahoo.zookeeper.AsyncCallback.StatCallback;
-import com.yahoo.zookeeper.KeeperException.Code;
-import com.yahoo.zookeeper.data.Stat;
-import com.yahoo.zookeeper.proto.WatcherEvent;
+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 {
 
@@ -776,30 +766,25 @@ public class DataMonitor implements Watcher, StatCallback {
 
         /**
          * The ZooKeeper session is no longer valid.
-         * 
+         *
          * @param rc
          *                the ZooKeeper reason code
          */
         void closing(int rc);
     }
 
-    @Override
-    /**
-     * This is a watch event callback. The node we were watching has changed or
-     * something happened to our connection to ZooKeeper.
-     */
-    public void process(WatcherEvent event) {
+    public void process(WatchedEvent event) {
         String path = event.getPath();
-        if (event.getType() == Watcher.Event.EventNone) {
+        if (event.getType() == Event.EventType.None) {
             // We are are being told that the state of the
             // connection has changed
             switch (event.getState()) {
-            case Event.KeeperStateSyncConnected:
+            case SyncConnected:
                 // Everything is happy. Lets kick things off
                 // again by checking the existence of the znode
                 zk.exists(znode, true, this, null);
                 break;
-            case Event.KeeperStateExpired:
+            case Expired:
                 // It's all over
                 dead = true;
                 listener.closing(KeeperException.Code.SessionExpired);
@@ -816,7 +801,6 @@ public class DataMonitor implements Watcher, StatCallback {
         }
     }
 
-    @Override
     public void processResult(int rc, String path, Object ctx, Stat stat) {
         boolean exists;
         switch (rc) {

ファイルの差分が大きいため隠しています
+ 1 - 1
docs/javaExample.pdf


+ 234 - 233
docs/zookeeperTutorial.html

@@ -211,35 +211,32 @@ document.write("Last Published: " + document.lastModified);
     These examples assume that you have at least one ZooKeeper server running.</p>
 <p>Both primitives use the following common excerpt of code:</p>
 <pre class="code">
-static ZooKeeper zk = null;
-static Integer mutex;
-
-String root;
-
-SyncPrimitive(String address) {
-	if(zk == null){
-	    try {
-		System.out.println("Starting ZK:");
-		zk = new ZooKeeper(address, 3000, this);
-		mutex = new Integer(-1);
-		System.out.println("Finished starting ZK: " + zk);
-	    } catch (KeeperException e) {
-		System.out.println("Keeper exception when starting new session: "
-			+ e.toString());
-		zk = null;
-	    } catch (IOException e) {
-		System.out.println(e.toString());
-		zk = null;
-	    }
-	}
-}
+    static ZooKeeper zk = null;
+    static Integer mutex;
 
-synchronized public void process(WatcherEvent event) {
-	synchronized (mutex) {
-	    mutex.notify();
-	}
-}
+    String root;
 
+    SyncPrimitive(String address) {
+        if(zk == null){
+            try {
+                System.out.println("Starting ZK:");
+                zk = new ZooKeeper(address, 3000, this);
+                mutex = new Integer(-1);
+                System.out.println("Finished starting ZK: " + zk);
+            } catch (IOException e) {
+                System.out.println(e.toString());
+                zk = null;
+            }
+        }
+        //else mutex = new Integer(-1);
+    }
+
+    synchronized public void process(WatchedEvent event) {
+        synchronized (mutex) {
+            //System.out.println("Process: " + event.getType());
+            mutex.notify();
+        }
+    }
 </pre>
 <p>Both classes extend SyncPrimitive. In this way, we execute steps that are 
 common to all primitives in the constructor of SyncPrimitive. To keep the examples 
@@ -291,40 +288,43 @@ one does not exist. The constructor of Barrier then creates a
 barrier node on ZooKeeper, which is the parent node of all process nodes, and 
 we call root (<strong>Note:</strong> This is not the ZooKeeper root "/").</p>
 <pre class="code">
- /**
- * Barrier constructor
- *
- * @param address
- * @param name
- * @param size
- */
-Barrier(String address, String name, int size) {
-    super(address);
-    this.root = name;
-    this.size = size;
-
-    // Create barrier node
-    if (zk != null) {
-	try {
-	    Stat s = zk.exists(root, false);
-	    if (s == null) {
-		zk.create(root, new byte[0], Ids.OPEN_ACL_UNSAFE, 0);
-	    }
-	} catch (KeeperException e) {
-	    System.out.println("Keeper exception when instantiating queue: " + e.toString());
-	} catch (InterruptedException e) {
-	    System.out.println("Interrupted exception");
-	}
-    }
+        /**
+         * Barrier constructor
+         *
+         * @param address
+         * @param name
+         * @param size
+         */
+        Barrier(String address, String name, int size) {
+            super(address);
+            this.root = name;
+            this.size = size;
 
-    // My node name
-    try {
-	name = new String(InetAddress.getLocalHost().getCanonicalHostName().toString());
-    } catch (UnknownHostException e) {
-	System.out.println(e.toString());
-    }
+            // Create barrier node
+            if (zk != null) {
+                try {
+                    Stat s = zk.exists(root, false);
+                    if (s == null) {
+                        zk.create(root, new byte[0], Ids.OPEN_ACL_UNSAFE,
+                                CreateMode.PERSISTENT);
+                    }
+                } catch (KeeperException e) {
+                    System.out
+                            .println("Keeper exception when instantiating queue: "
+                                    + e.toString());
+                } catch (InterruptedException e) {
+                    System.out.println("Interrupted exception");
+                }
+            }
 
-}
+            // My node name
+            try {
+                name = new String(InetAddress.getLocalHost().getCanonicalHostName().toString());
+            } catch (UnknownHostException e) {
+                System.out.println(e.toString());
+            }
+
+        }
 </pre>
 <p>
 To enter the barrier, a process calls enter(). The process creates a node under 
@@ -338,29 +338,29 @@ has two parameters. The first one states the node to read from, and the second i
 a boolean flag that enables the process to set a watch. In the code the flag is true.
 </p>
 <pre class="code">
- /**
- * Join barrier
- *
- * @return
- * @throws KeeperException
- * @throws InterruptedException
- */
-
-boolean enter() throws KeeperException, InterruptedException{
-    zk.create(root + "/" + name, new byte[0], Ids.OPEN_ACL_UNSAFE,
-	    CreateFlags.EPHEMERAL);
-    while (true) {
-	synchronized (mutex) {
-	    ArrayList&lt;String&gt; list = zk.getChildren(root, true);
-
-	    if (list.size() &lt; size) {
-		mutex.wait();
-	    } else {
-		return true;
-	    }
-	}
-    }
-}
+        /**
+         * Join barrier
+         *
+         * @return
+         * @throws KeeperException
+         * @throws InterruptedException
+         */
+
+        boolean enter() throws KeeperException, InterruptedException{
+            zk.create(root + "/" + name, new byte[0], Ids.OPEN_ACL_UNSAFE,
+                    CreateMode.EPHEMERAL);
+            while (true) {
+                synchronized (mutex) {
+                    List&lt;String&gt; list = zk.getChildren(root, true);
+
+                    if (list.size() &lt; size) {
+                        mutex.wait();
+                    } else {
+                        return true;
+                    }
+                }
+            }
+        }
 </pre>
 <p>
 Note that enter() throws both KeeperException and InterruptedException, so it is 
@@ -373,27 +373,28 @@ that the second parameter of the call to getChildren() is true, meaning that
 ZooKeeper has to set a watch on the the root node). Upon reception of a notification, 
 it checks once more whether the root node has any child.</p>
 <pre class="code">
- /**
- * Wait until all reach barrier
- *
- * @return
- * @throws KeeperException
- * @throws InterruptedException
- */
-
-boolean leave() throws KeeperException, InterruptedException{
-    zk.delete(root + "/" + name, 0);
-    while (true) {
-	synchronized (mutex) {
-	    ArrayList&lt;String&gt; list = zk.getChildren(root, true);
-		if (list.size() &gt; 0) {
-		    mutex.wait();
-		} else {
-		    return true;
-		}
-	    }
-	}
-}
+        /**
+         * Wait until all reach barrier
+         *
+         * @return
+         * @throws KeeperException
+         * @throws InterruptedException
+         */
+
+        boolean leave() throws KeeperException, InterruptedException{
+            zk.delete(root + "/" + name, 0);
+            while (true) {
+                synchronized (mutex) {
+                    List&lt;String&gt; list = zk.getChildren(root, true);
+                        if (list.size() &gt; 0) {
+                            mutex.wait();
+                        } else {
+                            return true;
+                        }
+                    }
+                }
+        }
+    }
 </pre>
 </div>
 
@@ -415,32 +416,33 @@ that creates a ZooKeeper object if one doesn't exist. It then verifies if the ro
 node of the queue exists, and creates if it doesn't.
 </p>
 <pre class="code">
-/**
- * Constructor of producer-consumer queue
- *
- * @param address
- * @param name
- */
-Queue(String address, String name) {
-    super(address);
-    this.root = name;
-    // Create ZK node name
-    if (zk != null) {
-	try {
-	    Stat s = zk.exists(root, false);
-	    if (s == null) {
-		zk.create(root, new byte[0], Ids.OPEN_ACL_UNSAFE, 0);
-	    }
-	} catch (KeeperException e) {
-	    System.out
-		    .println("Keeper exception when instantiating queue: "
-			    + e.toString());
-	} catch (InterruptedException e) {
-	    System.out.println("Interrupted exception");
-	}
-    }
-}
- </pre>
+        /**
+         * Constructor of producer-consumer queue
+         *
+         * @param address
+         * @param name
+         */
+        Queue(String address, String name) {
+            super(address);
+            this.root = name;
+            // Create ZK node name
+            if (zk != null) {
+                try {
+                    Stat s = zk.exists(root, false);
+                    if (s == null) {
+                        zk.create(root, new byte[0], Ids.OPEN_ACL_UNSAFE,
+                                CreateMode.PERSISTENT);
+                    }
+                } catch (KeeperException e) {
+                    System.out
+                            .println("Keeper exception when instantiating queue: "
+                                    + e.toString());
+                } catch (InterruptedException e) {
+                    System.out.println("Interrupted exception");
+                }
+            }
+        }
+</pre>
 <p>
 A producer process calls "produce()" to add an element to the queue, and passes 
 an integer as an argument. To add an element to the queue, the method creates a 
@@ -450,25 +452,25 @@ we impose a total order on the elements of the queue, thus guaranteeing that the
 oldest element of the queue is the next one consumed.
 </p>
 <pre class="code">
-/**
- * Add element to the queue.
- *
- * @param i
- * @return
- */
-
-boolean produce(int i) throws KeeperException, InterruptedException{
-    ByteBuffer b = ByteBuffer.allocate(4);
-    byte[] value;
-
-    // Add child with value i
-    b.putInt(i);
-    value = b.array();
-    zk.create(root + "/element", value, Ids.OPEN_ACL_UNSAFE,
-		CreateFlags.SEQUENCE);
-
-    return true;
-}
+        /**
+         * Add element to the queue.
+         *
+         * @param i
+         * @return
+         */
+
+        boolean produce(int i) throws KeeperException, InterruptedException{
+            ByteBuffer b = ByteBuffer.allocate(4);
+            byte[] value;
+
+            // Add child with value i
+            b.putInt(i);
+            value = b.array();
+            zk.create(root + "/element", value, Ids.OPEN_ACL_UNSAFE,
+                        CreateMode.PERSISTENT_SEQUENTIAL);
+
+            return true;
+        }
 </pre>
 <p>
 To consume an element, a consumer process obtains the children of the root node, 
@@ -482,41 +484,44 @@ values, we need to decide which element is the smallest. To decide which one has
 the smallest counter value, we traverse the list, and remove the prefix "element" 
 from each one.</p>
 <pre class="code">
-/**
- * Remove first element from the queue.
- *
- * @return
- * @throws KeeperException
- * @throws InterruptedException
- */
-int consume() throws KeeperException, InterruptedException{
-    int retvalue = -1;
-    Stat stat = null;
-
-    // Get the first element available
-    while (true) {
-	synchronized (mutex) {
-	    ArrayList&lt;String&gt; list = zk.getChildren(root, true);
-	    if (list.size() == 0) {
-		System.out.println("Going to wait");
-		mutex.wait();
-	    } else {
-		Integer min = new Integer(list.get(0).substring(7));
-		for(String s : list){
-		    Integer tempValue = new Integer(s.substring(7));
-		    if(tempValue &gt; min) min = tempValue;
-		}
-		System.out.println("Temporary value: " + root + "/element" + min);
-		byte[] b = zk.getData(root + "/element" + min, false, stat);
-		zk.delete(root + "/element" + min, 0);
-		ByteBuffer buffer = ByteBuffer.wrap(b);
-		retvalue = buffer.getInt();
-
-		return retvalue;
-	    }
-	}
+        /**
+         * Remove first element from the queue.
+         *
+         * @return
+         * @throws KeeperException
+         * @throws InterruptedException
+         */
+        int consume() throws KeeperException, InterruptedException{
+            int retvalue = -1;
+            Stat stat = null;
+
+            // Get the first element available
+            while (true) {
+                synchronized (mutex) {
+                    List&lt;String&gt; list = zk.getChildren(root, true);
+                    if (list.size() == 0) {
+                        System.out.println("Going to wait");
+                        mutex.wait();
+                    } else {
+                        Integer min = new Integer(list.get(0).substring(7));
+                        for(String s : list){
+                            Integer tempValue = new Integer(s.substring(7));
+                            //System.out.println("Temporary value: " + tempValue);
+                            if(tempValue &lt; min) min = tempValue;
+                        }
+                        System.out.println("Temporary value: " + root + "/element" + min);
+                        byte[] b = zk.getData(root + "/element" + min,
+                                    false, stat);
+                        zk.delete(root + "/element" + min, 0);
+                        ByteBuffer buffer = ByteBuffer.wrap(b);
+                        retvalue = buffer.getInt();
+
+                        return retvalue;
+                    }
+                }
+            }
+        }
     }
-}
 </pre>
 </div>
 
@@ -530,29 +535,26 @@ int consume() throws KeeperException, InterruptedException{
 <title>SyncPrimitive.Java</title>
 
 <pre class="code">
-package com.yahoo.SyncPrimitive;
-
 import java.io.IOException;
 import java.net.InetAddress;
 import java.net.UnknownHostException;
 import java.nio.ByteBuffer;
-import java.lang.InterruptedException;
-import java.util.ArrayList;
+import java.util.List;
 import java.util.Random;
 
-import com.yahoo.zookeeper.Watcher;
-import com.yahoo.zookeeper.data.Stat;
-import com.yahoo.zookeeper.proto.WatcherEvent;
-import com.yahoo.zookeeper.KeeperException;
-import com.yahoo.zookeeper.ZooDefs.Ids;
-import com.yahoo.zookeeper.ZooDefs.CreateFlags;
-import com.yahoo.zookeeper.ZooKeeper;
+import org.apache.zookeeper.CreateMode;
+import org.apache.zookeeper.KeeperException;
+import org.apache.zookeeper.WatchedEvent;
+import org.apache.zookeeper.Watcher;
+import org.apache.zookeeper.ZooKeeper;
+import org.apache.zookeeper.ZooDefs.Ids;
+import org.apache.zookeeper.data.Stat;
 
 public class SyncPrimitive implements Watcher {
 
     static ZooKeeper zk = null;
     static Integer mutex;
-    
+
     String root;
 
     SyncPrimitive(String address) {
@@ -562,10 +564,6 @@ public class SyncPrimitive implements Watcher {
                 zk = new ZooKeeper(address, 3000, this);
                 mutex = new Integer(-1);
                 System.out.println("Finished starting ZK: " + zk);
-            } catch (KeeperException e) {
-                System.out.println("Keeper exception when starting new session: "
-                        + e.toString());
-                zk = null;
             } catch (IOException e) {
                 System.out.println(e.toString());
                 zk = null;
@@ -574,7 +572,7 @@ public class SyncPrimitive implements Watcher {
         //else mutex = new Integer(-1);
     }
 
-    synchronized public void process(WatcherEvent event) {
+    synchronized public void process(WatchedEvent event) {
         synchronized (mutex) {
             //System.out.println("Process: " + event.getType());
             mutex.notify();
@@ -590,7 +588,7 @@ public class SyncPrimitive implements Watcher {
 
         /**
          * Barrier constructor
-         * 
+         *
          * @param address
          * @param name
          * @param size
@@ -605,7 +603,8 @@ public class SyncPrimitive implements Watcher {
                 try {
                     Stat s = zk.exists(root, false);
                     if (s == null) {
-                        zk.create(root, new byte[0], Ids.OPEN_ACL_UNSAFE, 0);
+                        zk.create(root, new byte[0], Ids.OPEN_ACL_UNSAFE,
+                                CreateMode.PERSISTENT);
                     }
                 } catch (KeeperException e) {
                     System.out
@@ -615,31 +614,31 @@ public class SyncPrimitive implements Watcher {
                     System.out.println("Interrupted exception");
                 }
             }
-            
+
             // My node name
             try {
                 name = new String(InetAddress.getLocalHost().getCanonicalHostName().toString());
             } catch (UnknownHostException e) {
                 System.out.println(e.toString());
             }
-            
+
         }
 
         /**
          * Join barrier
-         * 
+         *
          * @return
          * @throws KeeperException
          * @throws InterruptedException
          */
-        
+
         boolean enter() throws KeeperException, InterruptedException{
             zk.create(root + "/" + name, new byte[0], Ids.OPEN_ACL_UNSAFE,
-                    CreateFlags.EPHEMERAL);
+                    CreateMode.EPHEMERAL);
             while (true) {
-                synchronized (mutex) {                    
-                    ArrayList&lt;String&gt; list = zk.getChildren(root, true);
-       
+                synchronized (mutex) {
+                    List&lt;String&gt; list = zk.getChildren(root, true);
+
                     if (list.size() &lt; size) {
                         mutex.wait();
                     } else {
@@ -651,17 +650,17 @@ public class SyncPrimitive implements Watcher {
 
         /**
          * Wait until all reach barrier
-         * 
+         *
          * @return
          * @throws KeeperException
          * @throws InterruptedException
          */
-        
+
         boolean leave() throws KeeperException, InterruptedException{
             zk.delete(root + "/" + name, 0);
             while (true) {
                 synchronized (mutex) {
-                    ArrayList&lt;String&gt; list = zk.getChildren(root, true);
+                    List&lt;String&gt; list = zk.getChildren(root, true);
                         if (list.size() &gt; 0) {
                             mutex.wait();
                         } else {
@@ -679,7 +678,7 @@ public class SyncPrimitive implements Watcher {
 
         /**
          * Constructor of producer-consumer queue
-         * 
+         *
          * @param address
          * @param name
          */
@@ -691,7 +690,8 @@ public class SyncPrimitive implements Watcher {
                 try {
                     Stat s = zk.exists(root, false);
                     if (s == null) {
-                        zk.create(root, new byte[0], Ids.OPEN_ACL_UNSAFE, 0);
+                        zk.create(root, new byte[0], Ids.OPEN_ACL_UNSAFE,
+                                CreateMode.PERSISTENT);
                     }
                 } catch (KeeperException e) {
                     System.out
@@ -705,11 +705,11 @@ public class SyncPrimitive implements Watcher {
 
         /**
          * Add element to the queue.
-         * 
+         *
          * @param i
          * @return
          */
-        
+
         boolean produce(int i) throws KeeperException, InterruptedException{
             ByteBuffer b = ByteBuffer.allocate(4);
             byte[] value;
@@ -718,15 +718,15 @@ public class SyncPrimitive implements Watcher {
             b.putInt(i);
             value = b.array();
             zk.create(root + "/element", value, Ids.OPEN_ACL_UNSAFE,
-                        CreateFlags.SEQUENCE);
-            
+                        CreateMode.PERSISTENT_SEQUENTIAL);
+
             return true;
         }
 
 
         /**
          * Remove first element from the queue.
-         * 
+         *
          * @return
          * @throws KeeperException
          * @throws InterruptedException
@@ -734,11 +734,11 @@ public class SyncPrimitive implements Watcher {
         int consume() throws KeeperException, InterruptedException{
             int retvalue = -1;
             Stat stat = null;
-            
+
             // Get the first element available
             while (true) {
                 synchronized (mutex) {
-                    ArrayList&lt;String&gt; list = zk.getChildren(root, true);
+                    List&lt;String&gt; list = zk.getChildren(root, true);
                     if (list.size() == 0) {
                         System.out.println("Going to wait");
                         mutex.wait();
@@ -755,11 +755,11 @@ public class SyncPrimitive implements Watcher {
                         zk.delete(root + "/element" + min, 0);
                         ByteBuffer buffer = ByteBuffer.wrap(b);
                         retvalue = buffer.getInt();
-                        
+
                         return retvalue;
                     }
                 }
-            }           
+            }
         }
     }
 
@@ -781,16 +781,16 @@ public class SyncPrimitive implements Watcher {
         if (args[3].equals("p")) {
             System.out.println("Producer");
             for (i = 0; i &lt; max; i++)
-                try{    
+                try{
                     q.produce(10 + i);
                 } catch (KeeperException e){
-                    
+
                 } catch (InterruptedException e){
-                    
+
                 }
         } else {
             System.out.println("Consumer");
-   
+
             for (i = 0; i &lt; max; i++) {
                 try{
                     int r = q.consume();
@@ -798,7 +798,7 @@ public class SyncPrimitive implements Watcher {
                 } catch (KeeperException e){
                     i--;
                 } catch (InterruptedException e){
-                    
+
                 }
             }
         }
@@ -811,9 +811,9 @@ public class SyncPrimitive implements Watcher {
             System.out.println("Entered barrier: " + args[2]);
             if(!flag) System.out.println("Error when entering the barrier");
         } catch (KeeperException e){
-            
+
         } catch (InterruptedException e){
-            
+
         }
 
         // Generate random integer
@@ -830,13 +830,14 @@ public class SyncPrimitive implements Watcher {
         try{
             b.leave();
         } catch (KeeperException e){
-            
+
         } catch (InterruptedException e){
-            
+
         }
         System.out.println("Left barrier");
     }
-}</pre>
+}
+</pre>
 </div>
 </div>
 </div>

ファイルの差分が大きいため隠しています
+ 3 - 4
docs/zookeeperTutorial.pdf


+ 83 - 99
src/docs/src/documentation/content/xdocs/javaExample.xml

@@ -83,47 +83,44 @@
     <xref linkend="sc_design"/>.  </para>
     
     <programlisting>
-// from the Executor class...
+    // from the Executor class...
     
-public static void main(String[] args) {
-    if (args.length &lt; 4) {
-        System.err
-                .println("USAGE: Executor hostPort znode filename program [args ...]");
-        System.exit(2);
+    public static void main(String[] args) {
+        if (args.length &lt; 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();
+        }
     }
-    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 {
-        Executor theExectutor = new Executor(hostPort, znode, filename, exec);
-	theExectutor.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 Executor(String hostPort, String znode, String filename,
-    String exec[]) throws KeeperException, IOException {
-    this.filename = filename;
-    this.exec = exec;
-    
-    //create a new zookeeper object, passing a self-reference in a the Watcher
-    zk = new ZooKeeper(hostPort, 3000, this);
-    dm = new DataMonitor(zk, znode, null, this);
-}
-   
-public void run() {
-    try {
-        synchronized (this) {
-            while (!dm.dead) {
-                wait();
+
+    public void run() {
+        try {
+            synchronized (this) {
+                while (!dm.dead) {
+                    wait();
+                }
             }
+        } catch (InterruptedException e) {
         }
-    } catch (InterruptedException e) {    
     }
-}
 </programlisting>
 
 
@@ -136,7 +133,7 @@ public void run() {
     </para>
     
     <programlisting>
-public class Executor implements Watcher, Runnable, DataMonitorListener {
+public class Executor implements Watcher, Runnable, DataMonitor.DataMonitorListener {
 ...
     </programlisting>
     
@@ -148,9 +145,9 @@ public class Executor implements Watcher, Runnable, DataMonitorListener {
     events to other objects. It also uses this as the default channel on which to fire watch events. (More on this later.)</para>
     
 <programlisting>
-public void process(WatcherEvent event) {
-    dm.process(event);
-}
+    public void process(WatchedEvent event) {
+        dm.process(event);
+    }
 </programlisting>
     
     <para>The <emphasis role="bold">DataMonitorListener</emphasis> 
@@ -330,33 +327,33 @@ the connection comes back up.
 
 <para>Finally, notice how DataMonitor processes watch events: </para>
 <programlisting>
-public void process(WatcherEvent event) {
-    String path = event.getPath();
-    if (event.getType() == Watcher.Event.EventNone) {
-        // We are are being told that the state of the
-        // connection has changed
-        switch (event.getState()) {
-        case Event.KeeperStateSyncConnected:
-            // Everything is happy. Lets kick things off
-            // again by checking the existence of the znode
-            zk.exists(znode, true, this, null);
-            break;
-        case Event.KeeperStateExpired:
-            // It's all over
-            dead = true;
-            listener.closing(KeeperException.Code.SessionExpired);
-            break;
+    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:
+                // Everything is happy. Lets kick things off
+                // again by checking the existence of the znode
+                zk.exists(znode, true, this, null);
+                break;
+            case Expired:
+                // It's all over
+                dead = true;
+                listener.closing(KeeperException.Code.SessionExpired);
+                break;
+            }
+        } else {
+            if (path != null &amp;&amp; path.equals(znode)) {
+                // Something has changed on the node, let's find out
+                zk.exists(znode, true, this, null);
+            }
         }
-    } else {
-        if (path != null &amp;&amp; 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);
         }
-   }
-   if (chainedWatcher != null) {
-        chainedWatcher.process(event);
-   }
-}
+    }
 </programlisting>
 <para>
 If the client-side ZooKeeper libraries can reestablish the communication channel to ZooKeeper, DataMonitor simply kicks
@@ -376,20 +373,19 @@ If it gets an event for a znode, it calls <command>ZooKeeper.exists()</command>
  * with the specified arguments when the znode exists and kills
  * the program if the znode goes away.
  */
-package com.yahoo.zk.executor;
-
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
 
-import com.yahoo.zk.executor.DataMonitor.DataMonitorListener;
-import com.yahoo.zookeeper.KeeperException;
-import com.yahoo.zookeeper.Watcher;
-import com.yahoo.zookeeper.ZooKeeper;
-import com.yahoo.zookeeper.proto.WatcherEvent;
+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, DataMonitorListener {
+public class Executor
+    implements Watcher, Runnable, DataMonitor.DataMonitorListener
+{
     String znode;
 
     DataMonitor dm;
@@ -433,15 +429,13 @@ public class Executor implements Watcher, Runnable, DataMonitorListener {
 
     /***************************************************************************
      * We do process any events ourselves, we just need to forward them on.
-     * 
-     * @see com.yahoo.zookeeper.Watcher#process(com.yahoo.zookeeper.proto.WatcherEvent)
+     *
+     * @see org.apache.zookeeper.Watcher#process(org.apache.zookeeper.proto.WatcherEvent)
      */
-    @Override
-    public void process(WatcherEvent event) {
+    public void process(WatchedEvent event) {
         dm.process(event);
     }
 
-    @Override
     public void run() {
         try {
             synchronized (this) {
@@ -453,7 +447,6 @@ public class Executor implements Watcher, Runnable, DataMonitorListener {
         }
     }
 
-    @Override
     public void closing(int rc) {
         synchronized (this) {
             notifyAll();
@@ -484,7 +477,6 @@ public class Executor implements Watcher, Runnable, DataMonitorListener {
         }
     }
 
-    @Override
     public void exists(byte[] data) {
         if (data == null) {
             if (child != null) {
@@ -535,17 +527,15 @@ public class Executor implements Watcher, Runnable, DataMonitorListener {
  * A simple class that monitors the data and existence of a ZooKeeper
  * node. It uses asynchronous ZooKeeper APIs.
  */
-package com.yahoo.zk.executor;
-
 import java.util.Arrays;
 
-import com.yahoo.zookeeper.KeeperException;
-import com.yahoo.zookeeper.Watcher;
-import com.yahoo.zookeeper.ZooKeeper;
-import com.yahoo.zookeeper.AsyncCallback.StatCallback;
-import com.yahoo.zookeeper.KeeperException.Code;
-import com.yahoo.zookeeper.data.Stat;
-import com.yahoo.zookeeper.proto.WatcherEvent;
+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 {
 
@@ -583,30 +573,25 @@ public class DataMonitor implements Watcher, StatCallback {
 
         /**
          * The ZooKeeper session is no longer valid.
-         * 
+         *
          * @param rc
          *                the ZooKeeper reason code
          */
         void closing(int rc);
     }
 
-    @Override
-    /**
-     * This is a watch event callback. The node we were watching has changed or
-     * something happened to our connection to ZooKeeper.
-     */
-    public void process(WatcherEvent event) {
+    public void process(WatchedEvent event) {
         String path = event.getPath();
-        if (event.getType() == Watcher.Event.EventNone) {
+        if (event.getType() == Event.EventType.None) {
             // We are are being told that the state of the
             // connection has changed
             switch (event.getState()) {
-            case Event.KeeperStateSyncConnected:
+            case SyncConnected:
                 // Everything is happy. Lets kick things off
                 // again by checking the existence of the znode
                 zk.exists(znode, true, this, null);
                 break;
-            case Event.KeeperStateExpired:
+            case Expired:
                 // It's all over
                 dead = true;
                 listener.closing(KeeperException.Code.SessionExpired);
@@ -623,7 +608,6 @@ public class DataMonitor implements Watcher, StatCallback {
         }
     }
 
-    @Override
     public void processResult(int rc, String path, Object ctx, Stat stat) {
         boolean exists;
         switch (rc) {

+ 235 - 234
src/docs/src/documentation/content/xdocs/zookeeperTutorial.xml

@@ -51,35 +51,32 @@
     <para>Both primitives use the following common excerpt of code:</para>
     
     <programlisting>
-static ZooKeeper zk = null;
-static Integer mutex;
-
-String root;
-
-SyncPrimitive(String address) {
-	if(zk == null){
-	    try {
-		System.out.println("Starting ZK:");
-		zk = new ZooKeeper(address, 3000, this);
-		mutex = new Integer(-1);
-		System.out.println("Finished starting ZK: " + zk);
-	    } catch (KeeperException e) {
-		System.out.println("Keeper exception when starting new session: "
-			+ e.toString());
-		zk = null;
-	    } catch (IOException e) {
-		System.out.println(e.toString());
-		zk = null;
-	    }
-	}
-}
+    static ZooKeeper zk = null;
+    static Integer mutex;
 
-synchronized public void process(WatcherEvent event) {
-	synchronized (mutex) {
-	    mutex.notify();
-	}
-}
+    String root;
+
+    SyncPrimitive(String address) {
+        if(zk == null){
+            try {
+                System.out.println("Starting ZK:");
+                zk = new ZooKeeper(address, 3000, this);
+                mutex = new Integer(-1);
+                System.out.println("Finished starting ZK: " + zk);
+            } catch (IOException e) {
+                System.out.println(e.toString());
+                zk = null;
+            }
+        }
+        //else mutex = new Integer(-1);
+    }
 
+    synchronized public void process(WatchedEvent event) {
+        synchronized (mutex) {
+            //System.out.println("Process: " + event.getType());
+            mutex.notify();
+        }
+    }
 </programlisting>
 
 <para>Both classes extend SyncPrimitive. In this way, we execute steps that are 
@@ -124,40 +121,43 @@ barrier node on ZooKeeper, which is the parent node of all process nodes, and
 we call root (<emphasis role="bold">Note:</emphasis> This is not the ZooKeeper root "/").</para>
 
 <programlisting>
- /**
- * Barrier constructor
- *
- * @param address
- * @param name
- * @param size
- */
-Barrier(String address, String name, int size) {
-    super(address);
-    this.root = name;
-    this.size = size;
-
-    // Create barrier node
-    if (zk != null) {
-	try {
-	    Stat s = zk.exists(root, false);
-	    if (s == null) {
-		zk.create(root, new byte[0], Ids.OPEN_ACL_UNSAFE, 0);
-	    }
-	} catch (KeeperException e) {
-	    System.out.println("Keeper exception when instantiating queue: " + e.toString());
-	} catch (InterruptedException e) {
-	    System.out.println("Interrupted exception");
-	}
-    }
+        /**
+         * Barrier constructor
+         *
+         * @param address
+         * @param name
+         * @param size
+         */
+        Barrier(String address, String name, int size) {
+            super(address);
+            this.root = name;
+            this.size = size;
 
-    // My node name
-    try {
-	name = new String(InetAddress.getLocalHost().getCanonicalHostName().toString());
-    } catch (UnknownHostException e) {
-	System.out.println(e.toString());
-    }
+            // Create barrier node
+            if (zk != null) {
+                try {
+                    Stat s = zk.exists(root, false);
+                    if (s == null) {
+                        zk.create(root, new byte[0], Ids.OPEN_ACL_UNSAFE,
+                                CreateMode.PERSISTENT);
+                    }
+                } catch (KeeperException e) {
+                    System.out
+                            .println("Keeper exception when instantiating queue: "
+                                    + e.toString());
+                } catch (InterruptedException e) {
+                    System.out.println("Interrupted exception");
+                }
+            }
 
-}
+            // My node name
+            try {
+                name = new String(InetAddress.getLocalHost().getCanonicalHostName().toString());
+            } catch (UnknownHostException e) {
+                System.out.println(e.toString());
+            }
+
+        }
 </programlisting>
 <para>
 To enter the barrier, a process calls enter(). The process creates a node under 
@@ -172,29 +172,29 @@ a boolean flag that enables the process to set a watch. In the code the flag is
 </para>
 
 <programlisting>
- /**
- * Join barrier
- *
- * @return
- * @throws KeeperException
- * @throws InterruptedException
- */
-
-boolean enter() throws KeeperException, InterruptedException{
-    zk.create(root + "/" + name, new byte[0], Ids.OPEN_ACL_UNSAFE,
-	    CreateFlags.EPHEMERAL);
-    while (true) {
-	synchronized (mutex) {
-	    ArrayList&lt;String&gt; list = zk.getChildren(root, true);
-
-	    if (list.size() &lt; size) {
-		mutex.wait();
-	    } else {
-		return true;
-	    }
-	}
-    }
-}
+        /**
+         * Join barrier
+         *
+         * @return
+         * @throws KeeperException
+         * @throws InterruptedException
+         */
+
+        boolean enter() throws KeeperException, InterruptedException{
+            zk.create(root + "/" + name, new byte[0], Ids.OPEN_ACL_UNSAFE,
+                    CreateMode.EPHEMERAL);
+            while (true) {
+                synchronized (mutex) {
+                    List&lt;String&gt; list = zk.getChildren(root, true);
+
+                    if (list.size() &lt; size) {
+                        mutex.wait();
+                    } else {
+                        return true;
+                    }
+                }
+            }
+        }
 </programlisting>
 <para>
 Note that enter() throws both KeeperException and InterruptedException, so it is 
@@ -209,27 +209,28 @@ ZooKeeper has to set a watch on the the root node). Upon reception of a notifica
 it checks once more whether the root node has any child.</para>
 
 <programlisting>
- /**
- * Wait until all reach barrier
- *
- * @return
- * @throws KeeperException
- * @throws InterruptedException
- */
-
-boolean leave() throws KeeperException, InterruptedException{
-    zk.delete(root + "/" + name, 0);
-    while (true) {
-	synchronized (mutex) {
-	    ArrayList&lt;String&gt; list = zk.getChildren(root, true);
-		if (list.size() &gt; 0) {
-		    mutex.wait();
-		} else {
-		    return true;
-		}
-	    }
-	}
-}
+        /**
+         * Wait until all reach barrier
+         *
+         * @return
+         * @throws KeeperException
+         * @throws InterruptedException
+         */
+
+        boolean leave() throws KeeperException, InterruptedException{
+            zk.delete(root + "/" + name, 0);
+            while (true) {
+                synchronized (mutex) {
+                    List&lt;String&gt; list = zk.getChildren(root, true);
+                        if (list.size() &gt; 0) {
+                            mutex.wait();
+                        } else {
+                            return true;
+                        }
+                    }
+                }
+        }
+    }
 </programlisting>
 </section>
 <section id="sc_producerConsumerQueues"><title>Producer-Consumer Queues</title>
@@ -249,32 +250,33 @@ that creates a ZooKeeper object if one doesn't exist. It then verifies if the ro
 node of the queue exists, and creates if it doesn't.
 </para>
 <programlisting>
-/**
- * Constructor of producer-consumer queue
- *
- * @param address
- * @param name
- */
-Queue(String address, String name) {
-    super(address);
-    this.root = name;
-    // Create ZK node name
-    if (zk != null) {
-	try {
-	    Stat s = zk.exists(root, false);
-	    if (s == null) {
-		zk.create(root, new byte[0], Ids.OPEN_ACL_UNSAFE, 0);
-	    }
-	} catch (KeeperException e) {
-	    System.out
-		    .println("Keeper exception when instantiating queue: "
-			    + e.toString());
-	} catch (InterruptedException e) {
-	    System.out.println("Interrupted exception");
-	}
-    }
-}
- </programlisting>
+        /**
+         * Constructor of producer-consumer queue
+         *
+         * @param address
+         * @param name
+         */
+        Queue(String address, String name) {
+            super(address);
+            this.root = name;
+            // Create ZK node name
+            if (zk != null) {
+                try {
+                    Stat s = zk.exists(root, false);
+                    if (s == null) {
+                        zk.create(root, new byte[0], Ids.OPEN_ACL_UNSAFE,
+                                CreateMode.PERSISTENT);
+                    }
+                } catch (KeeperException e) {
+                    System.out
+                            .println("Keeper exception when instantiating queue: "
+                                    + e.toString());
+                } catch (InterruptedException e) {
+                    System.out.println("Interrupted exception");
+                }
+            }
+        }
+</programlisting>
  
 <para>
 A producer process calls "produce()" to add an element to the queue, and passes 
@@ -286,25 +288,25 @@ oldest element of the queue is the next one consumed.
 </para>
 
 <programlisting>
-/**
- * Add element to the queue.
- *
- * @param i
- * @return
- */
-
-boolean produce(int i) throws KeeperException, InterruptedException{
-    ByteBuffer b = ByteBuffer.allocate(4);
-    byte[] value;
-
-    // Add child with value i
-    b.putInt(i);
-    value = b.array();
-    zk.create(root + "/element", value, Ids.OPEN_ACL_UNSAFE,
-		CreateFlags.SEQUENCE);
-
-    return true;
-}
+        /**
+         * Add element to the queue.
+         *
+         * @param i
+         * @return
+         */
+
+        boolean produce(int i) throws KeeperException, InterruptedException{
+            ByteBuffer b = ByteBuffer.allocate(4);
+            byte[] value;
+
+            // Add child with value i
+            b.putInt(i);
+            value = b.array();
+            zk.create(root + "/element", value, Ids.OPEN_ACL_UNSAFE,
+                        CreateMode.PERSISTENT_SEQUENTIAL);
+
+            return true;
+        }
 </programlisting>
 <para>
 To consume an element, a consumer process obtains the children of the root node, 
@@ -320,41 +322,44 @@ the smallest counter value, we traverse the list, and remove the prefix "element
 from each one.</para>
 
 <programlisting>
-/**
- * Remove first element from the queue.
- *
- * @return
- * @throws KeeperException
- * @throws InterruptedException
- */
-int consume() throws KeeperException, InterruptedException{
-    int retvalue = -1;
-    Stat stat = null;
-
-    // Get the first element available
-    while (true) {
-	synchronized (mutex) {
-	    ArrayList&lt;String&gt; list = zk.getChildren(root, true);
-	    if (list.size() == 0) {
-		System.out.println("Going to wait");
-		mutex.wait();
-	    } else {
-		Integer min = new Integer(list.get(0).substring(7));
-		for(String s : list){
-		    Integer tempValue = new Integer(s.substring(7));
-		    if(tempValue &gt; min) min = tempValue;
-		}
-		System.out.println("Temporary value: " + root + "/element" + min);
-		byte[] b = zk.getData(root + "/element" + min, false, stat);
-		zk.delete(root + "/element" + min, 0);
-		ByteBuffer buffer = ByteBuffer.wrap(b);
-		retvalue = buffer.getInt();
-
-		return retvalue;
-	    }
-	}
+        /**
+         * Remove first element from the queue.
+         *
+         * @return
+         * @throws KeeperException
+         * @throws InterruptedException
+         */
+        int consume() throws KeeperException, InterruptedException{
+            int retvalue = -1;
+            Stat stat = null;
+
+            // Get the first element available
+            while (true) {
+                synchronized (mutex) {
+                    List&lt;String&gt; list = zk.getChildren(root, true);
+                    if (list.size() == 0) {
+                        System.out.println("Going to wait");
+                        mutex.wait();
+                    } else {
+                        Integer min = new Integer(list.get(0).substring(7));
+                        for(String s : list){
+                            Integer tempValue = new Integer(s.substring(7));
+                            //System.out.println("Temporary value: " + tempValue);
+                            if(tempValue &lt; min) min = tempValue;
+                        }
+                        System.out.println("Temporary value: " + root + "/element" + min);
+                        byte[] b = zk.getData(root + "/element" + min,
+                                    false, stat);
+                        zk.delete(root + "/element" + min, 0);
+                        ByteBuffer buffer = ByteBuffer.wrap(b);
+                        retvalue = buffer.getInt();
+
+                        return retvalue;
+                    }
+                }
+            }
+        }
     }
-}
 </programlisting>
  
 </section>
@@ -362,29 +367,26 @@ int consume() throws KeeperException, InterruptedException{
 <example id="eg_SyncPrimitive_java">
 <title>SyncPrimitive.Java</title>
 <programlisting>
-package com.yahoo.SyncPrimitive;
-
 import java.io.IOException;
 import java.net.InetAddress;
 import java.net.UnknownHostException;
 import java.nio.ByteBuffer;
-import java.lang.InterruptedException;
-import java.util.ArrayList;
+import java.util.List;
 import java.util.Random;
 
-import com.yahoo.zookeeper.Watcher;
-import com.yahoo.zookeeper.data.Stat;
-import com.yahoo.zookeeper.proto.WatcherEvent;
-import com.yahoo.zookeeper.KeeperException;
-import com.yahoo.zookeeper.ZooDefs.Ids;
-import com.yahoo.zookeeper.ZooDefs.CreateFlags;
-import com.yahoo.zookeeper.ZooKeeper;
+import org.apache.zookeeper.CreateMode;
+import org.apache.zookeeper.KeeperException;
+import org.apache.zookeeper.WatchedEvent;
+import org.apache.zookeeper.Watcher;
+import org.apache.zookeeper.ZooKeeper;
+import org.apache.zookeeper.ZooDefs.Ids;
+import org.apache.zookeeper.data.Stat;
 
 public class SyncPrimitive implements Watcher {
 
     static ZooKeeper zk = null;
     static Integer mutex;
-    
+
     String root;
 
     SyncPrimitive(String address) {
@@ -394,10 +396,6 @@ public class SyncPrimitive implements Watcher {
                 zk = new ZooKeeper(address, 3000, this);
                 mutex = new Integer(-1);
                 System.out.println("Finished starting ZK: " + zk);
-            } catch (KeeperException e) {
-                System.out.println("Keeper exception when starting new session: "
-                        + e.toString());
-                zk = null;
             } catch (IOException e) {
                 System.out.println(e.toString());
                 zk = null;
@@ -406,7 +404,7 @@ public class SyncPrimitive implements Watcher {
         //else mutex = new Integer(-1);
     }
 
-    synchronized public void process(WatcherEvent event) {
+    synchronized public void process(WatchedEvent event) {
         synchronized (mutex) {
             //System.out.println("Process: " + event.getType());
             mutex.notify();
@@ -422,7 +420,7 @@ public class SyncPrimitive implements Watcher {
 
         /**
          * Barrier constructor
-         * 
+         *
          * @param address
          * @param name
          * @param size
@@ -437,7 +435,8 @@ public class SyncPrimitive implements Watcher {
                 try {
                     Stat s = zk.exists(root, false);
                     if (s == null) {
-                        zk.create(root, new byte[0], Ids.OPEN_ACL_UNSAFE, 0);
+                        zk.create(root, new byte[0], Ids.OPEN_ACL_UNSAFE,
+                                CreateMode.PERSISTENT);
                     }
                 } catch (KeeperException e) {
                     System.out
@@ -447,31 +446,31 @@ public class SyncPrimitive implements Watcher {
                     System.out.println("Interrupted exception");
                 }
             }
-            
+
             // My node name
             try {
                 name = new String(InetAddress.getLocalHost().getCanonicalHostName().toString());
             } catch (UnknownHostException e) {
                 System.out.println(e.toString());
             }
-            
+
         }
 
         /**
          * Join barrier
-         * 
+         *
          * @return
          * @throws KeeperException
          * @throws InterruptedException
          */
-        
+
         boolean enter() throws KeeperException, InterruptedException{
             zk.create(root + "/" + name, new byte[0], Ids.OPEN_ACL_UNSAFE,
-                    CreateFlags.EPHEMERAL);
+                    CreateMode.EPHEMERAL);
             while (true) {
-                synchronized (mutex) {                    
-                    ArrayList&lt;String&gt; list = zk.getChildren(root, true);
-       
+                synchronized (mutex) {
+                    List&lt;String&gt; list = zk.getChildren(root, true);
+
                     if (list.size() &lt; size) {
                         mutex.wait();
                     } else {
@@ -483,18 +482,18 @@ public class SyncPrimitive implements Watcher {
 
         /**
          * Wait until all reach barrier
-         * 
+         *
          * @return
          * @throws KeeperException
          * @throws InterruptedException
          */
-        
+
         boolean leave() throws KeeperException, InterruptedException{
             zk.delete(root + "/" + name, 0);
             while (true) {
                 synchronized (mutex) {
-                    ArrayList&lt;String&gt; list = zk.getChildren(root, true);
-                        if (list.size() > 0) {
+                    List&lt;String&gt; list = zk.getChildren(root, true);
+                        if (list.size() &gt; 0) {
                             mutex.wait();
                         } else {
                             return true;
@@ -511,7 +510,7 @@ public class SyncPrimitive implements Watcher {
 
         /**
          * Constructor of producer-consumer queue
-         * 
+         *
          * @param address
          * @param name
          */
@@ -523,7 +522,8 @@ public class SyncPrimitive implements Watcher {
                 try {
                     Stat s = zk.exists(root, false);
                     if (s == null) {
-                        zk.create(root, new byte[0], Ids.OPEN_ACL_UNSAFE, 0);
+                        zk.create(root, new byte[0], Ids.OPEN_ACL_UNSAFE,
+                                CreateMode.PERSISTENT);
                     }
                 } catch (KeeperException e) {
                     System.out
@@ -537,11 +537,11 @@ public class SyncPrimitive implements Watcher {
 
         /**
          * Add element to the queue.
-         * 
+         *
          * @param i
          * @return
          */
-        
+
         boolean produce(int i) throws KeeperException, InterruptedException{
             ByteBuffer b = ByteBuffer.allocate(4);
             byte[] value;
@@ -550,15 +550,15 @@ public class SyncPrimitive implements Watcher {
             b.putInt(i);
             value = b.array();
             zk.create(root + "/element", value, Ids.OPEN_ACL_UNSAFE,
-                        CreateFlags.SEQUENCE);
-            
+                        CreateMode.PERSISTENT_SEQUENTIAL);
+
             return true;
         }
 
 
         /**
          * Remove first element from the queue.
-         * 
+         *
          * @return
          * @throws KeeperException
          * @throws InterruptedException
@@ -566,11 +566,11 @@ public class SyncPrimitive implements Watcher {
         int consume() throws KeeperException, InterruptedException{
             int retvalue = -1;
             Stat stat = null;
-            
+
             // Get the first element available
             while (true) {
                 synchronized (mutex) {
-                    ArrayList&lt;String&gt; list = zk.getChildren(root, true);
+                    List&lt;String&gt; list = zk.getChildren(root, true);
                     if (list.size() == 0) {
                         System.out.println("Going to wait");
                         mutex.wait();
@@ -587,11 +587,11 @@ public class SyncPrimitive implements Watcher {
                         zk.delete(root + "/element" + min, 0);
                         ByteBuffer buffer = ByteBuffer.wrap(b);
                         retvalue = buffer.getInt();
-                        
+
                         return retvalue;
                     }
                 }
-            }           
+            }
         }
     }
 
@@ -613,16 +613,16 @@ public class SyncPrimitive implements Watcher {
         if (args[3].equals("p")) {
             System.out.println("Producer");
             for (i = 0; i &lt; max; i++)
-                try{    
+                try{
                     q.produce(10 + i);
                 } catch (KeeperException e){
-                    
+
                 } catch (InterruptedException e){
-                    
+
                 }
         } else {
             System.out.println("Consumer");
-   
+
             for (i = 0; i &lt; max; i++) {
                 try{
                     int r = q.consume();
@@ -630,7 +630,7 @@ public class SyncPrimitive implements Watcher {
                 } catch (KeeperException e){
                     i--;
                 } catch (InterruptedException e){
-                    
+
                 }
             }
         }
@@ -643,9 +643,9 @@ public class SyncPrimitive implements Watcher {
             System.out.println("Entered barrier: " + args[2]);
             if(!flag) System.out.println("Error when entering the barrier");
         } catch (KeeperException e){
-            
+
         } catch (InterruptedException e){
-            
+
         }
 
         // Generate random integer
@@ -662,13 +662,14 @@ public class SyncPrimitive implements Watcher {
         try{
             b.leave();
         } catch (KeeperException e){
-            
+
         } catch (InterruptedException e){
-            
+
         }
         System.out.println("Left barrier");
     }
-}</programlisting></example>
+}
+</programlisting></example>
 </section>
 
 </article>

この差分においてかなりの量のファイルが変更されているため、一部のファイルを表示していません