• zookeeper事务


    zookeeper事物操作,其实只是其multi操作的简单封装:

    public List<OpResult> multi(Iterable<Op> ops)
    基本操作:new Transaction(zk).create(...).setData(...)... .commit();
    因为每次返回 this 可以串行操作,最后执行commit(),提交批量事务操作,并返回List<OpResult>结果。
    package org.apache.zookeeper;
    
    import org.apache.zookeeper.data.ACL;
    import java.util.ArrayList;
    import java.util.List;
    
    /**
     * Provides a builder style interface for doing multiple updates.  This is
     * really just a thin layer on top of Zookeeper.multi().
     *
     * @since 3.4.0
     *
     */
    public class Transaction {
        private ZooKeeper zk;
        private List<Op> ops = new ArrayList<Op>();
    
        protected Transaction(ZooKeeper zk) {
            this.zk = zk;
        }
    
        public Transaction create(final String path, byte data[], List<ACL> acl,
                                  CreateMode createMode) {
            ops.add(Op.create(path, data, acl, createMode.toFlag()));
            return this;
        }
    
        public Transaction delete(final String path, int version) {
            ops.add(Op.delete(path, version));
            return this;
        }
    
        public Transaction check(String path, int version) {
            ops.add(Op.check(path, version));
            return this;
        }
    
        public Transaction setData(final String path, byte data[], int version) {
            ops.add(Op.setData(path, data, version));
            return this;
        }
    
        public List<OpResult> commit() throws InterruptedException, KeeperException {
            return zk.multi(ops);
        }
    }
  • 相关阅读:
    腾讯2014年实习生招聘笔试面试经历
    Unity MVC框架 StrangeIoC
    Android入门第八篇之GridView(九宫图)
    拓扑排序
    C:打印菱形(自己的方法)
    JSP中Session的使用
    继承Application实现Android数据共享
    使用Java高速实现进度条
    首次启动优美新手指引tip
    递归算法
  • 原文地址:https://www.cnblogs.com/niejunlei/p/6113552.html
Copyright © 2020-2023  润新知