@RouteMapping(method = RouteMethod.GET) public Handler<RoutingContext> getAll() { return ctx -> { JDBCClient client = AppUtil.getJdbcClient(Vertx.vertx()); client.getConnection( conn -> { if (conn.failed()) { LOGGER.error(conn.cause().getMessage(), conn.cause()); ctx.fail(400); } SQLUtil.query( conn.result(), "select * from patient", rs -> { JsonArray patients = new JsonArray(); for (JsonObject row : rs.getRows()) { patients.add(row); } SQLUtil.close(conn.result()); ctx.response().end(patients.encode()); }); }); }; }
public static <T> JsonArray makeArrayToJsonArrayObject(final T[] array) { final JsonArray jsonArray = new JsonArray(); for (final T v : array) { jsonArray.add(v); } return jsonArray; }
@Override protected void add(JsonArray array, Object value) { if (value != null) { array.add(value); } else { array.addNull(); } }
private Handler<AsyncResult<Set<Character>>> createSetCharHandler(Message msg) { return res -> { if (res.failed()) { msg.fail(-1, res.cause().getMessage()); } else { JsonArray arr = new JsonArray(); for (Character chr : res.result()) { arr.add((int) chr); } msg.reply(arr); } }; }
@Test public void testPublishJsonArray() { JsonArray arr = new JsonArray(); arr.add(TestUtils.randomUnicodeString(100)) .add(TestUtils.randomInt()) .add(TestUtils.randomBoolean()); testPublish( arr, (received) -> { assertEquals(arr, received); assertFalse(arr == received); // Make sure it's copied }); }
// Add some information on a deployment in the cluster so other nodes know about it private void addToHA( String deploymentID, String verticleName, DeploymentOptions deploymentOptions) { String encoded; synchronized (haInfo) { JsonObject verticleConf = new JsonObject().put("dep_id", deploymentID); verticleConf.put("verticle_name", verticleName); verticleConf.put("options", deploymentOptions.toJson()); JsonArray haMods = haInfo.getJsonArray("verticles"); haMods.add(verticleConf); encoded = haInfo.encode(); clusterMap.put(nodeID, encoded); } }
private Handler<AsyncResult<List<Character>>> createListCharHandler(Message msg) { return res -> { if (res.failed()) { if (res.cause() instanceof ServiceException) { msg.reply(res.cause()); } else { msg.reply(new ServiceException(-1, res.cause().getMessage())); } } else { JsonArray arr = new JsonArray(); for (Character chr : res.result()) { arr.add((int) chr); } msg.reply(arr); } }; }
@Test public void testWithJsonArray() { AtomicReference<JsonArray> result = new AtomicReference<>(); Service service = Service.createProxy(consumerNode.get(), "my.service"); TestDataObject data = new TestDataObject().setBool(true).setNumber(25).setString("vert.x"); JsonArray array = new JsonArray(); array.add("vert.x").add(data.toJson()); service.methodWithJsonArray( array, ar -> { result.set(ar.result()); }); Awaitility.await().atMost(10, TimeUnit.SECONDS).until(() -> result.get() != null); TestDataObject out = new TestDataObject(result.get().getJsonObject(1)); assertThat(array.getString(0)).isEqualToIgnoringCase("vert.x"); assertThat(out.getNumber()).isEqualTo(25); assertThat(out.isBool()).isTrue(); assertThat(out.getString()).isEqualTo("vert.x"); }
/** Iterate and deploy verticles */ private void deployVerticle(final Message<JsonObject> event) { // iterate over all candidates to be deployed Set<String> candidates = this.workingCopy.fieldNames(); // detach from underlying json Map<String, JsonObject> initiants = new HashMap<>(); candidates.forEach( id -> { JsonObject info = this.workingCopy.getJsonObject(id); JsonArray dependsOn = info.getJsonArray("dependsOn"); if (dependsOn != null && deployed.getList().containsAll(dependsOn.getList()) || dependsOn == null || dependsOn.isEmpty()) { initiants.put(id, info); } }); // remove the initiants initiants.keySet().forEach(id -> this.workingCopy.remove(id)); // setup latch for the reply CountDownLatch latch = new CountDownLatch(initiants.size()); if (initiants.isEmpty()) { event.reply(Boolean.TRUE); return; } // run over all dependencies initiants.forEach( (id, info) -> { // get the name of the verticle String name = info.getString("name"); final JsonObject localConfig = new JsonObject(); localConfig.mergeIn(globalConfig); localConfig.mergeIn(info.getJsonObject("config", new JsonObject())); Handler<AsyncResult<String>> handler = innerEvent -> { if (innerEvent.succeeded()) { // add service to deployed-list deployed.add(id); // re-emit vertx .eventBus() .send( LOOPBACK, workingCopy, (AsyncResult<Message<Boolean>> recursiveReply) -> { // always decrease latch latch.countDown(); if (recursiveReply.succeeded() && recursiveReply.result().body()) { if (latch.getCount() == 0) { event.reply(recursiveReply.result().body() & Boolean.TRUE); } } else { event.fail(500, this.getFailure(id, recursiveReply)); } }); } else { event.fail(500, id + " >> " + innerEvent.cause().getMessage()); } }; LOG.log(Level.INFO, "Deploying: ''{0}''", new Object[] {id}); DeploymentOptions deploymentOptions = new DeploymentOptions(info); vertx.deployVerticle(name, deploymentOptions.setConfig(localConfig), handler); }); }
@Override public void onExecute(int what, JsonObject resultJO) { if (resultJO.containsKey("result_code") && resultJO.getInteger("result_code") == -1) { request.response().end(resultJO.toString()); return; } JsonObject rs = new JsonObject(); switch (what) { case Config.getSession: if (!resultJO.containsKey("result") || resultJO.getString("result") == null) { rs.put("result_code", -1); rs.put("result_msg", "로그인이 필요합니다."); request.response().end(rs.toString()); break; } String user_info[] = resultJO.getString("result").split(","); String user_id = user_info[0]; params.put("user_id", user_id); String query = String.format( "UPDATE app_user_list SET user_nick='%s' WHERE user_id='%s' and app_id='%s'", params.getString("user_nick"), params.getString("user_id"), params.getString("app_id")); insertCustomQuery(this, Config.setNickApp, query); break; case Config.setNickApp: String query2 = String.format( "UPDATE channel_user_list SET user_nick='%s' WHERE user_id='%s' and app_id='%s'", params.getString("user_nick"), params.getString("user_id"), params.getString("app_id")); insertCustomQuery(this, 100, query2); rs.put("result_code", 0); rs.put("result_msg", "닉네임을 변경하였습니다."); request.response().end(rs.toString()); getRedis(this, Config.getRedis, "app:" + params.getString("user_id")); break; case Config.getRedis: JsonArray ja = new JsonArray(resultJO.getString("result")); for (int i = 0; i < ja.size(); i++) { if (params.getString("app_id").equals(ja.getJsonObject(i).getString("app_id"))) { JsonObject jo = ja.getJsonObject(i); jo.put("user_nick", params.getString("user_nick")); ja.remove(i); ja.add(jo); } } JsonObject table = new JsonObject(); table.put("key", "app:" + Util.getUserId(params.getString("token"))); table.put("value", ja.toString()); setRedis(this, Config.setRedis, table); break; } }
public static <T> JsonArray makeCollectionToJsonArrayObject(final Collection<T> collection) { final JsonArray jsonArray = new JsonArray(); collection.forEach(e -> jsonArray.add(e)); return jsonArray; }