
org.springframework.util.FutureUtils Maven / Gradle / Ivy
package org.springframework.util;
import java.lang.reflect.Array;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import rx.exceptions.OnErrorThrowable;
/**
* @see org.springframework.scheduling.concurrent.ConcurrentTaskExecutor
* @see java.util.concurrent.Executors.Executors#newSingleThreadExecutor()
*/
public class FutureUtils {
private static final long DEFAULT_TIMEOUT = 5000L;
private static final TimeUnit DEFAULT_TIME_UNIT = TimeUnit.MILLISECONDS;
public static T get(Future future) {
return get(future, DEFAULT_TIMEOUT, DEFAULT_TIME_UNIT);
}
public static T get(Future future, long timeout, TimeUnit unit) {
try {
return timeout < 0 ? future.get() : future.get(timeout, unit);
}
catch (InterruptedException | TimeoutException e) {
future.cancel(true);
throw new RuntimeException(e);
}
catch (ExecutionException e) {
if (e.getCause() instanceof Throwable) {
StackTraceElement[] causeStackTrace = e.getCause().getStackTrace();
StackTraceElement[] stackTrace = e.getStackTrace();
StackTraceElement[] stackTraceElements = (StackTraceElement[]) Array.newInstance(StackTraceElement.class, causeStackTrace.length + stackTrace.length);
System.arraycopy(causeStackTrace, 0, stackTraceElements, 0, causeStackTrace.length);
System.arraycopy(stackTrace, 0, stackTraceElements, causeStackTrace.length, stackTrace.length);
e.getCause().setStackTrace(stackTraceElements);
throw new RuntimeException(e.getCause());
}
throw new RuntimeException(e);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy