public class ZKClient extends Object
BarrierListener can be registered with a specific region and initial data by the registerBarrier(java.lang.String, org.glassfish.grizzly.memcached.zookeeper.BarrierListener, byte[]) method and unregistered by the unregisterBarrier(java.lang.String) method.
If the zookeeper server doesn't have the data node, the given initial data will be set in the server when the registerBarrier(java.lang.String, org.glassfish.grizzly.memcached.zookeeper.BarrierListener, byte[]) method is called.
If the specific data will be changed in remote zookeeper server, all clients which have joined will receive changes simultaneously.
If all clients receive changes successfully,
BarrierListener.onCommit(java.lang.String, java.lang.String, byte[]) will be called simultaneously at the scheduled time(data modification time + commitDelayTimeInSecs).
This also supports some safe APIs which is similar to original ZooKeeper's APIs
like create, delete, exists, getChildren, getData and setData.
Examples of barrier's use:
// initial
<p/>
final ZKClient.Builder builder = new ZKClient.Builder("myZookeeperClient", "localhost:2181");
builder.rootPath(ROOT).connectTimeoutInMillis(3000).sessionTimeoutInMillis(30000).commitDelayTimeInSecs(60);
final ZKClient zkClient = builder.build();
zkClient.connect()
final String registeredPath = zkClient.registerBarrier( "user", myListener, initData );
// ...
// cleanup
zkClient.unregisterBarrier( "user" );
zkClient.shutdown();
[NOTE]
Zookeeper already guides some simple barrier examples:
http://zookeeper.apache.org/doc/r3.3.4/zookeeperTutorial.html
http://code.google.com/p/good-samples/source/browse/trunk/zookeeper-3.x/src/main/java/com/googlecode/goodsamples/zookeeper/barrier/Barrier.java
But, their examples have a race condision issue:
https://issues.apache.org/jira/browse/ZOOKEEPER-1011| Modifier and Type | Class and Description |
|---|---|
static class |
ZKClient.Builder
Builder for ZKClient
|
| Modifier and Type | Method and Description |
|---|---|
boolean |
connect()
Connect this client to the zookeeper server
this method will wait for
Watcher.Event.KeeperState#SyncConnected from the zookeeper server. |
String |
create(String path,
byte[] data,
List<org.apache.zookeeper.data.ACL> acl,
org.apache.zookeeper.CreateMode createMode) |
boolean |
delete(String path,
int version) |
org.apache.zookeeper.data.Stat |
exists(String path,
boolean watch) |
org.apache.zookeeper.data.Stat |
exists(String path,
org.apache.zookeeper.Watcher watch) |
List<String> |
getChildren(String path,
boolean watch) |
List<String> |
getChildren(String path,
org.apache.zookeeper.Watcher watcher) |
byte[] |
getData(String path,
boolean watch,
org.apache.zookeeper.data.Stat stat) |
byte[] |
getData(String path,
org.apache.zookeeper.Watcher watcher) |
String |
registerBarrier(String regionName,
BarrierListener listener,
byte[] initialData)
Register the specific barrier
|
org.apache.zookeeper.data.Stat |
setData(String path,
byte[] data,
int version) |
void |
shutdown()
Close this client
|
String |
toString() |
void |
unregisterBarrier(String regionName)
Unregister the listener which was registered by
registerBarrier(java.lang.String, org.glassfish.grizzly.memcached.zookeeper.BarrierListener, byte[]) |
public boolean connect()
throws IOException,
InterruptedException
Watcher.Event.KeeperState#SyncConnected from the zookeeper server.IOException - the io exception of internal ZooKeeperInterruptedException - the interrupted exception of internal ZooKeeperpublic void shutdown()
public String registerBarrier(String regionName, BarrierListener listener, byte[] initialData)
regionName - specific region namelistener - BarrierListener implementationsinitialData - initial data. if the zookeeper server doesn't have any data, this will be set. "null" means NO_DATA which is byte[0].public void unregisterBarrier(String regionName)
registerBarrier(java.lang.String, org.glassfish.grizzly.memcached.zookeeper.BarrierListener, byte[])regionName - specific region namepublic String create(String path, byte[] data, List<org.apache.zookeeper.data.ACL> acl, org.apache.zookeeper.CreateMode createMode)
public org.apache.zookeeper.data.Stat exists(String path, boolean watch)
public org.apache.zookeeper.data.Stat exists(String path, org.apache.zookeeper.Watcher watch)
public byte[] getData(String path, boolean watch, org.apache.zookeeper.data.Stat stat)
public byte[] getData(String path, org.apache.zookeeper.Watcher watcher)
public org.apache.zookeeper.data.Stat setData(String path, byte[] data, int version)
public boolean delete(String path, int version)
Copyright © 2015 Project Grizzly. All Rights Reserved.