当前位置: 首页>>代码示例>>Java>>正文


Java Futures.catchingAsync方法代码示例

本文整理汇总了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);
		}
	});
}
 
开发者ID:equella,项目名称:Equella,代码行数:49,代码来源:URLCheckerService.java


注:本文中的com.google.common.util.concurrent.Futures.catchingAsync方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。