some small refactors

This commit is contained in:
DigitalSonic
2020-02-05 16:40:40 +08:00
parent 7f43c62cb6
commit ac170d5e97
3 changed files with 44 additions and 9 deletions
@@ -76,6 +76,20 @@ public class NodeEvent {
return list;
}
@Override
public String toString() {
String str = "NodeEvent{" +
"type=" + type +
", nodeName='" + nodeName + '\'' +
", url='" + url + '\'' +
", username='" + username + '\'';
if (password != null) {
str += ", password.length=" + password.length();
}
str += '}';
return str;
}
public NodeEventTypeEnum getType() {
return type;
}
@@ -15,6 +15,7 @@
*/
package com.alibaba.druid.pool.ha.node;
import com.alibaba.druid.DruidRuntimeException;
import com.alibaba.druid.support.logging.Log;
import com.alibaba.druid.support.logging.LogFactory;
@@ -57,7 +58,7 @@ public abstract class NodeListener extends Observable {
*/
public void init() {
if (observer == null) {
throw new RuntimeException("No Observer(such as PoolUpdater) specified.");
throw new DruidRuntimeException("No Observer(such as PoolUpdater) specified.");
}
this.addObserver(observer);
}
@@ -40,6 +40,7 @@ public class ZookeeperNodeRegister {
private String path = "/ha-druid-datasources";
private CuratorFramework client;
private GroupMember member;
private boolean privateZkClient = false; // Should I close the client?
private Lock lock = new ReentrantLock();
/**
@@ -54,6 +55,7 @@ public class ZookeeperNodeRegister {
.sessionTimeoutMs(30000)
.build();
client.start();
privateZkClient = true;
}
}
@@ -74,6 +76,7 @@ public class ZookeeperNodeRegister {
}
try {
lock.lock();
createPathIfNotExisted();
if (member != null) {
LOG.warn("GroupMember has already registered. Please deregister first.");
return false;
@@ -96,6 +99,9 @@ public class ZookeeperNodeRegister {
member.close();
member = null;
}
if (client != null && privateZkClient) {
client.close();
}
}
/**
@@ -105,6 +111,17 @@ public class ZookeeperNodeRegister {
deregister();
}
private void createPathIfNotExisted() {
try {
if (client.checkExists().forPath(path) == null) {
LOG.info("Path[" + path + "] is NOT existed, create it.");
client.create().creatingParentsIfNeeded().forPath(path);
}
} catch(Exception e) {
LOG.error("Can NOT check the path.", e);
}
}
private String getPropertiesString(List<ZookeeperNodeInfo> payload) {
Properties properties = new Properties();
for (ZookeeperNodeInfo n : payload) {
@@ -130,6 +147,17 @@ public class ZookeeperNodeRegister {
return sw.toString();
}
public void setClient(CuratorFramework client) {
if (client != null) {
this.client = client;
privateZkClient = false;
}
}
public CuratorFramework getClient() {
return client;
}
public String getZkConnectString() {
return zkConnectString;
}
@@ -145,12 +173,4 @@ public class ZookeeperNodeRegister {
public void setPath(String path) {
this.path = path;
}
public CuratorFramework getClient() {
return client;
}
public void setClient(CuratorFramework client) {
this.client = client;
}
}