Java Code Examples for com.hazelcast.core.HazelcastInstance#shutdown()

The following examples show how to use com.hazelcast.core.HazelcastInstance#shutdown() . 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: HazelcastNodeIntegrationTest.java    From brooklyn-library with Apache License 2.0 6 votes vote down vote up
@Test(groups = {"Integration"})
public void testHazelcastClient() throws URISyntaxException {
    hazelcastNode = app.createAndManageChild(EntitySpec.create(HazelcastNode.class));
    app.start(ImmutableList.of(testLocation));

    EntityAsserts.assertAttributeEqualsEventually(hazelcastNode, Startable.SERVICE_UP, true);
    HazelcastTestHelper helper = new HazelcastTestHelper(hazelcastNode.getAttribute(Attributes.HOSTNAME), hazelcastNode.getAttribute(HazelcastNode.NODE_PORT));

    HazelcastInstance client = helper.getClient();
    HazelcastInstance client2 = helper.getClient();

    client.getMap(HazelcastTestHelper.GROUP_NAME).put("A", "a");
    client2.getMap(HazelcastTestHelper.GROUP_NAME).put("B", "b");

    final IMap<Object, Object> map = client.getMap(HazelcastTestHelper.GROUP_NAME);
    assertEquals("a", map.get("A"));
    assertEquals("b", map.get("B"));

    client.shutdown();
    client2.shutdown();
}
 
Example 2
Source File: InstanceHelper.java    From spring-data-hazelcast with Apache License 2.0 6 votes vote down vote up
/**
 * <p>
 * Spring will shutdown the test Hazelcast instance, as the {@code @Bean} is defined as a
 * {@link org.springframework.beans.factory.DisposableBean}. Shut down any other server instances started, which may
 * be needed for cluster tests.
 * </P>
 */
@PreDestroy
public void preDestroy() {
    boolean testInstanceWasRunning = false;

    Set<HazelcastInstance> hazelcastInstances = Hazelcast.getAllHazelcastInstances();
    if (hazelcastInstances.size() != 0) {
        for (HazelcastInstance hazelcastInstance : hazelcastInstances) {
            if (TestConstants.CLIENT_INSTANCE_NAME.equals(hazelcastInstance.getName())) {
                testInstanceWasRunning = true;
            }
            LOG.debug("Closing '{}'", hazelcastInstance);
            hazelcastInstance.shutdown();
        }
    }
    ;

    if (testInstanceWasRunning) {
        LOG.error("'{}' was still running", TestConstants.CLIENT_INSTANCE_NAME);
    } else {
        LOG.debug("'{}' already closed by Spring", TestConstants.CLIENT_INSTANCE_NAME);
    }
}
 
Example 3
Source File: ConnectDisconnectTest.java    From hazelcast-simulator with Apache License 2.0 6 votes vote down vote up
@TimeStep
public void timestep() throws Exception {
    Thread thread = new Thread(() -> {
        try {
            HazelcastInstance client = HazelcastClient.newHazelcastClient(clientConfig);
            client.shutdown();
        } catch (Exception e) {
            ExceptionReporter.report(testContext.getTestId(), e);
        }
    });
    thread.start();
    thread.join(timeoutMillis);
    if (thread.isAlive()) {
        throw new RuntimeException("Connect/Disconnect cycle failed to complete in " + timeoutMillis + " millis");
    }
}
 
Example 4
Source File: ConnectDisconnectTest.java    From hazelcast-simulator with Apache License 2.0 6 votes vote down vote up
@TimeStep
public void timestep() throws Exception {
    Thread thread = new Thread(() -> {
        try {
            HazelcastInstance client = HazelcastClient.newHazelcastClient(clientConfig);
            client.shutdown();
        } catch (Exception e) {
            ExceptionReporter.report(testContext.getTestId(), e);
        }
    });
    thread.start();
    thread.join(timeoutMillis);
    if (thread.isAlive()) {
        throw new RuntimeException("Connect/Disconnect cycle failed to complete in " + timeoutMillis + " millis");
    }
}
 
Example 5
Source File: HazelcastITest.java    From java-specialagent with Apache License 2.0 5 votes vote down vote up
public static void main(final String[] args) {
  final HazelcastInstance instance = Hazelcast.newHazelcastInstance(new Config().setProperty("hazelcast.phone.home.enabled", "false"));
  final IMap<String,String> map = instance.getMap("map");
  map.put("key", "value");
  if (!"value".equals(map.get("key")))
    throw new AssertionError("ERROR: wrong value");

  instance.shutdown();

  TestUtil.checkSpan(new ComponentSpanCount("java-hazelcast", 2));
}
 
Example 6
Source File: HazelcastSessionDao.java    From dpCms with Apache License 2.0 5 votes vote down vote up
/**
 * Destroys currently allocated instance.
 */
public void destroy() {
    log.info("Shutting down Hazelcast instance [{}]..", hcInstanceName);
    final HazelcastInstance instance = Hazelcast.getHazelcastInstanceByName(
            hcInstanceName);
    if (instance != null) {
        instance.shutdown();
    }
}
 
Example 7
Source File: HazelcastSessionDao.java    From spring-boot-shiro-orientdb with Apache License 2.0 5 votes vote down vote up
/**
 * Destroys currently allocated instance.
 */
public void destroy() {
    log.info("Shutting down Hazelcast instance [{}]..", hcInstanceName);
    final HazelcastInstance instance = Hazelcast.getHazelcastInstanceByName(
            hcInstanceName);
    if (instance != null) {
        instance.shutdown();
    }
}
 
Example 8
Source File: SubscriptionMain.java    From chuidiang-ejemplos with GNU Lesser General Public License v3.0 5 votes vote down vote up
public static void main(String[] args){
    Config config = new Config();
    GlobalSerializerConfig globalConfig = new GlobalSerializerConfig();
    globalConfig.setOverrideJavaSerialization(true).setImplementation(new DataSerializer());

    config.getSerializationConfig().setGlobalSerializerConfig(globalConfig);

   HazelcastInstance instance = Hazelcast.newHazelcastInstance(config);

   IMap<String, String> aMap = instance.getMap("aMap");

   aMap.addEntryListener(new MyListener(), true);

   
   aMap.put("key", "value");
   aMap.put("key", "Another value");
   aMap.remove("key");
   aMap.put("other key", "other value");
   aMap.clear();


   IMap<String, Data> otherMap = instance.getMap("otherMap");

   otherMap.addEntryListener(new MyListener(), true);
   otherMap.put("key", new Data());
   Data data = otherMap.get("key");
   data.date=new Date();
   data.value=1000;
   otherMap.put("key",data);

   instance.shutdown();
}
 
Example 9
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 10
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 11
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);
}