コード例 #1
0
  @Test
  public void testSubmitToKeyOwnerCallable() throws Exception {
    final int k = simpleTestNodeCount;
    TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(k);
    final HazelcastInstance[] instances = factory.newInstances(new Config());
    final AtomicInteger count = new AtomicInteger(0);
    final CountDownLatch latch = new CountDownLatch(k / 2);
    final ExecutionCallback callback =
        new ExecutionCallback() {
          public void onResponse(Object response) {
            if ((Boolean) response) count.incrementAndGet();
            latch.countDown();
          }

          public void onFailure(Throwable t) {}
        };
    for (int i = 0; i < k; i++) {
      final HazelcastInstance instance = instances[i];
      final IExecutorService service = instance.getExecutorService("testSubmitToKeyOwnerCallable");
      final String script = "hazelcast.getCluster().getLocalMember().equals(member)";
      final HashMap map = new HashMap();
      final Member localMember = instance.getCluster().getLocalMember();
      map.put("member", localMember);
      int key = 0;
      while (!localMember.equals(instance.getPartitionService().getPartition(++key).getOwner())) ;
      if (i % 2 == 0) {
        final Future f = service.submitToKeyOwner(new ScriptCallable(script, map), key);
        assertTrue((Boolean) f.get(5, TimeUnit.SECONDS));
      } else {
        service.submitToKeyOwner(new ScriptCallable(script, map), key, callback);
      }
    }
    assertTrue(latch.await(30, TimeUnit.SECONDS));
    assertEquals(k / 2, count.get());
  }
コード例 #2
0
  private void testCancel_submitToKeyOwner(boolean smartRouting)
      throws ExecutionException, InterruptedException {
    HazelcastInstance client = createClient(smartRouting);

    IExecutorService executorService = client.getExecutorService(randomString());
    Future<Boolean> future =
        executorService.submitToKeyOwner(new CancellationAwareTask(SLEEP_TIME), randomString());
    boolean cancelled = future.cancel(true);
    assertTrue(cancelled);
    future.get();
  }
コード例 #3
0
  @Test
  public void submitCallableToKeyOwner() throws Exception {
    IExecutorService service = client.getExecutorService(randomString());

    String msg = randomString();
    Callable<String> callable = new AppendCallable(msg);

    Future<String> result = service.submitToKeyOwner(callable, "key");

    assertEquals(msg + AppendCallable.APPENDAGE, result.get());
  }
コード例 #4
0
  @Test
  public void submitRunnableToKeyOwner() throws Exception {
    IExecutorService service = client.getExecutorService(randomString());

    String mapName = randomString();
    Runnable runnable = new MapPutRunnable(mapName);
    final CountDownLatch responseLatch = new CountDownLatch(1);

    service.submitToKeyOwner(
        runnable,
        "key",
        new ExecutionCallback() {
          public void onResponse(Object response) {
            responseLatch.countDown();
          }

          public void onFailure(Throwable t) {}
        });
    IMap map = client.getMap(mapName);

    assertOpenEventually("responseLatch", responseLatch);
    assertEquals(1, map.size());
  }
コード例 #5
0
  @Test
  public void testSubmitToKeyOwnerRunnable() throws InterruptedException {
    final int k = simpleTestNodeCount;
    TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(k);
    final HazelcastInstance[] instances = factory.newInstances(new Config());
    final AtomicInteger count = new AtomicInteger(0);
    final CountDownLatch latch = new CountDownLatch(k);
    final ExecutionCallback callback =
        new ExecutionCallback() {
          public void onResponse(Object response) {
            if (response == null) count.incrementAndGet();
            latch.countDown();
          }

          public void onFailure(Throwable t) {}
        };
    for (int i = 0; i < k; i++) {
      final HazelcastInstance instance = instances[i];
      final IExecutorService service = instance.getExecutorService("testSubmitToKeyOwnerRunnable");
      final String script =
          "if(!hazelcast.getCluster().getLocalMember().equals(member)) "
              + "hazelcast.getAtomicLong('testSubmitToKeyOwnerRunnable').incrementAndGet();";
      final HashMap map = new HashMap();
      map.put("member", instance.getCluster().getLocalMember());
      int key = 0;
      while (!instance
          .getCluster()
          .getLocalMember()
          .equals(instance.getPartitionService().getPartition(++key).getOwner())) {
        Thread.sleep(1);
      }
      service.submitToKeyOwner(new ScriptRunnable(script, map), key, callback);
    }
    latch.await(10, TimeUnit.SECONDS);
    assertEquals(0, instances[0].getAtomicLong("testSubmitToKeyOwnerRunnable").get());
    assertEquals(k, count.get());
  }
コード例 #6
0
  @Test
  public void submitCallableToKeyOwner_withExecutionCallback() throws Exception {
    IExecutorService service = client.getExecutorService(randomString());

    String msg = randomString();
    Callable<String> callable = new AppendCallable(msg);
    final CountDownLatch responseLatch = new CountDownLatch(1);
    final AtomicReference<String> result = new AtomicReference<String>();

    service.submitToKeyOwner(
        callable,
        "key",
        new ExecutionCallback<String>() {
          public void onResponse(String response) {
            result.set(response);
            responseLatch.countDown();
          }

          public void onFailure(Throwable t) {}
        });

    assertOpenEventually("responseLatch", responseLatch);
    assertEquals(msg + AppendCallable.APPENDAGE, result.get());
  }