使用Spring WebFlux和WebClient替换传统的Java以循环逻辑为反应性编程

发布于 2025-02-09 11:13:49 字数 503 浏览 2 评论 0原文

我正在将Spring Boot(MVC)应用程序转换为反应性编程(Spring WebFlux),

用于进行外部休息,我正在使用WebClient。

List<String> targetUrls = Arrays.asList("one","two","three");

        int iteration = 1;
        for(String target:targetUrls){

            // make a rest call here 
            // if 200 break the loop
            // else  loop continues
        }

其余的电话在这里返回单声道。

是否有一种方法可以迭代URL列表(在这种情况下)并根据

单声道上尝试过的flatmap响应打破循环,并且基于响应可以对另一个URL进行休息 - &gt;如果我们事先知道URL的数量,这起作用了

I'm converting Spring Boot (MVC) application to Reactive Programming (Spring Webflux)

For making external rest calls, I'm using WebClient.

List<String> targetUrls = Arrays.asList("one","two","three");

        int iteration = 1;
        for(String target:targetUrls){

            // make a rest call here 
            // if 200 break the loop
            // else  loop continues
        }

The rest call here returns a Mono.

Is there a way to iterate over the list of urls(here in this case) and break the loop based on some response

Tried flatMap on the Mono and based on response can make a rest call to another url --> This works in case we know before hand the number of urls

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蓝海 2025-02-16 11:13:49

我不确定您是如何从WebClient调用URL的,但是仅假设URL传递给方法webclient.get.get(String url),我将继续使用该示例。

这些步骤将有点像您首先获得URL的通量,然后使用flatmap(因为您的响应是单声道),然后在这些上使用map

Flux.fromIterable(targetUrls)
  .flatMap(webClient::get)
  .map(response -> doSomethingWith(response))

现在,您应该有很多回应。如果WebClient的错误处理还不够,您也可以插入之间的某些内容。

Flux.fromIterable(targetUrls)
  .flatMap(webClient::get)
  .doOnError(errorHandler::handle)
  .map(response -> doSomethingWith(response))

I'm not sure how you call the urls from the WebClient, but just assuming the URLs are passed to a method WebClient.get(String url), I will continue with the example.

The steps would be somewhat like you will first get a Flux of the URLs, then use flatMap (because your response is a Mono), then use map on these.

Flux.fromIterable(targetUrls)
  .flatMap(webClient::get)
  .map(response -> doSomethingWith(response))

Now you should have a Flux of responses. If the error handling from the WebClient isn't enough, you can also insert something inbetween.

Flux.fromIterable(targetUrls)
  .flatMap(webClient::get)
  .doOnError(errorHandler::handle)
  .map(response -> doSomethingWith(response))
终止放荡 2025-02-16 11:13:49

您可以使用几种模式来实现此逻辑,

您可以使用flatmap同时执行多个查询。默认情况下,flatmap将处理queues.small_buffer_size = 256机上内部序列的数字。

Flux.fromIterable(targetUrls)
  .flatMap(url -> webClient.get(url)) 
  .map(response -> process(response))

您可以控制并发flatmap(url - &gt; webclient.get(url),并发),或使用concatmap操作员(如果要顺序执行查询)。

另一个有用的模式,如果您需要响应才能做出有关下一个请求的决定 - 使用展开操作员。例如,如果有分页的请求。

webclient.get(url)
        .expand(response -> {
            if (<should stop>) {
                // stop the loop
                return Mono.empty();
            }

            return webclient.get(url);
        });

There are several patterns you could apply to implement this logic

You can use flatMap to execute multiple queries concurrently. By default, flatMap will process Queues.SMALL_BUFFER_SIZE = 256 number of in-flight inner sequences.

Flux.fromIterable(targetUrls)
  .flatMap(url -> webClient.get(url)) 
  .map(response -> process(response))

You could control concurrency flatMap(url -> webClient.get(url), concurrency) or use concatMap operator if you want to execute queries sequentially.

Another useful pattern in case you need response to make decision about next request - use expand operator. For example, in case of paginated requests.

webclient.get(url)
        .expand(response -> {
            if (<should stop>) {
                // stop the loop
                return Mono.empty();
            }

            return webclient.get(url);
        });

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文