立即返回弹簧幅面通量 [英] Return immediately in spring web flux

查看:111
本文介绍了立即返回弹簧幅面通量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建应该返回201响应的框架.当对API提出请求时,我想立即发送回响应并在后台处理该请求.

I am trying to create framework which should return 201 response. When a request is made to an API then i want to send back response immediately and process the request in background.

Flux.fromIterable(request.getApiRequests()).log().flatMap(item -> {

        WebClient.RequestHeadersSpec apiCallSpec = WebClient.create(request.getBasePath())
                .method(item.getHttpMethod()).uri(item.getPath()).accept(MediaType.valueOf(item.getAccept()))
                .contentType(MediaType.valueOf(item.getContentType())).body(BodyInserters.fromObject(item.getPayload()));

        return apiCallSpec.retrieve().bodyToMono(String.class);
    }).subscribe();

    return Mono.just("Created");

我希望助焊剂部分在后台发生,但是到目前为止,API会在整个助焊剂流化后等待并响应.

I want the flux part to happen in the background but as of now API waits and responds once whole flux is streamed.

推荐答案

如果我正确理解了您要做什么,我建议您使用其他设计.

If I correctly understand what you are trying to do, I can suggest you use a different design.

不要在上面的控制器(我想是)中创建助焊剂.相反,您可以在应用程序中创建一个Hot Stream实例(在您的上下文中可以是Bean),然后像这样使用该实例:

Don't create a Flux in your (what I presume to be) controller above. Instead you could create an instance of a Hot Stream in your app (could be a Bean in your context) and then use that instance like this:

myHotStream.next(item);

应用中的另一个组件可以使用相同的实例来订阅它.

Another component in you app can the use the same instance to subscribe to it.

在此处阅读文档: http://projectreactor.io/docs/core/release/reference/#reactor.hotCold 此处第8章.热门流

Read the documentation here: http://projectreactor.io/docs/core/release/reference/#reactor.hotCold And another example here, chapter 8. Hot Streams

修改

只是一个旁注.通常 HTTP 201 Created 表示已成功创建实体.如果此 creation 发送请求后异步发生,则最好发送 HTTP 200 OK .您不能保证 creation 成功,但可以传达已成功接收到请求的信息(例如:通过验证).

Just a sidenote. Usually HTTP 201 Created implies that an entity has been successfully created. If this creation happens asynchronously after you send the response, it might be better to send an HTTP 200 OK instead. You cannot guarantee that the creation was successful but you can communicate that the request has been successfully received (ex: passed validation).

这篇关于立即返回弹簧幅面通量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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