代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
Java
】
分布式锁的创建
作者:
智瑞
/ 发布于
2016/4/12
/
560
<dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.7</version> </dependency> Lock.java import java.net.InetAddress; import java.net.UnknownHostException; import org.apache.zookeeper.KeeperException; import org.apache.zookeeper.ZooKeeper; import org.apache.zookeeper.data.Stat; /** * 类名:<b>Lock</b> <br/> * * 类描述: * * 创建人:jiacheo <br/> * 创建时间:2011-1-27 上午01:30:25 <br/> * @version 2011-1-27 * */ public class Lock { private String path; private ZooKeeper zooKeeper; public Lock(String path){ this.path = path; } /** * * 方法描述: 上锁 lock it * * 创建人:jiacheo <br/> * 创建时间:2011-1-27 上午01:30:50 <br/> * @throws Exception */ public synchronized void lock() throws Exception{ Stat stat = zooKeeper.exists(path, true); String data = InetAddress.getLocalHost().getHostAddress()+":lock"; zooKeeper.setData(path, data.getBytes(), stat.getVersion()); } /** * * 方法描述:开锁 unlock it * * 创建人:jiacheo <br/> * 创建时间:2011-1-27 上午01:31:20 <br/> * @throws Exception */ public synchronized void unLock() throws Exception{ Stat stat = zooKeeper.exists(path, true); String data = InetAddress.getLocalHost().getHostAddress()+":unlock"; zooKeeper.setData(path, data.getBytes(), stat.getVersion()); } /** * * 方法描述:是否锁住了, isLocked? * * 创建人:jiacheo <br/> * 创建时间:2011-1-27 上午01:31:43 <br/> * @return */ public synchronized boolean isLock(){ try { Stat stat = zooKeeper.exists(path, true); String data = InetAddress.getLocalHost().getHostAddress()+":lock"; String nodeData = new String(zooKeeper.getData(path, true, stat)); if(data.equals(nodeData)){ // lock = true; return true; } } catch (UnknownHostException e) { // ignore it } catch (KeeperException e) { //TODO use log system and throw a new exception } catch (InterruptedException e) { // TODO use log system and throw a new exception } return false; } public String getPath() { return path; } public void setPath(String path) { this.path = path; } public void setZooKeeper(ZooKeeper zooKeeper) { this.zooKeeper = zooKeeper; } } LockFactory.java import java.io.IOException; import java.net.InetAddress; import java.util.Collections; import org.apache.zookeeper.CreateMode; import org.apache.zookeeper.WatchedEvent; import org.apache.zookeeper.Watcher; import org.apache.zookeeper.ZooKeeper; import org.apache.zookeeper.ZooDefs.Ids; import org.apache.zookeeper.ZooDefs.Perms; import org.apache.zookeeper.data.ACL; import org.apache.zookeeper.data.Stat; public class LockFactory { public static final ZooKeeper DEFAULT_ZOOKEEPER = getDefaultZookeeper(); //data格式: ip:stat 如: 10.232.35.70:lock 10.232.35.70:unlock public static synchronized Lock getLock(String path,String ip) throws Exception{ if(DEFAULT_ZOOKEEPER != null){ Stat stat = null; try{ stat = DEFAULT_ZOOKEEPER.exists(path, true); }catch (Exception e) { // TODO: use log system and throw new exception } if(stat!=null){ byte[] data = DEFAULT_ZOOKEEPER.getData(path, null, stat); String dataStr = new String(data); String[] ipv = dataStr.split(":"); if(ip.equals(ipv[0])){ Lock lock = new Lock(path); lock.setZooKeeper(DEFAULT_ZOOKEEPER); return lock; } //is not your lock, return null else{ return null; } } //no lock created yet, you can get it else{ createZnode(path); Lock lock = new Lock(path); lock.setZooKeeper(DEFAULT_ZOOKEEPER); return lock; } } return null; } private static ZooKeeper getDefaultZookeeper() { try { ZooKeeper zooKeeper = new ZooKeeper("10.232.35.72", 10*1000, new Watcher(){ public void process(WatchedEvent event) { //节点的事件处理. you can do something when the node's data change // System.out.println("event " + event.getType() + " has happened!"); } }); return zooKeeper; } catch (IOException e) { e.printStackTrace(); } return null; } private static void createZnode(String path) throws Exception{ if(DEFAULT_ZOOKEEPER!=null){ InetAddress address = InetAddress.getLocalHost(); String data = address.getHostAddress()+":unlock"; DEFAULT_ZOOKEEPER.create(path, data.getBytes(),Collections.singletonList(new ACL(Perms.ALL,Ids.ANYONE_ID_UNSAFE)) , CreateMode.EPHEMERAL); } } }
试试其它关键字
分布式
锁
同语言下
.
List 切割成几份 工具类
.
一行一行读取txt的内容
.
Java PDF转换成图片并输出给前台展示
.
java 多线程框架
.
double类型如果小数点后为零则显示整数否则保留两位小
.
将图片转换为Base64字符串公共类抽取
.
sqlParser 处理SQL(增删改查) 替换schema 用于多租户
.
JAVA 月份中的第几周处理 1-7属于第一周 依次类推 29-
.
java计算两个经纬度之间的距离
.
输入时间参数计算年龄
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
智瑞
贡献的其它代码
(
12
)
.
60s倒计时
.
获取json对象的Key、value
.
置字体和字体颜色和字体样式
.
xml格式化输出函数
.
队列的基本操作,出队,入队,读取head
.
自动换行的流式布局
.
多线程访问数据库
.
分布式锁的创建
.
有向图强连通分支(bfs邻接阵形式)
.
最小生成树(kruskal邻接表形式)
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3