@Test
  public void submitRunnableToAllMembers_withMultiExecutionCallback() throws Exception {
    IExecutorService service = client.getExecutorService(randomString());

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

    service.submitToAllMembers(
        runnable,
        new MultiExecutionCallback() {
          public void onResponse(Member member, Object value) {
            responseLatch.countDown();
          }

          public void onComplete(Map<Member, Object> values) {
            completeLatch.countDown();
          }
        });
    IMap map = client.getMap(mapName);

    assertOpenEventually("responseLatch", responseLatch);
    assertOpenEventually("completeLatch", completeLatch);
    assertEquals(CLUSTER_SIZE, map.size());
  }
  @Test
  public void submitCallable_withExecutionCallback() {
    IExecutorService service = client.getExecutorService(randomString());

    final CountDownLatch responseLatch = new CountDownLatch(1);
    String msg = randomString();
    Callable runnable = new AppendCallable(msg);
    MemberSelector selector = new SelectAllMembers();
    final AtomicReference<Object> result = new AtomicReference<Object>();

    service.submit(
        runnable,
        selector,
        new ExecutionCallback() {
          public void onResponse(Object response) {
            result.set(response);
            responseLatch.countDown();
          }

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

    assertOpenEventually("responseLatch", responseLatch);
    assertEquals(msg + AppendCallable.APPENDAGE, result.get());
  }
  @Test
  public void submitCallableToMembers_withExecutionCallback() {
    IExecutorService service = client.getExecutorService(randomString());

    final CountDownLatch responseLatch = new CountDownLatch(CLUSTER_SIZE);
    final CountDownLatch completeLatch = new CountDownLatch(1);
    final String msg = randomString();
    Callable callable = new AppendCallable(msg);
    MemberSelector selector = new SelectAllMembers();

    service.submitToMembers(
        callable,
        selector,
        new MultiExecutionCallback() {
          public void onResponse(Member member, Object value) {
            if (value.equals(msg + AppendCallable.APPENDAGE)) {
              responseLatch.countDown();
            }
          }

          public void onComplete(Map<Member, Object> values) {
            completeLatch.countDown();
          }
        });

    assertOpenEventually("responseLatch", responseLatch);
    assertOpenEventually("completeLatch", completeLatch);
  }
  @Test
  public void submitCallableToMember_withMultiExecutionCallback() throws Exception {
    IExecutorService service = client.getExecutorService(randomString());

    final CountDownLatch responseLatch = new CountDownLatch(CLUSTER_SIZE);
    final CountDownLatch completeLatch = new CountDownLatch(CLUSTER_SIZE);
    final String msg = randomString();
    Callable<String> callable = new AppendCallable(msg);
    Collection<Member> collection = server.getCluster().getMembers();

    service.submitToMembers(
        callable,
        collection,
        new MultiExecutionCallback() {
          public void onResponse(Member member, Object value) {
            if (value.equals(msg + AppendCallable.APPENDAGE)) {
              responseLatch.countDown();
            }
          }

          public void onComplete(Map<Member, Object> values) {
            for (Member member : values.keySet()) {
              Object value = values.get(member);
              if (value.equals(msg + AppendCallable.APPENDAGE)) {
                completeLatch.countDown();
              }
            }
          }
        });

    assertOpenEventually("responseLatch", responseLatch);
    assertOpenEventually("completeLatch", completeLatch);
  }
  @Test
  public void testSubmitCallableToMember_withExecutionCallback() throws Exception {
    IExecutorService service = client.getExecutorService(randomString());

    Callable getUuidCallable = new GetMemberUuidTask();
    Member member = server.getCluster().getLocalMember();
    final CountDownLatch responseLatch = new CountDownLatch(1);
    final AtomicReference<Object> result = new AtomicReference<Object>();

    service.submitToMember(
        getUuidCallable,
        member,
        new ExecutionCallback() {
          @Override
          public void onResponse(Object response) {
            result.set(response);
            responseLatch.countDown();
          }

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

    assertOpenEventually("responseLatch", responseLatch);
    assertEquals(member.getUuid(), result.get());
  }
예제 #6
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());
  }
  @Test
  public void submitCallableWithNullResultToAllMembers_withMultiExecutionCallback()
      throws Exception {
    IExecutorService service = client.getExecutorService(randomString());

    final CountDownLatch responseLatch = new CountDownLatch(CLUSTER_SIZE);
    final CountDownLatch completeLatch = new CountDownLatch(CLUSTER_SIZE);
    Callable callable = new NullCallable();

    service.submitToAllMembers(
        callable,
        new MultiExecutionCallback() {
          public void onResponse(Member member, Object value) {
            if (value == null) {
              responseLatch.countDown();
            }
          }

          public void onComplete(Map<Member, Object> values) {
            for (Member member : values.keySet()) {
              Object value = values.get(member);
              if (value == null) {
                completeLatch.countDown();
              }
            }
          }
        });

    assertOpenEventually("responseLatch", responseLatch);
    assertOpenEventually("completeLatch", completeLatch);
  }
예제 #8
0
  @Test
  public void testSubmitToMemberRunnable() 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("testSubmitToMemberRunnable");
      final String script =
          "if(!hazelcast.getCluster().getLocalMember().equals(member)) "
              + "hazelcast.getAtomicLong('testSubmitToMemberRunnable').incrementAndGet();";
      final HashMap map = new HashMap();
      map.put("member", instance.getCluster().getLocalMember());
      service.submitToMember(
          new ScriptRunnable(script, map), instance.getCluster().getLocalMember(), callback);
    }
    latch.await(10, TimeUnit.SECONDS);
    assertEquals(0, instances[0].getAtomicLong("testSubmitToMemberRunnable").get());
    assertEquals(k, count.get());
  }
  @Test(expected = NullPointerException.class)
  public void testSubmitCallableNullTask() throws Exception {
    IExecutorService service = client.getExecutorService(randomString());
    Callable<String> callable = null;

    service.submit(callable);
  }
  @Test
  public void submitCallablePartitionAware_WithExecutionCallback() throws Exception {
    IExecutorService service = client.getExecutorService(randomString());

    String mapName = randomString();
    IMap map = client.getMap(mapName);
    String key = HazelcastTestSupport.generateKeyOwnedBy(server);
    Member member = server.getCluster().getLocalMember();

    Callable<String> runnable = new MapPutPartitionAwareCallable<String, String>(mapName, key);

    final AtomicReference<String> result = new AtomicReference<String>();
    final CountDownLatch responseLatch = new CountDownLatch(1);

    service.submit(
        runnable,
        new ExecutionCallback<String>() {
          public void onResponse(String response) {
            result.set(response);
            responseLatch.countDown();
          }

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

    assertOpenEventually("responseLatch", responseLatch);
    assertEquals(member.getUuid(), result.get());
    assertTrue(map.containsKey(member.getUuid()));
  }
  @Test
  public void submitRunnablePartitionAware_withExecutionCallback() throws Exception {
    IExecutorService service = client.getExecutorService(randomString());

    String mapName = randomString();
    String key = HazelcastTestSupport.generateKeyOwnedBy(server);
    Member member = server.getCluster().getLocalMember();
    Runnable runnable = new MapPutPartitionAwareRunnable<String>(mapName, key);
    final CountDownLatch responseLatch = new CountDownLatch(1);

    service.submit(
        runnable,
        new ExecutionCallback() {
          @Override
          public void onResponse(Object response) {
            responseLatch.countDown();
          }

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

    assertOpenEventually("responseLatch", responseLatch);
    assertTrue(map.containsKey(member.getUuid()));
  }
  public static Map<Member, Long> getOperationCount(HazelcastInstance hz) {
    IExecutorService executorService = hz.getExecutorService("operationCountExecutor");

    Map<Member, Future<Long>> futures = new HashMap<Member, Future<Long>>();
    for (Member member : hz.getCluster().getMembers()) {
      Future<Long> future = executorService.submitToMember(new GetOperationCount(), member);
      futures.put(member, future);
    }

    Map<Member, Long> result = new HashMap<Member, Long>();
    for (Map.Entry<Member, Future<Long>> entry : futures.entrySet()) {
      try {
        Member member = entry.getKey();
        Long value = entry.getValue().get();
        if (value == null) {
          value = 0L;
        }
        result.put(member, value);
      } catch (InterruptedException e) {
        throw new TestException(e);
      } catch (ExecutionException e) {
        throw new TestException(e);
      }
    }

    return result;
  }
  private void testCancel_submitRandom(boolean smartRouting)
      throws ExecutionException, InterruptedException {
    HazelcastInstance client = createClient(smartRouting);

    IExecutorService executorService = client.getExecutorService(randomString());
    Future<Boolean> future = executorService.submit(new CancellationAwareTask(SLEEP_TIME));
    boolean cancelled = future.cancel(true);
    assertTrue(cancelled);
    future.get();
  }
 public static void main(String[] args) throws InterruptedException, ExecutionException {
   HazelcastInstance hazelCastE = Hazelcast.newHazelcastInstance(null);
   List<Future<Integer>> results = new ArrayList<Future<Integer>>();
   for (int i = 0; i < 10; i++) {
     results.add(hazelCastE.getExecutorService().submit(new RunnableImplementation(i)));
   }
   for (Future<Integer> result : results) {
     System.out.println(result.get());
   }
 }
  @Test
  public void testSubmitCallable() throws Exception {
    IExecutorService service = client.getExecutorService(randomString());

    String msg = randomString();
    Callable callable = new AppendCallable(msg);
    Future result = service.submit(callable);

    assertEquals(msg + AppendCallable.APPENDAGE, result.get());
  }
  @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());
  }
예제 #17
0
  @Test
  public void hazelcastInstanceAwareAndLocal() throws Exception {
    final Config config = new Config();
    config.addExecutorConfig(new ExecutorConfig("test", 1));
    final HazelcastInstance instance = createHazelcastInstance(config);
    IExecutorService executor = instance.getExecutorService("test");

    HazelcastInstanceAwareRunnable task = new HazelcastInstanceAwareRunnable();
    executor.submit(task).get();
    assertTrue("The setHazelcastInstance should have been called", task.initializeCalled);
  }
  @Test
  public void testSubmitCallableToMember() throws Exception {
    IExecutorService service = client.getExecutorService(randomString());

    Callable<String> getUuidCallable = new GetMemberUuidTask();
    Member member = server.getCluster().getLocalMember();

    Future<String> result = service.submitToMember(getUuidCallable, member);

    assertEquals(member.getUuid(), result.get());
  }
  @Test
  public void testSubmitCallable_withMemberSelector() throws Exception {
    IExecutorService service = client.getExecutorService(randomString());

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

    Future<String> f = service.submit(callable, selectAll);

    assertEquals(msg + AppendCallable.APPENDAGE, f.get());
  }
  @Test
  public void submitCallableToAllMembers() throws Exception {
    IExecutorService service = client.getExecutorService(randomString());

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

    Map<Member, Future<String>> map = service.submitToAllMembers(callable);
    for (Member member : map.keySet()) {
      Future<String> result = map.get(member);
      assertEquals(msg + AppendCallable.APPENDAGE, result.get());
    }
  }
  @Test
  public void submitRunnable() {
    IExecutorService service = client.getExecutorService(randomString());

    String mapName = randomString();
    Runnable runnable = new MapPutRunnable(mapName);

    service.submit(runnable);

    IMap map = client.getMap(mapName);

    assertSizeEventually(1, map);
  }
  @Test
  public void testSubmitRunnable_WithResult() throws ExecutionException, InterruptedException {
    IExecutorService service = client.getExecutorService(randomString());

    String mapName = randomString();
    Object givenResult = "givenResult";
    Future future = service.submit(new MapPutRunnable(mapName), givenResult);
    Object result = future.get();

    IMap map = client.getMap(mapName);

    assertEquals(givenResult, result);
    assertEquals(1, map.size());
  }
예제 #23
0
  @Test(expected = ExecutionException.class, timeout = 120000)
  public void testIssue2509() throws Exception {

    TestHazelcastInstanceFactory nodeFactory = createHazelcastInstanceFactory(2);

    HazelcastInstance h1 = nodeFactory.newHazelcastInstance();
    HazelcastInstance h2 = nodeFactory.newHazelcastInstance();

    UnDeserializable unDeserializable = new UnDeserializable(1);
    IExecutorService executorService = h1.getExecutorService("default");
    Issue2509Runnable task = new Issue2509Runnable(unDeserializable);
    Future<?> future = executorService.submitToMember(task, h2.getCluster().getLocalMember());
    future.get();
  }
  @Test
  public void submitCallablePartitionAware() throws Exception {
    IExecutorService service = client.getExecutorService(randomString());

    String mapName = randomString();
    IMap map = client.getMap(mapName);
    String key = HazelcastTestSupport.generateKeyOwnedBy(server);
    Member member = server.getCluster().getLocalMember();

    Callable<String> callable = new MapPutPartitionAwareCallable<String, String>(mapName, key);
    Future<String> result = service.submit(callable);

    assertEquals(member.getUuid(), result.get());
    assertTrue(map.containsKey(member.getUuid()));
  }
  @Test
  public void testSubmitCallableToMembers_withMemberSelector() throws Exception {
    IExecutorService service = client.getExecutorService(randomString());

    Callable<String> getUuidCallable = new GetMemberUuidTask();
    MemberSelector selectAll = new SelectAllMembers();

    Map<Member, Future<String>> map = service.submitToMembers(getUuidCallable, selectAll);
    for (Member member : map.keySet()) {
      Future<String> result = map.get(member);
      String uuid = result.get();

      assertEquals(member.getUuid(), uuid);
    }
  }
  @Test
  public void testExecuteOnAllMembers() {
    final IExecutorService service = client.getExecutorService(randomString());
    final String mapName = randomString();

    service.executeOnAllMembers(new MapPutRunnable(mapName));

    final IMap map = client.getMap(mapName);
    assertTrueEventually(
        new AssertTask() {
          public void run() throws Exception {
            assertEquals(CLUSTER_SIZE, map.size());
          }
        });
  }
  @Test
  public void testExecuteOnMembers_withEmptyCollection() {
    final IExecutorService service = client.getExecutorService(randomString());
    final String mapName = randomString();
    final Collection collection = new ArrayList();

    service.executeOnMembers(new MapPutRunnable(mapName), collection);

    final IMap map = client.getMap(mapName);
    assertTrueEventually(
        new AssertTask() {
          public void run() throws Exception {
            assertTrue(map.isEmpty());
          }
        });
  }
  @Test
  public void testExecuteOnMember() {
    final IExecutorService service = client.getExecutorService(randomString());
    final String mapName = randomString();

    final Member member = instance1.getCluster().getLocalMember();
    final String targetUuid = member.getUuid();

    service.executeOnMember(new MapPutRunnable(mapName), member);

    final IMap map = client.getMap(mapName);
    assertTrueEventually(
        new AssertTask() {
          public void run() throws Exception {
            assertTrue(map.containsKey(targetUuid));
          }
        });
  }
  @Test
  public void testExecuteOnMembers() {
    final IExecutorService service = client.getExecutorService(randomString());
    final String mapName = randomString();
    final Collection collection = new ArrayList();
    final Member member1 = instance1.getCluster().getLocalMember();
    final Member member3 = instance3.getCluster().getLocalMember();
    collection.add(member1);
    collection.add(member3);

    service.executeOnMembers(new MapPutRunnable(mapName), collection);

    final IMap map = client.getMap(mapName);
    assertTrueEventually(
        new AssertTask() {
          public void run() throws Exception {
            assertTrue(map.containsKey(member1.getUuid()));
            assertTrue(map.containsKey(member3.getUuid()));
          }
        });
  }
  @Test
  public void submitRunnablePartitionAware_withResult() throws Exception {
    IExecutorService service = client.getExecutorService(randomString());

    String expectedResult = "result";
    String mapName = randomString();
    String key = HazelcastTestSupport.generateKeyOwnedBy(server);
    final Member member = server.getCluster().getLocalMember();

    Runnable runnable = new MapPutPartitionAwareRunnable<String>(mapName, key);

    Future result = service.submit(runnable, expectedResult);
    final IMap map = client.getMap(mapName);

    assertEquals(expectedResult, result.get());
    assertTrueEventually(
        new AssertTask() {
          public void run() throws Exception {
            assertTrue(map.containsKey(member.getUuid()));
          }
        });
  }