use of org.I0Itec.zkclient.IZkChildListener in project databus by linkedin.
the class GroupLeadershipConnectionZkClientImpl method joinGroup.
@Override
public GroupLeadershipSession joinGroup(final String baseZkPath, final String groupName, String memberName, final AcceptLeadershipCallback acceptLeadershipCallback) {
final String resolvedMemberName = memberName == null ? makeUniqueMemberName() : memberName;
LOG.info("joinGroup: groupName=" + groupName + "; memberName=" + memberName + "; resolvedMemberName=" + resolvedMemberName);
ensurePersistentMembersZkPathExists(baseZkPath, groupName);
final GroupLeadershipSessionZkClientImpl groupLeadershipSession = new GroupLeadershipSessionZkClientImpl(baseZkPath, groupName, resolvedMemberName, acceptLeadershipCallback);
//Create and set call backs for data and child changes;
IZkChildListener childListener = new IZkChildListener() {
@Override
public void handleChildChange(String parentPath, List<String> currentChildren) throws Exception {
takeLeadershipIfNeeded(groupLeadershipSession);
}
};
//Data change call back ; try and get notified on creation of the ephemeral member node
IZkDataListener dataListener = new IZkDataListener() {
@Override
public void handleDataChange(String dataPath, Object data) throws Exception {
LOG.info("LeaderElection: Data change in: " + dataPath);
takeLeadershipIfNeeded(groupLeadershipSession);
}
@Override
public void handleDataDeleted(String dataPath) throws Exception {
relinquishLeadership(groupLeadershipSession);
}
};
groupLeadershipSession.setListener(childListener);
groupLeadershipSession.setDataListener(dataListener);
//create a ephemeral node for the member
String memberPath = makeMemberNodePath(baseZkPath, groupName, memberName);
String path = _zkClient.createEphemeralSequential(memberPath, groupLeadershipSession.getUniqueID());
groupLeadershipSession.setMemberZkName(memberName + path.substring(path.length() - 10));
String dataWatchPath = makeMemberNodePath(baseZkPath, groupName, groupLeadershipSession.getMemberZkName());
LOG.info("Created path:" + path + " zkName = " + groupLeadershipSession.getMemberZkName() + " data watch path: " + dataWatchPath);
//subscribe for data changes in this node; in case the node is not created at time takeLeadership is invoked
//could be a back door to trigger takeLeadershipIfNeeded without a restart
_zkClient.subscribeDataChanges(dataWatchPath, dataListener);
takeLeadershipIfNeeded(groupLeadershipSession);
return groupLeadershipSession;
}
use of org.I0Itec.zkclient.IZkChildListener in project motan by weibocom.
the class ZookeeperRegistry method subscribeService.
@Override
protected void subscribeService(final URL url, final ServiceListener serviceListener) {
try {
clientLock.lock();
ConcurrentHashMap<ServiceListener, IZkChildListener> childChangeListeners = serviceListeners.get(url);
if (childChangeListeners == null) {
serviceListeners.putIfAbsent(url, new ConcurrentHashMap<ServiceListener, IZkChildListener>());
childChangeListeners = serviceListeners.get(url);
}
IZkChildListener zkChildListener = childChangeListeners.get(serviceListener);
if (zkChildListener == null) {
childChangeListeners.putIfAbsent(serviceListener, new IZkChildListener() {
@Override
public void handleChildChange(String parentPath, List<String> currentChilds) {
serviceListener.notifyService(url, getUrl(), nodeChildsToUrls(parentPath, currentChilds));
LoggerUtil.info(String.format("[ZookeeperRegistry] service list change: path=%s, currentChilds=%s", parentPath, currentChilds.toString()));
}
});
zkChildListener = childChangeListeners.get(serviceListener);
}
// 防止旧节点未正常注销
removeNode(url, ZkNodeType.CLIENT);
createNode(url, ZkNodeType.CLIENT);
String serverTypePath = ZkUtils.toNodeTypePath(url, ZkNodeType.AVAILABLE_SERVER);
zkClient.subscribeChildChanges(serverTypePath, zkChildListener);
LoggerUtil.info(String.format("[ZookeeperRegistry] subscribe service: path=%s, info=%s", ZkUtils.toNodePath(url, ZkNodeType.AVAILABLE_SERVER), url.toFullStr()));
} catch (Throwable e) {
throw new MotanFrameworkException(String.format("Failed to subscribe %s to zookeeper(%s), cause: %s", url, getUrl(), e.getMessage()), e);
} finally {
clientLock.unlock();
}
}
use of org.I0Itec.zkclient.IZkChildListener in project jesos by groupon.
the class ZookeeperMasterDetector method start.
public void start() {
if (!running.getAndSet(true)) {
processChildList(client.getChildren(zookeeperPath));
client.subscribeChildChanges(zookeeperPath, new IZkChildListener() {
@Override
public void handleChildChange(final String parentPath, final List<String> currentChildren) throws Exception {
checkState(zookeeperPath.equals(parentPath), "Received Event for %s (expected %s)", parentPath, zookeeperPath);
processChildList(currentChildren);
}
});
}
}
use of org.I0Itec.zkclient.IZkChildListener in project motan by weibocom.
the class ZookeeperRegistry method reconnectClient.
@SuppressWarnings("rawtypes")
private void reconnectClient() {
if (serviceListeners != null && !serviceListeners.isEmpty()) {
try {
clientLock.lock();
for (Map.Entry entry : serviceListeners.entrySet()) {
URL url = (URL) entry.getKey();
ConcurrentHashMap<ServiceListener, IZkChildListener> childChangeListeners = serviceListeners.get(url);
if (childChangeListeners != null) {
for (Map.Entry e : childChangeListeners.entrySet()) {
subscribeService(url, (ServiceListener) e.getKey());
}
}
}
for (Map.Entry entry : commandListeners.entrySet()) {
URL url = (URL) entry.getKey();
ConcurrentHashMap<CommandListener, IZkDataListener> dataChangeListeners = commandListeners.get(url);
if (dataChangeListeners != null) {
for (Map.Entry e : dataChangeListeners.entrySet()) {
subscribeCommand(url, (CommandListener) e.getKey());
}
}
}
LoggerUtil.info("[{}] reconnect all clients", registryClassName);
} finally {
clientLock.unlock();
}
}
}
use of org.I0Itec.zkclient.IZkChildListener in project motan by weibocom.
the class ZookeeperRegistry method unsubscribeService.
@Override
protected void unsubscribeService(URL url, ServiceListener serviceListener) {
try {
clientLock.lock();
Map<ServiceListener, IZkChildListener> childChangeListeners = serviceListeners.get(url);
if (childChangeListeners != null) {
IZkChildListener zkChildListener = childChangeListeners.get(serviceListener);
if (zkChildListener != null) {
zkClient.unsubscribeChildChanges(ZkUtils.toNodeTypePath(url, ZkNodeType.CLIENT), zkChildListener);
childChangeListeners.remove(serviceListener);
}
}
} catch (Throwable e) {
throw new MotanFrameworkException(String.format("Failed to unsubscribe service %s to zookeeper(%s), cause: %s", url, getUrl(), e.getMessage()), e);
} finally {
clientLock.unlock();
}
}
Aggregations