Java Code Examples for com.google.common.util.concurrent.Futures#immediateFailedCheckedFuture()

The following examples show how to use com.google.common.util.concurrent.Futures#immediateFailedCheckedFuture() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Scheduler.java    From The-5zig-Mod with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Posts a runnable to the Main Server Thread.
 * <p/>
 *
 * @param runnable The runnable that should be executed.
 * @return a Listenable Future.
 */
public ListenableFuture postToMainThread(Runnable runnable, boolean noThreadCheck, int delay) {
	Callable callable = Executors.callable(runnable);
	ListenableFuture listenableFuture = ListenableFutureTask.create(callable);
	if (noThreadCheck || delay > 0 || !The5zigMod.getVars().isMainThread()) {
		synchronized (jobs) {
			jobs.add(new Task(listenableFuture, delay));
			return listenableFuture;
		}
	} else {
		try {
			return Futures.immediateFuture(callable.call());
		} catch (Exception e) {
			return Futures.immediateFailedCheckedFuture(e);
		}
	}
}
 
Example 2
Source File: Scheduler.java    From The-5zig-Mod with MIT License 6 votes vote down vote up
/**
 * Posts a runnable to the Main Server Thread.
 * <p/>
 *
 * @param runnable The runnable that should be executed.
 * @return a Listenable Future.
 */
public ListenableFuture postToMainThread(Runnable runnable, boolean noThreadCheck, int delay) {
	Callable callable = Executors.callable(runnable);
	ListenableFuture listenableFuture = ListenableFutureTask.create(callable);
	if (noThreadCheck || delay > 0 || !The5zigMod.getVars().isMainThread()) {
		synchronized (jobs) {
			jobs.add(new Task(listenableFuture, delay));
			return listenableFuture;
		}
	} else {
		try {
			return Futures.immediateFuture(callable.call());
		} catch (Exception e) {
			return Futures.immediateFailedCheckedFuture(e);
		}
	}
}
 
Example 3
Source File: SemaphoredDelegatingExecutor.java    From stocator with Apache License 2.0 5 votes vote down vote up
@Override
public <T> ListenableFuture<T> submit(Callable<T> task) {
  try {
    queueingPermits.acquire();
  } catch (InterruptedException e) {
    Thread.currentThread().interrupt();
    return Futures.immediateFailedCheckedFuture(e);
  }
  return super.submit(new CallableWithPermitRelease<>(task));
}
 
Example 4
Source File: SemaphoredDelegatingExecutor.java    From stocator with Apache License 2.0 5 votes vote down vote up
@Override
public <T> ListenableFuture<T> submit(Runnable task, T result) {
  try {
    queueingPermits.acquire();
  } catch (InterruptedException e) {
    Thread.currentThread().interrupt();
    return Futures.immediateFailedCheckedFuture(e);
  }
  return super.submit(new RunnableWithPermitRelease(task), result);
}
 
Example 5
Source File: SemaphoredDelegatingExecutor.java    From stocator with Apache License 2.0 5 votes vote down vote up
@Override
public ListenableFuture<?> submit(Runnable task) {
  try {
    queueingPermits.acquire();
  } catch (InterruptedException e) {
    Thread.currentThread().interrupt();
    return Futures.immediateFailedCheckedFuture(e);
  }
  return super.submit(new RunnableWithPermitRelease(task));
}
 
Example 6
Source File: AbstractOAuthURLFetchService.java    From appengine-gcs-client with Apache License 2.0 5 votes vote down vote up
@Override
public Future<HTTPResponse> fetchAsync(HTTPRequest req) {
  HTTPRequest authorizedRequest;
  try {
    authorizedRequest = createAuthorizeRequest(req);
  } catch (RetryHelperException e) {
    return Futures.immediateFailedCheckedFuture(e);
  }
  return urlFetch.fetchAsync(authorizedRequest);
}