我有一个用例,需要从监听器创建一个冷发布器。我已使用publish和autoConnect方法来创建ConnectableFlux并在第一个订阅者上启动发布者。我还使用了publishOn在不同线程上运行使用者。我与不同的使用者订阅了两次,以不同的速度使用数据。 一旦我订阅了第二次的问题,即第二个用户的速度很慢,它就会使第一个用户的节奏变慢。
package test;
import reactor.core.publisher.Flux;
import reactor.core.scheduler.Schedulers;
public class ProblemSample {
public static void main(String[] args) {
EventProcessor myEventProcessor = new EventProcessor();
Flux<String> flux = Flux.<String>create(sink -> {
MyEventListener<String> myEventListener = new MyEventListener<String>() {
public void onDataChunk(String chunk) {
sink.next(chunk);
System.out.println("onnext request " + Thread.currentThread().getName());
}
public void processComplete() {
sink.complete();
}
};
myEventProcessor.register(myEventListener);
}).publish().autoConnect().publishOn(Schedulers.newParallel("reactor"), 1);
flux.subscribe((v) -> {
System.out.println("1111111 having received " + Thread.currentThread().getName() + " value : " + v);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
});
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("------------------subscribe second main---------------------");
flux.subscribe((v) -> {
System.out.println("22222222 having received " + Thread.currentThread().getName() + " value : " + v);
try {
Thread.sleep(6000);
} catch (InterruptedException e) {
e.printStackTrace();
}
});
System.out.println("main thread ended");
}
static class EventProcessor {
public void register(MyEventListener<String> myEventListener) {
for (int i = 0; i < 10; i++) {
myEventListener.onDataChunk("chunk " + i);
}
myEventListener.processComplete();
}
}
static interface MyEventListener<T> {
public void onDataChunk(String chunk);
public void processComplete();
}
}
我希望两个使用者都可以使用自己的节奏来消费数据,而不会互相影响。