FutureFallback提供一个Future的备用来替代之前失败的Future,常被用来作为Future的备份或者默认的值。

@Test
public void testFuturesFallback() throws ExecutionException, InterruptedException {
listenableFuture = executorService.submit(new Callable<String>() {
@Override
public String call() throws Exception {
throw new RuntimeException();
}
});
FutureFallback<String> futureFallback = new FutureFallback<String>() {
@Override
public ListenableFuture create(Throwable t) throws Exception {
if (t instanceof RuntimeException) {
SettableFuture<String> settableFuture =
SettableFuture.create();
settableFuture.set("Not Found");
return settableFuture;
}
throw new Exception(t);
}
};
ListenableFuture<String> lf =
Futures.withFallback(listenableFuture,futureFallback);
assertThat(lf.get(), is("Not Found"));
}

代码的逻辑其实和AsyncFunction 一样,只是这里是Futures.withFallback而已。

guava学习--FutureFallback的更多相关文章

  1. Guava学习笔记目录

    Guava 是一个 Google 的基于java1.6的类库集合的扩展项目,包括 collections, caching, primitives support, concurrency libra ...

  2. guava 学习笔记 使用瓜娃(guava)的选择和预判断使代码变得简洁

    guava 学习笔记 使用瓜娃(guava)的选择和预判断使代码变得简洁 1,本文翻译自 http://eclipsesource.com/blogs/2012/06/06/cleaner-code- ...

  3. guava 学习笔记(二) 瓜娃(guava)的API快速熟悉使用

    guava 学习笔记(二) 瓜娃(guava)的API快速熟悉使用 1,大纲 让我们来熟悉瓜娃,并体验下它的一些API,分成如下几个部分: Introduction Guava Collection ...

  4. Guava学习

    Guava学习笔记目录 Guava 是一个 Google 的基于java1.6的类库集合的扩展项目,包括 collections, caching, primitives support, concu ...

  5. [置顶] Guava学习之ArrayListMultimap

    ArrayListMultimap类的继承关系如下图所示: Guava ArrayListMultimap List Multimap 是一个接口,继承自 Multimap 接口.ListMultim ...

  6. [置顶] Guava学习之Splitter

    Splitter:在Guava官方的解释为:Extracts non-overlapping substrings from an input string, typically by recogni ...

  7. [置顶] Guava学习之Iterators

    Iterators类提供了返回Iterator类型的对象或者对Iterator类型对象操作的方法.除了特别的说明,Iterators类中所有的方法都在Iterables类中有相应的基于Iterable ...

  8. [置顶] Guava学习之Lists

    Lists类主要提供了对List类的子类构造以及操作的静态方法.在Lists类中支持构造ArrayList.LinkedList以及newCopyOnWriteArrayList对象的方法.其中提供了 ...

  9. [置顶] Guava学习之Immutable集合

    Immutable中文意思就是不可变.那为什么需要构建一个不可变的对象?原因有以下几点: 在并发程序中,使用Immutable既保证线程安全性,也大大增强了并发时的效率(跟并发锁方式相比).尤其当一个 ...

随机推荐

  1. Eclipse创建maven项目

    许久不创建maven web项目了,今天上手很是陌生,搜集资料后终于创建成功,跟大家也分享一下,同时也便于以后再次忘记使用... 新建maven项目(右击new,若不存在,可在other里面寻找)

  2. apache代理服务器为nodejs服务设置域名

    本机以apache为主,其中 在httpd.conf中先设置 <VirtualHost *:80> ServerName nodejs.cc ServerAlias www.nodejs. ...

  3. JAVA中取余(%)规则和介绍

    在java中%的含义为取余. java :a%b 数学公式a%b=a-(a/b)*b

  4. 【线段树区间合并】HDU1540-Tunnel Warfare

    一.题目 Description During the War of Resistance Against Japan, tunnel warfare was carried out extensiv ...

  5. rewrite规则中参数多于9个的处理方式 apache nginx

    RewriteRule ^index-([0-9]+)-([0-9]+)-([0-9]+)-([0-9]+)-([0-9]+)-([0-9]+)-([0-9]+)-([0-9]+)(.*)$ $9&a ...

  6. for循环嵌套的优化

    public static void main(String[] args) {     int x = 0;     for (int i = 0; i < 2; i++) {         ...

  7. python之路3:

    class set(object): """ set() -> new empty set object set(iterable) -> new set o ...

  8. 微信touchmove不生效

    最近在写一个微信里面滑动切换图片的功能,发现在chrome下都正常显示,可是在微信和qq浏览器里面就是不行. 经过一番排查,发现了问题: touchmove只触发了一次. 解决方案: 在touchst ...

  9. Intent四个重要属性

    Intent四个重要属性   Intent作为联系各Activity之间的纽带,其作用并不仅仅只限于简单的数据传递.通过其自带的属性,其实可以方便的完成很多较为复杂的操作.例如直接调用拨号功能.直接自 ...

  10. Ember.js入门教程、博文汇总

    第一章 对象模型 Ember.js 入门指南——类的定义.初始化.继承 Ember.js 入门指南——类的扩展(reopen) Ember.js 入门指南——计算属性(compute properti ...