Project Reactor 的 flatMap 中关于线程的混淆

我正在玩弄 Project Reactor 和反应式 MongoDB 存储库。我有以下代码:


@Builder

@FieldDefaults(level = AccessLevel.PRIVATE)

@Getter

@Setter

@AllArgsConstructor

@NoArgsConstructor

@ToString

@Document

public class Person {

    @Id

    Integer id;

    String name;

}

public interface ReactivePersonRepository extends ReactiveCrudRepository<Person, Integer> {

}

和主要@SpringBootApplication课程:


@SpringBootApplication

@EnableReactiveMongoRepositories

@RequiredArgsConstructor

public class ReactiveDatabaseApplication {


    private final ReactivePersonRepository reactivePersonRepository;


    public static void main(String[] args) {

        SpringApplication.run(ReactiveDatabaseApplication.class, args);

    }


    @PostConstruct

    public void postConstruct() {

        Scheduler single = Schedulers.newSingle("single-scheduler");

        IntStream.range(0, 10).forEach(i ->

                Flux.just(Person.builder()

                        .id(i)

                        .name("PersonName")

                        .build())

                        .flatMap(personToSave -> {

                            System.out.println(String.format(

                                    "Saving person from thread %s", Thread.currentThread().getName()));

                            return reactivePersonRepository.save(personToSave);

                        })

                        })

    }

}

方法Flux::subscribeOn描述说:


因此,将此运算符放置在链中的任何位置也会影响执行 * onNext/onError/onComplete 信号的上下文,从链的开头到 * 下一次出现 {@link publishOn(Scheduler) publishOn}


这让我有点困惑,因为当我在处理链中没有任何publishOn指定时,线程名称的打印值是:


从线程 single-scheduler-1 中拯救人员 - 正如预期的那样

我不明白为什么。方法中指定的调度程序不应该subscribeOn用于每次flatMap执行吗?


当我取消注释publishOn行时,一切都由给定的单个调度程序执行,这是预期的。


谁能解释为什么操作不使用单个调度程序flatMap,而没有publishOn?


哔哔one
浏览 114回答 1
1回答

慕的地8271018

这个人为的例子可能会更清楚:Scheduler single = Schedulers.newSingle("single-scheduler");Flux.just("Bob")&nbsp; &nbsp; &nbsp; &nbsp; .flatMap(x -> {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(String.format(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Saving person from thread %s", Thread.currentThread().getName()));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return Mono.just(x).publishOn(Schedulers.elastic());&nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; &nbsp; &nbsp; .flatMap(x -> {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(String.format(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Finding person from thread %s", Thread.currentThread().getName()));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return Mono.just(x).publishOn(Schedulers.elastic());&nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; &nbsp; &nbsp; .flatMap(x -> {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(String.format(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Deleting person from thread %s", Thread.currentThread().getName()));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return Mono.just(x).publishOn(Schedulers.elastic());&nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; &nbsp; &nbsp; .subscribeOn(single)&nbsp; &nbsp; &nbsp; &nbsp; .subscribe(aVoid -> System.out.println(String.format(&nbsp; &nbsp; &nbsp; &nbsp; "Subscription from thread %s", Thread.currentThread().getName())));这将给出类似的东西:Saving person from thread single-scheduler-1Finding person from thread elastic-2Deleting person from thread elastic-3Subscription from thread elastic-4或者,换句话说,您的反应式存储库没有在同一个调度程序上发布,这就是您看到您所做的行为的原因。“Up until the next occurrence of publishOn()”并不意味着下次您的代码调用publishOn()- 它也可以在您的任何调用中的任何发布者中flatMap(),您将无法控制。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java