List of usage examples for io.vertx.core Future succeededFuture
static <T> Future<T> succeededFuture(T result)
From source file:de.braintags.netrelay.templateengine.thymeleaf.ThymeleafTemplateEngineImplBt.java
License:Open Source License
@Override public void render(RoutingContext context, String templateFileName, Handler<AsyncResult<Buffer>> handler) { Buffer buffer = Buffer.buffer(); try {//from w w w . j av a 2s .co m Map<String, Object> data = new HashMap<>(); data.put("context", context); data.putAll(context.data()); synchronized (this) { templateResolver.setVertx(context.vertx()); final List<Locale> acceptableLocales = context.acceptableLocales(); io.vertx.ext.web.Locale locale; if (acceptableLocales.size() == 0) { locale = io.vertx.ext.web.Locale.create(); } else { // this is the users preferred locale locale = acceptableLocales.get(0); } templateEngine.process(templateFileName, new WebIContext(data, locale), new Writer() { @Override public void write(char[] cbuf, int off, int len) throws IOException { buffer.appendString(new String(cbuf, off, len)); } @Override public void flush() throws IOException { } @Override public void close() throws IOException { } }); } handler.handle(Future.succeededFuture(buffer)); } catch (Exception ex) { handler.handle(Future.failedFuture(ex)); } }
From source file:de.fraunhofer.fokus.redistest.DoInterestingThings.java
License:Creative Commons License
public void doIt(final Vertx vertx, final JsonObject redisConfig, String uuid, Logger logger, Handler<AsyncResult<JsonArray>> handler) { String zSetName = String.format(ZSET_NAME_FORMAT, uuid); final RangeLimitOptions rangeLimitOptions = (RangeLimitOptions) new RangeLimitOptions().setLimit(0, Constants.MAX_SEARCH_RESULTS); /*//from ww w . j a va 2s . co m * To prevent errors in streams due to empty result set and to enable subscription * a dummy element will be prepended to the result set and removed in the subscription */ final List<JsonArray> secureResultList = new ArrayList<JsonArray>(); secureResultList.add(new JsonArray().add("default")); Single.using(DisposableRedisConnection::new, f -> f.create(vertx, redisConfig, logger), f -> f.dispose()) .subscribe(redisClient -> { // emulate searech time in database // vertx.setTimer(40, xx->{ Observable.range(1, 40) .map(x -> new JsonObject().put(Constants.KEY_TYPE, Constants.KEY_DICE_OBJECT) .put(Constants.KEY_RECORD_ID, String.valueOf(x)) .put(Constants.KEY_DICE_VALUE, x * 0.1).put(Constants.KEY_FAMILYNAME, x * 0.1) .put(Constants.KEY_FIRSTNAME, 1)) .filter(x -> x != null) //remove results where comparison has been stopped due to bad dice values // side effect - store objects in redis .subscribe(res -> { String handle = res.getString(Constants.KEY_RECORD_ID); String hashName = String.format(HASH_NAME_FORMAT, handle); Single.zip( redisClient.rxZadd(zSetName, res.getDouble(Constants.KEY_DICE_VALUE), handle), redisClient.rxExpire(hashName, Constants.EXPIRE_AFTER), redisClient.rxHmset(hashName, res), (a, b, c) -> { return res; }).subscribe(r -> { // do nothing }, t -> handler.handle(Future.failedFuture(t))); }, t -> handler.handle(Future.failedFuture(t)), () -> { //set expiration and retrieve record_ids Observable .zip(redisClient.rxExpire(zSetName, Constants.EXPIRE_AFTER).toObservable(), // set expiration redisClient .rxZrevrangebyscore(zSetName, "1", "0", rangeLimitOptions) .toObservable(), // list of record_ids as JsonArray (a, b) -> Observable.from(b)) .flatMap(x -> x) .map(handle -> redisClient .rxHgetall(String.format(HASH_NAME_FORMAT, handle)).toObservable()) // retrieve hash from redis .flatMap(x -> x).map(json -> toEntry(json)) .collect(() -> new JsonArray(), (eList, e) -> eList.add(e)) .subscribe(collectedJson -> handler .handle(Future.succeededFuture(collectedJson)), t -> { System.out.println("XXXX: " + t); logger.error("XXX", t); handler.handle(Future.failedFuture(t)); }); // }); }); }, t -> { CharArrayWriter cw = new CharArrayWriter(); PrintWriter w = new PrintWriter(cw); t.printStackTrace(w); w.close(); logger.error("trace", cw.toString()); logger.error("YYY", t); handler.handle(Future.failedFuture(t)); }); }
From source file:de.fraunhofer.fokus.redistest.Dummy.java
License:Creative Commons License
public void dummy(Vertx vertx, Query query, Logger logger, Handler<AsyncResult<JsonArray>> handler) { vertx.setTimer(4, res -> {/*w w w. j ava 2 s. c o m*/ handler.handle(Future.succeededFuture(new JsonArray())); }); }
From source file:examples.HTTPExamples.java
License:Open Source License
public void exampleFollowRedirect03(HttpClient client) { client.redirectHandler(response -> { // Only follow 301 code if (response.statusCode() == 301 && response.getHeader("Location") != null) { // Compute the redirect URI String absoluteURI = resolveURI(response.request().absoluteURI(), response.getHeader("Location")); // Create a new ready to use request that the client will use return Future.succeededFuture(client.getAbs(absoluteURI)); }//from w w w . j a v a2 s .c o m // We don't redirect return null; }); }
From source file:fr.pjthin.vertx.client.UserDaoVertxEBProxy.java
License:Apache License
public void save(User newUser, Handler<AsyncResult<String>> complete) { if (closed) { complete.handle(Future.failedFuture(new IllegalStateException("Proxy is closed"))); return;/*from w w w. j a v a2s . c o m*/ } JsonObject _json = new JsonObject(); _json.put("newUser", newUser == null ? null : newUser.toJson()); DeliveryOptions _deliveryOptions = (_options != null) ? new DeliveryOptions(_options) : new DeliveryOptions(); _deliveryOptions.addHeader("action", "save"); _vertx.eventBus().<String>send(_address, _json, _deliveryOptions, res -> { if (res.failed()) { complete.handle(Future.failedFuture(res.cause())); } else { complete.handle(Future.succeededFuture(res.result().body())); } }); }
From source file:fr.pjthin.vertx.client.UserDaoVertxEBProxy.java
License:Apache License
public void findAll(Handler<AsyncResult<List<User>>> complete) { if (closed) { complete.handle(Future.failedFuture(new IllegalStateException("Proxy is closed"))); return;//from w ww . j a va 2 s. co m } JsonObject _json = new JsonObject(); DeliveryOptions _deliveryOptions = (_options != null) ? new DeliveryOptions(_options) : new DeliveryOptions(); _deliveryOptions.addHeader("action", "findAll"); _vertx.eventBus().<JsonArray>send(_address, _json, _deliveryOptions, res -> { if (res.failed()) { complete.handle(Future.failedFuture(res.cause())); } else { complete.handle(Future.succeededFuture(res.result().body().stream() .map(o -> o instanceof Map ? new User(new JsonObject((Map) o)) : new User((JsonObject) o)) .collect(Collectors.toList()))); } }); }
From source file:fr.pjthin.vertx.client.UserDaoVertxEBProxy.java
License:Apache License
public void findUserByLogin(String login, Handler<AsyncResult<User>> complete) { if (closed) { complete.handle(Future.failedFuture(new IllegalStateException("Proxy is closed"))); return;//from w w w . j a va 2 s.c o m } JsonObject _json = new JsonObject(); _json.put("login", login); DeliveryOptions _deliveryOptions = (_options != null) ? new DeliveryOptions(_options) : new DeliveryOptions(); _deliveryOptions.addHeader("action", "findUserByLogin"); _vertx.eventBus().<JsonObject>send(_address, _json, _deliveryOptions, res -> { if (res.failed()) { complete.handle(Future.failedFuture(res.cause())); } else { complete.handle( Future.succeededFuture(res.result().body() == null ? null : new User(res.result().body()))); } }); }
From source file:fr.pjthin.vertx.client.UserDaoVertxEBProxy.java
License:Apache License
public void deleteByLogin(String login, Handler<AsyncResult<Void>> complete) { if (closed) { complete.handle(Future.failedFuture(new IllegalStateException("Proxy is closed"))); return;/*from w w w. j ava 2 s . c o m*/ } JsonObject _json = new JsonObject(); _json.put("login", login); DeliveryOptions _deliveryOptions = (_options != null) ? new DeliveryOptions(_options) : new DeliveryOptions(); _deliveryOptions.addHeader("action", "deleteByLogin"); _vertx.eventBus().<Void>send(_address, _json, _deliveryOptions, res -> { if (res.failed()) { complete.handle(Future.failedFuture(res.cause())); } else { complete.handle(Future.succeededFuture(res.result().body())); } }); }
From source file:io.apiman.gateway.platforms.vertx3.services.impl.IngestorToPolicyImpl.java
License:Apache License
public void ready() { log.debug(String.format("%s indicated #ready", uuid)); readyHandler.handle(Future.succeededFuture(true)); }
From source file:io.apiman.gateway.platforms.vertx3.services.impl.IngestorToPolicyImpl.java
License:Apache License
public void failHead() { log.debug(String.format("%s indicated #failHead", uuid)); readyHandler.handle(Future.succeededFuture(false)); }