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

io.vertx.up.verticle.ZeroHttpWorker Maven / Gradle / Ivy

There is a newer version: 0.9.0
Show newest version
package io.vertx.up.verticle;

import io.vertx.core.AbstractVerticle;
import io.vertx.core.eventbus.EventBus;
import io.vertx.up.annotations.Ipc;
import io.vertx.up.annotations.Worker;
import io.vertx.up.atom.worker.Receipt;
import io.vertx.up.commune.Envelop;
import io.vertx.up.eon.Values;
import io.vertx.up.fn.Fn;
import io.vertx.up.log.Annal;
import io.vertx.up.runtime.ZeroAnno;
import io.vertx.up.uca.micro.invoke.Invoker;
import io.vertx.up.uca.micro.invoke.InvokerUtil;
import io.vertx.up.uca.micro.invoke.JetSelector;
import io.vertx.up.util.Ut;

import java.lang.reflect.Method;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * Default Http Server worker for different handler.
 * Recommend: Do not modify any workers that vertx zero provided.
 */
@Worker
public class ZeroHttpWorker extends AbstractVerticle {

    private static final Annal LOGGER = Annal.get(ZeroHttpWorker.class);

    private static final Set RECEIPTS = ZeroAnno.getReceipts();

    private static final ConcurrentMap INVOKER_MAP =
            new ConcurrentHashMap<>();

    private static final AtomicBoolean LOGGED = new AtomicBoolean(Boolean.FALSE);

    @Override
    public void start() {
        // 1. Get event bus
        final EventBus bus = vertx.eventBus();
        // 2. Consume address
        for (final Receipt receipt : RECEIPTS) {
            // 3. Deploy for each type
            final String address = receipt.getAddress();

            // 4. Get target reference and method
            final Object reference = receipt.getProxy();
            final Method method = receipt.getMethod();
            /*
             * In new version, there is not needed to verify
             * signature of length in arguments, because the new version
             * will support multi arguments in Worker component
             * Parameter length must be > 0.
             */
            InvokerUtil.verifyArgs(method, getClass());

            // length = 1
            final Class[] params = method.getParameterTypes();
            final Class returnType = method.getReturnType();
            final Class paramCls = params[Values.IDX];

            // 6. Invoker select
            final Invoker invoker = JetSelector.select(returnType, paramCls);
            invoker.ensure(returnType, paramCls);
            // 7. Record for different invokers
            INVOKER_MAP.put(receipt.hashCode(), invoker);

            Fn.safeJvm(() -> Fn.safeNull(() -> bus.consumer(address,
                    message -> {
                        if (method.isAnnotationPresent(Ipc.class)) {
                            // Rpc continue replying
                            invoker.next(reference, method, message, vertx);
                        } else {
                            // Direct replying
                            invoker.invoke(reference, method, message);
                        }
                    }),
                    address, reference, method), LOGGER
            );
        }
        // Record all the information;
        if (!LOGGED.getAndSet(Boolean.TRUE)) {
            final ConcurrentMap, Set> outputMap = new ConcurrentHashMap<>();
            INVOKER_MAP.forEach((key, value) -> {
                if (outputMap.containsKey(value.getClass())) {
                    outputMap.get(value.getClass()).add(key);
                } else {
                    outputMap.put(value.getClass(), new HashSet<>());
                }
            });
            outputMap.forEach((key, value) -> LOGGER.info(Info.MSG_INVOKER, key, Ut.toString(value), String.valueOf(value.size())));
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy