gpt4 book ai didi

java - react 堆核心 - java.lang.IllegalStateException : Queue full? !在 Hot Publisher (ConnectableFlux) 上

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:23:01 24 4
gpt4 key购买 nike

到目前为止,我一直在使用 RxJava,但我开始使用来自 projectreactor.io 的 reactor-core,因为它符合 react 流规范。

在下面的测试中,我创建了一个生成随机数的热通量 (ConnectableFlux)。我立即连接()它并预取 256 个值(我可以在日志中看到 258 个值)。我等待 5 秒来模拟订阅者直到一段时间后才会订阅。

主线程唤醒后,RnApp 订阅 ConnectableFlux,randomNumberGenerator.subscribe(new RnApp());。然后调用 RnApp.onSubscribe() 并请求 10 个元素。之后,引发了一个java.lang.IllegalStateException: Queue full?!异常(调用了RnApp.onError()),为什么?

订户:

public class RnApp implements Subscriber<Float>{

private Subscription subscription;
private List<Float> randomNumbers = new ArrayList<Float>();

@Override
public void onComplete() {
System.out.println("onComplete");
}

@Override
public void onError(Throwable err) {
err.printStackTrace();
}

@Override
public void onNext(Float f) {
if(this.randomNumbers.size()>=10){
this.subscription.cancel();
}else{
this.randomNumbers.add(f);
}
}

@Override
public void onSubscribe(Subscription subs) {
this.subscription = subs;
this.subscription.request(10);
}
}

发布者测试:

@Test
public void randomNumberReading() throws InterruptedException {

CountDownLatch latch = new CountDownLatch(1);
ConnectableFlux<Float> randomNumberGenerator = ConnectableFlux.<Float>create( (c) -> {
SecureRandom sr = new SecureRandom();
int i = 1;
while(true){
try {
Thread.sleep(10);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("-----------------------------------------------------"+(i++));
c.onNext(sr.nextFloat());
}
}).log().subscribeOn(Computations.concurrent()).publish();

randomNumberGenerator.connect();

Thread.sleep(5000);

randomNumberGenerator.subscribe(new RnApp());

latch.await();
}

日志:

11:12:05.125 [main] DEBUG r.core.util.Logger$LoggerFactory - Using Slf4j logging framework
11:12:05.363 [concurrent-1] INFO reactor.core.publisher.FluxLog - onSubscribe(io.pivotal.literx.Part10SubscribeOnPublishOn$$Lambda$1/1586600255@29d4caeb)
11:12:05.371 [concurrent-1] INFO reactor.core.publisher.FluxLog - request(256)
-----------------------------------------------------1
11:12:06.000 [concurrent-1] INFO reactor.core.publisher.FluxLog - onNext(0.39189225)
-----------------------------------------------------2
...
-----------------------------------------------------257
11:12:08.683 [concurrent-1] INFO reactor.core.publisher.FluxLog - onNext(0.34729618)
-----------------------------------------------------258
11:12:08.697 [concurrent-1] INFO reactor.core.publisher.FluxLog - onNext(0.7729547)
java.lang.IllegalStateException: Queue full?!
at reactor.core.publisher.FluxPublish$State.onNext(FluxPublish.java:246)
at reactor.core.publisher.FluxSubscribeOn$SubscribeOnPipeline.onNext(FluxSubscribeOn.java:134)
at reactor.core.publisher.FluxLog$LoggerBarrier.doNext(FluxLog.java:130)
at reactor.core.subscriber.SubscriberBarrier.onNext(SubscriberBarrier.java:85)
at reactor.core.subscriber.SubscriberWithContext.onNext(SubscriberWithContext.java:92)
at io.pivotal.literx.Part10SubscribeOnPublishOn.lambda$1(Part10SubscribeOnPublishOn.java:132)
at reactor.core.publisher.FluxGenerate$ForEachBiConsumer.accept(FluxGenerate.java:145)
at reactor.core.publisher.FluxGenerate$ForEachBiConsumer.accept(FluxGenerate.java:114)
at reactor.core.publisher.FluxGenerate$SubscriberProxy.request(FluxGenerate.java:245)
at reactor.core.subscriber.SubscriberBarrier.doRequest(SubscriberBarrier.java:146)
at reactor.core.publisher.FluxLog$LoggerBarrier.doRequest(FluxLog.java:160)
at reactor.core.subscriber.SubscriberBarrier.request(SubscriberBarrier.java:135)
at reactor.core.util.DeferredSubscription.set(DeferredSubscription.java:71)
at reactor.core.publisher.FluxSubscribeOn$SubscribeOnPipeline.onSubscribe(FluxSubscribeOn.java:129)
at reactor.core.publisher.FluxLog$LoggerBarrier.doOnSubscribe(FluxLog.java:122)
at reactor.core.subscriber.SubscriberBarrier.onSubscribe(SubscriberBarrier.java:67)
at reactor.core.publisher.FluxGenerate.subscribe(FluxGenerate.java:72)
at reactor.core.publisher.FluxLog.subscribe(FluxLog.java:67)
at reactor.core.publisher.FluxSubscribeOn$SourceSubscribeTask.run(FluxSubscribeOn.java:363)
at reactor.core.publisher.Computations$ProcessorWorker.onNext(Computations.java:919)
at reactor.core.publisher.Computations$ProcessorWorker.onNext(Computations.java:883)
at reactor.core.publisher.WorkQueueProcessor$QueueSubscriberLoop.run(WorkQueueProcessor.java:842)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)

最佳答案

与 RxJava 一样,如果您使用 create(),您将自行处理取消和背压。您可以改为从标准运算符构建生成器:

ConnectableFlux<Double> secureRandomFlux = Flux.using(
() -> new SecureRandom(),
sr -> Flux.interval(10, TimeUnit.MILLISECONDS)
.map(v -> sr.nextDouble())
.onBackpressureDrop()
sr -> { }
).publish();

关于java - react 堆核心 - java.lang.IllegalStateException : Queue full? !在 Hot Publisher (ConnectableFlux) 上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37000396/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com