Search in sources :

Example 36 with ChildData

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;
    }
}
Also used : ReliableMessageRegisterDto(com.paascloud.core.registry.base.ReliableMessageRegisterDto) ChildData(org.apache.curator.framework.recipes.cache.ChildData)

Example 37 with ChildData

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);
    }
}
Also used : PathChildrenCacheEvent(org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent) ChildData(org.apache.curator.framework.recipes.cache.ChildData) ServiceDTO(io.fabric8.gateway.ServiceDTO) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException)

Example 38 with ChildData

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);
    }
}
Also used : PathChildrenCacheEvent(org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent) ChildData(org.apache.curator.framework.recipes.cache.ChildData) ServiceDTO(io.fabric8.gateway.ServiceDTO) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException)

Example 39 with ChildData

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;
}
Also used : PathChildrenCacheEvent(org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent) ChildData(org.apache.curator.framework.recipes.cache.ChildData)

Example 40 with ChildData

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);
    }
}
Also used : Type(org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent.Type) ChildData(org.apache.curator.framework.recipes.cache.ChildData) ServiceDTO(io.fabric8.gateway.ServiceDTO) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException)

Aggregations

ChildData (org.apache.curator.framework.recipes.cache.ChildData)119 Test (org.junit.Test)75 TreeCacheEvent (org.apache.curator.framework.recipes.cache.TreeCacheEvent)70 IOException (java.io.IOException)10 PathChildrenCacheEvent (org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent)9 CuratorFramework (org.apache.curator.framework.CuratorFramework)8 LeaderElectionJobListener (com.dangdang.ddframe.job.lite.internal.election.ElectionListenerManager.LeaderElectionJobListener)7 SimpleJobConfiguration (com.dangdang.ddframe.job.config.simple.SimpleJobConfiguration)6 PathChildrenCache (org.apache.curator.framework.recipes.cache.PathChildrenCache)6 KeeperException (org.apache.zookeeper.KeeperException)6 JobPausedStatusJobListener (com.dangdang.ddframe.job.lite.internal.server.JobOperationListenerManager.JobPausedStatusJobListener)5 PathChildrenCacheListener (org.apache.curator.framework.recipes.cache.PathChildrenCacheListener)5 TreeCache (org.apache.curator.framework.recipes.cache.TreeCache)5 TestSimpleJob (com.dangdang.ddframe.job.lite.fixture.TestSimpleJob)4 CronSettingAndJobEventChangedJobListener (com.dangdang.ddframe.job.lite.internal.config.ConfigurationListenerManager.CronSettingAndJobEventChangedJobListener)4 MonitorExecutionChangedJobListener (com.dangdang.ddframe.job.lite.internal.execution.ExecutionListenerManager.MonitorExecutionChangedJobListener)4 ByteArrayInputStream (java.io.ByteArrayInputStream)4 ServiceDTO (io.fabric8.gateway.ServiceDTO)3 ObjectInputStream (java.io.ObjectInputStream)3 URISyntaxException (java.net.URISyntaxException)3