io.vertx.core.spi.VerticleFactory Java Examples

The following examples show how to use io.vertx.core.spi.VerticleFactory. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: KonduitServingLauncher.java    From konduit-serving with Apache License 2.0 5 votes vote down vote up
@Override
public void afterStartingVertx(Vertx vertx) {
    vertx.registerVerticleFactory(new VerticleFactory() {

        @Override
        public String prefix() {
            return KONDUIT_PREFIX;
        }

        @Override
        public Verticle createVerticle(String verticleName, ClassLoader classLoader)
                throws ClassNotFoundException,
                IllegalAccessException,
                InstantiationException,
                InvocationTargetException,
                NoSuchMethodException {
            String serviceType = verticleName.replace(KONDUIT_PREFIX + ":", "");

            if(services.containsKey(serviceType)) {
                return (Verticle) ClassLoader.getSystemClassLoader().loadClass(services.get(serviceType)).getConstructor().newInstance();
            } else {
                log.error("Invalid service type {}. Possible values are: {}", serviceType, services.keySet());
                System.exit(1);
                return null;
            }
        }
    });
}
 
Example #2
Source File: DeployKonduitServing.java    From konduit-serving with Apache License 2.0 5 votes vote down vote up
public static void registerInferenceVerticleFactory(Vertx vertx) {
    vertx.registerVerticleFactory(new VerticleFactory() {
        @Override
        public String prefix() {
            return SERVICE_PREFIX;
        }

        @Override
        public Verticle createVerticle(String verticleName, ClassLoader classLoader) throws Exception {
            return createInferenceVerticleFromProtocolName(verticleName.substring(verticleName.lastIndexOf(':') + 1));
        }

        private Verticle createInferenceVerticleFromProtocolName(String protocolName) throws Exception {
            ServerProtocol serverProtocol = ServerProtocol.valueOf(protocolName.toUpperCase());
            if(PROTOCOL_SERVICE_MAP.containsKey(serverProtocol)) {
                try {
                    return (Verticle) ClassLoader.getSystemClassLoader()
                            .loadClass(PROTOCOL_SERVICE_MAP.get(serverProtocol))
                            .getConstructor().newInstance();
                } catch (ClassNotFoundException classNotFoundException) {
                    vertx.close();
                    throw new IllegalStateException(
                            String.format("Missing classes for protocol service %s. Make sure the binaries contain the '%s' module.",
                                    protocolName,
                                    "konduit-serving-" + serverProtocol.name().toLowerCase())
                    );
                }
            } else {
                vertx.close();
                throw new IllegalStateException(
                        String.format("No inference service found for type: %s. Available service types are: [%s]",
                                protocolName,
                                StringUtils.join(PROTOCOL_SERVICE_MAP.keySet(), ", ")
                        )
                );
            }
        }
    });
}
 
Example #3
Source File: FeatherVerticleFactory.java    From kyoko with MIT License 5 votes vote down vote up
@Override
public Verticle createVerticle(String s, ClassLoader classLoader) throws Exception {
    var className = VerticleFactory.removePrefix(s);
    Class<? extends Verticle> clazz;
    try {
        clazz = classLoader.loadClass(className).asSubclass(Verticle.class);
    } catch (ClassNotFoundException e) {
        var cls = (Class<?>) injector.instance(ModuleManager.class).findClass(className);
        clazz = cls.asSubclass(Verticle.class);
    }
    return injector.instance(clazz);
}
 
Example #4
Source File: SpringVerticleFactory.java    From spring-vertx-ext with Apache License 2.0 5 votes vote down vote up
@Override
public synchronized Verticle createVerticle(final String verticleName, final ClassLoader classLoader) throws Exception {
    final String className = VerticleFactory.removePrefix(verticleName);
    Class<?> clazz;
    if (className.endsWith(SUFFIX)) {
        CompilingClassLoader compilingLoader = new CompilingClassLoader(classLoader, className);
        clazz = compilingLoader.loadClass(compilingLoader.resolveMainClassName());
    } else {
        clazz = classLoader.loadClass(className);
    }
    return createVerticle(clazz, classLoader);
}
 
Example #5
Source File: BaseVerticleTest.java    From konduit-serving with Apache License 2.0 4 votes vote down vote up
@Before
public void before(TestContext context) throws Exception {
    port = PortUtils.getAvailablePort();
    pubsubPort = PortUtils.getAvailablePort();
    Pair<MicrometerMetricsOptions, MeterRegistry> micrometerMetricsOptionsMeterRegistryPair = MetricsUtils.setupPrometheus();

    System.setProperty("vertx.options.maxEventLoopExecuteTime", "240000");
    VertxOptions vertxOptions = new VertxOptions();
    vertxOptions.setMaxEventLoopExecuteTime(240000);
    vertxOptions.setMetricsOptions(micrometerMetricsOptionsMeterRegistryPair.getFirst());
    vertx = Vertx.vertx(vertxOptions);
    Nd4j.getWorkspaceManager().setDebugMode(DebugMode.SPILL_EVERYTHING);
    setupVertx(vertx);
    if (isPubSub()) {
        httpServer = vertx.createHttpServer().requestHandler(getRequest());
        httpServer.listen(pubsubPort);
    }

    vertx.exceptionHandler(context.exceptionHandler());

    Nd4j.getRandom().setSeed(42);

    DeploymentOptions options = new DeploymentOptions()
            .setWorker(true).setInstances(1)
            .setWorkerPoolSize(1)
            .setConfig(getConfigObject());

    vertx.registerVerticleFactory(new VerticleFactory() {

        @Override
        public String prefix() {
            String[] split = getVertexName().split("\\.");
            return split[split.length -1];
        }

        @Override
        public Verticle createVerticle(String verticleName, ClassLoader classLoader) throws Exception {
            verticle = (Verticle) classLoader.loadClass(verticleName).newInstance();
            return verticle;
        }
    });

    vertx.deployVerticle(getVertexName(), options, context.asyncAssertSuccess());
}
 
Example #6
Source File: KonduitServlet.java    From konduit-serving with Apache License 2.0 4 votes vote down vote up
@Override
public void init(ServletConfig config) throws ServletException {
    super.init(config);
    vertx = Vertx.vertx();
    httpClient = vertx.createHttpClient();

    String configStorePath = System.getProperty(CONFIG_JSON);
    JsonObject config1 = new JsonObject();
    config1.put("path", configStorePath);
    ConfigStoreOptions httpStore = new ConfigStoreOptions()
            .setType("file")
            .setOptional(true)
            .setConfig(config1);

    CountDownLatch countDownLatch = new CountDownLatch(2);
    ConfigRetrieverOptions options = new ConfigRetrieverOptions()
            .addStore(httpStore);
    ConfigRetriever retriever = ConfigRetriever.create(vertx, options);
    String verticleClassName = System.getProperty(CLASS_NAME);
    retriever.getConfig(ar -> {
        if (ar.failed() || !new File(configStorePath).exists()) {
            log("Unable to find configuration. Continuing without.");
            log.debug("Unable to find configuration. Continuing without.");
            vertxConfig = new JsonObject().put("httpPort", DEFAULT_HTTP_PORT);
        } else {
            JsonObject config2 = ar.result();
            vertxConfig = config2;

        }

        log.debug("Attempting to deploy verticle " + verticleClassName);
        log("Attempting to deploy verticle " + verticleClassName);
        DeploymentOptions deploymentOptions = new DeploymentOptions()
                .setConfig(vertxConfig).setWorker(true)
                .setHa(false).setInstances(1)
                .setWorkerPoolSize(1);

        String[] split = verticleClassName.split("\\.");
        vertx.registerVerticleFactory(new VerticleFactory() {
            @Override
            public String prefix() {
                return split[split.length - 1];
            }

            @Override
            public Verticle createVerticle(String s, ClassLoader classLoader) throws Exception {
                Object verticle = classLoader.loadClass(verticleClassName).newInstance();
                Verticle syntaxNetVerticle = (Verticle) verticle;
                countDownLatch.countDown();
                return syntaxNetVerticle;
            }
        });


        vertx.deployVerticle(verticleClassName, deploymentOptions, handler -> {
            if (handler.failed()) {
                log.error("Unable to deploy verticle", handler.cause());
                log("Unable to deploy verticle", handler.cause());
            } else {
                log.debug("Deployed verticle");
                log("Deployed verticle");
                countDownLatch.countDown();

            }

        });

    });

    log("Initializing server");
    log.debug("Initializing server");
    try {
        countDownLatch.await();
    } catch (InterruptedException e) {
        log("Interrupting await call for servlet start", e.getCause());
        Thread.currentThread().interrupt();
    }

    log("Initialized server");
    log.debug("Initialized server");

}
 
Example #7
Source File: SfsVertxImpl.java    From sfs with Apache License 2.0 4 votes vote down vote up
@Override
public void registerVerticleFactory(VerticleFactory factory) {
    vertx.registerVerticleFactory(factory);
}
 
Example #8
Source File: SfsVertxImpl.java    From sfs with Apache License 2.0 4 votes vote down vote up
@Override
public void unregisterVerticleFactory(VerticleFactory factory) {
    vertx.unregisterVerticleFactory(factory);
}
 
Example #9
Source File: SfsVertxImpl.java    From sfs with Apache License 2.0 4 votes vote down vote up
@Override
public Set<VerticleFactory> verticleFactories() {
    return vertx.verticleFactories();
}
 
Example #10
Source File: WisdomVertxServer.java    From wisdom with Apache License 2.0 4 votes vote down vote up
/**
 * Starts the servers (HTTP and HTTPS).
 * The actual start is asynchronous.
 */
@Validate
public synchronized void start() {

    LOGGER.info("Starting the vert.x server");

    // Check whether we have a specific vertx configuration, if not try the global one, and if not use default.
    int httpPort = accessor.getConfiguration().getIntegerWithDefault(
            "vertx.http.port",
            accessor.getConfiguration().getIntegerWithDefault(ApplicationConfiguration.HTTP_PORT, 9000));
    int httpsPort = accessor.getConfiguration().getIntegerWithDefault(
            "vertx.https.port",
            accessor.getConfiguration().getIntegerWithDefault(ApplicationConfiguration.HTTPS_PORT, -1));

    initializeInetAddress();


    // Parse server configuration if any
    Configuration servers = configuration.getConfiguration("vertx.servers");
    if (servers == null) {
        if (httpPort != -1) {
            LOGGER.info("Configuring default HTTP Server");
            this.servers.add(Server.defaultHttp(accessor, vertx));
        }
        if (httpsPort != -1) {
            LOGGER.info("Configuring default HTTPS Server");
            this.servers.add(Server.defaultHttps(accessor, vertx));
        }
    } else {
        // Custom configuration
        for (String name : servers.asMap().keySet()) {
            LOGGER.info("Configuring server {}", name);
            this.servers.add(Server.from(accessor, vertx, name,
                    servers.getConfiguration(name)));
        }
    }

    // Check whether or not the wisdom-internal verticle factory is already registered
    boolean found = false;
    for (VerticleFactory factory : vertx.verticleFactories()) {
        if (factory.prefix().equalsIgnoreCase("wisdom-internal")) {
            found = true;
        }
    }

    if (!found) {
        vertx.registerVerticleFactory(new WisdomInternalVerticleFactory(accessor, this.servers));
    }

    vertx.runOnContext(v -> vertx.deployVerticle("wisdom-internal:wisdom", ar -> {
        LOGGER.info("Wisdom verticle deployed : " + ar.result());
        deploymentId = ar.result();
    }));
}