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

io.vertx.up.uca.monitor.meansure.VerticleQuota Maven / Gradle / Ivy

There is a newer version: 0.9.0
Show newest version
package io.vertx.up.uca.monitor.meansure;

import io.vertx.core.Future;
import io.vertx.core.Promise;
import io.vertx.core.Vertx;
import io.vertx.core.json.JsonObject;
import io.vertx.core.shareddata.AsyncMap;
import io.vertx.ext.healthchecks.Status;
import io.vertx.up.eon.Constants;
import io.vertx.up.unity.Ux;

import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

/**
 * @author Lang
 */
class VerticleQuota extends AbstractQuota {
    VerticleQuota(final Vertx vertx) {
        super(vertx);
    }

    @Override
    public void handle(final Promise event) {
        // Context reference
        this.mapAsync(Constants.Pool.DEPLOYMENT, map -> {
            /*
             * map calculated
             */
            final ConcurrentMap> mapped =
                new ConcurrentHashMap<>();
            map.keys(keyRes -> {
                if (keyRes.succeeded()) {
                    final Set keys = keyRes.result();
                    keys.forEach(key -> mapped.put(key, this.readAsync(key, map)));
                }
            });
            final Future> future = Ux.thenCombine(mapped);
            future.onComplete(handler -> {
                if (handler.succeeded()) {
                    final JsonObject meansure = new JsonObject();
                    final ConcurrentMap result = handler.result();
                    result.forEach(meansure::put);
                    event.complete(Status.OK(meansure));
                }
            });
        });
    }

    private Future readAsync(final String name, final AsyncMap map) {
        final Promise promise = Promise.promise();
        map.get(name, res -> {
            if (res.succeeded()) {
                final JsonObject completed = (JsonObject) res.result();
                promise.complete(completed);
            }
        });
        return promise.future();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy