コード例 #1
0
  @Override
  public boolean process(String type, String message, Object object) {

    Topology topology = (Topology) object;
    if (ClusterCreatedEvent.class.getName().equals(type)) {
      // Return if topology has not been initialized
      if (!TopologyManager.isInitialized()) {
        return false;
      }

      // Parse complete message and build event
      ClusterCreatedEvent event =
          (ClusterCreatedEvent) MessagingUtil.jsonToObject(message, ClusterCreatedEvent.class);
      String serviceName = event.getCluster().getServiceName();
      TopologyUpdater.acquireWriteLockForService(serviceName);
      try {
        return doProcess(event, topology);

      } finally {
        TopologyUpdater.releaseWriteLockForService(serviceName);
      }

    } else {
      if (nextProcessor != null) {
        // ask the next processor to take care of the message.
        return nextProcessor.process(type, message, topology);
      } else {
        throw new RuntimeException(
            String.format(
                "Failed to process message using available message processors: [type] %s [body] %s",
                type, message));
      }
    }
  }
コード例 #2
0
  private boolean doProcess(ClusterCreatedEvent event, Topology topology) {
    Cluster cluster = event.getCluster();
    String applicationId = cluster.getAppId();
    String serviceName = cluster.getServiceName();
    String clusterId = cluster.getClusterId();

    // Apply application filter
    if (TopologyApplicationFilter.apply(applicationId)) {
      return false;
    }

    // Apply service filter
    if (TopologyServiceFilter.apply(serviceName)) {
      return false;
    }

    // Apply cluster filter
    if (TopologyClusterFilter.apply(clusterId)) {
      return false;
    }

    // Validate event against the existing topology
    Service service = topology.getService(serviceName);
    if (service == null) {
      if (log.isWarnEnabled()) {
        log.warn(String.format("Service does not exist: [service] %s", serviceName));
      }
      return false;
    }
    if (service.clusterExists(clusterId)) {
      if (log.isDebugEnabled()) {
        log.debug(
            String.format(
                "Cluster already exists in service: [service] %s " + "[cluster] %s",
                serviceName, clusterId));
      }
    } else {

      // Apply changes to the topology
      service.addCluster(cluster);
      if (log.isInfoEnabled()) {
        log.info(String.format("Cluster created: %s", cluster.toString()));
      }
    }

    // Notify event listeners
    notifyEventListeners(event);
    return true;
  }