Search in sources :

Example 1 with CompletedFuture

use of com.hazelcast.util.executor.CompletedFuture in project hazelcast by hazelcast.

the class ExecutorServiceProxy method submitToPartitionOwner.

private <T> Future<T> submitToPartitionOwner(Callable<T> task, int partitionId, boolean preventSync) {
    checkNotNull(task, "task can't be null");
    checkNotShutdown();
    NodeEngine nodeEngine = getNodeEngine();
    Data taskData = nodeEngine.toData(task);
    String uuid = newUnsecureUuidString();
    boolean sync = !preventSync && checkSync();
    Operation op = new CallableTaskOperation(name, uuid, taskData).setPartitionId(partitionId);
    InternalCompletableFuture future = invokeOnPartition(op);
    if (sync) {
        Object response;
        try {
            response = future.get();
        } catch (Exception e) {
            response = e;
        }
        return new CompletedFuture<T>(nodeEngine.getSerializationService(), response, getAsyncExecutor());
    }
    return new CancellableDelegatingFuture<T>(future, nodeEngine, uuid, partitionId);
}
Also used : NodeEngine(com.hazelcast.spi.NodeEngine) InternalCompletableFuture(com.hazelcast.spi.InternalCompletableFuture) Data(com.hazelcast.nio.serialization.Data) AbstractDistributedObject(com.hazelcast.spi.AbstractDistributedObject) UuidUtil.newUnsecureUuidString(com.hazelcast.util.UuidUtil.newUnsecureUuidString) ShutdownOperation(com.hazelcast.executor.impl.operations.ShutdownOperation) CallableTaskOperation(com.hazelcast.executor.impl.operations.CallableTaskOperation) Operation(com.hazelcast.spi.Operation) MemberCallableTaskOperation(com.hazelcast.executor.impl.operations.MemberCallableTaskOperation) HazelcastInstanceNotActiveException(com.hazelcast.core.HazelcastInstanceNotActiveException) TimeoutException(java.util.concurrent.TimeoutException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) ExecutionException(java.util.concurrent.ExecutionException) CallableTaskOperation(com.hazelcast.executor.impl.operations.CallableTaskOperation) MemberCallableTaskOperation(com.hazelcast.executor.impl.operations.MemberCallableTaskOperation) CompletedFuture(com.hazelcast.util.executor.CompletedFuture)

Example 2 with CompletedFuture

use of com.hazelcast.util.executor.CompletedFuture in project hazelcast by hazelcast.

the class ClientExecutorServiceProxy method invokeAll.

@Override
public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks) throws InterruptedException {
    final List<Future<T>> futures = new ArrayList<Future<T>>(tasks.size());
    final List<Future<T>> result = new ArrayList<Future<T>>(tasks.size());
    for (Callable<T> task : tasks) {
        futures.add(submitToRandomInternal(task, null, true));
    }
    Executor userExecutor = getContext().getExecutionService().getUserExecutor();
    for (Future<T> future : futures) {
        Object value = retrieveResult(future);
        result.add(new CompletedFuture<T>(getContext().getSerializationService(), value, userExecutor));
    }
    return result;
}
Also used : Executor(java.util.concurrent.Executor) ArrayList(java.util.ArrayList) ClientAddressCancellableDelegatingFuture(com.hazelcast.client.util.ClientAddressCancellableDelegatingFuture) ClientPartitionCancellableDelegatingFuture(com.hazelcast.client.util.ClientPartitionCancellableDelegatingFuture) ClientDelegatingFuture(com.hazelcast.client.util.ClientDelegatingFuture) Future(java.util.concurrent.Future) CompletedFuture(com.hazelcast.util.executor.CompletedFuture) ClientInvocationFuture(com.hazelcast.client.spi.impl.ClientInvocationFuture)

Example 3 with CompletedFuture

use of com.hazelcast.util.executor.CompletedFuture in project hazelcast by hazelcast.

the class ClientExecutorServiceProxy method checkSync.

private <T> Future<T> checkSync(ClientInvocationFuture f, String uuid, int partitionId, boolean preventSync, T defaultValue) {
    boolean sync = isSyncComputation(preventSync);
    if (sync) {
        Object response = retrieveResultFromMessage(f);
        Executor userExecutor = getContext().getExecutionService().getUserExecutor();
        return new CompletedFuture<T>(getContext().getSerializationService(), response, userExecutor);
    } else {
        return new ClientPartitionCancellableDelegatingFuture<T>(f, getContext(), uuid, partitionId, defaultValue, SUBMIT_TO_PARTITION_DECODER);
    }
}
Also used : Executor(java.util.concurrent.Executor) CompletedFuture(com.hazelcast.util.executor.CompletedFuture) ClientPartitionCancellableDelegatingFuture(com.hazelcast.client.util.ClientPartitionCancellableDelegatingFuture)

Example 4 with CompletedFuture

use of com.hazelcast.util.executor.CompletedFuture in project hazelcast by hazelcast.

the class ExecutorServiceProxy method invokeAll.

@Override
public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks) throws InterruptedException {
    List<Future<T>> futures = new ArrayList<Future<T>>(tasks.size());
    List<Future<T>> result = new ArrayList<Future<T>>(tasks.size());
    for (Callable<T> task : tasks) {
        futures.add(submit(task));
    }
    for (Future<T> future : futures) {
        Object value;
        try {
            value = future.get();
        } catch (ExecutionException e) {
            value = e;
        }
        result.add(new CompletedFuture<T>(getNodeEngine().getSerializationService(), value, getAsyncExecutor()));
    }
    return result;
}
Also used : ArrayList(java.util.ArrayList) InternalCompletableFuture(com.hazelcast.spi.InternalCompletableFuture) Future(java.util.concurrent.Future) CompletedFuture(com.hazelcast.util.executor.CompletedFuture) AbstractDistributedObject(com.hazelcast.spi.AbstractDistributedObject) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) ExecutionException(java.util.concurrent.ExecutionException)

Example 5 with CompletedFuture

use of com.hazelcast.util.executor.CompletedFuture in project hazelcast by hazelcast.

the class FutureUtilTest method testGetAllDone_whenSomeFuturesAreCompleted.

@Test
public void testGetAllDone_whenSomeFuturesAreCompleted() {
    Future completedFuture = new CompletedFuture(null, null, null);
    Collection<Future> futures = asList(new UncancellableFuture(), completedFuture, new UncancellableFuture());
    assertEquals(1, FutureUtil.getAllDone(futures).size());
    assertEquals(completedFuture, FutureUtil.getAllDone(futures).get(0));
}
Also used : Future(java.util.concurrent.Future) CompletedFuture(com.hazelcast.util.executor.CompletedFuture) AbstractCompletableFuture(com.hazelcast.spi.impl.AbstractCompletableFuture) CompletedFuture(com.hazelcast.util.executor.CompletedFuture) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Aggregations

CompletedFuture (com.hazelcast.util.executor.CompletedFuture)10 InternalCompletableFuture (com.hazelcast.spi.InternalCompletableFuture)5 ExecutionException (java.util.concurrent.ExecutionException)5 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)5 Data (com.hazelcast.nio.serialization.Data)4 AbstractDistributedObject (com.hazelcast.spi.AbstractDistributedObject)4 Future (java.util.concurrent.Future)4 TimeoutException (java.util.concurrent.TimeoutException)4 HazelcastInstanceNotActiveException (com.hazelcast.core.HazelcastInstanceNotActiveException)3 MemberCallableTaskOperation (com.hazelcast.executor.impl.operations.MemberCallableTaskOperation)3 NodeEngine (com.hazelcast.spi.NodeEngine)3 Operation (com.hazelcast.spi.Operation)3 UuidUtil.newUnsecureUuidString (com.hazelcast.util.UuidUtil.newUnsecureUuidString)3 Executor (java.util.concurrent.Executor)3 ClientAddressCancellableDelegatingFuture (com.hazelcast.client.util.ClientAddressCancellableDelegatingFuture)2 ClientPartitionCancellableDelegatingFuture (com.hazelcast.client.util.ClientPartitionCancellableDelegatingFuture)2 CallableTaskOperation (com.hazelcast.executor.impl.operations.CallableTaskOperation)2 ShutdownOperation (com.hazelcast.executor.impl.operations.ShutdownOperation)2 ArrayList (java.util.ArrayList)2 ClientInvocationFuture (com.hazelcast.client.spi.impl.ClientInvocationFuture)1