本文整理匯總了Java中com.google.common.util.concurrent.Futures.transformAsync方法的典型用法代碼示例。如果您正苦於以下問題:Java Futures.transformAsync方法的具體用法?Java Futures.transformAsync怎麽用?Java Futures.transformAsync使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類com.google.common.util.concurrent.Futures
的用法示例。
在下文中一共展示了Futures.transformAsync方法的9個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: recipe
import com.google.common.util.concurrent.Futures; //導入方法依賴的package包/類
@Produces
static ListenableFuture<Recipe> recipe(
List<String> ingredients,
SearchResponse searchResponse,
Supplier<Random> randomSupplier,
YummlyApi yummly) {
int totalCount = searchResponse.totalMatchCount();
ListenableFuture<SearchResponse> future = Futures.immediateFuture(null);
// Get a random recipe to return. Search request fails randomly so try a few times.
Executor executor = RequestContext.current().contextAwareEventLoop();
Random random = randomSupplier.get();
for (int i = 0; i < 5; i++) {
int resultIndex = random.nextInt(totalCount);
future =
Futures.transformAsync(
future,
result -> {
if (result != null && !result.matches().isEmpty()) {
return Futures.immediateFuture(result);
}
return yummly.search(
EggworldConstants.EGG_QUERY,
ingredients,
resultIndex,
1,
true,
ImmutableList.of());
},
executor);
}
return Futures.transform(future, r -> r.matches().get(0), MoreExecutors.directExecutor());
}
示例2: submit
import com.google.common.util.concurrent.Futures; //導入方法依賴的package包/類
@Override
public ListenableFuture<Void> submit() {
LOG.debug("Submitting transaction for shard {}", shardRoot);
checkTransactionReadied();
final AsyncFunction<Boolean, Void> validateFunction = input -> prepare();
final AsyncFunction<Void, Void> prepareFunction = input -> commit();
// transform validate into prepare
final ListenableFuture<Void> prepareFuture = Futures.transformAsync(validate(), validateFunction,
MoreExecutors.directExecutor());
// transform prepare into commit and return as submit result
return Futures.transformAsync(prepareFuture, prepareFunction, MoreExecutors.directExecutor());
}
示例3: doSubmit
import com.google.common.util.concurrent.Futures; //導入方法依賴的package包/類
private static ListenableFuture<Void> doSubmit(final DOMStoreThreePhaseCommitCohort cohort) {
final AsyncFunction<Boolean, Void> validateFunction = input -> cohort.preCommit();
final AsyncFunction<Void, Void> prepareFunction = input -> cohort.commit();
final ListenableFuture<Void> prepareFuture = Futures.transformAsync(cohort.canCommit(), validateFunction,
MoreExecutors.directExecutor());
return Futures.transformAsync(prepareFuture, prepareFunction, MoreExecutors.directExecutor());
}
示例4: makeBreakfast
import com.google.common.util.concurrent.Futures; //導入方法依賴的package包/類
@Override
public Future<RpcResult<Void>> makeBreakfast(EggsType eggsType, Class<? extends ToastType> toastType,
int toastDoneness) {
// Call makeToast and use JdkFutureAdapters to convert the Future to a ListenableFuture, The
// OpendaylightToaster impl already returns a ListenableFuture so the conversion is actually a no-op.
ListenableFuture<RpcResult<Void>> makeToastFuture = JdkFutureAdapters
.listenInPoolThread(makeToast(toastType, toastDoneness), executor);
ListenableFuture<RpcResult<Void>> makeEggsFuture = makeEggs(eggsType);
// Combine the 2 ListenableFutures into 1 containing a list RpcResults.
ListenableFuture<List<RpcResult<Void>>> combinedFutures = Futures
.allAsList(ImmutableList.of(makeToastFuture, makeEggsFuture));
// Then transform the RpcResults into 1.
return Futures.transformAsync(combinedFutures, results -> {
boolean atLeastOneSucceeded = false;
Builder<RpcError> errorList = ImmutableList.builder();
for (RpcResult<Void> result : results) {
if (result.isSuccessful()) {
atLeastOneSucceeded = true;
}
if (result.getErrors() != null) {
errorList.addAll(result.getErrors());
}
}
return Futures.immediateFuture(RpcResultBuilder.<Void>status(atLeastOneSucceeded)
.withRpcErrors(errorList.build()).build());
});
}
示例5: checkUrl
import com.google.common.util.concurrent.Futures; //導入方法依賴的package包/類
/**
* Checks the referenced URL and returns a future to an updated referenced
* URL. The future referenced URL is not persisted, but the caller should do
* that to ensure the checking records are correct.
*/
ListenableFuture<ReferencedURL> checkUrl(final ReferencedURL rurl)
{
final ListenableFuture<Pair<ReferencedURL, Boolean>> checkUrlFuture = checkUrl(rurl, true);
// Map the Pair to just the ReferencedUrl
final ListenableFuture<ReferencedURL> mapToRurl = Futures.transformAsync(checkUrlFuture,
new AsyncFunction<Pair<ReferencedURL, Boolean>, ReferencedURL>()
{
@Override
public ListenableFuture<ReferencedURL> apply(Pair<ReferencedURL, Boolean> input)
{
return Futures.immediateFuture(input.getFirst());
}
});
// If the above futures throw an exception, it's probably because we
// haven't been able to check the URL (eg,
// java.nio.UnresolvedAddressException) so return an updated
// ReferencedURL based on the old one.
return Futures.catchingAsync(mapToRurl, Throwable.class, new AsyncFunction<Throwable, ReferencedURL>()
{
@Override
public ListenableFuture<ReferencedURL> apply(Throwable t)
{
final String url = rurl.getUrl();
if( LOGGER.isDebugEnabled() )
{
LOGGER.debug("Exception checking " + url, t);
}
ReferencedURL newRurl = new ReferencedURL();
newRurl.setId(rurl.getId());
newRurl.setUrl(rurl.getUrl());
newRurl.setSuccess(false);
newRurl.setStatus(0);
newRurl.setTries(rurl.getTries() + 1);
newRurl.setLastChecked(new Date(System.currentTimeMillis()));
newRurl.setLastIndexed(new Date());
newRurl.setMessage(t.getClass().getName() + ": " + t.getMessage());
return Futures.immediateFuture(newRurl);
}
});
}
示例6: commit
import com.google.common.util.concurrent.Futures; //導入方法依賴的package包/類
@Override
public ListenableFuture<RpcResult<TransactionStatus>> commit() {
return Futures.transformAsync(submit(), input -> SUCCESS_FUTURE);
}
開發者ID:hashsdn,項目名稱:hashsdn-controller,代碼行數:5,代碼來源:ShardedDOMDataBrokerDelegatingWriteTransaction.java
示例7: commit
import com.google.common.util.concurrent.Futures; //導入方法依賴的package包/類
@Override
public ListenableFuture<RpcResult<TransactionStatus>> commit() {
return Futures.transformAsync(submit(), input -> SUCCESS_FUTURE, MoreExecutors.directExecutor());
}
開發者ID:hashsdn,項目名稱:hashsdn-controller,代碼行數:5,代碼來源:ShardedDOMDataBrokerDelegatingReadWriteTransaction.java
示例8: convertToLegacyCommitFuture
import com.google.common.util.concurrent.Futures; //導入方法依賴的package包/類
public static ListenableFuture<RpcResult<TransactionStatus>> convertToLegacyCommitFuture(final CheckedFuture<Void,TransactionCommitFailedException> from) {
return Futures.transformAsync(from, input -> SUCCESS_FUTURE);
}
示例9: checkStatusAndMakeToast
import com.google.common.util.concurrent.Futures; //導入方法依賴的package包/類
private void checkStatusAndMakeToast(final MakeToastInput input, final SettableFuture<RpcResult<Void>> futureResult,
final int tries) {
// Read the ToasterStatus and, if currently Up, try to write the status to Down.
// If that succeeds, then we essentially have an exclusive lock and can proceed
// to make toast.
final ReadWriteTransaction tx = dataBroker.newReadWriteTransaction();
ListenableFuture<Optional<Toaster>> readFuture = tx.read(OPERATIONAL, TOASTER_IID);
final ListenableFuture<Void> commitFuture =
Futures.transformAsync(readFuture, toasterData -> {
ToasterStatus toasterStatus = ToasterStatus.Up;
if (toasterData.isPresent()) {
toasterStatus = toasterData.get().getToasterStatus();
}
LOG.debug("Read toaster status: {}", toasterStatus);
if (toasterStatus == ToasterStatus.Up) {
if (outOfBread()) {
LOG.debug("Toaster is out of bread");
return Futures.immediateFailedCheckedFuture(
new TransactionCommitFailedException("", makeToasterOutOfBreadError()));
}
LOG.debug("Setting Toaster status to Down");
// We're not currently making toast - try to update the status to Down
// to indicate we're going to make toast. This acts as a lock to prevent
// concurrent toasting.
tx.put(OPERATIONAL, TOASTER_IID, buildToaster(ToasterStatus.Down));
return tx.submit();
}
LOG.debug("Oops - already making toast!");
// Return an error since we are already making toast. This will get
// propagated to the commitFuture below which will interpret the null
// TransactionStatus in the RpcResult as an error condition.
return Futures.immediateFailedCheckedFuture(
new TransactionCommitFailedException("", makeToasterInUseError()));
});
Futures.addCallback(commitFuture, new FutureCallback<Void>() {
@Override
public void onSuccess(final Void result) {
// OK to make toast
currentMakeToastTask.set(executor.submit(new MakeToastTask(input, futureResult)));
}
@Override
public void onFailure(final Throwable ex) {
if (ex instanceof OptimisticLockFailedException) {
// Another thread is likely trying to make toast simultaneously and updated the
// status before us. Try reading the status again - if another make toast is
// now in progress, we should get ToasterStatus.Down and fail.
if (tries - 1 > 0) {
LOG.debug("Got OptimisticLockFailedException - trying again");
checkStatusAndMakeToast(input, futureResult, tries - 1);
} else {
futureResult.set(RpcResultBuilder.<Void>failed()
.withError(ErrorType.APPLICATION, ex.getMessage()).build());
}
} else if (ex instanceof TransactionCommitFailedException) {
LOG.debug("Failed to commit Toaster status", ex);
// Probably already making toast.
futureResult.set(RpcResultBuilder.<Void>failed()
.withRpcErrors(((TransactionCommitFailedException)ex).getErrorList()).build());
} else {
LOG.debug("Unexpected error committing Toaster status", ex);
futureResult.set(RpcResultBuilder.<Void>failed().withError(ErrorType.APPLICATION,
"Unexpected error committing Toaster status", ex).build());
}
}
});
}