com.spring.boxes.dollar.FluxString Maven / Gradle / Ivy
package com.spring.boxes.dollar;
import cn.hutool.core.util.StrUtil;
import lombok.NonNull;
import reactor.core.Disposable;
import reactor.core.publisher.Flux;
import java.time.Duration;
import java.util.Random;
import java.util.function.Consumer;
public class FluxString {
public static Flux streamDelay(String source) {
return streamDelay(source, 30);
}
public static Flux streamDelay(String source, int millisDelay) {
return streamDelay(source, 5, millisDelay);
}
public static Flux streamDelay(String source, int length, int millisDelay) {
String[] stringArray = StrUtil.split(source, length);
return Flux.just(stringArray).delayElements(Duration.ofMillis(millisDelay));
}
public static Disposable streamDelay(String source, @NonNull Consumer super String> consumer) {
return streamDelay(source, 30, consumer);
}
public static Disposable streamDelay(String source, @NonNull Consumer super String> consumer, @NonNull Consumer super Throwable> errorConsumer) {
return streamDelay(source, 30, consumer, errorConsumer);
}
public static Disposable streamDelay(String source, @NonNull Consumer super String> consumer, @NonNull Consumer super Throwable> errorConsumer, @NonNull Runnable completeConsumer) {
return streamDelay(source, 30, consumer, errorConsumer, completeConsumer);
}
public static Disposable streamDelay(String source, int millisDelay, @NonNull Consumer super String> consumer) {
return streamDelay(source, 5, millisDelay, consumer);
}
public static Disposable streamDelay(String source, int millisDelay, @NonNull Consumer super String> consumer, @NonNull Consumer super Throwable> errorConsumer) {
return streamDelay(source, 5, millisDelay, consumer, errorConsumer);
}
public static Disposable streamDelay(String source, int millisDelay, @NonNull Consumer super String> consumer, @NonNull Consumer super Throwable> errorConsumer, @NonNull Runnable completeConsumer) {
return streamDelay(source, 5, millisDelay, consumer, errorConsumer, completeConsumer);
}
public static Disposable streamDelay(String source, int length, int millisDelay, @NonNull Consumer super String> consumer) {
String[] stringArray = StrUtil.split(source, length);
return Flux.just(stringArray)
.delayElements(Duration.ofMillis(millisDelay))
.subscribe(consumer);
}
public static Disposable streamDelay(String source, int length, int millisDelay, @NonNull Consumer super String> consumer, @NonNull Consumer super Throwable> errorConsumer) {
String[] stringArray = StrUtil.split(source, length);
return Flux.just(stringArray)
.delayElements(Duration.ofMillis(millisDelay))
.subscribe(consumer, errorConsumer);
}
public static Disposable streamDelay(String source, int length, int millisDelay, @NonNull Consumer super String> consumer, @NonNull Consumer super Throwable> errorConsumer, @NonNull Runnable completeConsumer) {
String[] stringArray = StrUtil.split(source, length);
return Flux.just(stringArray)
.delayElements(Duration.ofMillis(millisDelay))
.subscribe(consumer, errorConsumer, completeConsumer);
}
public static void main(String[] args) throws Exception {
String source = "这是一段流式输出的测试代码,实现没间隔固定毫秒时长,输出特定长度内容的效果。";
Random random = new Random();
FluxString.streamDelay(source, random.nextInt(3) + 1, 50, System.out::print, error -> System.err.println("Error: " + error), () -> System.out.println("Completed!"));
// Adjust the sleep time to allow all elements to be printed
Thread.sleep(5000);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy