options = new VertxOptions().setMetricsOptions(metricsOptions); configureFromSystemProperties(options, VERTX_OPTIONS_PROP_PREFIX); if (clustered) { CountDownLatch latch = new CountDownLatch(1); AtomicReference<AsyncResult<Vertx>> result = new AtomicReference<>(); options.setClusterHost(clusterHost).setClusterPort(clusterPort).setClustered(true); if (ha) { String haGroup = args.map.get("-hagroup"); int quorumSize = args.getInt("-quorum"); options.setHAEnabled(true); if (haGroup != null) { options.setHAGroup(haGroup); options.setQuorumSize(quorumSize); return null; if (result.get().failed()) { log.error("Failed to form cluster"); result.get().cause().printStackTrace(); return null; vertx = result.get().result(); } else { beforeStartingVertx(options); vertx = Vertx.vertx(options);
public static void runExample(String exampleDir, String verticleID, VertxOptions options, DeploymentOptions deploymentOptions) { if (options == null) { options = new VertxOptions(); try { if (deploymentOptions != null) { vertx.deployVerticle(verticleID, deploymentOptions); } else { vertx.deployVerticle(verticleID); if (options.isClustered()) { Vertx.clusteredVertx(options, res -> { if (res.succeeded()) { Vertx vertx = res.result(); runner.accept(vertx); } else { res.cause().printStackTrace();
protected Vertx startVertx(String haGroup, int quorumSize, boolean ha) throws Exception { VertxOptions options = new VertxOptions().setHAEnabled(ha).setClustered(true). setClusterHost("localhost").setClusterManager(getClusterManager()); if (ha) { options.setQuorumSize(quorumSize); if (haGroup != null) { options.setHAGroup(haGroup); } } CountDownLatch latch = new CountDownLatch(1); AtomicReference<Vertx> vertxRef = new AtomicReference<>(); clusteredVertx(options, onSuccess(vertx -> { vertxRef.set(vertx); latch.countDown(); })); latch.await(2, TimeUnit.MINUTES); return vertxRef.get(); }
/** * Specifies Vert.x instance configuration, this is essential for clustering on multiple separate machines and * Docker containers in order to make Event Bus send/consume messages appropriately * @param clusterManager represents the cluster manager * @return VertxOptions object to be used in deployment */ private static VertxOptions configureVertx(ClusterManager clusterManager) { /* the default value of the cluster host (localhost) is used here, if we want to have multiple machines/docker containers in our cluster we must configure the cluster host properly on each node in order for the event bus to send/consume messages properly between our verticles, to do so we use the method setClusterHost and give it this node's IP. For example: options.setClusterHost(192.168.1.12); */ VertxOptions options = new VertxOptions() .setClustered(true) .setClusterManager(clusterManager); return options; } }
protected VertxOptions getOptions() { VertxOptions options = new VertxOptions(); options.setPreferNativeTransport(USE_NATIVE_TRANSPORT); return options; }
public static void main(String[] args) { Vertx.clusteredVertx(new VertxOptions().setClustered(true), ar -> { if (ar.failed()) { System.err.println("Cannot create vert.x instance : " + ar.cause()); } else { Vertx vertx = ar.result(); vertx.deployVerticle(DisplayStatsApp.class.getName()); } }); } }
@Test public void testSearchDomainWithNdots2() throws Exception { Map<String, String> records = new HashMap<>(); records.put("host1.sub.foo.com", "127.0.0.1"); records.put("host2.sub.foo.com", "127.0.0.2"); records.put("host2.sub", "127.0.0.3"); dnsServer.testResolveA(records); VertxInternal vertx = (VertxInternal) vertx(new VertxOptions().setAddressResolverOptions( new AddressResolverOptions(). addServer(dnsServerAddress.getAddress().getHostAddress() + ":" + dnsServerAddress.getPort()). setOptResourceEnabled(false). addSearchDomain("foo.com"). setNdots(2) )); CountDownLatch latch1 = new CountDownLatch(1); vertx.resolveAddress("host1.sub", onSuccess(resolved -> { assertEquals("127.0.0.1", resolved.getHostAddress()); latch1.countDown(); })); awaitLatch(latch1); // "host2.sub" is resolved with the foo.com search domain as ndots = 2 CountDownLatch latch2 = new CountDownLatch(1); vertx.resolveAddress("host2.sub", onSuccess(resolved -> { assertEquals("127.0.0.2", resolved.getHostAddress()); latch2.countDown(); })); awaitLatch(latch2); }
protected void startNodes(int numNodes, VertxOptions options) { CountDownLatch latch = new CountDownLatch(numNodes); vertices = new Vertx[numNodes]; for (int i = 0; i < numNodes; i++) { int index = i; clusteredVertx(options.setClusterHost("localhost").setClusterPort(0).setClustered(true) .setClusterManager(getClusterManager()), ar -> { try { if (ar.failed()) { ar.cause().printStackTrace(); } assertTrue("Failed to start node", ar.succeeded()); vertices[index] = ar.result(); } finally { latch.countDown(); } }); } try { assertTrue(latch.await(2, TimeUnit.MINUTES)); } catch (InterruptedException e) { fail(e.getMessage()); } }
@Test public void testResolveFromFile() { File f = new File(new File(new File(new File("src"), "test"), "resources"), "hosts_config.txt"); VertxInternal vertx = (VertxInternal) vertx(new VertxOptions().setAddressResolverOptions(new AddressResolverOptions().setHostsPath(f.getAbsolutePath()))); vertx.resolveAddress("server.net", onSuccess(addr -> { assertEquals("192.168.0.15", addr.getHostAddress()); assertEquals("server.net", addr.getHostName()); testComplete(); })); await(); }
if (optionsJson == null) { MetricsOptions metricsOptions = getMetricsOptions(); options = new VertxOptions().setMetricsOptions(metricsOptions); } else { MetricsOptions metricsOptions = getMetricsOptions(optionsJson.getJsonObject("metricsOptions")); options = new VertxOptions(optionsJson).setMetricsOptions(metricsOptions); if (isClustered()) { log.info("Starting clustering..."); if (!Objects.equals(options.getClusterHost(), VertxOptions.DEFAULT_CLUSTER_HOST)) { clusterHost = options.getClusterHost(); if (options.getClusterPort() != VertxOptions.DEFAULT_CLUSTER_PORT) { clusterPort = options.getClusterPort(); if (!Objects.equals(options.getClusterPublicHost(), VertxOptions.DEFAULT_CLUSTER_PUBLIC_HOST)) { clusterPublicHost = options.getClusterPublicHost(); if (options.getClusterPublicPort() != VertxOptions.DEFAULT_CLUSTER_PUBLIC_PORT) { clusterPublicPort = options.getClusterPublicPort(); options.setClustered(true) .setClusterHost(clusterHost).setClusterPort(clusterPort) .setClusterPublicHost(clusterPublicHost); if (clusterPublicPort != -1) { options.setClusterPublicPort(clusterPublicPort); options.setHAEnabled(true); if (haGroup != null) {
CountDownLatch latch = new CountDownLatch(1); Vertx vertx = vertx(new VertxOptions().setMetricsOptions(new MetricsOptions().setEnabled(true).setFactory(factory))); Context ctx = contextFactory.apply(vertx); ctx.runOnContext(v1 -> { HttpServer server = vertx.createHttpServer().websocketHandler(ws -> { ws.handler(buf -> { ws.write(Buffer.buffer("bye")); server.listen(8080, "localhost", onSuccess(s -> { expectedThread.set(Thread.currentThread()); expectedContext.set(Vertx.currentContext()); latch.countDown(); })); }); awaitLatch(latch); HttpClient client = vertx.createHttpClient(); client.websocket(8080, "localhost", "/", ws -> { ws.handler(buf -> {
@Test public void testCallbackInvokedOnFailure() throws Exception { // will trigger java.net.UnknownHostException String hostName = "zoom.zoom.zen.tld"; VertxOptions options = new VertxOptions() .setClusterManager(new FakeClusterManager()) .setClusterHost(hostName); AtomicReference<AsyncResult<Vertx>> resultRef = new AtomicReference<>(); CountDownLatch latch = new CountDownLatch(1); Vertx.clusteredVertx(options, ar -> { resultRef.set(ar); latch.countDown(); }); awaitLatch(latch); assertFalse(resultRef.get() == null); assertTrue(resultRef.get().failed()); assertTrue("Was expecting failure to be an instance of UnknownHostException", resultRef.get().cause() instanceof UnknownHostException); } }
CountDownLatch latch = new CountDownLatch(1); Vertx vertx = vertx(new VertxOptions().setMetricsOptions(new MetricsOptions().setEnabled(true).setFactory(factory))); Context ctx = contextFactory.apply(vertx); ctx.runOnContext(v1 -> { NetServer server = vertx.createNetServer().connectHandler(so -> { so.handler(buf -> { so.write("bye"); server.listen(1234, "localhost", onSuccess(s -> { expectedThread.set(Thread.currentThread()); expectedContext.set(Vertx.currentContext()); checker.accept(expectedThread.get(), expectedContext.get()); latch.countDown(); })); }); awaitLatch(latch); NetClient client = vertx.createNetClient(); client.connect(1234, "localhost", onSuccess(so -> { so.handler(buf -> {
@Test public void testResolveMissingLocalhost() throws Exception { InetAddress localhost = InetAddress.getByName("localhost"); VertxInternal vertx = (VertxInternal) vertx(new VertxOptions().setAddressResolverOptions( new AddressResolverOptions(). addServer(dnsServerAddress.getAddress().getHostAddress() + ":" + dnsServerAddress.getPort()). setOptResourceEnabled(false) )); CompletableFuture<Void> test1 = new CompletableFuture<>(); vertx.resolveAddress("localhost", ar -> { if (ar.succeeded()) { InetAddress resolved = ar.result(); if (resolved.equals(localhost)) { test1.complete(null); } else { test1.completeExceptionally(ar.cause()); vertx.resolveAddress("LOCALHOST", ar -> { if (ar.succeeded()) { InetAddress resolved = ar.result(); NetServer server = vertx.createNetServer(new NetServerOptions().setPort(1234).setHost(localhost.getHostAddress())); try { server.connectHandler(so -> {
@Test public void testSimpleFailover() throws Exception { startNodes(2, new VertxOptions().setHAEnabled(true)); DeploymentOptions options = new DeploymentOptions().setHa(true); JsonObject config = new JsonObject().put("foo", "bar"); options.setConfig(config); CountDownLatch latch = new CountDownLatch(1); vertices[0].deployVerticle("java:" + HAVerticle1.class.getName(), options, ar -> { assertTrue(ar.succeeded()); assertEquals(1, vertices[0].deploymentIDs().size()); assertEquals(0, vertices[1].deploymentIDs().size()); latch.countDown(); }); awaitLatch(latch); kill(0); assertWaitUntil(() -> vertices[1].deploymentIDs().size() == 1); checkDeploymentExists(1, "java:" + HAVerticle1.class.getName(), options); }
@Test public void testNetSearchDomain() throws Exception { Map<String, String> records = new HashMap<>(); records.put("host1.foo.com", "127.0.0.1"); dnsServer.testResolveA(records); vertx.close(); vertx = vertx(new VertxOptions().setAddressResolverOptions( new AddressResolverOptions(). setHostsValue(Buffer.buffer()). setNdots(1). addServer(dnsServerAddress.getAddress().getHostAddress() + ":" + dnsServerAddress.getPort()). setOptResourceEnabled(false). addSearchDomain("foo.com") )); testNet("host1"); }
AtomicBoolean bytesReadCalled = new AtomicBoolean(); AtomicBoolean bytesWrittenCalled = new AtomicBoolean(); CountDownLatch closeCalled = new CountDownLatch(1); VertxMetricsFactory factory = (options) -> new DummyVertxMetrics() { @Override Vertx vertx = vertx(new VertxOptions().setMetricsOptions(new MetricsOptions().setEnabled(true).setFactory(factory))); Context ctx = contextFactory.apply(vertx); ctx.runOnContext(v1 -> { expectedThread.set(Thread.currentThread()); expectedContext.set(Vertx.currentContext()); DatagramSocket socket = vertx.createDatagramSocket(); socket.listen(1234, "localhost", ar1 -> { assertTrue(ar1.succeeded()); checker.accept(expectedThread.get(), expectedContext.get()); socket.handler(packet -> { }); socket.send(Buffer.buffer("msg"), 1234, "localhost", ar2 -> { assertTrue(ar2.succeeded()); }); });
@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 { future.completeExceptionally(ar.cause()); } }); vertx = future.get(); }
Vertx vertx = vertx(new VertxOptions().setMetricsOptions(new MetricsOptions().setEnabled(true).setFactory(factory))); vertx.deployVerticle(new AbstractVerticle() { @Override public void start() throws Exception { assertTrue(ar1.succeeded()); vertx.undeploy(ar1.result(), ar2 -> { assertTrue(ar1.succeeded()); assertTrue(deployedCalled.get()); assertTrue(undeployedCalled.get());
@Test public void testCreateClusteredVertxAsync() { VertxOptions options = new VertxOptions(); options.setClustered(true); clusteredVertx(options, ar -> { assertTrue(ar.succeeded()); assertNotNull(ar.result()); assertTrue(ar.result().isClustered()); Vertx v = ar.result(); v.close(ar2 -> { assertTrue(ar2.succeeded()); testComplete(); }); }); await(); }