在Completablefuture和Observable之间转换

Java 8中的CompletableFuture<T>是对T类型的值将来将可用的承诺的高级抽象。 Observable<T>非常相似,但是它承诺将来会出现任意数量的项,从0到无穷大。 异步结果的这两种表示与仅使用一项即可使用Observable而不是CompletableFuture情况非常相似,反之亦然。 另一方面, CompletableFuture更专业,并且由于它现在是JDK的一部分,因此应该很快就会流行起来。 让我们用简短的文章来庆祝RxJava 1.0发行版,该文章展示了如何在两者之间进行转换而又不失去它们的异步和事件驱动性质。

CompletableFuture表示将来的一个值,因此将其变为Observable非常简单。 当Future以某个值完成时, Observable也将立即发出该值并关闭流:

class FuturesTest extends Specification {public static final String MSG = "Don't panic"def 'should convert completed Future to completed Observable'() {given:CompletableFuture<String> future = CompletableFuture.completedFuture("Abc")when:Observable<String> observable = Futures.toObservable(future)then:observable.toBlocking().toIterable().toList() == ["Abc"]}def 'should convert failed Future into Observable with failure'() {given:CompletableFuture<String> future = failedFuture(new IllegalStateException(MSG))when:Observable<String> observable = Futures.toObservable(future)then:observable.onErrorReturn({ th -> th.message } as Func1).toBlocking().toIterable().toList() == [MSG]}  CompletableFuture failedFuture(Exception error) {CompletableFuture future = new CompletableFuture()future.completeExceptionally(error)return future}}

尚未执行的 Futures.toObservable()第一个测试会将Future转换为Observable ,并确保正确传播值。 第二个测试创建了失败的Future ,将失败替换为异常的消息,并确保传播了异常。 实现要短得多:

public static <T> Observable<T> toObservable(CompletableFuture<T> future) {return Observable.create(subscriber ->future.whenComplete((result, error) -> {if (error != null) {subscriber.onError(error);} else {subscriber.onNext(result);subscriber.onCompleted();}}));
}

注意: Observable.fromFuture()存在,但是我们想充分利用ComplatableFuture的异步运算符。

实际上,有两种将Observable转换为Future -创建CompletableFuture<List<T>>CompletableFuture<T> (如果我们假设Observable仅包含一项)。 让我们从前一种情况开始,用以下测试用例进行描述:

def 'should convert Observable with many items to Future of list'() {given:Observable<Integer> observable = Observable>just(1, 2, 3)when:CompletableFuture<List<Integer>> future = Futures>fromObservable(observable)then:future>get() == [1, 2, 3]
}def 'should return failed Future when after few items exception was emitted'() {given:Observable<Integer> observable = Observable>just(1, 2, 3)>concatWith(Observable>error(new IllegalStateException(MSG)))when:Futures>fromObservable(observable)then:def e = thrown(Exception)e>message == MSG
}

显然,直到源Observable信号流结束, Future才完成。 因此, Observable.never()将永远不会完成包装Future ,而是使用空列表来完成它。 该实现更短,更甜蜜:

public static <T> CompletableFuture<List<T>> fromObservable(Observable<T> observable) {final CompletableFuture<List<T>> future = new CompletableFuture<>();observable.doOnError(future::completeExceptionally).toList().forEach(future::complete);return future;
}

关键是Observable.toList() ,它可以方便地从Observable<T>Observable<List<T>> 。 当源Observable<T>完成时,后者将发出List<T>类型的一项。

当我们知道CompletableFuture<T>将恰好返回一项时,就会发生上一次转换的特殊情况。 在这种情况下,我们可以将其直接转换为CompletableFuture<T> ,而不是仅包含一项的CompletableFuture<List<T>> 。 首先测试:

def 'should convert Observable with single item to Future'() {given:Observable<Integer> observable = Observable.just(1)when:CompletableFuture<Integer> future = Futures.fromSingleObservable(observable)then:future.get() == 1
}def 'should create failed Future when Observable fails'() {given:Observable<String> observable = Observable.<String> error(new IllegalStateException(MSG))when:Futures.fromSingleObservable(observable)then:def e = thrown(Exception)e.message == MSG
}def 'should fail when single Observable produces too many items'() {given:Observable<Integer> observable = Observable.just(1, 2)when:Futures.fromSingleObservable(observable)then:def e = thrown(Exception)e.message.contains("too many elements")
}

同样,实现非常简单并且几乎相同:

public static <T> CompletableFuture<T> fromSingleObservable(Observable<T> observable) {final CompletableFuture<T> future = new CompletableFuture<>();observable.doOnError(future::completeExceptionally).single().forEach(future::complete);return future;
}

上面的Helper方法还不够完善,但是,如果您需要在JDK 8和RxJava风格的异步计算之间进行转换,那么这篇文章应该足以帮助您入门。

翻译自: https://www.javacodegeeks.com/2014/12/converting-between-completablefuture-and-observable.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/361383.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Box 'laravel/homestead' could not be found.

vbox vagrant.box Homestead 都安裝好的情況下。在homestead目錄下&#xff0c;執行vagrant up出錯 Homestead\scripts\文件夹中&#xff0c;打开homestead.rb文件打开homestead.rb文件 把config.vm.box_version settings["version"] || "> 0.4.0"改为…

DOM操作案例之--全选与反选

全选与反选在表单类的项目中还是很常见的&#xff0c;电商项目中的购物车一定少不了这个功能。 下面我只就用一个简单的案例做个演示吧。 <div class"wrap"><table><thead><tr><th><input type"checkbox" id"j_cbA…

沈阳出差归来

两个礼拜的出差终于结束了&#xff0c;现场开发锦江之星晚上熬夜到2点...我们开发着并快乐着&#xff0c;至少有一点是出差最大的收获&#xff0c;如何在高强度的项管中把握每个人的内心活动&#xff0c;在最及时的时候给与最贴心的帮助&#xff0c;以至于能让开发人员得到满足…

带有Swagger的Spring Rest API –公开文档

创建API文档后&#xff0c;将其提供给涉众非常重要。 在理想情况下&#xff0c;此发布的文档将具有足够的灵活性以解决任何最新更改&#xff0c;并且易于分发&#xff08;就成本以及完成此操作所需的时间而言&#xff09;。 为了使这成为可能&#xff0c;我们将利用我在上一篇文…

Laravel 不同环境加载不同的.env文件

假设有4个.env文件。分别为 .env.local .env.dev .env.test .env.prd 方式一 第一步&#xff1a;bootstrap\app.php 加入代码 $envs [local,dev,test,prd]; $env get_cfg_var(env); if(empty($env) || !in_array($env, $envs)){exit(no env files); } $env .env..$env; $app…

伪类 选择器优先级

:link 匹配所有未被点击的链接&#xff1b;a:link{color:red} :visited 匹配所有被点击的链接&#xff1b;a:visited{color:red} :hover 匹配鼠标悬停其上的E元素 a:hover{text-decoration:underline} :active 匹配鼠标已经其上按下&#xff0c;还没有释放的E元素 a:active{…

hinkphp项目部署到Linux服务器上报错“模板不存在”如何解决

检查了服务器上的文件&#xff0c;并没有缺少文件&#xff0c;再次上传文件到服务器&#xff0c;还是报错。莫名其妙&#xff0c;怀疑是代码问题。 仔细检查后&#xff0c;发现是模板的文件名问题&#xff1a; 用过TP的都知道&#xff1a;thinkphp会在$this->display()的时候…

VS2008调试总结

从文件Form1.cs和文件Form1.Designer.cs中&#xff0c;可以看到它们定义了同一个类Form1&#xff0c;并且都有一个关键字partial。该关键字作用是允许类分开定义&#xff0c;并且可以将类定义放在不同的文件里面&#xff08;命名空间必须相同&#xff09;。 总结上述三个文件&a…

使用FizzBu​​zz和JUnitParams进行单元测试

我有时使用FizzBu​​zz向新手演示单元测试的基础。 尽管FizzBu​​zz确实是一个简单的问题&#xff0c;但它也可以用于演示更高级的单元测试技术&#xff0c;例如实现参数化测试。 FizzBu​​zz的可能解决方案之一是&#xff1a; public class FizzBuzz {private static fin…

Elements in iteration expect to have v-bind:key directives错误的解决办法

一、错误如下[eslint-plugin-vue][vue/require-v-for-key]Elements in iteration expect to have v-bind:key directives.Renders the element or template block multiple times based on the source data. 使用VS Code 出现如下问题&#xff0c;如图 二、解决 在用vscode编写…

统计年鉴 付费下载 夏泽网

夏泽网链接 转载于:https://www.cnblogs.com/camelroyu/p/11597533.html

浏览文件按钮

private void btnSelect2_Click(object sender, EventArgs e){string path GetPath();if (!string.IsNullOrEmpty(path)){tbText2.Text path;}}private string GetPath(){OpenFileDialog fileDialog new OpenFileDialog();fileDialog.Title "选择文件";fileDialo…

无法使用JDK 8卸载JavaFX SceneBuilder 1.0

我最近从旧的基于Vista的笔记本电脑中删除了一些我曾经使用过的软件开发应用程序&#xff0c;工具和文件&#xff0c;因为主要使用该笔记本电脑的人们现在对软件开发不再感兴趣。 作为该工作的一部分&#xff0c;我尝试删除了几年前在该笔记本电脑上安装的JavaFX Scene Builder…

分享一个不错的表格样式

先贴个HTML生成的源码出来&#xff1a; <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns"http://www.w3.org/1999/xhtml"> <head>…

微信小程序如何检测接收iBeacon信号

前话 微信小程序开发带着许多坑&#xff0c;最近就遇到了个需求&#xff0c;检测iBeacon来进行地点签到。 (╯▔皿▔)╯ 微信小程序对于iBeacon的文档也写的十分精简&#xff0c;只简单介绍了每个接口的作用&#xff0c;这就导致我以为简单调用单个接口即可实现功能&#xf…

centos7.3 docker安装grafana

一、编写docker-cmpose文件 docker-compose.yml文件如下&#xff1a; 1 version: "3.3"2 services:3 grafana:4 image: grafana/grafana:latest5 environment:6 GF_SECURITY_ADMIN_PASSWORD: aibili_grafana7 ports:8 - 3000:30009 us…

如何将SQL GROUP BY和聚合转换为Java 8

我无法抗拒。 我已经阅读了Hugo Prudente在Stack Overflow上提出的问题 。 而且我知道必须有比JDK提供的更好的方法。 问题如下&#xff1a; 我正在寻找一个lambda来优化已检索的数据。 我有一个原始的结果集&#xff0c;如果用户不更改我想要的日期&#xff0c;则使用Java的…

类库,随机数,属性,值类型和引用类型

类库&#xff0c;随机数&#xff0c;属性&#xff0c;值类型和引用类型 1.文档注释与类库的生成 (1)类库的生成 右击添加新建项&#xff0c;建立类库&#xff08;DLL&#xff09;。 (2)导入类库 在项目中建立文件夹DLL&#xff0c;直接把外部DLL和XML文件拖入文件夹&#xff0c…

input获得焦点和失去焦点

总结&#xff1a;placeholder因为在IE7 8 9 浏览器不支持所以没用它效果&#xff1a;当input获取光标的时候如果是默认提示则input内容为空。如果不是则为输入内容 当失去光标的时候&#xff0c;如果input内容为空&#xff0c;则恢复默认提示&#xff0c;如果不是则为…

带有DIY的Openshift上的Spring Boot / Java 8 / Tomcat 8

DIY墨盒是一种实验性墨盒&#xff0c;它提供了一种在OpenShift上测试不受支持的语言的方法。 它提供了最小限度的自由形式的支架&#xff0c;将墨盒的所有细节留给了应用程序开发人员 。 这篇博客文章说明了结合了PostgreSQL服务的Spring Boot / Java 8 / Tomcat 8应用程序的使…