public void update(PathChildrenCacheEvent event) { LOGGER.debug("{}", event); String domain = ZKPaths.getNodeFromPath(event.getData().getPath()); switch (event.getType()) { case CHILD_ADDED: try { if (shouldConnectTo(domain)) { connectors.put( domain, startConnector(domain, toHostAndPort(event.getData().getData()))); } } catch (ExecutionException e) { LOGGER.error("Failed to start tunnel connector to {}: {}", domain, e.getCause()); } catch (InterruptedException ignored) { } break; case CHILD_REMOVED: String cell = connectors.remove(domain); if (cell != null) { getNucleus().kill(cell); } break; } }
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()); } // Lets just use the group name as the service path. path = Strings.splitAndTrimAsList(path, "/").get(0); 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) { LOG.info("Removed gateway service: " + path + ": " + new String(data, "UTF-8")); serviceMap.serviceRemoved(path, dto); } else { LOG.info("Updated gateway service: " + path + ": " + new String(data, "UTF-8")); 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); } }