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());
}
});
}
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));
}
}
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));
}
}
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;
}
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;
}
Aggregations