Example usage for org.apache.zookeeper Watcher process

List of usage examples for org.apache.zookeeper Watcher process

Introduction

In this page you can find the example usage for org.apache.zookeeper Watcher process.

Prototype

void process(WatchedEvent event);

Source Link

Usage

From source file:org.midonet.midolman.state.MockDirectory.java

License:Apache License

@Override
public List<OpResult> multi(List<Op> ops) throws InterruptedException, KeeperException {
    List<OpResult> results = new ArrayList<OpResult>();
    // Fire watchers after finishing multi operation.
    // Copy to the local Set to avoid concurrent access.
    Map<Watcher, WatchedEvent> watchers = new HashMap<Watcher, WatchedEvent>();
    try {//from  w ww .j  ava2s  .c  o m
        for (Op op : ops) {
            Record record = op.toRequestRecord();
            if (record instanceof CreateRequest) {
                // TODO(pino, ryu): should we use the try/catch and create
                // new ErrorResult? Don't for now, but this means that the
                // unit tests can't purposely make a bad Op.
                // try {
                CreateRequest req = CreateRequest.class.cast(record);
                String path = this.add(req.getPath(), req.getData(), CreateMode.fromFlag(req.getFlags()), true);
                results.add(new OpResult.CreateResult(path));
                // } catch (KeeperException e) {
                // e.printStackTrace();
                // results.add(
                // new OpResult.ErrorResult(e.code().intValue()));
                // }
            } else if (record instanceof SetDataRequest) {
                SetDataRequest req = SetDataRequest.class.cast(record);
                this.update(req.getPath(), req.getData(), true);
                // We create the SetDataResult with Stat=null. The
                // Directory interface doesn't provide the Stat object.
                results.add(new OpResult.SetDataResult(null));
            } else if (record instanceof DeleteRequest) {
                DeleteRequest req = DeleteRequest.class.cast(record);
                this.delete(req.getPath(), true);
                results.add(new OpResult.DeleteResult());
            } else {
                // might be CheckVersionRequest or some new type we miss.
                throw new IllegalArgumentException(
                        "This mock implementation only supports Create, " + "SetData and Delete operations");
            }
        }
    } finally {
        synchronized (multiDataWatchers) {
            watchers.putAll(multiDataWatchers);
            multiDataWatchers.clear();
        }
    }

    for (Watcher watcher : watchers.keySet()) {
        watcher.process(watchers.get(watcher));
    }

    return results;
}