日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

zookeeper分布式锁代码实例

發布時間:2024/9/30 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 zookeeper分布式锁代码实例 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

redis分布式鎖

模板:

/*** 分布式鎖模板類* Created by sunyujia@aliyun.com on 2016/2/23.*/ public interface DistributedLockTemplate {/**** @param lockId 鎖id(對應業務唯一ID)* @param timeout 單位毫秒* @param callback 回調函數* @return*/public Object execute(String lockId,int timeout,Callback callback); } public interface DistributedReentrantLock {public boolean tryLock(long timeout, TimeUnit unit) throws InterruptedException;public void unlock(); }

zk鎖:

public class ZkDistributedLockTemplate implements DistributedLockTemplate {private static final org.slf4j.Logger log = LoggerFactory.getLogger(ZkDistributedLockTemplate.class);private CuratorFramework client;public ZkDistributedLockTemplate(CuratorFramework client) {this.client = client;}private boolean tryLock(ZkReentrantLock distributedReentrantLock,Long timeout) throws Exception {return distributedReentrantLock.tryLock(timeout, TimeUnit.MILLISECONDS);}@Overridepublic Object execute(String lockId, int timeout, Callback callback) {ZkReentrantLock distributedReentrantLock = null;boolean getLock=false;try {distributedReentrantLock = new ZkReentrantLock(client,lockId);if(tryLock(distributedReentrantLock,new Long(timeout))){getLock=true;return callback.onGetLock();}else{return callback.onTimeout();}}catch(InterruptedException ex){log.error(ex.getMessage(), ex);Thread.currentThread().interrupt();}catch (Exception e) {log.error(e.getMessage(), e);}finally {if(getLock){distributedReentrantLock.unlock();}}return null;} } /*** 基于Zookeeper的可重入互斥鎖(關于重入:僅限于持有zk鎖的jvm內重入)* Created by sunyujia@aliyun.com on 2016/2/24.*/ public class ZkReentrantLock implements DistributedReentrantLock {private static final org.slf4j.Logger log = LoggerFactory.getLogger(ZkReentrantLock.class);/*** 線程池*/private static final ScheduledExecutorService executorService = Executors.newScheduledThreadPool(10);/*** 所有PERSISTENT鎖節點的根位置*/public static final String ROOT_PATH = "/ROOT_LOCK/";/*** 每次延遲清理PERSISTENT節點的時間 Unit:MILLISECONDS*/private long delayTimeForClean = 1000;/*** zk 共享鎖實現*/private InterProcessMutex interProcessMutex = null;/*** 鎖的ID,對應zk一個PERSISTENT節點,下掛EPHEMERAL節點.*/private String path;/*** zk的客戶端*/private CuratorFramework client;public ZkReentrantLock(CuratorFramework client, String lockId) {init(client, lockId);}public void init(CuratorFramework client, String lockId) {this.client = client;this.path = ROOT_PATH + lockId;interProcessMutex = new InterProcessMutex(client, this.path);}@Overridepublic boolean tryLock(long timeout, TimeUnit unit) throws InterruptedException {try {return interProcessMutex.acquire(timeout, unit);} catch (InterruptedException e) {throw e;} catch (Exception e) {log.error(e.getMessage(),e);throw new RuntimeException(e.getMessage(),e);}}@Overridepublic void unlock() {try {interProcessMutex.release();} catch (Throwable e) {log.error(e.getMessage(), e);} finally {executorService.schedule(new Cleaner(client, path), delayTimeForClean, TimeUnit.MILLISECONDS);}}static class Cleaner implements Runnable {CuratorFramework client;String path;public Cleaner(CuratorFramework client, String path) {this.client = client;this.path = path;}@Overridepublic void run() {try {List list = client.getChildren().forPath(path);if (list == null || list.isEmpty()) {client.delete().forPath(path);}} catch (KeeperException.NoNodeException e1) {//nothing} catch (KeeperException.NotEmptyException e2) {//nothing} catch (Exception e) {log.error(e.getMessage(), e);//準備刪除時,正好有線程創建鎖}}} }

總結

以上是生活随笔為你收集整理的zookeeper分布式锁代码实例的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。