07.Curator计数器
1.SharedCount
- SharedCount - 管理一个共享的整数。所有看同样的路径客户端将有共享的整数(考虑ZK的正常一致性保证)的最高最新的值。
- SharedCountReader - 一个共享的整数接口,并允许监听改变它的值。
- SharedCountListener - 用于监听共享整数发生变化的监听器。
public class SharedCounterExample implements SharedCountListener{private static final int QTY = 5;private static final String PATH = "/examples/counter";public static void main(String[] args) throws IOException, Exception{final Random rand = new Random();SharedCounterExample example = new SharedCounterExample();CuratorFramework client = CuratorFrameworkFactory.newClient("127.0.0.1:2181", new ExponentialBackoffRetry(1000, 3));client.start();SharedCount baseCount = new SharedCount(client, PATH, 0);baseCount.addListener(example);baseCount.start();List<SharedCount> examples = Lists.newArrayList();ExecutorService service = Executors.newFixedThreadPool(QTY);for (int i = 0; i < QTY; ++i){final SharedCount count = new SharedCount(client, PATH, 0);examples.add(count);Callable<Void> task = new Callable<Void>(){@Overridepublic Void call() throws Exception{count.start();Thread.sleep(rand.nextInt(10000));count.setCount(rand.nextInt(10000));System.out.println("计数器当前值:" + count.getVersionedValue().getValue());System.out.println("计数器当前版本:" + count.getVersionedValue().getVersion());System.out.println("trySetCount:" + count.trySetCount(count.getVersionedValue(), 123));return null;}};service.submit(task);}service.shutdown();service.awaitTermination(10, TimeUnit.MINUTES);for (int i = 0; i < QTY; ++i){examples.get(i).close();}baseCount.close();client.close();System.out.println("OK!");}@Overridepublic void stateChanged(CuratorFramework client, ConnectionState newState){System.out.println("连接状态: " + newState.toString());}@Overridepublic void countHasChanged(SharedCountReader sharedCount, int newCount) throws Exception{System.out.println("计数器值改变:" + newCount);}}
连接状态: CONNECTED计数器当前值:1684计数器当前版本:11trySetCount:true计数器值改变:123计数器当前值:8425计数器当前版本:13trySetCount:true计数器值改变:123计数器当前值:9369计数器当前版本:15trySetCount:true计数器值改变:123计数器当前值:4075计数器当前版本:17trySetCount:true计数器值改变:123计数器当前值:9221计数器当前版本:19trySetCount:trueOK!

2.DistributedAtomicLong
public class DistributedAtomicLong implements DistributedAtomicNumber<Long>{private final DistributedAtomicValue value;......}public class DistributedAtomicValue{......AtomicValue<byte[]> trySet(MakeValue makeValue) throws Exception{MutableAtomicValue<byte[]> result = new MutableAtomicValue<byte[]>(null, null, false);tryOptimistic(result, makeValue);if ( !result.succeeded() && (mutex != null) ){tryWithMutex(result, makeValue);}return result;}......}
- get(): 获取当前值
- increment(): 加一
- decrement(): 减一
- add(): 增加特定的值
- subtract(): 减去特定的值
- trySet(): 尝试设置计数值
- forceSet(): 强制设置计数值
public class DistributedAtomicLongExample{private static final int QTY = 5;private static final String PATH = "/examples/counter";public static void main(String[] args) throws IOException, Exception{final Random rand = new Random();CuratorFramework client = CuratorFrameworkFactory.newClient("127.0.0.1:2181", new ExponentialBackoffRetry(1000, 3));client.start();List<DistributedAtomicLong> examples = Lists.newArrayList();ExecutorService service = Executors.newFixedThreadPool(QTY);for (int i = 0; i < QTY; ++i){final DistributedAtomicLong count = new DistributedAtomicLong(client, PATH, new RetryNTimes(10, 10));examples.add(count);Callable<Void> task = new Callable<Void>(){@Overridepublic Void call() throws Exception{try{Thread.sleep(1000 + rand.nextInt(10000));AtomicValue<Long> value = count.increment();System.out.println("修改成功: " + value.succeeded());if (value.succeeded()){System.out.println("修改之前的值:" + value.preValue() + " | 修改之后的值:" + value.postValue());}}catch (Exception e){e.printStackTrace();}return null;}};service.submit(task);}service.shutdown();service.awaitTermination(10, TimeUnit.MINUTES);client.close();System.out.println("OK!");}}
修改成功: true修改之前的值:0 | 修改之后的值:1修改成功: true修改之前的值:1 | 修改之后的值:2修改成功: true修改之前的值:2 | 修改之后的值:3修改成功: true修改之前的值:3 | 修改之后的值:4修改成功: true修改之前的值:4 | 修改之后的值:5OK!

-------------------------------------------------------------------------------------------------------------------------------
07.Curator计数器的更多相关文章
- zookeeper之分布式锁以及分布式计数器(通过curator框架实现)
有人可能会问zookeeper我知道,但是curator是什么呢? 其实curator是apachede针对zookeeper开发的一个api框架是apache的顶级项目 他与zookeeper原生a ...
- PHP学习之[第07讲]PHP5.4 文件操作函数 之 图片计数器的实例
1.filetype():输出文件类型: 2.stat():获取文件的基本属性的数组: 3.clearstatcache().is_executable().isDir().idFile().scan ...
- Zookeeper开源客户端框架Curator简介
Curator是Netflix开源的一套ZooKeeper客户端框架. Netflix在使用ZooKeeper的过程中发现ZooKeeper自带的客户端太底层, 应用方在使用的时候需要自己处理很多事情 ...
- Zookeeper开源客户端框架Curator简介[转]
Curator是Netflix开源的一套ZooKeeper客户端框架. Netflix在使用ZooKeeper的过程中发现ZooKeeper自带的客户端太底层, 应用方在使用的时候需要自己处理很多事情 ...
- Python学习笔记——基础篇2【第三周】——计数器、有序字典、元组、单(双)向队列、深浅拷贝、函数、装饰器
目录 1.Python计数器Counter 2.Python有序字典OrderredDict 3.Python默认字典default 4.python可命名元组namedtuple 5.Python双 ...
- zookeeper(2)-curator
一.Curator介绍 zookeeper的提交人也说过,curator对于zookeeper而言就像是guava对于java差不多,更加优雅高效. 而且之前的zookeeper原生API,往往因为2 ...
- 15. 使用Apache Curator管理ZooKeeper
Apache ZooKeeper是为了帮助解决复杂问题的软件工具,它可以帮助用户从复杂的实现中解救出来. 然而,ZooKeeper只暴露了原语,这取决于用户如何使用这些原语来解决应用程序中的协调问题. ...
- Zookeeper客户端Curator基本API
在使用zookeper的时候一般不使用原生的API,Curator,解决了很多Zookeeper客户端非常底层的细节开发工作,包括连接重连.反复注册Watcher和NodeExistsExceptio ...
- MapReduce 计数器简介
转自:http://my.oschina.net/leejun2005/blog/276891?utm_source=tuicool&utm_medium=referral 1.计数器 简介 ...
随机推荐
- Sqrt算法
转自原文:http://www.cnblogs.com/pkuoliver/archive/2010/10/06/sotry-about-sqrt.html 一个Sqrt函数引发的血案 2010-10 ...
- zookper3.4.6集群配置
参考链接: http://blog.csdn.net/shirdrn/article/details/7183503 个人感觉zookeeper 安装在单机上无操作意义,所以直接记录集群配置过程. 连 ...
- Vuforia AR实战教程
官网:https://developer.vuforia.com/ Vuforia AR实战教程 http://www.taikr.com/my/course/531. AQaVpF//////AAA ...
- MathType初级教程:怎么安装MathType
MathType 由美国Design Science公司开发,是一款功能强大的数学公式编辑器,它同时支持Windows和Macintosh 两种操作系统,有很好的兼容性,能够在各种文档中加入复杂的数学 ...
- 如何使用ChemDraw绘制自由基符号
ChemDraw软件是一款全球领先的化学绘图工具,能够绘制各种复杂的化学符号和化学结构图形.ChemDraw汉化版结合了中国用户的使用习惯,可以帮助国内化学行业工作者更加轻松快捷地绘制化学图形.本教程 ...
- ScSR超分辨率的效果
- C语言中FILE是结构体,文件类型的指针
c语言文件类型指针 我们在定义文件类型指针变量后,称作该指针指向该文件,但本质上,它不是指向一个存储文件信息的结构型变量么?那么我们在用各个函数对所谓的“文件指针”进行操作时,本质上是不是函数通过获取 ...
- 超全面的JavaWeb笔记day09<Servlet&GenericServlet&HttpServlet&ServletContext>
1.Servlet概述 2.Servlet接口 3.GenericServlet 4.HttpServlet 5.Servlet细节 6.ServletContext(重要) Servlet概述 生命 ...
- 九度 1500:出操队形(LIS变形)
题目描述: 在读高中的时候,每天早上学校都要组织全校的师生进行跑步来锻炼身体,每当出操令吹响时,大家就开始往楼下跑了,然后身高矮的排在队伍的前面,身高较高的就要排在队尾.突然,有一天出操负责人想了一个 ...
- union和union all的并集(相加)区别
Union因为要进行重复值扫描,所以效率低.如果合并没有刻意要删除重复行,那么就使用Union All 两个要联合的SQL语句 字段个数必须一样,而且字段类型要“相容”(一致): 如果我们需要将两个 ...