io.vertx.spi.cluster.hazelcast.HazelcastClusterManager Java Examples

The following examples show how to use io.vertx.spi.cluster.hazelcast.HazelcastClusterManager. 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: Examples.java    From vertx-hazelcast with Apache License 2.0 6 votes vote down vote up
public void example2() {

    Config hazelcastConfig = new Config();

    // Now set some stuff on the config (omitted)

    ClusterManager mgr = new HazelcastClusterManager(hazelcastConfig);

    VertxOptions options = new VertxOptions().setClusterManager(mgr);

    Vertx.clusteredVertx(options, res -> {
      if (res.succeeded()) {
        Vertx vertx = res.result();
      } else {
        // failed!
      }
    });
  }
 
Example #2
Source File: Examples.java    From vertx-hazelcast with Apache License 2.0 6 votes vote down vote up
public void customizeDefaultConfig() {
  Config hazelcastConfig = ConfigUtil.loadConfig();

  hazelcastConfig.setClusterName("my-cluster-name");

  ClusterManager mgr = new HazelcastClusterManager(hazelcastConfig);

  VertxOptions options = new VertxOptions().setClusterManager(mgr);

  Vertx.clusteredVertx(options, res -> {
    if (res.succeeded()) {
      Vertx vertx = res.result();
    } else {
      // failed!
    }
  });
}
 
Example #3
Source File: Examples.java    From vertx-hazelcast with Apache License 2.0 6 votes vote down vote up
public void liteMemberConfig() {
  Config hazelcastConfig = ConfigUtil.loadConfig()
    .setLiteMember(true);

  ClusterManager mgr = new HazelcastClusterManager(hazelcastConfig);

  VertxOptions options = new VertxOptions().setClusterManager(mgr);

  Vertx.clusteredVertx(options, res -> {
    if (res.succeeded()) {
      Vertx vertx = res.result();
    } else {
      // failed!
    }
  });
}
 
Example #4
Source File: Application.java    From skywalking with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {
    System.setProperty("vertx.disableFileCPResolving", "true");
    ClusterManager mgr = new HazelcastClusterManager();
    VertxOptions options = new VertxOptions().setClusterManager(mgr);
    Vertx.clusteredVertx(options, cluster -> {
        if (cluster.succeeded()) {
            cluster.result().deployVerticle(new ClusterReceiver(), deploy -> {
                if (deploy.succeeded()) {
                    ClusterManager mgr2 = new HazelcastClusterManager();
                    VertxOptions options2 = new VertxOptions().setClusterManager(mgr2);
                    Vertx.clusteredVertx(options2, cluster2 -> {
                        if (cluster2.succeeded()) {
                            cluster2.result().deployVerticle(new VertxEventbusController());
                        } else {
                            cluster2.cause().printStackTrace();
                            System.exit(-1);
                        }
                    });
                } else {
                    deploy.cause().printStackTrace();
                    System.exit(-1);
                }
            });
        } else {
            cluster.cause().printStackTrace();
            System.exit(-1);
        }
    });
}
 
Example #5
Source File: Examples.java    From vertx-hazelcast with Apache License 2.0 5 votes vote down vote up
public void example1() {

    ClusterManager mgr = new HazelcastClusterManager();

    VertxOptions options = new VertxOptions().setClusterManager(mgr);

    Vertx.clusteredVertx(options, res -> {
      if (res.succeeded()) {
        Vertx vertx = res.result();
      } else {
        // failed!
      }
    });
  }
 
Example #6
Source File: Examples.java    From vertx-hazelcast with Apache License 2.0 5 votes vote down vote up
public void example3(HazelcastInstance hazelcastInstance) {
  ClusterManager mgr = new HazelcastClusterManager(hazelcastInstance);
  VertxOptions options = new VertxOptions().setClusterManager(mgr);
  Vertx.clusteredVertx(options, res -> {
    if (res.succeeded()) {
      Vertx vertx = res.result();
    } else {
      // failed!
    }
  });
}
 
Example #7
Source File: HazelcastDiscoveryImplClusteredTest.java    From vertx-hazelcast with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() {
  Random random = new Random();
  System.setProperty("vertx.hazelcast.test.group.name", new BigInteger(128, random).toString(32));
  VertxOptions options = new VertxOptions()
    .setClusterManager(new HazelcastClusterManager());
  Vertx.clusteredVertx(options, ar -> {
    vertx = ar.result();
  });
  await().until(() -> vertx != null);
  discovery = new DiscoveryImpl(vertx, new ServiceDiscoveryOptions());
}
 
Example #8
Source File: MainDeploy.java    From okapi with Apache License 2.0 5 votes vote down vote up
private void deployClustered(final Logger logger, Handler<AsyncResult<Vertx>> fut) {
  if (hazelcastConfig == null) {
    hazelcastConfig = ConfigUtil.loadConfig();
    if (clusterHost != null) {
      NetworkConfig network = hazelcastConfig.getNetworkConfig();
      InterfacesConfig interfacesConfig = network.getInterfaces();
      interfacesConfig.setEnabled(true).addInterface(clusterHost);
    }
  }
  hazelcastConfig.setProperty("hazelcast.logging.type", "log4j");

  HazelcastClusterManager mgr = new HazelcastClusterManager(hazelcastConfig);
  vopt.setClusterManager(mgr);
  EventBusOptions eventBusOptions = vopt.getEventBusOptions();
  if (clusterHost != null) {
    logger.info("clusterHost={}", clusterHost);
    eventBusOptions.setHost(clusterHost);
  } else {
    logger.warn("clusterHost not set");
  }
  if (clusterPort != -1) {
    logger.info("clusterPort={}", clusterPort);
    eventBusOptions.setPort(clusterPort);
  } else {
    logger.warn("clusterPort not set");
  }
  eventBusOptions.setClustered(true);

  Vertx.clusteredVertx(vopt, res -> {
    if (res.succeeded()) {
      MainVerticle v = new MainVerticle();
      v.setClusterManager(mgr);
      deploy(v, res.result(), fut);
    } else {
      fut.handle(Future.failedFuture(res.cause()));
    }
  });
}
 
Example #9
Source File: ProgrammaticHazelcastClusterManagerTest.java    From vertx-hazelcast with Apache License 2.0 5 votes vote down vote up
private void testProgrammatic(HazelcastClusterManager mgr, Config config) throws Exception {
  mgr.setConfig(config);
  assertEquals(config, mgr.getConfig());
  VertxOptions options = new VertxOptions().setClusterManager(mgr);
  Vertx.clusteredVertx(options, res -> {
    assertTrue(res.succeeded());
    assertNotNull(mgr.getHazelcastInstance());
    res.result().close(res2 -> {
      assertTrue(res2.succeeded());
      testComplete();
    });
  });
  await();
}
 
Example #10
Source File: ProgrammaticHazelcastClusterManagerTest.java    From vertx-hazelcast with Apache License 2.0 5 votes vote down vote up
@Test
public void testProgrammaticSetConfig() throws Exception {
  Config config = createConfig();
  HazelcastClusterManager mgr = new HazelcastClusterManager();
  mgr.setConfig(config);
  testProgrammatic(mgr, config);
}
 
Example #11
Source File: Lifecycle.java    From vertx-hazelcast with Apache License 2.0 5 votes vote down vote up
public static void closeClustered(List<Vertx> clustered) throws Exception {
  for (Vertx vertx : clustered) {
    VertxInternal vertxInternal = (VertxInternal) vertx;

    HazelcastClusterManager clusterManager = getHazelcastClusterManager(vertxInternal.getClusterManager());

    if (clusterManager != null) {
      HazelcastInstance hazelcastInstance = clusterManager.getHazelcastInstance();

      SECONDS.sleep(2); // Make sure rebalancing has been triggered

      long start = System.currentTimeMillis();
      try {
        while (!hazelcastInstance.getPartitionService().isClusterSafe()
          && System.currentTimeMillis() - start < MILLISECONDS.convert(2, MINUTES)) {
          MILLISECONDS.sleep(100);
        }
      } catch (Exception ignore) {
      }
    }

    CountDownLatch latch = new CountDownLatch(1);
    vertxInternal.close(ar -> {
      if (ar.failed()) {
        log.error("Failed to shutdown vert.x", ar.cause());
      }
      latch.countDown();
    });
    latch.await(2, TimeUnit.MINUTES);
  }
}
 
Example #12
Source File: Lifecycle.java    From vertx-hazelcast with Apache License 2.0 5 votes vote down vote up
private static HazelcastClusterManager getHazelcastClusterManager(ClusterManager cm) {
  if (cm == null) {
    return null;
  }
  if (cm instanceof HazelcastClusterManager) {
    return (HazelcastClusterManager) cm;
  }
  throw new ClassCastException("Unexpected cluster manager implementation: " + cm.getClass());
}
 
Example #13
Source File: ProgrammaticHazelcastClusterManagerTest.java    From vertx-hazelcast with Apache License 2.0 4 votes vote down vote up
@Test
public void testSharedDataUsingCustomHazelcast() throws Exception {
  HazelcastInstance instance1 = Hazelcast.newHazelcastInstance(createConfig());
  HazelcastInstance instance2 = Hazelcast.newHazelcastInstance(createConfig());

  HazelcastClusterManager mgr1 = new HazelcastClusterManager(instance1);
  HazelcastClusterManager mgr2 = new HazelcastClusterManager(instance2);
  VertxOptions options1 = new VertxOptions().setClusterManager(mgr1);
  options1.getEventBusOptions().setHost("127.0.0.1");
  VertxOptions options2 = new VertxOptions().setClusterManager(mgr2);
  options2.getEventBusOptions().setHost("127.0.0.1");

  AtomicReference<Vertx> vertx1 = new AtomicReference<>();
  AtomicReference<Vertx> vertx2 = new AtomicReference<>();

  Vertx.clusteredVertx(options1, res -> {
    assertTrue(res.succeeded());
    assertNotNull(mgr1.getHazelcastInstance());
    res.result().sharedData().getClusterWideMap("mymap1", ar -> {
      ar.result().put("news", "hello", v -> {
        vertx1.set(res.result());
      });
    });
  });

  assertWaitUntil(() -> vertx1.get() != null);

  Vertx.clusteredVertx(options2, res -> {
    assertTrue(res.succeeded());
    assertNotNull(mgr2.getHazelcastInstance());
    vertx2.set(res.result());
    res.result().sharedData().getClusterWideMap("mymap1", ar -> {
      ar.result().get("news", r -> {
        assertEquals("hello", r.result());
        testComplete();
      });
    });
  });

  await();

  vertx1.get().close(ar -> vertx1.set(null));
  vertx2.get().close(ar -> vertx2.set(null));

  assertWaitUntil(() -> vertx1.get() == null && vertx2.get() == null);

  // be sure stopping vertx did not cause or require our custom hazelcast to shutdown

  assertTrue(instance1.getLifecycleService().isRunning());
  assertTrue(instance2.getLifecycleService().isRunning());

  instance1.shutdown();
  instance2.shutdown();

}
 
Example #14
Source File: HazelcastClusteredSessionHandlerTest.java    From vertx-hazelcast with Apache License 2.0 4 votes vote down vote up
@Override
protected ClusterManager getClusterManager() {
  return new HazelcastClusterManager();
}
 
Example #15
Source File: ProgrammaticHazelcastClusterManagerTest.java    From vertx-hazelcast with Apache License 2.0 4 votes vote down vote up
@Test
public void testThatExternalHZInstanceCanBeShutdown() {
  // This instance won't be used by vert.x
  HazelcastInstance instance = Hazelcast.newHazelcastInstance(createConfig());
  String nodeID = instance.getCluster().getLocalMember().getUuid().toString();

  HazelcastClusterManager mgr = new HazelcastClusterManager(createConfig());
  VertxOptions options = new VertxOptions().setClusterManager(mgr);
  options.getEventBusOptions().setHost("127.0.0.1");

  AtomicReference<Vertx> vertx1 = new AtomicReference<>();

  Vertx.clusteredVertx(options, res -> {
    assertTrue(res.succeeded());
    assertNotNull(mgr.getHazelcastInstance());
    res.result().sharedData().getClusterWideMap("mymap1", ar -> {
      ar.result().put("news", "hello", v -> {
        vertx1.set(res.result());
      });
    });
  });

  assertWaitUntil(() -> vertx1.get() != null);
  int size = mgr.getNodes().size();
  assertTrue(mgr.getNodes().contains(nodeID));

  // Retrieve the value inserted by vert.x
  Map<Object, Object> map = instance.getMap("mymap1");
  Map<Object, Object> anotherMap = instance.getMap("mymap2");
  assertEquals(map.get("news"), "hello");
  map.put("another-key", "stuff");
  anotherMap.put("another-key", "stuff");
  map.remove("news");
  map.remove("another-key");
  anotherMap.remove("another-key");

  instance.shutdown();

  assertWaitUntil(() -> mgr.getNodes().size() == size - 1);
  vertx1.get().close(ar -> vertx1.set(null));

  assertWaitUntil(() -> vertx1.get() == null);
}
 
Example #16
Source File: HazelcastClusteredAsynchronousLockTest.java    From vertx-hazelcast with Apache License 2.0 4 votes vote down vote up
@Override
protected ClusterManager getClusterManager() {
  return new HazelcastClusterManager();
}
 
Example #17
Source File: HazelcastClusteredSharedCounterTest.java    From vertx-hazelcast with Apache License 2.0 4 votes vote down vote up
@Override
protected ClusterManager getClusterManager() {
  return new HazelcastClusterManager();
}
 
Example #18
Source File: HazelcastClusteredAsyncMapTest.java    From vertx-hazelcast with Apache License 2.0 4 votes vote down vote up
@Override
protected ClusterManager getClusterManager() {
  return new HazelcastClusterManager();
}
 
Example #19
Source File: HazelcastClusteredEventbusTest.java    From vertx-hazelcast with Apache License 2.0 4 votes vote down vote up
@Override
protected ClusterManager getClusterManager() {
  return new HazelcastClusterManager();
}
 
Example #20
Source File: HazelcastNodeInfoTest.java    From vertx-hazelcast with Apache License 2.0 4 votes vote down vote up
@Override
protected ClusterManager getClusterManager() {
  return new HazelcastClusterManager();
}
 
Example #21
Source File: HazelcastFaultToleranceTest.java    From vertx-hazelcast with Apache License 2.0 4 votes vote down vote up
@Override
protected ClusterManager getClusterManager() {
  return new HazelcastClusterManager();
}
 
Example #22
Source File: HazelcastComplexHATest.java    From vertx-hazelcast with Apache License 2.0 4 votes vote down vote up
@Override
protected ClusterManager getClusterManager() {
  return new HazelcastClusterManager();
}
 
Example #23
Source File: ProgrammaticHazelcastClusterManagerTest.java    From vertx-hazelcast with Apache License 2.0 4 votes vote down vote up
@Test
public void testEventBusWhenUsingACustomHazelcastInstance() throws Exception {
  HazelcastInstance instance1 = Hazelcast.newHazelcastInstance(createConfig());
  HazelcastInstance instance2 = Hazelcast.newHazelcastInstance(createConfig());

  HazelcastClusterManager mgr1 = new HazelcastClusterManager(instance1);
  HazelcastClusterManager mgr2 = new HazelcastClusterManager(instance2);
  VertxOptions options1 = new VertxOptions().setClusterManager(mgr1);
  options1.getEventBusOptions().setHost("127.0.0.1");
  VertxOptions options2 = new VertxOptions().setClusterManager(mgr2);
  options2.getEventBusOptions().setHost("127.0.0.1");

  AtomicReference<Vertx> vertx1 = new AtomicReference<>();
  AtomicReference<Vertx> vertx2 = new AtomicReference<>();

  Vertx.clusteredVertx(options1, res -> {
    assertTrue(res.succeeded());
    assertNotNull(mgr1.getHazelcastInstance());
    res.result().eventBus().consumer("news", message -> {
      assertNotNull(message);
      assertTrue(message.body().equals("hello"));
      testComplete();
    });
    vertx1.set(res.result());
  });

  assertWaitUntil(() -> vertx1.get() != null);

  Vertx.clusteredVertx(options2, res -> {
    assertTrue(res.succeeded());
    assertNotNull(mgr2.getHazelcastInstance());
    vertx2.set(res.result());
    res.result().eventBus().send("news", "hello");
  });

  await();

  vertx1.get().close(ar -> vertx1.set(null));
  vertx2.get().close(ar -> vertx2.set(null));

  assertTrue(instance1.getLifecycleService().isRunning());
  assertTrue(instance2.getLifecycleService().isRunning());

  assertWaitUntil(() -> vertx1.get() == null && vertx2.get() == null);

  instance1.shutdown();
  instance2.shutdown();

}
 
Example #24
Source File: ProgrammaticHazelcastClusterManagerTest.java    From vertx-hazelcast with Apache License 2.0 4 votes vote down vote up
@Test
public void testCustomHazelcastInstance() throws Exception {
  HazelcastInstance instance = Hazelcast.newHazelcastInstance(createConfig());
  HazelcastClusterManager mgr = new HazelcastClusterManager(instance);
  testProgrammatic(mgr, instance.getConfig());
}
 
Example #25
Source File: ProgrammaticHazelcastClusterManagerTest.java    From vertx-hazelcast with Apache License 2.0 4 votes vote down vote up
@Test
public void testProgrammaticSetWithConstructor() throws Exception {
  Config config = createConfig();
  HazelcastClusterManager mgr = new HazelcastClusterManager(config);
  testProgrammatic(mgr, config);
}
 
Example #26
Source File: HazelcastHATest.java    From vertx-hazelcast with Apache License 2.0 4 votes vote down vote up
@Override
protected ClusterManager getClusterManager() {
  return new HazelcastClusterManager();
}
 
Example #27
Source File: HazelcastComplexHATest.java    From vertx-hazelcast with Apache License 2.0 4 votes vote down vote up
@Override
protected ClusterManager getClusterManager() {
  return new HazelcastClusterManager(ConfigUtil.loadConfig().setLiteMember(true));
}
 
Example #28
Source File: HazelcastClusteredAsyncMapTest.java    From vertx-hazelcast with Apache License 2.0 4 votes vote down vote up
@Override
protected ClusterManager getClusterManager() {
  return new HazelcastClusterManager(ConfigUtil.loadConfig().setLiteMember(true));
}
 
Example #29
Source File: HazelcastClusteredSharedCounterTest.java    From vertx-hazelcast with Apache License 2.0 4 votes vote down vote up
@Override
protected ClusterManager getClusterManager() {
  return new HazelcastClusterManager(ConfigUtil.loadConfig().setLiteMember(true));
}
 
Example #30
Source File: HazelcastClusteredAsynchronousLockTest.java    From vertx-hazelcast with Apache License 2.0 4 votes vote down vote up
@Override
protected ClusterManager getClusterManager() {
  return new HazelcastClusterManager(ConfigUtil.loadConfig().setLiteMember(true));
}