Search in sources :

Example 46 with Future

use of io.vertx.core.Future in project VX-API-Gateway by EliMirren.

the class VxApiMain method start.

@Override
public void start(Future<Void> fut) throws Exception {
    System.out.println("start VX-API...");
    getConfig(conf -> {
        if (conf.succeeded()) {
            try {
                // verticle的配置信息
                JsonObject option = conf.result().getJsonObject("verticleConfig", new JsonObject());
                // 数据的配置文件
                JsonObject dataConfig = conf.result().getJsonObject("dataConfig", getDefaultDataConfig());
                // 客户端的配置文件
                JsonObject clientConfig = conf.result().getJsonObject("clientConfig", getDefaultClientConfig());
                // CLI配置文件
                JsonObject cliConfig = conf.result().getJsonObject("cliConfig", getDefauluCLIConfig());
                @SuppressWarnings("rawtypes") List<Future> futures = new ArrayList<>();
                // 启动系统服务Verticle
                futures.add(Future.<String>future(sysInfo -> {
                    vertx.deployVerticle(SysVerticle.class.getName(), new DeploymentOptions(option), sysInfo);
                }));
                // 启动数据服务Verticle
                futures.add(Future.<String>future(data -> {
                    vertx.deployVerticle(DATAVerticle.class.getName(), new DeploymentOptions(option).setConfig(dataConfig), data);
                }));
                // 启动应用服务Verticle
                futures.add(Future.<String>future(deploy -> {
                    vertx.deployVerticle(DeploymentVerticle.class.getName(), new DeploymentOptions(option), deploy);
                }));
                // 启动CLI服务Verticle
                futures.add(Future.<String>future(cli -> {
                    vertx.deployVerticle(CLIVerticle.class.getName(), new DeploymentOptions(option), cli);
                }));
                // 启动CLI服务Verticle
                futures.add(Future.<String>future(client -> {
                    vertx.deployVerticle(ClientVerticle.class.getName(), new DeploymentOptions(option).setConfig(clientConfig), client);
                }));
                CompositeFuture.all(futures).setHandler(res -> {
                    if (res.succeeded()) {
                        runCLICommand(cliConfig, cliRes -> {
                            if (cliRes.succeeded()) {
                                System.out.println("start VX-API successful");
                                System.out.println("The Clinet running on port " + clientConfig.getInteger("clientPort", 5256));
                                fut.complete();
                            } else {
                                System.out.println("start VX-API unsuccessful");
                                System.out.println(cliRes.cause());
                                fut.fail(cliRes.cause());
                            }
                        });
                    } else {
                        System.out.println("start VX-API unsuccessful");
                        System.out.println(res.cause());
                        fut.fail(res.cause());
                    }
                });
            } catch (Exception e) {
                System.out.println("start VX-API unsuccessful");
                System.out.println(e);
                fut.fail(e);
            }
        } else {
            System.out.println("start VX-API unsuccessful");
            System.out.println(conf.cause());
            fut.fail(conf.cause());
        }
    });
}
Also used : CLIVerticle(com.szmirren.vxApi.core.verticle.CLIVerticle) DeploymentVerticle(com.szmirren.vxApi.core.verticle.DeploymentVerticle) SysVerticle(com.szmirren.vxApi.core.verticle.SysVerticle) ClientVerticle(com.szmirren.vxApi.core.verticle.ClientVerticle) DATAVerticle(com.szmirren.vxApi.core.verticle.DATAVerticle) Future(io.vertx.core.Future) PathUtil(com.szmirren.vxApi.core.common.PathUtil) ArrayList(java.util.ArrayList) VxApiCluster(com.szmirren.vxApi.cluster.VxApiCluster) VxApiEventBusAddressConstant(com.szmirren.vxApi.core.common.VxApiEventBusAddressConstant) CompositeFuture(io.vertx.core.CompositeFuture) List(java.util.List) Logger(org.apache.logging.log4j.Logger) DeploymentOptions(io.vertx.core.DeploymentOptions) VxApiClusterFactory(com.szmirren.vxApi.cluster.VxApiClusterFactory) AbstractVerticle(io.vertx.core.AbstractVerticle) JsonObject(io.vertx.core.json.JsonObject) AsyncResult(io.vertx.core.AsyncResult) Handler(io.vertx.core.Handler) LogManager(org.apache.logging.log4j.LogManager) DeploymentOptions(io.vertx.core.DeploymentOptions) ArrayList(java.util.ArrayList) JsonObject(io.vertx.core.json.JsonObject) Future(io.vertx.core.Future) CompositeFuture(io.vertx.core.CompositeFuture)

Example 47 with Future

use of io.vertx.core.Future in project vertx-zero by silentbalanceyh.

the class AsyncInvoker method invoke.

@Override
@SuppressWarnings("unchecked")
public void invoke(final Object proxy, final Method method, final Message<Envelop> message) {
    final Envelop envelop = message.body();
    // Get type of parameter first element
    final Class<?> argType = method.getParameterTypes()[Values.IDX];
    // Deserialization from message bus.
    final Class<?> returnType = method.getReturnType();
    LOGGER.info(Info.MSG_FUTURE, this.getClass(), returnType, false);
    // Get T
    final Class<?> tCls = returnType.getComponentType();
    if (Envelop.class == tCls) {
        // Input type is Envelop, input directly
        final Future<Envelop> result = Instance.invoke(proxy, method.getName(), envelop);
        result.setHandler(item -> message.reply(item.result()));
    } else {
        final Object reference = envelop.data();
        final Object arguments = Ut.deserialize(Ut.toString(reference), argType);
        final Future tResult = Instance.invoke(proxy, method.getName(), arguments);
        tResult.setHandler(Ux.toHandler(message));
    }
}
Also used : Envelop(io.vertx.up.atom.Envelop) Future(io.vertx.core.Future)

Example 48 with Future

use of io.vertx.core.Future in project vertx-zero by silentbalanceyh.

the class FutureInvoker method next.

@Override
@SuppressWarnings("unchecked")
public void next(final Object proxy, final Method method, final Message<Envelop> message, final Vertx vertx) {
    // Invoke directly
    final Envelop envelop = message.body();
    // Future<T>
    final Class<?> returnType = method.getReturnType();
    // Get T
    final Class<?> tCls = returnType.getComponentType();
    LOGGER.info(Info.MSG_FUTURE, this.getClass(), returnType, true);
    if (Envelop.class == tCls) {
        // Execute Future<Envelop>
        final Future<Envelop> future = Instance.invoke(proxy, method.getName(), envelop);
        future.compose(item -> TunnelClient.create(this.getClass()).connect(vertx).connect(method).send(item)).setHandler(Ux.toHandler(message));
    } else {
        final Future future = Instance.invoke(proxy, method.getName(), envelop);
        future.compose(item -> TunnelClient.create(this.getClass()).connect(vertx).connect(method).send(Ux.to(item))).compose(item -> Future.succeededFuture(Ux.to(item))).setHandler(Ux.toHandler(message));
    }
}
Also used : TunnelClient(io.vertx.up.micro.ipc.client.TunnelClient) Vertx(io.vertx.core.Vertx) Annal(io.vertx.up.log.Annal) Message(io.vertx.core.eventbus.Message) Envelop(io.vertx.up.atom.Envelop) Ux(io.vertx.up.aiki.Ux) Future(io.vertx.core.Future) Method(java.lang.reflect.Method) Instance(io.vertx.up.tool.mirror.Instance) Envelop(io.vertx.up.atom.Envelop) Future(io.vertx.core.Future)

Example 49 with Future

use of io.vertx.core.Future in project vertx-zero by silentbalanceyh.

the class Async method toArrayFuture.

static <T> Future<JsonArray> toArrayFuture(final String pojo, final CompletableFuture<List<T>> completableFuture) {
    final Future<JsonArray> future = Future.future();
    Fn.safeSemi(null == completableFuture, null, () -> future.complete(new JsonArray()), () -> completableFuture.thenAcceptAsync((item) -> Fn.safeSemi(null == item, null, () -> future.complete(new JsonArray()), () -> future.complete(To.toArray(item, pojo)))).exceptionally((ex) -> {
        LOGGER.jvm(ex);
        future.fail(ex);
        return null;
    }));
    return future;
}
Also used : JsonArray(io.vertx.core.json.JsonArray) JsonArray(io.vertx.core.json.JsonArray) Fn(io.vertx.up.func.Fn) List(java.util.List) Annal(io.vertx.up.log.Annal) Envelop(io.vertx.up.atom.Envelop) JsonObject(io.vertx.core.json.JsonObject) CompletableFuture(java.util.concurrent.CompletableFuture) Future(io.vertx.core.Future)

Example 50 with Future

use of io.vertx.core.Future in project vertx-ext-spa-ssr by mach-kernel.

the class MessageBackedRenderEngineImpl method render.

/**
 * Render components by publishing messages to SSR services.
 * @param context Routing context object
 */
@SuppressWarnings("unchecked")
public Future render(RoutingContext context) {
    Object components = context.data().get(this.componentContextKey);
    if (!(components instanceof AbstractList))
        return Future.failedFuture("components must be an AbstractList");
    List<Object> componentList = (AbstractList) components;
    ConcurrentHashMap<String, JsonObject> initialState = new ConcurrentHashMap<>();
    Future renderJob = Future.future();
    // TODO: Is this still true?
    // https://groups.google.com/forum/#!topic/vertx/3qJS_nK-J6M
    componentList.parallelStream().parallel().forEach(meta -> {
        if (!(meta instanceof JsonObject))
            return;
        JsonObject metaObject = (JsonObject) meta;
        String token = metaObject.getString("token");
        JsonObject props = metaObject.getJsonObject("props", null);
        String propsKey = props == null ? "" : this.hashFunction.apply(props);
        String elementKey = domComponentIdPrefix + "-" + token;
        initialState.put(elementKey, props);
        // Every time props is null we treat it as a cache miss
        if (this.cacheEnabled && props != null) {
            String alreadyRendered = cache.getIfPresent(propsKey);
            if (alreadyRendered != null) {
                context.put(token, alreadyRendered);
                return;
            }
        }
        decorateFromMessage(context, metaObject, token, elementKey, propsKey);
    });
    context.put(ssrStateName, Json.encode(initialState));
    renderJob.complete();
    return renderJob;
}
Also used : AbstractList(java.util.AbstractList) JsonObject(io.vertx.core.json.JsonObject) Future(io.vertx.core.Future) JsonObject(io.vertx.core.json.JsonObject) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Aggregations

Future (io.vertx.core.Future)370 HttpURLConnection (java.net.HttpURLConnection)195 Handler (io.vertx.core.Handler)174 List (java.util.List)166 Objects (java.util.Objects)164 JsonObject (io.vertx.core.json.JsonObject)163 Promise (io.vertx.core.Promise)160 Vertx (io.vertx.core.Vertx)157 Buffer (io.vertx.core.buffer.Buffer)149 Optional (java.util.Optional)147 Logger (org.slf4j.Logger)136 LoggerFactory (org.slf4j.LoggerFactory)136 CompositeFuture (io.vertx.core.CompositeFuture)127 ClientErrorException (org.eclipse.hono.client.ClientErrorException)127 Map (java.util.Map)122 Span (io.opentracing.Span)117 AsyncResult (io.vertx.core.AsyncResult)112 TracingHelper (org.eclipse.hono.tracing.TracingHelper)98 Constants (org.eclipse.hono.util.Constants)97 ArrayList (java.util.ArrayList)94