List of usage examples for com.google.common.util.concurrent SettableFuture setException
@Override public boolean setException(Throwable throwable)
From source file:org.dcache.space.ReservationCaches.java
/** * Builds a loading cache for looking up space tokens by owner and * description./*from w w w .jav a2 s. c om*/ */ public static LoadingCache<GetSpaceTokensKey, long[]> buildOwnerDescriptionLookupCache(CellStub spaceManager, Executor executor) { return CacheBuilder.newBuilder().maximumSize(1000).expireAfterWrite(30, SECONDS) .refreshAfterWrite(10, SECONDS).recordStats().build(new CacheLoader<GetSpaceTokensKey, long[]>() { @Override public long[] load(GetSpaceTokensKey key) throws Exception { try { return spaceManager.sendAndWait(createRequest(key)).getSpaceTokens(); } catch (TimeoutCacheException e) { throw new SRMInternalErrorException("Space manager timeout", e); } catch (InterruptedException e) { throw new SRMInternalErrorException("Operation interrupted", e); } catch (CacheException e) { _log.warn("GetSpaceTokens failed with rc={} error={}", e.getRc(), e.getMessage()); throw new SRMException( "GetSpaceTokens failed with rc=" + e.getRc() + " error=" + e.getMessage(), e); } } private GetSpaceTokens createRequest(GetSpaceTokensKey key) { GetSpaceTokens message = new GetSpaceTokens(key.description); message.setSubject( new Subject(true, key.principals, Collections.emptySet(), Collections.emptySet())); return message; } @Override public ListenableFuture<long[]> reload(GetSpaceTokensKey key, long[] oldValue) throws Exception { final SettableFuture<long[]> future = SettableFuture.create(); CellStub.addCallback(spaceManager.send(createRequest(key)), new AbstractMessageCallback<GetSpaceTokens>() { @Override public void success(GetSpaceTokens message) { future.set(message.getSpaceTokens()); } @Override public void failure(int rc, Object error) { CacheException exception = CacheExceptionFactory.exceptionOf(rc, Objects.toString(error, null)); future.setException(exception); } }, executor); return future; } }); }
From source file:org.glowroot.central.util.Session.java
private static ListenableFuture<ResultSet> throttle(DoUnderThrottle doUnderThrottle, Semaphore overallSemaphore) throws Exception { overallSemaphore.acquire();//w w w . j a va 2 s. com SettableFuture<ResultSet> outerFuture = SettableFuture.create(); ResultSetFuture innerFuture; try { innerFuture = doUnderThrottle.execute(); } catch (Throwable t) { overallSemaphore.release(); throw t; } Futures.addCallback(innerFuture, new FutureCallback<ResultSet>() { @Override public void onSuccess(ResultSet result) { overallSemaphore.release(); outerFuture.set(result); } @Override public void onFailure(Throwable t) { overallSemaphore.release(); outerFuture.setException(t); } }, MoreExecutors.directExecutor()); return outerFuture; }
From source file:org.dcache.space.ReservationCaches.java
/** * Cache queries to discover if a directory has the "WriteToken" tag set. */// w w w . j av a 2s . c o m public static LoadingCache<FsPath, java.util.Optional<String>> buildWriteTokenLookupCache(PnfsHandler pnfs, Executor executor) { return CacheBuilder.newBuilder().maximumSize(1000).expireAfterWrite(10, MINUTES) .refreshAfterWrite(5, MINUTES).recordStats() .build(new CacheLoader<FsPath, java.util.Optional<String>>() { private java.util.Optional<String> writeToken(FileAttributes attr) { StorageInfo info = attr.getStorageInfo(); return java.util.Optional.ofNullable(info.getMap().get("writeToken")); } @Override public java.util.Optional<String> load(FsPath path) throws CacheException, NoRouteToCellException, InterruptedException { return writeToken(pnfs.getFileAttributes(path, EnumSet.of(FileAttribute.STORAGEINFO))); } @Override public ListenableFuture<java.util.Optional<String>> reload(FsPath path, java.util.Optional<String> old) { PnfsGetFileAttributes message = new PnfsGetFileAttributes(path.toString(), EnumSet.of(FileAttribute.STORAGEINFO)); SettableFuture<java.util.Optional<String>> future = SettableFuture.create(); CellStub.addCallback(pnfs.requestAsync(message), new AbstractMessageCallback<PnfsGetFileAttributes>() { @Override public void success(PnfsGetFileAttributes message) { future.set(writeToken(message.getFileAttributes())); } @Override public void failure(int rc, Object error) { CacheException exception = CacheExceptionFactory.exceptionOf(rc, Objects.toString(error, null)); future.setException(exception); } }, executor); return future; } }); }
From source file:org.dcache.space.ReservationCaches.java
/** * Build a loading cache for looking up space reservations by space token. *//* w w w . j ava2s. co m*/ public static LoadingCache<String, Optional<Space>> buildSpaceLookupCache(CellStub spaceManager, Executor executor) { return CacheBuilder.newBuilder().maximumSize(1000).expireAfterWrite(10, MINUTES) .refreshAfterWrite(30, SECONDS).recordStats().build(new CacheLoader<String, Optional<Space>>() { @Override public Optional<Space> load(String token) throws CacheException, NoRouteToCellException, InterruptedException { Space space = spaceManager.sendAndWait(new GetSpaceMetaData(token)).getSpaces()[0]; return Optional.ofNullable(space); } @Override public ListenableFuture<Optional<Space>> reload(String token, Optional<Space> oldValue) { final SettableFuture<Optional<Space>> future = SettableFuture.create(); CellStub.addCallback(spaceManager.send(new GetSpaceMetaData(token)), new AbstractMessageCallback<GetSpaceMetaData>() { @Override public void success(GetSpaceMetaData message) { future.set(Optional.ofNullable(message.getSpaces()[0])); } @Override public void failure(int rc, Object error) { CacheException exception = CacheExceptionFactory.exceptionOf(rc, Objects.toString(error, null)); future.setException(exception); } }, executor); return future; } }); }
From source file:io.soliton.protobuf.json.JsonRpcRequestInvoker.java
/** * Actually invokes the server method./*from w ww.j a v a 2 s . c om*/ * * @param method the method to invoke * @param parameter the request's parameter * @param id the request's client-side identifier * @param <I> the method's input proto-type * @param <O> the method's output proto-type */ private <I extends Message, O extends Message> ListenableFuture<JsonRpcResponse> invoke( ServerMethod<I, O> method, JsonObject parameter, JsonElement id) { I request; try { request = (I) Messages.fromJson(method.inputBuilder(), parameter); } catch (Exception e) { serverLogger.logServerFailure(method, e); SettableFuture<JsonRpcResponse> future = SettableFuture.create(); future.setException(e); return future; } ListenableFuture<O> response = method.invoke(request); return Futures.transform(response, new JsonConverter(id), TRANSFORM_EXECUTOR); }
From source file:org.opendaylight.openflowjava.protocol.impl.connection.SwitchConnectionProviderImpl.java
@Override public Future<List<Boolean>> shutdown() { LOGGER.debug("Shutdown summoned"); ListenableFuture<List<Boolean>> result = SettableFuture.create(); try {/*from w ww .ja va 2 s .c o m*/ List<ListenableFuture<Boolean>> shutdownChain = new ArrayList<>(); for (ServerFacade server : serverLot) { ListenableFuture<Boolean> shutdownFuture = server.shutdown(); shutdownChain.add(shutdownFuture); } if (!shutdownChain.isEmpty()) { result = Futures.allAsList(shutdownChain); } else { throw new IllegalStateException("No servers configured"); } } catch (Exception e) { SettableFuture<List<Boolean>> exFuture = SettableFuture.create(); exFuture.setException(e); result = exFuture; } return result; }
From source file:org.midonet.odp.protos.OvsDatapathConnection.java
@Nonnull private static <T> Callback<T> wrapFuture(final SettableFuture<T> future) { return new Callback<T>() { @Override//from ww w. j a v a 2 s . c om public void onSuccess(T data) { future.set(data); } @Override public void onError(NetlinkException e) { future.setException(e); } }; }
From source file:org.opendaylight.ocpjava.protocol.impl.core.RadioHeadConnectionProviderImpl.java
@Override public ListenableFuture<Boolean> startup() { LOG.debug("Startup summoned"); ListenableFuture<Boolean> result = null; try {/* w w w. j av a2 s. c o m*/ serverFacade = createAndConfigureServer(); if (radioHeadConnectionHandler == null) { throw new IllegalStateException("RadioHeadConnectionHandler is not set"); } new Thread(serverFacade).start(); result = serverFacade.getIsOnlineFuture(); } catch (Exception e) { SettableFuture<Boolean> exResult = SettableFuture.create(); exResult.setException(e); result = exResult; } return result; }
From source file:org.opendaylight.openflowjava.protocol.impl.connection.SwitchConnectionProviderImpl.java
@Override public Future<List<Boolean>> startup() { LOGGER.debug("startup summoned"); ListenableFuture<List<Boolean>> result = SettableFuture.create(); try {/*from ww w . ja v a 2s . c o m*/ if (serverLot.isEmpty()) { throw new IllegalStateException("No servers configured"); } for (ServerFacade server : serverLot) { if (server.getIsOnlineFuture().isDone()) { throw new IllegalStateException("Servers already running"); } } if (switchConnectionHandler == null) { throw new IllegalStateException("switchConnectionHandler is not set"); } List<ListenableFuture<Boolean>> starterChain = new ArrayList<>(); for (ServerFacade server : serverLot) { new Thread(server).start(); ListenableFuture<Boolean> isOnlineFuture = server.getIsOnlineFuture(); starterChain.add(isOnlineFuture); } if (!starterChain.isEmpty()) { result = Futures.allAsList(starterChain); } else { throw new IllegalStateException("No servers configured"); } } catch (Exception e) { SettableFuture<List<Boolean>> exFuture = SettableFuture.create(); exFuture.setException(e); result = exFuture; } return result; }
From source file:io.crate.jobs.ESJobContext.java
@Override protected void innerClose(@Nullable Throwable t) { if (t != null) { for (SettableFuture<Long> resultFuture : resultFutures) { if (!resultFuture.isDone()) { resultFuture.setException(t); }/*from ww w. java2 s.co m*/ } } }