Example usage for io.vertx.core Vertx clusteredVertx

List of usage examples for io.vertx.core Vertx clusteredVertx

Introduction

In this page you can find the example usage for io.vertx.core Vertx clusteredVertx.

Prototype

static void clusteredVertx(VertxOptions options, Handler<AsyncResult<Vertx>> resultHandler) 

Source Link

Document

Creates a clustered instance using the specified options.

Usage

From source file:es.upv.grycap.opengateway.core.VertxService.java

License:Apache License

@Override
protected void startUp() throws Exception {
    final AtomicBoolean success = new AtomicBoolean(true);
    final Consumer<Vertx> runner = vertx -> verticles.stream()
            .forEach(ver -> vertx.deployVerticle(verticleName(ver), deploymentOptions, res -> {
                if (res == null || !res.succeeded()) {
                    success.set(false);//from   ww w  .  j  a  va  2  s  . co m
                    if (res != null)
                        LOGGER.error(String.format("Failed to deploy verticle [type=%s].", ver.getSimpleName()),
                                res.cause());
                    else
                        LOGGER.error(String.format("Failed to deploy verticle [type=%s]. Unknown cause.",
                                ver.getSimpleName()));
                } else
                    LOGGER.info(String.format("New verticle deployed: [type=%s, id=%s].", ver.getSimpleName(),
                            res.result()));
            }));
    final CompletableFuture<Void> future = new CompletableFuture<>();
    if (vertxOptions.isClustered()) {
        // configure Hazelcast           
        final Config hazelcastConfig = new Config();
        hazelcastConfig.setInstanceName("opengateway-cluster");
        final NetworkConfig hazelcastNetwork = hazelcastConfig.getNetworkConfig();
        hazelcastNetwork.setPublicAddress(deploymentOptions.getConfig().getString("cluster.public-address"));
        final JoinConfig hazelcastJoin = hazelcastNetwork.getJoin();
        hazelcastJoin.getMulticastConfig().setEnabled(false);
        hazelcastJoin.getTcpIpConfig().addMember(deploymentOptions.getConfig().getString("cluster.network"))
                .setEnabled(true);
        hazelcastNetwork.getInterfaces()
                .addInterface(deploymentOptions.getConfig().getString("cluster.network")).setEnabled(true);
        final GroupConfig hazelcastGroup = hazelcastConfig.getGroupConfig();
        hazelcastGroup.setName(deploymentOptions.getConfig().getString("cluster.name"))
                .setPassword(deploymentOptions.getConfig().getString("cluster.secret"));
        final ClusterManager clusterManager = new HazelcastClusterManager(hazelcastConfig);
        vertxOptions.setClusterManager(clusterManager);
        Vertx.clusteredVertx(vertxOptions, res -> {
            if (res.succeeded()) {
                vertx = res.result();
                vertx.registerVerticleFactory(new OgVerticleFactory(loadBalancerClient));
                // TODO
                runner.accept(vertx);
            } else
                LOGGER.error("Failed to start Vert.x system.", res.cause());
            future.complete(null);
        });
    } else {
        vertx = Vertx.vertx(vertxOptions);
        vertx.registerVerticleFactory(new OgVerticleFactory(loadBalancerClient));
        // TODO
        runner.accept(vertx);
        future.complete(null);
    }
    checkState(success.get(), "Exiting since not all verticles were successfully deployed.");
    try {
        future.get(deploymentOptions.getConfig().getLong("daemon-service.startup-timeout"), TimeUnit.SECONDS);
        // additional startup operations could be executed here
    } catch (InterruptedException | TimeoutException e) {
        throw new IllegalStateException("Given up to start service daemon due to interruption or timeout.");
    } catch (ExecutionException e) {
        throw (e.getCause() instanceof Exception ? (Exception) e.getCause() : e);
    }
}

From source file:eu.rethink.mn.MsgNode.java

License:Apache License

public static void main(String[] args) {
    final NodeConfig config = readConfig();
    try {/*  w ww .j av  a  2 s.  c o  m*/
        final ClusterManager mgr = new HazelcastClusterManager();
        final MsgNode msgNode = new MsgNode(mgr, config);

        final VertxOptions options = new VertxOptions().setClusterManager(mgr);
        Vertx.clusteredVertx(options, res -> {
            if (res.succeeded()) {
                Vertx vertx = res.result();
                vertx.deployVerticle(msgNode);

                DeploymentOptions verticleOptions = new DeploymentOptions().setWorker(true);
                vertx.deployVerticle("js:./src/js/connector/RegistryConnectorVerticle.js", verticleOptions);
                vertx.deployVerticle("js:./src/js/connector/GlobalRegistryConnectorVerticle.js",
                        verticleOptions);
                vertx.deployVerticle("js:./src/js/connector/PoliciesConnectorVerticle.js", verticleOptions);
            } else {
                System.exit(-1);
            }
        });

    } catch (Exception e) {
        System.out.println("Problem in config setup.");
        System.exit(-1);
    }
}

From source file:examples.EventBusExamples.java

License:Open Source License

public void example12() {
    VertxOptions options = new VertxOptions();
    Vertx.clusteredVertx(options, res -> {
        if (res.succeeded()) {
            Vertx vertx = res.result();/* ww  w  . ja  v a 2s .c  om*/
            EventBus eventBus = vertx.eventBus();
            System.out.println("We now have a clustered event bus: " + eventBus);
        } else {
            System.out.println("Failed: " + res.cause());
        }
    });
}

From source file:examples.EventBusExamples.java

License:Open Source License

public void example13() {
    VertxOptions options = new VertxOptions().setEventBusOptions(new EventBusOptions().setSsl(true)
            .setKeyStoreOptions(new JksOptions().setPath("keystore.jks").setPassword("wibble"))
            .setTrustStoreOptions(new JksOptions().setPath("keystore.jks").setPassword("wibble"))
            .setClientAuth(ClientAuth.REQUIRED));

    Vertx.clusteredVertx(options, res -> {
        if (res.succeeded()) {
            Vertx vertx = res.result();// w  w w.j a  v a 2 s.  c o  m
            EventBus eventBus = vertx.eventBus();
            System.out.println("We now have a clustered event bus: " + eventBus);
        } else {
            System.out.println("Failed: " + res.cause());
        }
    });
}

From source file:examples.EventBusExamples.java

License:Open Source License

public void example14() {
    VertxOptions options = new VertxOptions().setEventBusOptions(
            new EventBusOptions().setClusterPublicHost("whatever").setClusterPublicPort(1234));

    Vertx.clusteredVertx(options, res -> {
        if (res.succeeded()) {
            Vertx vertx = res.result();/*from   w w  w .  ja  v a2  s.c o m*/
            EventBus eventBus = vertx.eventBus();
            System.out.println("We now have a clustered event bus: " + eventBus);
        } else {
            System.out.println("Failed: " + res.cause());
        }
    });
}

From source file:io.apiman.gateway.test.junit.vertx3.Vertx3GatewayTestServer.java

License:Apache License

@Override
public void start() {
    try {/*from   w  w  w.  j a va2  s  .co  m*/
        resetter.reset();

        if (clustered) {
            CountDownLatch clusteredStart = new CountDownLatch(1);
            Vertx.clusteredVertx(new VertxOptions().setClustered(clustered).setClusterHost("localhost")
                    .setBlockedThreadCheckInterval(9999999), result -> {
                        if (result.succeeded()) {
                            System.out.println("**** Clustered Vert.x started up successfully! ****");
                            this.vertx = result.result();
                            clusteredStart.countDown();
                        } else {
                            throw new RuntimeException(result.cause());
                        }
                    });
            clusteredStart.await();
        } else {
            vertx = Vertx.vertx(new VertxOptions().setBlockedThreadCheckInterval(9999999));
        }
        echoServer.start();

        startLatch = new CountDownLatch(1);

        DeploymentOptions options = new DeploymentOptions();
        options.setConfig(vertxConf);

        vertx.deployVerticle(InitVerticle.class.getCanonicalName(), options,
                new Handler<AsyncResult<String>>() {

                    @Override
                    public void handle(AsyncResult<String> event) {
                        System.out.println("Deployed init verticle!");
                        startLatch.countDown();
                    }
                });

        startLatch.await();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:io.github.jdocker.serviceregistry.ServiceRegistry.java

License:Apache License

public static void main(String[] args) {
    // DeploymentOptions options = new DeploymentOptions().setInstances(1).setConfig(new JsonObject().put("host","localhost"));
    // Vertx.vertx().deployVerticle(ServiceRegistry.class.getServiceName(),options);

    VertxOptions vOpts = new VertxOptions();
    DeploymentOptions options = new DeploymentOptions().setInstances(1);
    vOpts.setClustered(true);// w  w w  . java 2s  . co m
    Vertx.clusteredVertx(vOpts, cluster -> {
        if (cluster.succeeded()) {
            final Vertx result = cluster.result();
            result.deployVerticle(ServiceRegistry.class.getName(), options, handle -> {

            });
        }
    });
}

From source file:org.apache.tamaya.examples.distributed.Display.java

License:Apache License

public Display() {
    LOG.info("\n-----------------------------------\n" + "Starting Display...\n"
            + "-----------------------------------");
    LOG.info("--- Starting Vertx cluster...");
    // Reusing the hazelcast instance already in place for vertx...
    ClusterManager mgr = new HazelcastClusterManager(hazelCastPropertySource.getHazelcastInstance());
    VertxOptions vertxOptions = new VertxOptions().setClusterManager(mgr);
    Vertx.clusteredVertx(vertxOptions, h -> {
        vertx = h.result();/*w  w  w  .  ja  va 2  s  .c o m*/
    });
    LOG.info("--- Waiting for Vertx cluster...");
    while (vertx == null) {
        try {
            Thread.sleep(100L);
        } catch (InterruptedException e) {
            LOG.severe("Vertx cluster did not respond in time.");
            Thread.currentThread().interrupt();
        }
    }
    titleField.getStyleClass().add("title");
    contentField.getStyleClass().add("content");
    monitorField.getStyleClass().add("monitor");
    titleField.setId("title");
    titleField.setEditable(false);
    contentField.setId("scene");
    contentField.setEditable(false);
}

From source file:org.apache.tamaya.examples.distributed.DisplayManager.java

License:Apache License

public DisplayManager() {
    LOG.info("\n-----------------------------------\n" + "Starting DisplayDisplayManager...\n"
            + "-----------------------------------");
    LOG.info("--- Starting Vertx cluster...");
    // Reusing the hazelcast instance already in place for vertx...
    ClusterManager mgr = new HazelcastClusterManager(hazelCastPropertySource.getHazelcastInstance());
    VertxOptions vertxOptions = new VertxOptions().setClusterManager(mgr);
    Vertx.clusteredVertx(vertxOptions, h -> {
        vertx = h.result();//w ww. j ava2s  . co  m
    });
    LOG.info("--- Waiting for Vertx cluster...");
    while (vertx == null) {
        try {
            Thread.sleep(100L);
        } catch (InterruptedException e) {
            LOG.severe("Vertx cluster did not respond in time.");
            Thread.currentThread().interrupt();
        }
    }
    monitorField.getStyleClass().add("monitor");
    configField.getStyleClass().add("config");
}

From source file:org.hammer.verticle.VertxProducer.java

License:Apache License

@PostConstruct
void init() throws ExecutionException, InterruptedException {
    VertxOptions options = new VertxOptions().setClusterManager(new HazelcastClusterManager(hazelcastInstance));
    CompletableFuture<Vertx> future = new CompletableFuture<>();
    Vertx.clusteredVertx(options, ar -> {
        if (ar.succeeded()) {
            future.complete(ar.result());
        } else {/*  w ww  . j  a  v a 2 s .c om*/
            future.completeExceptionally(ar.cause());
        }
    });
    vertx = future.get();
}