org.infinispan.commons.util.concurrent.NotifyingFutureImpl Maven / Gradle / Ivy
package org.infinispan.commons.util.concurrent;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
/**
* Constructs an instance of a {@link org.infinispan.commons.util.concurrent.NotifyingFuture}.
*
* Typical usage:
*
*
* Object retval = ... // do some work here
* NotifyingFuture nf = new NotifyingFutureImpl();
* rpcManager.broadcastRpcCommandInFuture(nf, command);
* return nf;
*
*
* @author Manik Surtani
* @since 4.0
*/
public class NotifyingFutureImpl extends BaseNotifyingFuture implements NotifyingNotifiableFuture{
private T actualReturnValue;
private Throwable exceptionThrown;
private volatile Future future;
private final CountDownLatch latch = new CountDownLatch(1);
@Override
public void setFuture(Future future) {
this.future = future;
latch.countDown();
}
public Future getFuture() throws InterruptedException {
latch.await();
return future;
}
@Override
public boolean cancel(boolean mayInterruptIfRunning) {
try {
return getFuture().cancel(mayInterruptIfRunning);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
return false;
}
}
@Override
public boolean isCancelled() {
try {
return getFuture().isCancelled();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
return false;
}
}
@Override
public boolean isDone() {
if (callCompleted) {
return true;
}
try {
return getFuture().isDone();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
return false;
}
}
@Override
public T get() throws InterruptedException, ExecutionException {
if (!callCompleted) {
getFuture().get();
}
if (exceptionThrown != null) {
throw new ExecutionException(exceptionThrown);
}
return actualReturnValue;
}
@Override
public T get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, java.util.concurrent.TimeoutException {
if (!callCompleted) {
getFuture().get(timeout, unit);
}
if (exceptionThrown != null) {
throw new ExecutionException(exceptionThrown);
}
return actualReturnValue;
}
@Override
public void notifyDone(T result) {
actualReturnValue = result;
fireListeners();
}
@Override
public void notifyException(Throwable exception) {
exceptionThrown = exception;
fireListeners();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy