本文整理汇总了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());
}
}
});
}