我正在使用 RxJS 学习反应式编程,并遇到需要逐行读取文件的情况。实际上我使用类似的解决方案解决了它:
https://gist.github.com/yvele/447555b1c5060952a279
它可以工作,但我需要使用一些普通的 JS 代码将缓冲区流转换为行流。(在上面的示例中使用“readline”模块)
我想知道是否还有其他方法可以使用 RxJS 运算符将 Buffer 的 Observable 转换为 Line 的 Observable ,如下例所示。
var Rx = require('rx');
var fs = require('fs');
var lines = Rx.Observable
.fromEvent(rl, 'data') // emits buffers overtime
// some transforms ...
.subscribe(
(line) => console.log(line), // emit string line by line
err => console.log("Error: %s", err),
() => console.log("Completed")
);
Run Code Online (Sandbox Code Playgroud) 我有两个对象流,帐户和余额。
我需要合并(连接)两个流根据id和account_id
var accounts = Rx.Observable.from([
{ id: 1, name: 'account 1' },
{ id: 2, name: 'account 2' },
{ id: 3, name: 'account 3' },
]);
var balances = Rx.Observable.from([
{ account_id: 1, balance: 100 },
{ account_id: 2, balance: 200 },
{ account_id: 3, balance: 300 },
]);
Run Code Online (Sandbox Code Playgroud)
什么是预期:
var results = [
{ id: 1, name: 'account 1', balance: 100},
{ id: 2, name: 'account 2', balance: 200},
{ id: 3, name: 'account …Run Code Online (Sandbox Code Playgroud) 基本上,我想观察一个事件,当一个闪亮的仪表板选项卡的绘图完成渲染时,该事件将触发。其用途是隐藏覆盖的加载页面元素。
我目前使用这个:
observeEvent("plot", hide(id = "loading-content", anim = TRUE, animType = "fade"))
Run Code Online (Sandbox Code Playgroud)
但是,当我观察绘图时,加载页面的隐藏会在计算准备就绪时触发,而不是在渲染绘图时触发。这尤其困扰着我目前正在开发的多选项卡仪表板。
是否有一个偶数,我可以观察到,当选项卡中的绘图完全渲染时,它将触发?就像网络浏览器告诉 R“嘿,我完成了绘图渲染!”
假设我有以下链:
public Mono<B> someMethod( Object arg ) {
Mono<A> monoA = Mono.just( arg ).flatMap( adapter1::doSomething );
// success chain
return monoA.map( B::buildSuccessResponse );
}
Run Code Online (Sandbox Code Playgroud)
但是如果出现错误,我需要返回不同类型的 B,比如说,B.buildErrorResponse( a ). 任何 onError / doOnError 方法只能返回原始 Mono 类型 (A),但不能返回我需要返回的类型 (B)。出现错误时如何返回不同的 Mono 类型?
我正在使用 Spring WebFlux WebClient 从外部 API 检索数据,如下所示:
public WeatherWebClient() {
this.weatherWebClient = WebClient.create("http://api.openweathermap.org/data/2.5/weather");
}
public Mono<String> getWeatherByCityName(String cityName) {
return weatherWebClient
.get()
.uri(uriBuilder -> uriBuilder
.queryParam("q", cityName)
.queryParam("units", "metric")
.queryParam("appid", API_KEY)
.build())
.accept(MediaType.APPLICATION_JSON)
.retrieve()
.bodyToMono(String.class);
}
Run Code Online (Sandbox Code Playgroud)
这工作正常并产生如下响应:
{
"coord":{
"lon":-47.06,
"lat":-22.91
},
"weather":[
{
"id":800,
"main":"Clear",
"description":"clear sky",
"icon":"01d"
}
],
"base":"stations",
"main":{
"temp":16,
"pressure":1020,
"humidity":67,
"temp_min":16,
"temp_max":16
},
"visibility":10000,
"wind":{
"speed":1,
"deg":90
},
"clouds":{
"all":0
},
"dt":1527937200,
"sys":{
"type":1,
"id":4521,
"message":0.0038,
"country":"BR",
"sunrise":1527932532,
"sunset":1527971422
},
"id":3467865,
"name":"Campinas",
"cod":200
} …Run Code Online (Sandbox Code Playgroud) functional-programming reactive-programming jackson project-reactor spring-webflux
对反应式编程感兴趣,我玩了一点构建反应式 RESTful Web 服务指南。并想向前移动并添加一些单元测试。
我试图RouterFunction用普通的 Junit/Mockito 测试来测试我的处理程序 ( )。但是,因为它是响应式的,处理程序返回一个Mono<ServerResponse>. 所以我不得不用block()它来测试ServerResponse状态,但无法提取他的身体来测试它。在网上搜索解决方案时,似乎所有示例都使用WebTestClient.
我的问题是:
鉴于所有样本都WebTestClient用于测试反应式 REST 服务,并且(单元)测试ServerResponse. 对 a 进行单元测试是一种很好的做法,RouterFunction还是始终使用 进行更广泛的测试更好WebTestClient?
非常感谢。
java unit-testing reactive-programming spring-boot spring-webflux
我正在 Tomcat 上使用 Spring WebFlux 进行 POC。该堆栈是完全反应式的,并使用 Spring Reactor 和 Reactive Couchbase。
当我在系统上添加负载时,我看到 JVisualVM 中的线程数增加到 200+。基本上,reactor-http线程是有限的,如预期的那样大约有 4 个。
但是http-nio-exec线程跳到了 200 左右。如果我用 Netty 而不是 Tomcat 运行,线程数只限制在 30 左右。
有人能解释一下为什么 Tomcat 用反应式堆栈来启动这么多线程吗?
我有一个
list.stream()
.map(element -> func());
Run Code Online (Sandbox Code Playgroud)
其中 func() 以 Mono 返回。将此Mono<CustomObject>对象列表转换为Flux<CustomObject>流并从流返回它的最简单方法是什么?
通过使用 Blockhound io.projectreactor.tools blockhound-junit-platform我发现 UUID.randomUUID 是一个阻塞调用,这对我们来说是一个问题,因为我们使用的是 Spring Boot Webflux 版本 2.2.2.RELEASE
有没有其他方法可以以非阻塞方式获取随机 uuid,或者是否有任何其他 Java 库推荐用于非阻塞生成随机字符串。
来自blockhound的堆栈跟踪:
java.lang.Error: Blocking call! java.io.FileInputStream#readBytes
at reactor.blockhound.BlockHound$Builder.lambda$new$0(BlockHound.java:196) ~[blockhound-1.0.1.RELEASE.jar:na]
Suppressed: reactor.core.publisher.FluxOnAssembly$OnAssemblyException:
Error has been observed at the following site(s):
|_ checkpoint ? org.springframework.boot.actuate.metrics.web.reactive.server.MetricsWebFilter [DefaultWebFilterChain][ExceptionHandlingWebHandler]
Stack trace:
at reactor.blockhound.BlockHound$Builder.lambda$new$0(BlockHound.java:196) ~[blockhound-1.0.1.RELEASE.jar:na]
at reactor.blockhound.BlockHound$Builder.lambda$install$6(BlockHound.java:318) ~[blockhound-1.0.1.RELEASE.jar:na]
at reactor.blockhound.BlockHoundRuntime.checkBlocking(BlockHoundRuntime.java:46) ~[na:na]
at java.base/java.io.FileInputStream.readBytes(FileInputStream.java) ~[na:na]
at java.base/java.io.FileInputStream.read(FileInputStream.java:279) ~[na:na]
at java.base/java.io.FilterInputStream.read(FilterInputStream.java:133) ~[na:na]
at java.base/sun.security.provider.NativePRNG$RandomIO.readFully(NativePRNG.java:424) ~[na:na]
at java.base/sun.security.provider.NativePRNG$RandomIO.ensureBufferValid(NativePRNG.java:526) ~[na:na]
at java.base/sun.security.provider.NativePRNG$RandomIO.implNextBytes(NativePRNG.java:545) ~[na:na]
at java.base/sun.security.provider.NativePRNG.engineNextBytes(NativePRNG.java:220) ~[na:na]
at java.base/java.security.SecureRandom.nextBytes(SecureRandom.java:741) ~[na:na]
at java.base/java.util.UUID.randomUUID(UUID.java:150) ~[na:na]
Run Code Online (Sandbox Code Playgroud) java uuid reactive-programming project-reactor spring-webflux
我有一个异步调用节俭接口:
public CompletableFuture<List<Long>> getFavourites(Long userId){
CompletableFuture<List<Long>> future = new CompletableFuture();
OctoThriftCallback callback = new OctoThriftCallback(thriftExecutor);
callback.addObserver(new OctoObserver() {
@Override
public void onSuccess(Object o) {
future.complete((List<Long>) o);
}
@Override
public void onFailure(Throwable throwable) {
future.completeExceptionally(throwable);
}
});
try {
recommendAsyncService.getFavorites(userId, callback);
} catch (TException e) {
log.error("OctoCall RecommendAsyncService.getFavorites", e);
}
return future;
}
Run Code Online (Sandbox Code Playgroud)
现在它返回一个 CompletableFuture<List>。然后我用 Flux 调用它来做一些处理器。
public Flux<Product> getRecommend(Long userId) throws InterruptedException, ExecutionException, TimeoutException {
// do not like it
List<Long> recommendList = wrapper.getRecommend(userId).get(2, TimeUnit.SECONDS);
System.out.println(recommendList);
return Flux.fromIterable(recommendList)
.flatMap(id -> …Run Code Online (Sandbox Code Playgroud) java ×5
javascript ×2
rxjs ×2
jackson ×1
node.js ×1
plot ×1
r ×1
rxjs5 ×1
shiny ×1
spring ×1
spring-boot ×1
tomcat ×1
unit-testing ×1
uuid ×1