use of org.apache.curator.framework.recipes.cache.ChildData in project paascloud-master by paascloud.
the class MqProducerChangeListener method childEvent.
/**
* Child event.
*
* @param client the client
* @param event the event
*/
@Override
public void childEvent(final CuratorFramework client, final TreeCacheEvent event) {
ChildData data = event.getData();
if (data == null) {
return;
}
String path = data.getPath();
if (GlobalConstant.ZK_REGISTRY_PRODUCER_ROOT_PATH.equals(path) || GlobalConstant.ZK_REGISTRY_CONSUMER_ROOT_PATH.equals(path)) {
return;
}
String[] split = path.split(GlobalConstant.Symbol.SLASH);
String dataStr = new String(data.getData());
switch(event.getType()) {
case NODE_ADDED:
log.info("MqProducerChangeListener CHILD_ADDED path={}, data={}", path, dataStr);
if (split.length == GlobalConstant.Number.SIX_INT) {
String appPath = path.substring(0, path.lastIndexOf(GlobalConstant.Symbol.SLASH));
ReliableMessageRegisterDto dto = JSON.parseObject(getDirectly(client, appPath), ReliableMessageRegisterDto.class);
if (this.getNumChildren(client, appPath) > 0) {
tpcMqProducerService.updateOnLineStatusByPid(dto.getProducerGroup());
MqProducerBeanFactory.buildProducerBean(dto);
MqProducerBeanFactory.putPid(dto.getProducerGroup());
}
}
break;
case NODE_REMOVED:
log.info("MqProducerChangeListener NODE_REMOVED path={}, data={}", path, dataStr);
if (split.length == GlobalConstant.Number.SIX_INT) {
String appPath = path.substring(0, path.lastIndexOf(GlobalConstant.Symbol.SLASH));
ReliableMessageRegisterDto dto = JSON.parseObject(getDirectly(client, appPath), ReliableMessageRegisterDto.class);
if (this.getNumChildren(client, appPath) < 1) {
tpcMqProducerService.updateOffLineStatusByPid(dto.getProducerGroup());
MqProducerBeanFactory.rmPid(dto.getProducerGroup());
}
}
break;
case NODE_UPDATED:
log.error("本次版本不对更新做处理, path={}, data={}", path, new String(data.getData()));
break;
default:
break;
}
}
use of org.apache.curator.framework.recipes.cache.ChildData in project fabric8 by jboss-fuse.
the class GatewayServiceTreeCache method treeCacheEvent.
protected void treeCacheEvent(PathChildrenCacheEvent event) {
ChildData childData = event.getData();
if (childData == null) {
return;
}
String path = childData.getPath();
LOG.trace("Event {} on path {}", event.getType().toString(), path);
PathChildrenCacheEvent.Type type = event.getType();
byte[] data = childData.getData();
if (data == null || data.length == 0 || path == null) {
return;
}
if (path.startsWith(zkPath)) {
path = path.substring(zkPath.length());
}
List<String> split = Strings.splitAndTrimAsList(path, "/");
// Lets just use the group name as the service path.
path = split.get(0);
String zNode = split.get(1);
boolean remove = false;
switch(type) {
case CHILD_ADDED:
case CHILD_UPDATED:
break;
case CHILD_REMOVED:
remove = true;
break;
default:
return;
}
ServiceDTO dto = null;
try {
dto = mapper.readValue(data, ServiceDTO.class);
expandPropertyResolvers(dto);
dto.setContainer(dto.getContainer() + "_zNode_" + zNode);
if (remove) {
LOG.info("Removed gateway service: " + path + ": " + dto);
serviceMap.serviceRemoved(path, dto);
} else {
LOG.info("Updated gateway service: " + path + ": " + dto);
serviceMap.serviceUpdated(path, dto);
}
} catch (IOException e) {
LOG.warn("Failed to parse the JSON: " + new String(data) + ". Reason: " + e, e);
} catch (URISyntaxException e) {
LOG.warn("Failed to update URI for dto: " + dto + ", .Reason: " + e, e);
}
}
use of org.apache.curator.framework.recipes.cache.ChildData in project fabric8 by jboss-fuse.
the class GatewayServiceTreeCache method treeCacheEvent.
protected void treeCacheEvent(PathChildrenCacheEvent event) {
ChildData childData = event.getData();
if (childData == null) {
return;
}
String path = childData.getPath();
PathChildrenCacheEvent.Type type = event.getType();
byte[] data = childData.getData();
if (data == null || data.length == 0 || path == null) {
return;
}
if (path.startsWith(zkPath)) {
path = path.substring(zkPath.length());
}
boolean remove = false;
switch(type) {
case CHILD_ADDED:
case CHILD_UPDATED:
break;
case CHILD_REMOVED:
remove = true;
break;
default:
return;
}
ServiceDTO dto = null;
try {
dto = mapper.readValue(data, ServiceDTO.class);
expandPropertyResolvers(dto);
if (remove) {
serviceMap.serviceRemoved(path, dto);
} else {
serviceMap.serviceUpdated(path, dto);
}
} catch (IOException e) {
LOG.warn("Failed to parse the JSON: " + new String(data) + ". Reason: " + e, e);
} catch (URISyntaxException e) {
LOG.warn("Failed to update URI for dto: " + dto + ", .Reason: " + e, e);
}
}
use of org.apache.curator.framework.recipes.cache.ChildData in project fabric8 by jboss-fuse.
the class GatewayServiceTreeCacheTest method event.
private static PathChildrenCacheEvent event(String path, PathChildrenCacheEvent.Type type, String data) {
PathChildrenCacheEvent event = mock(PathChildrenCacheEvent.class);
ChildData childData = mock(ChildData.class);
when(event.getData()).thenReturn(childData);
when(childData.getPath()).thenReturn(path);
when(childData.getData()).thenReturn(data.getBytes());
when(event.getType()).thenReturn(type);
return event;
}
use of org.apache.curator.framework.recipes.cache.ChildData in project fabric8 by jboss-fuse.
the class HttpMappingZooKeeperTreeCache method treeCacheEvent.
protected void treeCacheEvent(PathChildrenCacheEvent event) {
String zkPath = zooKeeperPath;
ChildData childData = event.getData();
if (childData == null) {
return;
}
String path = childData.getPath();
Type type = event.getType();
byte[] data = childData.getData();
if (data == null || data.length == 0 || path == null) {
return;
}
if (path.startsWith(zkPath)) {
path = path.substring(zkPath.length());
}
// TODO should we remove the version too and pick that one?
// and include the version in the service chooser?
boolean remove = false;
switch(type) {
case CHILD_ADDED:
case CHILD_UPDATED:
break;
case CHILD_REMOVED:
remove = true;
break;
default:
return;
}
ServiceDTO dto = null;
try {
dto = mapper.readValue(data, ServiceDTO.class);
expandPropertyResolvers(dto);
List<String> services = dto.getServices();
Map<String, String> params = new HashMap<String, String>();
params.put("id", paramValue(dto.getId()));
params.put("container", paramValue(dto.getContainer()));
params.put("version", paramValue(dto.getVersion()));
params.put("bundleName", paramValue(dto.getBundleName()));
params.put("bundleVersion", paramValue(dto.getBundleVersion()));
mappingRuleConfiguration.updateMappingRules(remove, path, services, params, dto);
} catch (IOException e) {
LOG.warn("Failed to parse the JSON: " + new String(data) + ". Reason: " + e, e);
} catch (URISyntaxException e) {
LOG.warn("Failed to update URI for dto: " + dto + ", .Reason: " + e, e);
}
}
Aggregations