All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.vertx.ext.healthchecks.impl.DefaultCompositeProcedure Maven / Gradle / Ivy

The newest version!
package io.vertx.ext.healthchecks.impl;

import io.vertx.core.Handler;
import io.vertx.ext.healthchecks.CheckResult;

import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author Clement Escoffier
 */
public class DefaultCompositeProcedure implements CompositeProcedure {

  private Map children = new HashMap<>();

  @Override
  public DefaultCompositeProcedure add(String name, Procedure check) {
    Objects.requireNonNull(name);
    Objects.requireNonNull(check);

    synchronized (this) {
      children.put(name, check);
    }

    return this;
  }

  @Override
  public synchronized boolean remove(String name) {
    Objects.requireNonNull(name);
    return children.remove(name) != null;
  }

  @Override
  public synchronized Procedure get(String name) {
    return children.get(name);
  }

  @Override
  public void check(Handler resultHandler) {
    List> copy;
    synchronized (this) {
      copy = new ArrayList<>(children.entrySet());
    }

    List checks = new ArrayList<>();
    int size = copy.size();
    CheckResult[] completed = new CheckResult[size];

    Runnable task = () -> {
      for (int j = 0;j < size;j++) {
        CheckResult json = completed[j];
        if (json.getId() == null) {
          json.setId(copy.get(j).getKey());
        }
        checks.add(json);
      }

      CheckResult result = new CheckResult();
      result.setChecks(checks);

      resultHandler.handle(result);
    };

    if (size == 0) {
      task.run();
      return;
    }

    AtomicInteger count = new AtomicInteger(size);


    for (int i = 0; i < size; i++) {
      int idx = i;
      Map.Entry entry = copy.get(idx);
      entry.getValue().check(res -> {
        completed[idx] = res;
        if (count.decrementAndGet() == 0) {
          task.run();
        }
      });
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy