如何解决有没有办法与Hot Publisher并行运行多个订户-响应式Java
我正在尝试使用ractive核心库并行运行多个订户。
我尝试使用onSubscribe()和onPublish()方法,但仍按顺序执行。 创建通量后,它将启动新线程,但会运行所有订户并在单个线程中完成它们。 我希望多个订阅者并行运行,因为我认为数据已经发布给订阅者。
我使用的代码是:
package com.citi.dglobe.builder;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import reactor.core.publisher.Flux;
import reactor.core.scheduler.Schedulers;
public class Test {
private static Logger logger = LoggerFactory.getLogger(FluxShareTestApplication3.class);
public static void main(String[] args) throws InterruptedException,IOException {
//Reader
List<String> list1 = Arrays.asList("JAVA","SAMPLE");
Flux<String> seq = Flux.fromIterable(list1).subscribeOn(Schedulers.parallel()).publish().refCount(2); // refCount is the writerCount
//Writer. Could be more than 2
List<Integer> list = Arrays.asList(1,2);
list.stream().forEach((i) -> {
//First Writer
if(i==1) {
seq.subscribe(line -> {
try {
Thread.sleep(2000); // Thi ssleep is to check if the writer works in parallel
}
catch(InterruptedException e) {}
System.out.println("First Writer"+line);
logger.info("First Writer"+line);
});
}
//Second Writer
else {
seq.subscribe(line -> {
try {
Thread.sleep(2000); // This sleep is to check if the writer works in parallel
}
catch(InterruptedException e) {}
System.out.println("Second Writer"+line);
logger.info("Second Writer"+line);
});
}
}); // Closing list.stream.forEach
Thread.sleep(10000);
}
}
需要知道,如何为热门发布者并行运行多个订户?。
我得到的输出是:
18:11:36.390 [main] DEBUG reactor.util.Loggers$LoggerFactory - Using Slf4j logging framework
First WriterJAVA
18:11:38.560 [parallel-1] INFO com.citi.dglobe.builder.FluxShareTestApplication3 - First WriterJAVA
Second WriterJAVA
18:11:40.560 [parallel-1] INFO com.citi.dglobe.builder.FluxShareTestApplication3 - Second WriterJAVA
First WriterSAMPLE
18:11:42.561 [parallel-1] INFO com.citi.dglobe.builder.FluxShareTestApplication3 - First WriterSAMPLE
Second WriterSAMPLE
18:11:44.562 [parallel-1] INFO com.citi.dglobe.builder.FluxShareTestApplication3 - Second WriterSAMPLE
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。