zoukankan      html  css  js  c++  java
  • 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);
        }
    }
  • 相关阅读:
    window.history 和 DWZ 框架
    Ztree 随笔记
    eval的对于验证数学公式的用处
    lodop打印控件一点记录
    font和lineheight冲突。
    Windows CMD命令大全
    centos 下安装pip pip3
    Linux访问windows共享文件夹
    数据库主从和读写分离的配置和使用方法
    centos7 nginx+php7yum安装
  • 原文地址:https://www.cnblogs.com/niejunlei/p/6113552.html
Copyright © 2011-2022 走看看