您的位置:首页 > 其它

生成唯一值的思路之一

2016-06-30 17:34 253 查看
public class Sequence {
private static final String SEQUENCE_ROOT = "/sequence";
private static final String PREFIX_NODE = "num";
private ZookeeperClient zookeeperClient;

public Sequence() {
}

public String generate() {
String time = DateUtils.formatDate(new Date(), DATE_FORMAT.DETAIL.getFormat());

try {
String e = (String)((ACLBackgroundPathAndBytesable)this.zookeeperClient.getClient().create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT_SEQUENTIAL)).forPath(ZKPaths.makePath("/sequence", time, new String[]{"num"}), new byte[0]);
String[] nodes = e.split("/");
String node = nodes[nodes.length - 1];
String suffix = node.substring("num".length());
return time + suffix.substring(suffix.length() - 4);
} catch (Exception var6) {
var6.printStackTrace();
return null;
}
}

@Autowired
public void setZookeeperClient(ZookeeperClient zookeeperClient) {
this.zookeeperClient = zookeeperClient;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  唯一值