當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。