com.app.common.zk.ZookeeperClient Maven / Gradle / Ivy
The newest version!
package com.app.common.zk;
import java.nio.charset.Charset;
import java.util.List;
import org.I0Itec.zkclient.IZkChildListener;
import org.I0Itec.zkclient.IZkDataListener;
import org.I0Itec.zkclient.IZkStateListener;
import org.I0Itec.zkclient.ZkClient;
import org.I0Itec.zkclient.exception.ZkMarshallingError;
import org.I0Itec.zkclient.serialize.ZkSerializer;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.Watcher.Event.KeeperState;
import com.app.common.utils.StringUtil;
import com.mysql.jdbc.StringUtils;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class ZookeeperClient {
private ZkClient zk = null;;
private IEventListener eventlistener = null;
public IEventListener getEventlistener() {
return eventlistener;
}
public void setEventlistener(IEventListener eventlistener) {
this.eventlistener = eventlistener;
}
private void onEvent(int code) {
if (eventlistener != null) {
eventlistener.onEvent(code);
}
}
public class zkSerializer implements ZkSerializer {
@Override
public Object deserialize(byte[] bytes) throws ZkMarshallingError {
return new String(bytes, Charset.forName("UTF-8"));
}
@Override
public byte[] serialize(Object serializable) throws ZkMarshallingError {
return ((String) serializable).getBytes(Charset.forName("UTF-8"));
}
}
public ZookeeperClient(String hosts, String username, String password) {
zk = new ZkClient(hosts);
log.info("ZookeeperClient hosts ->{} username -> {} password -> {}",hosts, username, password);
if (StringUtil.isNotBlank(username) || StringUtil.isNotBlank(password)) {
zk.addAuthInfo("digest", (username + ":" + password).getBytes());
}
zk.setZkSerializer(new zkSerializer());
IZkStateListener listener = new IZkStateListener() {
@Override
public void handleStateChanged(KeeperState state) throws Exception {
if (state == KeeperState.Disconnected) {
onEvent(IEventListener.CONNECTION_CLOSED);
}
if (state == KeeperState.SyncConnected) {
onEvent(IEventListener.CONNECTION_SUCCESS);
}
}
@Override
public void handleSessionEstablishmentError(Throwable error) throws Exception {
// TODO Auto-generated method stub
}
@Override
public void handleNewSession() throws Exception {
// TODO Auto-generated method stub
}
};
zk.subscribeStateChanges(listener);
}
public void subscribeStateChanges(IZkStateListener listener) {
zk.subscribeStateChanges(listener);
}
public void subscribeDataChanges(String path, IZkDataListener listener) {
zk.subscribeDataChanges(path, listener);
}
public void subscribeChildChanges(String path, IZkChildListener listener) {
zk.subscribeChildChanges(path, listener);
}
public boolean exists(String path) {
try {
return zk.exists(path);
} catch (Exception e) {
return false;
}
}
public List getChildren(String path) {
return zk.getChildren(path);
}
public String create(final String path, Object data, final CreateMode mode) {
return zk.create(path, data, mode);
}
public Object readData(String path) {
try {
return zk.readData(path);
} catch (Exception e) {
return "";
}
}
public void writeData(String path, Object object) {
zk.writeData(path, object);
}
public void close() {
zk.close();
}
public void writeData2(String path, String data) {
String[] paths = path.split("/");
String pathTmp = "";
for (String str : paths) {
if (!StringUtils.isNullOrEmpty(str)) {
pathTmp = pathTmp + "/" + str;
String dataTmp = "";
if (pathTmp.equals(path)) {
dataTmp = data;
}
if (!exists(pathTmp)) {
create(pathTmp, dataTmp, CreateMode.PERSISTENT);
} else if (!StringUtils.isNullOrEmpty(dataTmp)) {
writeData(pathTmp, data);
}
}
}
}
}