/**
   * Submits a task to the Hazelcast task cluster.
   *
   * @param task The task
   * @param where A {@link Member}, a string (comma-separated member tags), or null to let Hazelcast
   *     decide
   * @return A future for the task
   */
  private <T> Future<T> task(SerializableApplicationTask<T> task, Object where) {
    IExecutorService executor = getHazelcastExecutorService();

    if (where instanceof String) {
      Member member = getTaskMember(HAZELCAST_MEMBER_TAGS_ATTRIBUTE, (String) where);
      if (member != null) return executor.submitToMember(task, member);
      else return null;
    } else if (where instanceof Member) return executor.submitToMember(task, (Member) where);
    else return executor.submit(task);
  }
Ejemplo n.º 2
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());
  }
  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;
  }
  @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());
  }
 public <V> V sendReceive(String memberId, TaskCall<V> taskCall) throws TaskException {
   IExecutorService es = this.getHazelcast().getExecutorService(NTASK_P2P_COMM_EXECUTOR);
   Future<V> taskExec = es.submitToMember(taskCall, this.getMemberFromId(memberId));
   try {
     return taskExec.get();
   } catch (Exception e) {
     throw new TaskException(
         "Error in cluster message send-receive: " + e.getMessage(), Code.UNKNOWN, e);
   }
 }
  @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());
  }
  private void testCancel_submitToMember(boolean smartRouting, Member member)
      throws ExecutionException, InterruptedException {

    HazelcastInstance client = createClient(smartRouting);

    IExecutorService executorService = client.getExecutorService(randomString());
    Future<Boolean> future =
        executorService.submitToMember(new CancellationAwareTask(SLEEP_TIME), member);
    boolean cancelled = future.cancel(true);
    assertTrue(cancelled);
    future.get();
  }
Ejemplo n.º 8
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();
  }
Ejemplo n.º 9
0
  @Test
  public void testSubmitToMemberCallable()
      throws ExecutionException, InterruptedException, TimeoutException {
    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("testSubmitToMemberCallable");
      final String script = "hazelcast.getCluster().getLocalMember().equals(member); ";
      final HashMap map = new HashMap();
      map.put("member", instance.getCluster().getLocalMember());
      if (i % 2 == 0) {
        final Future f =
            service.submitToMember(
                new ScriptCallable(script, map), instance.getCluster().getLocalMember());
        assertTrue((Boolean) f.get(5, TimeUnit.SECONDS));
      } else {
        service.submitToMember(
            new ScriptCallable(script, map), instance.getCluster().getLocalMember(), callback);
      }
    }
    assertTrue(latch.await(30, TimeUnit.SECONDS));
    assertEquals(k / 2, count.get());
  }
  @Test
  public void submitRunnableToMember_withExecutionCallback() {
    IExecutorService service = client.getExecutorService(randomString());

    String mapName = randomString();
    Runnable runnable = new MapPutRunnable(mapName);
    Member member = server.getCluster().getLocalMember();
    final CountDownLatch responseLatch = new CountDownLatch(1);

    service.submitToMember(
        runnable,
        member,
        new ExecutionCallback() {
          public void onResponse(Object response) {
            responseLatch.countDown();
          }

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

    assertOpenEventually("responseLatch", responseLatch);
    assertEquals(1, map.size());
  }