Java Code Examples for org.apache.curator.framework.recipes.cache.TreeCacheEvent#Type

The following examples show how to use org.apache.curator.framework.recipes.cache.TreeCacheEvent#Type . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LogSearchConfigZKHelper.java    From ambari-logsearch with Apache License 2.0 6 votes vote down vote up
/**
 * Create listener for znode of log level filters - can be used for Log Feeder as it can be useful if it's monitoring the log level changes
 * @param clusterName name of the cluster
 * @param gson object to be used for json serialization
 * @param logLevelFilterMonitor log level filter monitor object that can be used to do something during znode chagne
 * @return listener response
 */
public static TreeCacheListener createTreeCacheListener(String clusterName, Gson gson, LogLevelFilterMonitor logLevelFilterMonitor) {
  return new TreeCacheListener() {
    private final Set<TreeCacheEvent.Type> nodeEvents = ImmutableSet.of(TreeCacheEvent.Type.NODE_ADDED, TreeCacheEvent.Type.NODE_UPDATED, TreeCacheEvent.Type.NODE_REMOVED);
    public void childEvent(CuratorFramework client, TreeCacheEvent event) throws Exception {
      if (!nodeEvents.contains(event.getType())) {
        return;
      }
      String nodeName = ZKPaths.getNodeFromPath(event.getData().getPath());
      String nodeData = new String(event.getData().getData());
      TreeCacheEvent.Type eventType = event.getType();

      String configPathStab = String.format("/%s/", clusterName);

      if (event.getData().getPath().startsWith(configPathStab + "loglevelfilter/")) {
        handleLogLevelFilterChange(eventType, nodeName, nodeData, gson, logLevelFilterMonitor);
      }
    }
  };
}
 
Example 2
Source File: LogSearchConfigZKHelper.java    From ambari-logsearch with Apache License 2.0 6 votes vote down vote up
/**
 * Call log level filter monitor interface to handle node related operations (on update/remove)
 * @param eventType zookeeper event type (add/update/remove)
 * @param nodeName name of the znode
 * @param nodeData znode data
 * @param gson object that can serialize inputs
 * @param logLevelFilterMonitor monitor object that can pass business logic that should happen during znode events
 */
static void handleLogLevelFilterChange(final TreeCacheEvent.Type eventType, final String nodeName, final String nodeData,
                                              final Gson gson, final LogLevelFilterMonitor logLevelFilterMonitor) {
  switch (eventType) {
    case NODE_ADDED:
    case NODE_UPDATED:
      logger.info("Node added/updated under loglevelfilter ZK node: " + nodeName);
      LogLevelFilter logLevelFilter = gson.fromJson(nodeData, LogLevelFilter.class);
      logLevelFilterMonitor.setLogLevelFilter(nodeName, logLevelFilter);
      break;
    case NODE_REMOVED:
      logger.info("Node removed loglevelfilter input ZK node: " + nodeName);
      logLevelFilterMonitor.removeLogLevelFilter(nodeName);
      break;
    default:
      break;
  }
}
 
Example 3
Source File: ZookeeperTreeCacheListener.java    From Thunder with Apache License 2.0 5 votes vote down vote up
@Override
public void childEvent(CuratorFramework client, TreeCacheEvent event) throws Exception {
    TreeCacheEvent.Type type = event.getType();
    switch (type) {
        case INITIALIZED:
            initialized(event);
            break;
        case NODE_ADDED:
            nodeAdded(event);
            break;
        case NODE_UPDATED:
            nodeUpdated(event);
            break;
        case NODE_REMOVED:
            nodeRemoved(event);
            break;
        case CONNECTION_SUSPENDED:
            connectionSuspended(event);
            break;
        case CONNECTION_RECONNECTED:
            connectionReconnected(event);
            break;
        case CONNECTION_LOST:
            connectionLost(event);
            break;
    }
}
 
Example 4
Source File: SimpleEventListener.java    From metron with Apache License 2.0 5 votes vote down vote up
/**
 * Add a callback bound to one or more TreeCacheEvent.Type.
 * @param callback The iterable of callbacks to be called when an event of each of types happens
 * @param types The zookeeper event types to bind to
 * @return The Builder
 */
public Builder with(Iterable<? extends Callback> callback, TreeCacheEvent.Type... types) {
  for(TreeCacheEvent.Type t : types) {
    List<Callback> cbs = callbacks.get(t);
    if(cbs == null) {
      cbs = new ArrayList<>();
    }
    Iterables.addAll(cbs, callback);
    callbacks.put(t, cbs);
  }
  return this;
}
 
Example 5
Source File: ConfigWatcher.java    From spring-cloud-zookeeper with Apache License 2.0 5 votes vote down vote up
@Override
public void childEvent(CuratorFramework client, TreeCacheEvent event)
		throws Exception {
	TreeCacheEvent.Type eventType = event.getType();
	if (eventType == NODE_ADDED || eventType == NODE_REMOVED
			|| eventType == NODE_UPDATED) {
		this.publisher
				.publishEvent(new RefreshEvent(this, event, getEventDesc(event)));
	}
}
 
Example 6
Source File: CuratorZookeeperClient.java    From FATE-Serving with Apache License 2.0 4 votes vote down vote up
@Override
public void childEvent(CuratorFramework client, TreeCacheEvent event) throws Exception {
    if (dataListener != null) {
        if (logger.isDebugEnabled()) {
            logger.debug("listen the zookeeper changed. The changed data:" + event.getData());
        }
        TreeCacheEvent.Type type = event.getType();
        EventType eventType = null;
        String content = null;
        String path = null;
        switch (type) {
            case NODE_ADDED:
                eventType = EventType.NodeCreated;
                path = event.getData().getPath();
                content = event.getData().getData() == null ? "" : new String(event.getData().getData(), CHARSET);
                break;
            case NODE_UPDATED:
                eventType = EventType.NodeDataChanged;
                path = event.getData().getPath();
                content = event.getData().getData() == null ? "" : new String(event.getData().getData(), CHARSET);
                break;
            case NODE_REMOVED:
                path = event.getData().getPath();
                eventType = EventType.NodeDeleted;
                break;
            case INITIALIZED:
                eventType = EventType.INITIALIZED;
                break;
            case CONNECTION_LOST:
                eventType = EventType.CONNECTION_LOST;
                break;
            case CONNECTION_RECONNECTED:
                eventType = EventType.CONNECTION_RECONNECTED;
                break;
            case CONNECTION_SUSPENDED:
                eventType = EventType.CONNECTION_SUSPENDED;
                break;
            default:
                break;
        }
        dataListener.dataChanged(path, content, eventType);
    }
}
 
Example 7
Source File: SimpleEventListener.java    From metron with Apache License 2.0 4 votes vote down vote up
private SimpleEventListener(EnumMap<TreeCacheEvent.Type, List<Callback>> callbacks) {
  this.callbacks = callbacks;
}
 
Example 8
Source File: DubboServiceDiscoveryAutoConfiguration.java    From spring-cloud-alibaba with Apache License 2.0 3 votes vote down vote up
/**
 * The {@link TreeCacheEvent#getType() event type} is supported or not.
 * @param event {@link TreeCacheEvent}
 * @return the rule is same as
 * {@link ZookeeperServiceWatch#childEvent(CuratorFramework, TreeCacheEvent)}
 * method
 */
private boolean supportsEventType(TreeCacheEvent event) {
	TreeCacheEvent.Type eventType = event.getType();
	return eventType.equals(TreeCacheEvent.Type.NODE_ADDED)
			|| eventType.equals(TreeCacheEvent.Type.NODE_REMOVED)
			|| eventType.equals(TreeCacheEvent.Type.NODE_UPDATED);
}
 
Example 9
Source File: SimpleEventListener.java    From metron with Apache License 2.0 2 votes vote down vote up
/**
 * Add a callback bound to one or more TreeCacheEvent.Type.
 * @param callback The callback to be called when an event of each of types happens
 * @param types The zookeeper event types to bind to
 * @return The Builder
 */
public Builder with(Callback callback, TreeCacheEvent.Type... types) {
  return with(ImmutableList.of(callback), types);
}