springboot webflux flux 使用示例(創建操作)
*************************
just、defer
public class Test {
public static void main(String[] args) throws Exception{
Flux.defer(() -> Flux.just("hello world "+System.currentTimeMillis()))
.delaySubscription(Duration.ofSeconds(2))
.subscribe(s -> {
System.out.println(s);
System.out.println("hello world "+System.currentTimeMillis());
});
Flux.just("hello world2 "+System.currentTimeMillis())
.delaySubscription(Duration.ofSeconds(2))
.subscribe(s -> {
System.out.println(s);
System.out.println("hello world2 "+System.currentTimeMillis());
});
TimeUnit.SECONDS.sleep(6);
}
}
********************
控制檯輸出
hello world 1593229538551
hello world 1593229538555
hello world2 1593229536538
hello world2 1593229538555
defer當訂閱的時候創建flux對象流,just立刻創建對象流