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);
        }
    }
  • 相关阅读:
    qt tableview中如何添加右键菜单且不可编辑单元格
    C++typedef struct和struct的区别
    Qt5配置winpCap
    C++虚函数和纯虚函数的用法和区别
    c++11 智能指针 unique_ptr、shared_ptr与weak_ptr
    C++ windows进程间通信
    C++并发多线程(一)
    Maven无法下载2.1.7.js7版本的itext依赖
    PostgreSQL
    SQL
  • 原文地址:https://www.cnblogs.com/niejunlei/p/6113552.html
Copyright © 2011-2022 走看看