List of usage examples for com.google.common.util.concurrent Futures makeChecked
@GwtIncompatible("TODO") @CheckReturnValue public static <V, X extends Exception> CheckedFuture<V, X> makeChecked(ListenableFuture<V> future, Function<? super Exception, X> mapper)
From source file:org.onos.yangtools.yang.parser.repo.SharedSchemaContextFactory.java
@Override public CheckedFuture<SchemaContext, SchemaResolutionException> createSchemaContext( final Collection<SourceIdentifier> requiredSources) { // Make sources unique final List<SourceIdentifier> uniqueSourceIdentifiers = deDuplicateSources(requiredSources); final SchemaContext existing = cache.getIfPresent(uniqueSourceIdentifiers); if (existing != null) { LOG.debug("Returning cached context {}", existing); return Futures.immediateCheckedFuture(existing); }/*from w w w. ja v a2s . c o m*/ // Request all sources be loaded ListenableFuture<List<ASTSchemaSource>> sf = Futures .allAsList(Collections2.transform(uniqueSourceIdentifiers, requestSources)); // Detect mismatch between requested Source IDs and IDs that are extracted from parsed source // Also remove duplicates if present // We are relying on preserved order of uniqueSourceIdentifiers as well as sf sf = Futures.transform(sf, new SourceIdMismatchDetector(uniqueSourceIdentifiers)); // Assemble sources into a schema context final ListenableFuture<SchemaContext> cf = Futures.transform(sf, assembleSources); // Populate cache when successful Futures.addCallback(cf, new FutureCallback<SchemaContext>() { @Override public void onSuccess(final SchemaContext result) { cache.put(uniqueSourceIdentifiers, result); } @Override public void onFailure(final Throwable t) { LOG.debug("Failed to assemble sources", t); } }); return Futures.makeChecked(cf, MAPPER); }
From source file:org.opendaylight.distributed.tx.impl.DTXTestTransaction.java
@Override public CheckedFuture<Void, TransactionCommitFailedException> submit() { final SettableFuture<Void> retFuture = SettableFuture.create(); final Runnable submitResult = new Runnable() { @Override/*www . j a v a 2 s . c o m*/ public void run() { try { Thread.sleep(delayTime); } catch (InterruptedException e) { e.printStackTrace(); } if (!submitException) { retFuture.set(null); } else { setSubmitException(false); retFuture.setException(new RuntimeException("Submit error")); } retFuture.notifyAll(); } }; new Thread(submitResult).start(); Function<Exception, TransactionCommitFailedException> f = new Function<Exception, TransactionCommitFailedException>() { @Nullable @Override public TransactionCommitFailedException apply(@Nullable Exception e) { return new TransactionCommitFailedException("Submit failed", e); } }; return Futures.makeChecked(retFuture, f); }
From source file:org.opendaylight.controller.sal.connect.netconf.sal.tx.NetconfDeviceWriteOnlyTx.java
@Override public CheckedFuture<Void, TransactionCommitFailedException> submit() { final ListenableFuture<Void> commmitFutureAsVoid = Futures.transform(commit(), new Function<RpcResult<TransactionStatus>, Void>() { @Override// ww w . java2 s . co m public Void apply(final RpcResult<TransactionStatus> input) { return null; } }); return Futures.makeChecked(commmitFutureAsVoid, new Function<Exception, TransactionCommitFailedException>() { @Override public TransactionCommitFailedException apply(final Exception input) { return new TransactionCommitFailedException( "Submit of transaction " + getIdentifier() + " failed", input); } }); }
From source file:org.opendaylight.distributed.tx.impl.CachingReadWriteTx.java
public <T extends DataObject> CheckedFuture<Void, DTxException> asyncMerge( final LogicalDatastoreType logicalDatastoreType, final InstanceIdentifier<T> instanceIdentifier, final T t) { increaseOperation();//ww w . j av a2 s .c o m CheckedFuture<Optional<T>, ReadFailedException> readFuture = null; try { readFuture = delegate.read(logicalDatastoreType, instanceIdentifier); } catch (Exception e) { readFuture = Futures .immediateFailedCheckedFuture(new ReadFailedException("Read exception in merge action")); } final SettableFuture<Void> retFuture = SettableFuture.create(); Futures.addCallback(readFuture, new FutureCallback<Optional<T>>() { @Override public void onSuccess(final Optional<T> result) { synchronized (this) { cache.add(new CachedData(logicalDatastoreType, instanceIdentifier, result.orNull(), ModifyAction.MERGE)); } final ListeningExecutorService executorService = MoreExecutors .listeningDecorator(executorPoolPerCache); final ListenableFuture asyncMergeFuture = executorService.submit(new Callable() { @Override public Object call() throws Exception { delegate.merge(logicalDatastoreType, instanceIdentifier, t); return null; } }); Futures.addCallback(asyncMergeFuture, new FutureCallback() { @Override public void onSuccess(@Nullable Object result) { decreaseOperation(); retFuture.set(null); } @Override public void onFailure(Throwable t) { decreaseOperation(); LOG.trace("async merge failure"); retFuture.setException(new DTxException.EditFailedException("async merge failure", t)); } }); } @Override public void onFailure(final Throwable t) { decreaseOperation(); retFuture.setException( new DTxException.ReadFailedException("failed to read from node in merge action", t)); } }); return Futures.makeChecked(retFuture, new Function<Exception, DTxException>() { @Nullable @Override public DTxException apply(@Nullable Exception e) { e = (Exception) e.getCause(); return e instanceof DTxException ? (DTxException) e : new DTxException("merge operation failed", e); } }); }
From source file:com.google.gerrit.server.index.change.ChangeIndexer.java
private static <T> CheckedFuture<T, IOException> submit(Callable<T> task, ListeningExecutorService executor) { return Futures.makeChecked(Futures.nonCancellationPropagating(executor.submit(task)), MAPPER); }
From source file:org.opendaylight.distributed.tx.impl.CachingReadWriteTx.java
public <T extends DataObject> CheckedFuture<Void, DTxException> asyncPut( final LogicalDatastoreType logicalDatastoreType, final InstanceIdentifier<T> instanceIdentifier, final T t) { increaseOperation();/*w w w . j ava 2 s . com*/ final SettableFuture<Void> retFuture = SettableFuture.create(); CheckedFuture<Optional<T>, ReadFailedException> readFuture = null; try { readFuture = delegate.read(logicalDatastoreType, instanceIdentifier); } catch (Exception e) { readFuture = Futures .immediateFailedCheckedFuture(new ReadFailedException("Read exception in put action")); } Futures.addCallback(readFuture, new FutureCallback<Optional<T>>() { @Override public void onSuccess(final Optional<T> result) { synchronized (this) { cache.add(new CachedData(logicalDatastoreType, instanceIdentifier, result.orNull(), ModifyAction.REPLACE)); } final ListeningExecutorService executorService = MoreExecutors .listeningDecorator(executorPoolPerCache); final ListenableFuture asyncPutFuture = executorService.submit(new Callable() { @Override public Object call() throws Exception { delegate.put(logicalDatastoreType, instanceIdentifier, t); return null; } }); Futures.addCallback(asyncPutFuture, new FutureCallback() { @Override public void onSuccess(@Nullable Object result) { decreaseOperation(); retFuture.set(null); } @Override public void onFailure(Throwable t) { decreaseOperation(); LOG.trace("async put failure"); retFuture.setException(new DTxException.EditFailedException("async put failure", t)); } }); } @Override public void onFailure(final Throwable t) { decreaseOperation(); retFuture.setException( new DTxException.ReadFailedException("failed to read from node in put action", t)); } }); return Futures.makeChecked(retFuture, new Function<Exception, DTxException>() { @Nullable @Override public DTxException apply(@Nullable Exception e) { e = (Exception) e.getCause(); return e instanceof DTxException ? (DTxException) e : new DTxException("put operation failed", e); } }); }
From source file:org.opendaylight.distributed.tx.impl.DtxImpl.java
@Override public CheckedFuture<Void, TransactionCommitFailedException> submit() throws DTxException.SubmitFailedException, DTxException.RollbackFailedException { waitForAllTxsDone();//from ww w. j ava 2 s. c o m int totalSubmitSize = getNumberofNodes(); final Map<InstanceIdentifier<?>, PerNodeTxState> commitStatus = Maps .newHashMapWithExpectedSize(totalSubmitSize); final SettableFuture<Void> distributedSubmitFuture = SettableFuture.create(); for (DTXLogicalTXProviderType type : this.perNodeTransactionsbyLogicalType.keySet()) { Map<InstanceIdentifier<?>, CachingReadWriteTx> transactions = this.perNodeTransactionsbyLogicalType .get(type); for (final Map.Entry<InstanceIdentifier<?>, CachingReadWriteTx> perNodeTx : transactions.entrySet()) { CheckedFuture<Void, TransactionCommitFailedException> submitFuture = null; try { submitFuture = perNodeTx.getValue().submit(); } catch (Exception submitFailException) { new PerNodeSubmitCallback(type, commitStatus, perNodeTx, distributedSubmitFuture) .failedWithException(submitFailException); continue; } Futures.addCallback(submitFuture, new PerNodeSubmitCallback(type, commitStatus, perNodeTx, distributedSubmitFuture)); } } return Futures.makeChecked(distributedSubmitFuture, new Function<Exception, TransactionCommitFailedException>() { @Nullable @Override public TransactionCommitFailedException apply(@Nullable final Exception input) { return new TransactionCommitFailedException( "Submit failed. Check nested exception for rollback status", input); } }); }
From source file:org.opendaylight.distributed.tx.impl.DtxImpl.java
/** * Perform submit rollback with the caches and empty rollback transactions for every node *///from ww w . j av a 2 s.co m private CheckedFuture<Void, DTxException.RollbackFailedException> rollbackUponCommitFailure( final Map<InstanceIdentifier<?>, PerNodeTxState> commitStatus) { Map<InstanceIdentifier<?>, CachingReadWriteTx> perNodeCache = new HashMap<>(); for (DTXLogicalTXProviderType type : this.perNodeTransactionsbyLogicalType.keySet()) { Map<InstanceIdentifier<?>, CachingReadWriteTx> tmpMap = this.perNodeTransactionsbyLogicalType.get(type); perNodeCache.putAll(tmpMap); } Rollback rollback = new RollbackImpl(); final ListenableFuture<Void> rollbackFuture = rollback.rollback(perNodeCache, Maps.transformValues(commitStatus, new Function<PerNodeTxState, ReadWriteTransaction>() { @Nullable @Override public ReadWriteTransaction apply(@Nullable final PerNodeTxState input) { return input.getRollbackTx(); } })); return Futures.makeChecked(rollbackFuture, new Function<Exception, DTxException.RollbackFailedException>() { @Nullable @Override public DTxException.RollbackFailedException apply(@Nullable final Exception input) { return new DTxException.RollbackFailedException(input); } }); }
From source file:org.opendaylight.distributed.tx.impl.DtxImpl.java
private CheckedFuture<Void, DTxException.RollbackFailedException> rollbackUponOperationFailure() { waitForAllTxsDone();/* ww w .j a v a 2 s. c o m*/ Rollback rollback = new RollbackImpl(); Map<InstanceIdentifier<?>, CachingReadWriteTx> perNodeCache = new HashMap<>(); for (DTXLogicalTXProviderType type : this.perNodeTransactionsbyLogicalType.keySet()) { Map<InstanceIdentifier<?>, CachingReadWriteTx> tmpMap = this.perNodeTransactionsbyLogicalType.get(type); perNodeCache.putAll(tmpMap); } final ListenableFuture<Void> rollbackFuture = rollback.rollback(perNodeCache, this.readWriteTxMap); return Futures.makeChecked(rollbackFuture, new Function<Exception, DTxException.RollbackFailedException>() { @Nullable @Override public DTxException.RollbackFailedException apply(@Nullable final Exception input) { return new DTxException.RollbackFailedException(input); } }); }
From source file:diskCacheV111.srm.dcache.Storage.java
@Override public CheckedFuture<Pin, ? extends SRMException> pinFile(SRMUser user, URI surl, String clientHost, long pinLifetime, String requestToken) { try {/*from w w w . j a v a 2 s .c om*/ return Futures.makeChecked(PinCompanion.pinFile(asDcacheUser(user).getSubject(), config.getPath(surl), clientHost, pinLifetime, requestToken, _isOnlinePinningEnabled, _poolMonitor, _pnfsStub, _poolManagerStub, _pinManagerStub, _executor), new ToSRMException()); } catch (SRMAuthorizationException | SRMInvalidPathException e) { return Futures.immediateFailedCheckedFuture(e); } }