Example usage for io.vertx.core.json JsonArray size

List of usage examples for io.vertx.core.json JsonArray size

Introduction

In this page you can find the example usage for io.vertx.core.json JsonArray size.

Prototype

public int size() 

Source Link

Document

Get the number of values in this JSON array

Usage

From source file:com.cyngn.kafka.consume.SimpleConsumer.java

License:Apache License

@Override
public void start(final Future<Void> startedResult) {

    try {/* ww  w .jav a2 s. com*/
        bus = vertx.eventBus();
        running = new AtomicBoolean(true);

        verticleConfig = config();
        Properties kafkaConfig = populateKafkaConfig(verticleConfig);
        JsonArray topicConfig = verticleConfig.getJsonArray(ConfigConstants.TOPICS);

        busAddress = verticleConfig.getString(ConfigConstants.EVENTBUS_ADDRESS, EVENTBUS_DEFAULT_ADDRESS);
        pollIntervalMs = verticleConfig.getInteger(ConfigConstants.CONSUMER_POLL_INTERVAL_MS, DEFAULT_POLL_MS);

        Runtime.getRuntime().addShutdownHook(new Thread() {
            // try to disconnect from ZK as gracefully as possible
            public void run() {
                shutdown();
            }
        });

        backgroundConsumer = Executors.newSingleThreadExecutor();
        backgroundConsumer.submit(() -> {
            try {
                consumer = new KafkaConsumer(kafkaConfig);

                topics = new ArrayList<>();
                for (int i = 0; i < topicConfig.size(); i++) {
                    topics.add(topicConfig.getString(i));
                    logger.info("Subscribing to topic ");
                }

                // signal success before we enter read loop
                startedResult.complete();
                consume();
            } catch (Exception ex) {
                String error = "Failed to startup";
                logger.error(error, ex);
                bus.send(ConfigConstants.CONSUMER_ERROR_TOPIC, getErrorString(error, ex.getMessage()));
                startedResult.fail(ex);
            }
        });
    } catch (Exception ex) {
        String error = "Failed to startup";
        logger.error(error, ex);
        bus.send(ConfigConstants.CONSUMER_ERROR_TOPIC, getErrorString("Failed to startup", ex.getMessage()));
        startedResult.fail(ex);
    }
}

From source file:com.cyngn.kafka.MessageConsumer.java

License:Apache License

@Override
public void start(final Future<Void> startedResult) {
    try {//from ww  w. java  2 s. c o  m
        consumerConfig = config().getJsonObject("consumer");

        Properties consumerProperties = populateKafkaConfig(consumerConfig);

        busAddress = consumerConfig.getString(ConfigConstants.EVENTBUS_ADDRESS, EVENTBUS_DEFAULT_ADDRESS);

        int workersPerTopic = consumerConfig.getInteger(ConfigConstants.WORKERS_PER_TOPIC, 1);
        JsonArray topics = consumerConfig.getJsonArray(ConfigConstants.TOPICS);
        int totalTopics = topics.size() * workersPerTopic;

        consumer = Consumer.createJavaConsumerConnector(new ConsumerConfig(consumerProperties));

        String topicList = getTopicFilter(topics);
        TopicFilter sourceTopicFilter = new Whitelist(topicList);
        List<KafkaStream<byte[], byte[]>> streams = consumer.createMessageStreamsByFilter(sourceTopicFilter,
                totalTopics);

        threadPool = Executors.newFixedThreadPool(streams.size());

        logger.info("Starting to consume messages group: " + consumerConfig.getString(ConfigConstants.GROUP_ID)
                + " topic(s): " + topicList);
        for (final KafkaStream<byte[], byte[]> stream : streams) {
            threadPool.submit(() -> startConsumer(stream));
        }

        Runtime.getRuntime().addShutdownHook(new Thread() {
            // try to disconnect from ZK as gracefully as possible
            public void run() {
                shutdown();
            }
        });

        startedResult.complete();
    } catch (Exception ex) {
        logger.error("Message consumer initialization failed with ex: {}", ex);
        startedResult.fail(ex);
    }

}

From source file:com.cyngn.vertx.opentsdb.service.OpenTsDbService.java

License:Apache License

private void initializeWorkers(Future<Void> startedResult) {
    final AtomicInteger count = new AtomicInteger();
    processor = new MetricsProcessor(workers, options.getMaxBufferBytes(), vertx.eventBus());
    JsonArray hosts = options.getHosts();
    for (int i = 0; i < hosts.size(); i++) {
        JsonObject jsonHost = hosts.getJsonObject(i);

        // we setup one worker dedicated to each endpoint, the same worker always rights to the same outbound socket
        OpenTsDbClient worker = new OpenTsDbClient(jsonHost.getString("host"), jsonHost.getInteger("port"),
                vertx, success -> {//from  w  w  w .  j a va 2  s  .com
                    if (!success) {
                        String error = String.format("Failed to connect to host: %s", jsonHost.encode());
                        logger.error(error);
                        vertx.close();
                        startedResult.fail(error);
                        return;
                    }

                    count.incrementAndGet();
                    if (count.get() == hosts.size()) {
                        flushTimerId = vertx.setPeriodic(options.getFlushInterval(),
                                timerId -> processor.processMetrics(metrics));
                        logger.info(options);
                        startReporter();
                        startedResult.complete();
                    }
                });
        workers.add(worker);
    }
}

From source file:com.cyngn.vertx.opentsdb.service.OpenTsDbService.java

License:Apache License

private void processMetricBatch(Message<JsonObject> message) {
    JsonArray metricsObjects = message.body().getJsonArray(MetricsParser.METRICS_FIELD);

    if (metricsObjects == null) {
        String errMsg = String.format("invalid batch message request no 'metrics' field supplied, msg: %s",
                message.body());//from   www  .  j a va 2  s. c o  m
        logger.warn(errMsg);
        sendError(message, errMsg);
    }

    // roll through and add all the metrics
    for (int i = 0; i < metricsObjects.size(); i++) {
        String metric = metricsParser.createMetricString(message, metricsObjects.getJsonObject(i));
        if (metric != null) {
            if (!addMetric(null, metric)) {
                reportFullBacklog(message);
                break;
            }
        } else {
            // something is bad in the batch, the parsers error handler will reply to the message as failed
            return;
        }
    }
    message.reply(OK_REPLY);
}

From source file:com.englishtown.vertx.guice.GuiceVerticleLoader.java

License:Open Source License

private Verticle createRealVerticle(Class<?> clazz) throws Exception {

    JsonObject config = context.config();
    Object field = config.getValue(CONFIG_BOOTSTRAP_BINDER_NAME);
    JsonArray bootstrapNames;
    List<Module> bootstraps = new ArrayList<>();

    if (field instanceof JsonArray) {
        bootstrapNames = (JsonArray) field;
    } else {//from   w  w w .j av  a2s .  com
        bootstrapNames = new JsonArray().add((field == null ? BOOTSTRAP_BINDER_NAME : field));
    }

    for (int i = 0; i < bootstrapNames.size(); i++) {
        String bootstrapName = bootstrapNames.getString(i);
        try {
            Class bootstrapClass = classLoader.loadClass(bootstrapName);
            Object obj = bootstrapClass.newInstance();

            if (obj instanceof Module) {
                bootstraps.add((Module) obj);
            } else {
                logger.error("Class " + bootstrapName + " does not implement Module.");
            }
        } catch (ClassNotFoundException e) {
            logger.error("Guice bootstrap binder class " + bootstrapName
                    + " was not found.  Are you missing injection bindings?");
        }
    }

    // Add vert.x binder
    bootstraps.add(new GuiceVertxBinder(vertx));

    // Each verticle factory will have it's own injector instance
    Injector injector = Guice.createInjector(bootstraps);
    return (Verticle) injector.getInstance(clazz);
}

From source file:com.englishtown.vertx.hk2.HK2VerticleLoader.java

License:Open Source License

private Verticle createRealVerticle(Class<?> clazz) throws Exception {

    JsonObject config = config();/*from   w  ww  .  j a va2  s.  c  o m*/
    Object field = config.getValue(CONFIG_BOOTSTRAP_BINDER_NAME);
    JsonArray bootstrapNames;
    List<Binder> bootstraps = new ArrayList<>();

    if (field instanceof JsonArray) {
        bootstrapNames = (JsonArray) field;
    } else {
        bootstrapNames = new JsonArray().add((field == null ? BOOTSTRAP_BINDER_NAME : field));
    }

    for (int i = 0; i < bootstrapNames.size(); i++) {
        String bootstrapName = bootstrapNames.getString(i);
        try {
            Class bootstrapClass = classLoader.loadClass(bootstrapName);
            Object obj = bootstrapClass.newInstance();

            if (obj instanceof Binder) {
                bootstraps.add((Binder) obj);
            } else {
                logger.error("Class " + bootstrapName + " does not implement Binder.");
            }
        } catch (ClassNotFoundException e) {
            logger.error("HK2 bootstrap binder class " + bootstrapName
                    + " was not found.  Are you missing injection bindings?");
        }
    }

    // Each verticle factory will have it's own service locator instance
    // Passing a null name will not cache the locator in the factory
    locator = ServiceLocatorFactory.getInstance().create(null, parent);

    bootstraps.add(0, new HK2VertxBinder(vertx));
    ServiceLocatorUtilities.bind(locator, bootstraps.toArray(new Binder[bootstraps.size()]));

    return (Verticle) locator.createAndInitialize(clazz);
}

From source file:com.englishtown.vertx.jersey.impl.DefaultJerseyOptions.java

License:Open Source License

protected ResourceConfig getResourceConfig() {
    checkState();/*from  w w w  . j a v  a2  s. c o  m*/
    JsonArray resources = config.getJsonArray(CONFIG_RESOURCES, null);

    if (resources == null || resources.size() == 0) {
        throw new RuntimeException(
                "At least one resource package name must be specified in the config " + CONFIG_RESOURCES);
    }

    String[] resourceArr = new String[resources.size()];
    for (int i = 0; i < resources.size(); i++) {
        resourceArr[i] = resources.getString(i);
    }

    ResourceConfig rc = new ResourceConfig();
    rc.packages(resourceArr);

    ClassLoader cl = Thread.currentThread().getContextClassLoader();

    JsonArray features = config.getJsonArray(CONFIG_FEATURES, null);
    if (features != null && features.size() > 0) {
        for (int i = 0; i < features.size(); i++) {
            try {
                Class<?> clazz = cl.loadClass(features.getString(i));
                rc.register(clazz);
            } catch (ClassNotFoundException e) {
                throw new RuntimeException(e);
            }
        }
    }

    // Always register the InternalVertxJerseyBinder
    rc.register(new InternalVertxJerseyBinder(vertx));

    // Register configured binders
    JsonArray binders = config.getJsonArray(CONFIG_BINDERS, null);
    if (binders != null && binders.size() > 0) {
        for (int i = 0; i < binders.size(); i++) {
            try {
                Class<?> clazz = cl.loadClass(binders.getString(i));
                rc.register(clazz.newInstance());
            } catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
                throw new RuntimeException(e);
            }
        }
    }

    final JsonObject resourceConfigAdditions = config.getJsonObject(CONFIG_RESOURCE_CONFIG);
    if (resourceConfigAdditions != null) {
        for (final String fieldName : resourceConfigAdditions.fieldNames()) {
            rc.property(fieldName, resourceConfigAdditions.getValue(fieldName));
        }
    }

    return rc;
}

From source file:com.funmix.service.DriverServiceImpl.java

@Override
public Future<JsonObject> getAll(JsonObject params) {
    Future<JsonObject> result = Future.future();
    StringBuffer sqlCount = new StringBuffer(SQL_QUERY_COUNT);
    StringBuffer sqlQuery = new StringBuffer(SQL_QUERY_ALL);
    StringBuffer order = new StringBuffer();
    StringBuffer where = new StringBuffer();
    JsonArray queryparams = new JsonArray();
    JsonArray countparams = new JsonArray();
    params.forEach(r -> {//from w w  w.  j  av a 2 s.c om
        String key = r.getKey();
        if (key.equals("orderby")) {
            order.append(" order by ").append(r.getValue());
        } else if (key.equals("page")) {
        } else if (key.equals("page_size")) {
        } else {
            if (where.length() == 0)
                where.append(" where ");
            else
                where.append(" and ");
            where.append(key).append(" like ? ");
            queryparams.add("%" + r.getValue() + "%");
            countparams.add("%" + r.getValue() + "%");
        }
    });

    if (where.length() > 0) {
        sqlCount.append(where);
        sqlQuery.append(where);
    }
    JsonObject jrs = new JsonObject();
    client.getConnection(connHandler(result, connection -> {
        log.info(sqlCount);
        if (countparams.size() == 0)
            connection.query(sqlCount.toString(), r -> {
                if (r.failed()) {
                    result.fail(r.cause());
                } else {
                    int count = r.result().getRows().get(0).getInteger("count");
                    jrs.put("total", count);
                }
            });
        else
            connection.queryWithParams(sqlCount.toString(), countparams, r -> {
                if (r.failed()) {
                    result.fail(r.cause());
                } else {
                    int count = r.result().getRows().get(0).getInteger("count");
                    jrs.put("total", count);
                }
            });
        if (order.length() > 0) {
            sqlQuery.append(order);
            queryparams.add(params.getString("orderby"));
        }
        if (params.getValue("page") == null) {
            params.put("page", "0");
            params.put("page_size", "" + Integer.MAX_VALUE);
        }
        sqlQuery.append(" limit ?,? ");
        log.info(sqlQuery);
        int page = Integer.parseInt(params.getString("page"));
        int limit = Integer.parseInt(params.getString("page_size"));
        queryparams.add(Utils.calcPage(page, limit)).add(limit);
        connection.queryWithParams(sqlQuery.toString(), queryparams, r -> {
            if (r.failed()) {
                result.fail(r.cause());
            } else {
                jrs.put("data", r.result().getRows().stream().map(Driver::new).collect(Collectors.toList()));
                jrs.put("per_page", limit);
                jrs.put("current_page", page);
            }
            result.complete(new JsonObject().put("status", 200).put("data", new JsonObject().put("list", jrs)));
        });
        connection.close();

    }));
    return result;
}

From source file:com.funmix.service.LineServiceImpl.java

@Override
public Future<JsonObject> getAll(JsonObject params) {
    Future<JsonObject> result = Future.future();
    StringBuffer sqlCount = new StringBuffer(SQL_QUERY_COUNT);
    StringBuffer sqlQuery = new StringBuffer(SQL_QUERY_ALL);
    StringBuffer order = new StringBuffer();
    StringBuffer where = new StringBuffer();
    JsonArray queryparams = new JsonArray();
    JsonArray countparams = new JsonArray();
    params.forEach(r -> {/* w  w  w . j  av  a2 s .  c  o m*/
        String key = r.getKey();
        if (key.equals("orderby")) {
            order.append(" order by ").append(r.getValue());
        } else if (key.equals("page")) {
        } else if (key.equals("page_size")) {
        } else {
            if (where.length() == 0)
                where.append(" where ");
            else
                where.append(" and ");
            where.append(key).append(" like ? ");
            queryparams.add("%" + r.getValue() + "%");
            countparams.add("%" + r.getValue() + "%");
        }
    });

    if (where.length() > 0) {
        sqlCount.append(where);
        sqlQuery.append(where);
    }
    JsonObject jrs = new JsonObject();
    client.getConnection(connHandler(result, connection -> {
        log.info(sqlCount);
        if (countparams.size() == 0)
            connection.query(sqlCount.toString(), r -> {
                if (r.failed()) {
                    result.fail(r.cause());
                } else {
                    int count = r.result().getRows().get(0).getInteger("count");
                    jrs.put("total", count);
                }
            });
        else
            connection.queryWithParams(sqlCount.toString(), countparams, r -> {
                if (r.failed()) {
                    result.fail(r.cause());
                } else {
                    int count = r.result().getRows().get(0).getInteger("count");
                    jrs.put("total", count);
                }
            });
        if (order.length() > 0) {
            sqlQuery.append(order);
            queryparams.add(params.getString("orderby"));
        }
        if (params.getValue("page") == null) {
            params.put("page", "0");
            params.put("page_size", "" + Integer.MAX_VALUE);
        }
        sqlQuery.append(" limit ?,? ");
        log.info(sqlQuery);
        int page = Integer.parseInt(params.getString("page"));
        int limit = Integer.parseInt(params.getString("page_size"));
        queryparams.add(Utils.calcPage(page, limit)).add(limit);
        connection.queryWithParams(sqlQuery.toString(), queryparams, r -> {
            if (r.failed()) {
                result.fail(r.cause());
            } else {
                log.info(r.result().getRows().toString());
                jrs.put("data", r.result().getRows().stream().map(Line::new).collect(Collectors.toList()));
                jrs.put("per_page", limit);
                jrs.put("current_page", page);
            }
            result.complete(new JsonObject().put("status", 200).put("data", new JsonObject().put("list", jrs)));
        });
        connection.close();

    }));
    return result;
}

From source file:com.funmix.service.TruckServiceImpl.java

@Override
public Future<JsonObject> getAll(JsonObject params) {
    Future<JsonObject> result = Future.future();
    StringBuffer sqlCount = new StringBuffer(SQL_QUERY_COUNT);
    StringBuffer sqlQuery = new StringBuffer(SQL_QUERY_ALL);
    StringBuffer order = new StringBuffer();
    StringBuffer where = new StringBuffer();
    JsonArray queryparams = new JsonArray();
    JsonArray countparams = new JsonArray();
    params.forEach(r -> {//from  w  w  w .  ja  v a 2s.  c o  m
        String key = r.getKey();
        if (key.equals("orderby")) {
            order.append(" order by ").append(r.getValue());
        } else if (key.equals("page")) {
        } else if (key.equals("page_size")) {
        } else {
            if (where.length() == 0)
                where.append(" where ");
            else
                where.append(" and ");
            where.append(key).append(" like ? ");
            queryparams.add("%" + r.getValue() + "%");
            countparams.add("%" + r.getValue() + "%");
        }
    });

    if (where.length() > 0) {
        sqlCount.append(where);
        sqlQuery.append(where);
    }
    JsonObject jrs = new JsonObject();
    client.getConnection(connHandler(result, connection -> {
        log.info(sqlCount);
        if (countparams.size() == 0)
            connection.query(sqlCount.toString(), r -> {
                if (r.failed()) {
                    result.fail(r.cause());
                } else {
                    int count = r.result().getRows().get(0).getInteger("count");
                    jrs.put("total", count);
                }
            });
        else
            connection.queryWithParams(sqlCount.toString(), countparams, r -> {
                if (r.failed()) {
                    result.fail(r.cause());
                } else {
                    int count = r.result().getRows().get(0).getInteger("count");
                    jrs.put("total", count);
                }
            });
        if (order.length() > 0) {
            sqlQuery.append(order);
            queryparams.add(params.getString("orderby"));
        }
        if (params.getValue("page") == null) {
            params.put("page", "0");
            params.put("page_size", "" + Integer.MAX_VALUE);
        }
        sqlQuery.append(" limit ?,? ");
        log.info(sqlQuery);
        int page = Integer.parseInt(params.getString("page"));
        int limit = Integer.parseInt(params.getString("page_size"));
        queryparams.add(Utils.calcPage(page, limit)).add(limit);
        connection.queryWithParams(sqlQuery.toString(), queryparams, r -> {
            if (r.failed()) {
                result.fail(r.cause());
            } else {
                jrs.put("data", r.result().getRows().stream().map(Truck::new).collect(Collectors.toList()));
                jrs.put("per_page", limit);
                jrs.put("current_page", page);
            }
            result.complete(new JsonObject().put("status", 200).put("data", new JsonObject().put("list", jrs)));
        });
        connection.close();

    }));
    return result;
}