read text file in reactive way
在 Java8 之前,使用 Java,我们可以按字节、按字符、按行来读取文本文件。
Java8 提供了 Files.lines() 方法,可以让我们以 stream 的方式读取,stream 是延迟读取的。
stream 风格之后,现在我们可以更进一步,以 reactive 的方式读取文件。Flux.fromStream 提供了方便的接口,直接把 stream 转为 flux。考虑到对文件这种资源使用之后都要有 close 这种操作,那么直接使用 Flux.using 就再合适不过了,免去了手动释放资源这种模版式的代码。
private static Flux
return Flux.using(() -> Files.lines(path),
Flux::fromStream,
BaseStream::close
);
}
Reference:
https://simonbasle.github.io/2017/10/file-reading-in-reactor/
这篇文章里有关于的 window 各种用法的解释:https://blog.51cto.com/liukang/2094073。
通过 Jackson 流式解析 Json:https://cassiomolin.com/2019/08/19/combining-jackson-streaming-api-with-objectmapper-for-parsing-json/
Flux 的 create 和 generate 的区别。
Mokito https://semaphoreci.com/community/tutorials/stubbing-and-mocking-with-mockito-2-and-junit