FluxTest.java:
import reactor.core.publisher.Flux;
import java.util.ArrayList;
import java.util.List;
public class FluxTest {
public static void main(String[] args) {
List<String> names = new ArrayList<>();
names.add( "张三" );
names.add( "李四" );
names.add( "王五" );
// 输出( id=111 ):张三
// 输出( id=111 ):李四
// 输出( id=111 ):王五
Flux<String> flux_name = Flux.fromIterable(names);
// printFlux( flux_name,true,"111" );
// 输出( id=222 ):hello,my name is 张三 !
// 输出( id=222 ):hello,my name is 李四 !
// 输出( id=222 ):hello,my name is 王五 !
Flux<String> flux_helloInfo = flux_name.map(name -> {
String helloInfo = "hello,my name is " + name + " !";
return helloInfo;
});
// printFlux( flux_helloInfo,true,"222" );
// 输出( id=333 ):张三_1
// 输出( id=333 ):张三_2
// 输出( id=333 ):张三_3
// 输出( id=333 ):李四_1
// 输出( id=333 ):李四_2
// 输出( id=333 ):李四_3
// 输出( id=333 ):王五_1
// 输出( id=333 ):王五_2
// 输出( id=333 ):王五_3
Flux<String> flux_name1 = flux_name.flatMap(name -> {
Flux<String> just = Flux.just(name + "_1", name + "_2", name + "_3");
return just;
});
// printFlux( flux_name1,true,"333" );
List<Integer> ids = new ArrayList<>();
for (int i = 0; i < 100; i++) {
ids.add( i + 1 );
}
// 输出( id=444 ):1
// 输出( id=444 ):2
// 输出( id=444 ):3
// 输出( id=444 ):4
// 输出( id=444 ):5
// ...
// 输出( id=444 ):96
// 输出( id=444 ):97
// 输出( id=444 ):98
// 输出( id=444 ):99
// 输出( id=444 ):100
Flux<Integer> flux_id = Flux.fromIterable(ids);
// printFlux( flux_id,true,"444" );
// 输出( id=555 ):[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
// 输出( id=555 ):[21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40]
// 输出( id=555 ):[41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60]
// 输出( id=555 ):[61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80]
// 输出( id=555 ):[81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100]
Flux<List<Integer>> flux_ids = flux_id.buffer(20);
printFlux( flux_ids,true,"555" );
}
private static <T> void printFlux(Flux<T> flux ,boolean relallyPrint,String id){
if( flux==null ){
return;
}
flux.subscribe( element->{
if( relallyPrint ){
System.out.println( "输出( id="+ id +" ):" + element );
}
} );
}
}