与RxJava并行的Spring请求

时间:2020-11-03 16:09:14

标签: java spring spring-boot rx-java

如何与RxJava并行发送来自Spring应用程序的请求?

我的Spring应用程序中有以下请求

public Foo request(A a, B b, C c, D d) {
      E e = mapper.map(b, d);
      
      return Single.zip(
              requestG(a, e),
              requestF(a, e),
              (g, f) -> entityMapper.map(g, f, b, c))
          .blockingGet();       
    }

requestGrequestF也返回Singles。它们可以彼此完全独立地运行。如果现在调用在日志中看到的函数,它运行requestG,请等待结果,然后运行“ requestF”。

但是我希望它可以并行运行。所以我将其更改为以下内容:

public Foo request(A a, B b, C c, D d) {
      E e = mapper.map(b, d);
      
      return Single.zip(
              requestG(a, e).subscribeOn(Schedulers.io()),
              requestF(a, e).subscribeOn(Schedulers.io()),
              (g, f) -> entityMapper.map(g, f, b, c))
          .blockingGet();       
    }

我还尝试添加Schedulers.newThread()

但是通过该实现,我收到以下异常:

java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.

我发现了一些有关RequestContextListener的信息,但我不知道如何或在何处附加它以及如何将其与RxJava一起使用

0 个答案:

没有答案