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;
}
}
生成唯一值的思路之一
最新推荐文章于 2024-02-26 10:26:25 发布