List of usage examples for io.vertx.core Future future
future
From source file:com.groupon.vertx.redis.RedisTransaction.java
License:Apache License
public Future<JsonObject> exec() { final Future<JsonObject> finalResult = Future.future(); if (!pendingCommands.isEmpty()) { JsonArray commands = new JsonArray(); final List<Future<JsonObject>> clientCommandResponses = new ArrayList<>(); clientCommandResponses.add(finalResult); RedisCommand command = pendingCommands.poll(); while (command != null) { clientCommandResponses.add(command.getClientCommandResponse()); commands.add(command.toJson()); command = pendingCommands.poll(); }// w ww . j av a 2 s . c o m JsonObject transactionCommands = new JsonObject(); transactionCommands.put("isTransaction", true); transactionCommands.put("commands", commands); final DeliveryOptions deliveryOptions = new DeliveryOptions().setSendTimeout(replyTimeout); eventBus.send(eventBusAddress, transactionCommands, deliveryOptions, new Handler<AsyncResult<Message<JsonObject>>>() { @Override public void handle(AsyncResult<Message<JsonObject>> messageAsyncResult) { JsonObject response; if (messageAsyncResult.failed()) { response = new JsonObject().put("status", "error").put("code", HttpURLConnection.HTTP_GATEWAY_TIMEOUT); } else { response = messageAsyncResult.result().body(); } int index = 0; executeResponse(clientCommandResponses.remove(0), response); // EXEC response for (Future<JsonObject> clientCommandResponse : clientCommandResponses) { if (clientCommandResponse != null) { JsonObject result = constructTransactionCommandResult(response, index); executeResponse(clientCommandResponse, result); } index++; } } }); } else { // Nothing to execute. finalResult.complete(null); } return finalResult; }
From source file:com.groupon.vertx.redis.RedisTransaction.java
License:Apache License
@Override protected Future<JsonObject> sendCommand(RedisCommand command) { final Future<JsonObject> finalResult = Future.future(); command.setClientCommandResponse(finalResult); pendingCommands.add(command);//from w w w . j a v a2 s . c o m return finalResult; }
From source file:com.groupon.vertx.utils.config.ConfigLoader.java
License:Apache License
public Future<JsonObject> load(Object field) { Future<JsonObject> configFuture; if (field != null) { if (field instanceof String) { configFuture = getOrLoadConfig((String) field); } else if (field instanceof JsonObject) { configFuture = Future.future(); configFuture.complete((JsonObject) field); } else {/*from ww w.j ava 2 s. c om*/ configFuture = Future.future(); configFuture.fail(new IllegalStateException( "Field `config` must contain an object or a string (path to the JSON config file)")); } } else { configFuture = Future.future(); configFuture.complete(new JsonObject()); } return configFuture; }
From source file:com.groupon.vertx.utils.config.ConfigLoader.java
License:Apache License
/** * Check if the configuration has already been loaded, and if so return that, otherwise * attempt to load the configuration from the filesystem and save the result * * @param path path to the configuration file * @return future that eventually contains the JsonObject representing the configuration *//*from w ww. j ava 2 s. c o m*/ @SuppressFBWarnings("SIC_INNER_SHOULD_BE_STATIC_ANON") private Future<JsonObject> getOrLoadConfig(final String path) { final Future<JsonObject> configFuture = Future.future(); if (loadedConfigs.containsKey(path)) { configFuture.complete(loadedConfigs.get(path)); } else { final Future<JsonObject> loadedConfigFuture = loadAndParseConfigFromFilesystem(path); loadedConfigFuture.setHandler(new AsyncResultHandler<JsonObject>() { @Override public void handle(AsyncResult<JsonObject> result) { if (result.succeeded()) { JsonObject loadedConfig = result.result(); loadedConfigs.put(path, loadedConfig); configFuture.complete(loadedConfig); } else { configFuture.fail(result.cause()); } } }); } return configFuture; }
From source file:com.groupon.vertx.utils.config.ConfigLoader.java
License:Apache License
/** * Load configuration from the filesystem and parse it into a JsonObject * * @param path path to the configuration file * @return future that eventually contains the JsonObject representing the configuration *///from w ww. j a v a2s .com @SuppressFBWarnings("SIC_INNER_SHOULD_BE_STATIC_ANON") private Future<JsonObject> loadAndParseConfigFromFilesystem(final String path) { final Future<JsonObject> configFuture = Future.future(); fileSystem.readFile(path, new AsyncResultHandler<Buffer>() { @Override @SuppressFBWarnings("REC_CATCH_EXCEPTION") public void handle(AsyncResult<Buffer> result) { if (result.succeeded()) { try { final ConfigParser configParser = getConfigParser(); JsonObject loadedConfig = configParser.parse(result.result().toString()); configFuture.complete(loadedConfig); } catch (Exception e) { configFuture.fail(e); } } else { configFuture.fail(result.cause()); } } }); return configFuture; }
From source file:com.groupon.vertx.utils.deployment.DeploymentMonitorHandler.java
License:Apache License
/** * @param totalVerticles number of verticles to wait for before invoking the finished handler * @param finishedHandler handler to invoke after all verticles have deployed *///from www . j a v a2s.co m public DeploymentMonitorHandler(int totalVerticles, AsyncResultHandler<Void> finishedHandler) { this.totalVerticles = totalVerticles; failures = new ConcurrentLinkedQueue<>(); deploymentsRemaining = new AtomicInteger(totalVerticles); future = Future.future(); future.setHandler(finishedHandler); }
From source file:com.groupon.vertx.utils.deployment.MultiVerticleDeployment.java
License:Apache License
/** * Deploy all of the verticles/* ww w .ja va2 s .c o m*/ * @return future representing success or failure for the requested deploys */ @SuppressFBWarnings("SIC_INNER_SHOULD_BE_STATIC_ANON") public Future<Void> deploy(final JsonObject config) { if (started) { throw new IllegalStateException("Deployment already started"); } log.info("deploy", "start"); started = true; final Future<Void> deploymentResult = Future.future(); final Config deployConfig; try { deployConfig = new Config(config); } catch (Exception e) { deploymentResult.fail(e); return deploymentResult; } final int totalVerticles = deployConfig.size(); log.info("start", "start", new String[] { "message" }, String.format("Deploying %d verticle(s)", totalVerticles)); final DeploymentMonitorHandler deploymentMonitorHandler = new DeploymentMonitorHandler(totalVerticles, new AsyncResultHandler<Void>() { @Override public void handle(AsyncResult<Void> result) { if (result.succeeded()) { deploymentResult.complete(null); } else { deploymentResult.fail(result.cause()); } } }); final Iterator<VerticleConfig> verticleConfigIterator = deployConfig.iterator(); VerticleConfig verticleConfig = verticleConfigIterator.next(); log.info("deploy", "deployFirstVerticle", new String[] { "message" }, String.format("Deploying verticle %s", verticleConfig.getName())); deployVerticle(verticleConfig, new AsyncResultHandler<String>() { @Override public void handle(AsyncResult<String> result) { if (verticleConfigIterator.hasNext()) { VerticleConfig nextVerticleConfig = verticleConfigIterator.next(); log.info("deploy", "deployNextVerticle", new String[] { "message" }, String.format("Deploying verticle %s", nextVerticleConfig.getName())); deployVerticle(nextVerticleConfig, this); } deploymentMonitorHandler.handle(result); } }); return deploymentResult; }
From source file:com.groupon.vertx.utils.deployment.VerticleDeployment.java
License:Apache License
public VerticleDeployment(Vertx vertx, String name, String className, AsyncResultHandler<String> finishedHandler) { this.vertx = vertx; this.name = name; this.className = className; deployId = Future.future(); deployId.setHandler(finishedHandler); }
From source file:com.hubrick.vertx.kafka.consumer.KafkaConsumer.java
License:Apache License
private void handle(String msg, Long offset, int tries, int delaySeconds) { final Future<Void> futureResult = Future.future(); futureResult.setHandler(result -> { if (result.succeeded()) { unacknowledgedOffsets.remove(offset); phaser.arriveAndDeregister(); } else {//from www . j ava 2s . c o m final int nextDelaySeconds = computeNextDelay(delaySeconds); if (tries > 0) { LOG.error("Exception occurred during kafka message processing, will retry in {} seconds: {}", delaySeconds, msg, result.cause()); final int nextTry = tries - 1; vertx.setTimer(delaySeconds * 1000, event -> handle(msg, offset, nextTry, nextDelaySeconds)); } else { LOG.error( "Exception occurred during kafka message processing. Max number of retries reached. Skipping message: {}", msg, result.cause()); unacknowledgedOffsets.remove(offset); phaser.arriveAndDeregister(); } } }); handler.handle(msg, futureResult); }
From source file:com.themonkee.vertx.web.impl.MongoSessionStoreImpl.java
License:Open Source License
public MongoSessionStoreImpl(Vertx vertx, MongoClient mongoClient, JsonObject options, Future<MongoSessionStore> resultHandler) { this.random = new PRNG(vertx); this.vertx = vertx; this.mongoClient = mongoClient; if (options != null) { if (options.containsKey("collection")) this.sessionCollection = options.getString("collection"); }/*from w ww.jav a 2 s.c om*/ Future<Void> futCreateColl = Future.future(); // try to create collection, if it is created or already exists its OK this.mongoClient.createCollection(this.sessionCollection, (AsyncResult<Void> res) -> { if (res.succeeded() || res.cause().getMessage().contains("collection already exists")) { futCreateColl.complete(); } else { futCreateColl.fail(res.cause()); } }); futCreateColl.compose(v -> { // create the session expiry index // SessionImpl sets _expire field to Date when session document must be deleted on save // so we set expireAfterSeconds to 0 so its deleted when that Date is hit // see https://docs.mongodb.com/manual/tutorial/expire-data/ this.mongoClient.createIndexWithOptions(this.sessionCollection, new JsonObject().put(SessionImpl.EXPIRE_FIELD, 1), new IndexOptions().expireAfter(0L, TimeUnit.SECONDS), res -> { if (res.succeeded()) { resultHandler.complete(this); } else { resultHandler.fail(res.cause()); } }); }, resultHandler); }