Spring Webflux 抛出一个“block()/blockFirst()/blockLast() 正在阻塞,这在线程 reactor-http-nio-2 中不支持"; [英] Spring Webflux throws a "block()/blockFirst()/blockLast() are blocking, which is not supported in thread reactor-http-nio-2"

查看:2938
本文介绍了Spring Webflux 抛出一个“block()/blockFirst()/blockLast() 正在阻塞,这在线程 reactor-http-nio-2 中不支持";的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Spring Webflux 中执行阻塞操作时遇到了一个小问题.我检索了文章文档列表,并从文章文档列表中更新了另一个对象.

I have a small issue with doing a blocking operation in Spring Webflux. I retrieve a list of article documents and from the list of article documents, i would like to update another object.

当我执行以下内容时,有时它会起作用,有时它会抛出block()/blockFirst()/blockLast() 正在阻塞,这在线程 reactor-http-nio-2 中不受支持".你能否建议如何修复.我真的不想让它阻塞,但不知道如何继续.stackoverflow 中有类似的线程,但不符合我的要求.

When i execute the below, sometimes it works and sometimes it throws a "block()/blockFirst()/blockLast() are blocking, which is not supported in thread reactor-http-nio-2". Could you please suggest how to fix. I dont really want to make it blocking but not sure how to proceed. There are similar threads in stackoverflow but not with respective to my requirement.

如果有人可以提出一种解决方法,那真是太好了?

It would be really nice if someone could suggest a way to work around ?

private OrderInfo setPrices(final OrderInfo orderInfo) {
    final List<ArticleDocument> articleDocuments = getArticleDocuments(orderInfo).block(); // Problematic line
    for (ArticleDocument article : articleDocuments) {
         //Update orderInfo based on one of the article price and few more condition.
  }
 return orderInfo;
}

private Mono<List<ArticleDocument>> getArticleDocuments(final OrderInfo orderInfo) {
    return this.articleRepository.findByArticleName(orderInfo.getArticleName()).collectList();
}

推荐答案

它必须是这样的.请注意,我尚未在我的 IDE 上对其进行测试.要修改任何东西,请评论并一起弄清楚.

It has to be something like this. Please take note that I have not tested it on my IDE. To modify anything please comment and figure it out together.

private Mono<OrderInfo> setPrices(final OrderInfo orderInfo) {
    getArticleDocuments(orderInfo)
        .map(articleDocuments -> {
            articleDocuments.forEach(article -> // UPDATE AS YOU NEED);
            return orderInfo;
        });

private Mono<List<ArticleDocument>> getArticleDocuments(final OrderInfo orderInfo) {
    return this.articleRepository.findByArticleName(orderInfo.getArticleName()).collectList();
}

请记住,您必须将所有内容置于链接之下.这就是为什么您必须从 setPrices 方法返回 Mono 而不是 OrderInfo 的原因.如果您发现我建议的代码难以适应您当前的编码结构,您可以向我展示完整的代码.让我们看看我们能不能建立一个好的链.

Remember, you have to put everything under chaining. that's why you have to return Mono<OrderInfo> instead of OrderInfo from setPrices method. If you find my suggested code is tough to adapt to your current coding structure, you can show me the full code. Let's find out we can build a good chain or not.

顺便说一句,您使用的是 getArticleDocuments(orderInfo).block();.看?你在使用 .block() 吗?不要在链中这样做.永远不要阻止对响应链进程的请求中的任何内容.你将从控制器返回单声道或通量,一切都将由 webflux 处理

BTW, you were using getArticleDocuments(orderInfo).block();. See? you were using .block()? Don't do that in a chain. don't ever block anything in a request to the response chain process. you will return mono or flux from the controller and everything will be handled by webflux

这篇关于Spring Webflux 抛出一个“block()/blockFirst()/blockLast() 正在阻塞,这在线程 reactor-http-nio-2 中不支持";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆