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

io.vertx.up.uca.web.anima.WorkerScatter Maven / Gradle / Ivy

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

import io.vertx.core.DeploymentOptions;
import io.vertx.core.Vertx;
import io.vertx.tp.plugin.etcd.center.EtcdData;
import io.vertx.up.annotations.Worker;
import io.vertx.up.eon.em.MessageModel;
import io.vertx.up.log.Annal;
import io.vertx.up.verticle.ZeroHttpWorker;
import io.vertx.up.uca.rs.Extractor;
import io.vertx.up.uca.rs.config.WorkerExtractor;
import io.vertx.up.util.Ut;
import io.vertx.up.runtime.ZeroAnno;

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

/**
 * Worker scatter to deploy workers
 */
public class WorkerScatter implements Scatter {

    @Override
    public void connect(final Vertx vertx) {
        /* 1.Find Workers for deploy **/
        final Set> sources = ZeroAnno.getWorkers();
        /* 2.Default Workers **/
        if (sources.isEmpty()) {
            sources.add(ZeroHttpWorker.class);
        }
        // Filter and extract by message model, this scatter only support
        // MessageModel equal REQUEST_RESPONSE
        final Set> workers = this.getTargets(sources);
        final Extractor extractor =
                Ut.instance(WorkerExtractor.class);
        final ConcurrentMap, DeploymentOptions> options =
                new ConcurrentHashMap<>();
        for (final Class worker : workers) {
            // 2.1 Worker deployment options
            final DeploymentOptions option = extractor.extract(worker);
            options.put(worker, option);
            // 2.2 Worker deployment
            Verticles.deploy(vertx, worker, option, this.getLogger());
        }
        // Runtime hooker
        Runtime.getRuntime().addShutdownHook(new Thread(() ->
                Ut.itSet(workers, (clazz, index) -> {
                    // 4. Undeploy Agent.
                    final DeploymentOptions opt = options.get(clazz);
                    Verticles.undeploy(vertx, clazz, opt, this.getLogger());
                })));
    }

    private Annal getLogger() {
        return Annal.get(this.getClass());
    }

    private Set> getTargets(final Set> sources) {
        final Set> workers = new HashSet<>();
        for (final Class source : sources) {
            final MessageModel model = Ut.invoke(source.getAnnotation(Worker.class), "value");
            if (this.getModel().contains(model)) {
                workers.add(source);
            }
        }
        return workers;
    }

    protected Set getModel() {
        // Enabled Micro model
        final Set models = new HashSet() {
            {
                this.add(MessageModel.REQUEST_RESPONSE);
            }
        };
        if (EtcdData.enabled()) {
            models.add(MessageModel.REQUEST_MICRO_WORKER);
        }
        return models;
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy