本文整理汇总了Java中com.google.common.util.concurrent.Futures.catchingAsync方法的典型用法代码示例。如果您正苦于以下问题:Java Futures.catchingAsync方法的具体用法?Java Futures.catchingAsync怎么用?Java Futures.catchingAsync使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.google.common.util.concurrent.Futures
的用法示例。
在下文中一共展示了Futures.catchingAsync方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: 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);
}
});
}