spring-integration:如何从WebFlux集成流创建Spring Reactor Flux?

时间:2018-05-05 10:25:59

标签: spring-integration spring-webflux

How to create a Spring Reactor Flux from Http integration flow? artem-bilan mentioned in a comment中,将来可以使用webflux集成。

自撰写评论以来,WebFlux集成一直是factored out to spring-integration-webflux。我已经尝试了以下内容,通过用 for a in range((row['max_move_%']) * (- 1), row['max_move_%']): 和{替换MVC版本的Http.inboundChannelAdapter@GetRequest处理程序来复制基于MVC的基于http> flux的集成与基于WebFlux的处理器。 {1}}:

WebFlux.inboundChannelAdapter

}

WebFlux.inboundGateway发布商的流量似乎没有收到任何消息,至少没有记录我的@SpringBootApplication public class WebfluxApplication { public static void main(String[] args) { SpringApplication.run(WebfluxApplication.class, args); } @Bean public Publisher<Message<String>> reactiveSource() { return IntegrationFlows. from(WebFlux.inboundChannelAdapter("/message/{id}") .requestMapping(r -> r .methods(HttpMethod.POST) ) .payloadExpression("#pathVariables.id") ) .log() .channel(MessageChannels.flux()) .toReactivePublisher(); } @Bean public IntegrationFlow eventMessages() { return IntegrationFlows .from(WebFlux.inboundGateway("/events") .requestMapping(m -> m.produces(MediaType.TEXT_EVENT_STREAM_VALUE))) .handle((p, h) -> reactiveSource()) .get(); } 声明。

当我替换reactiveSource()流程中的.log()发布商

reactiveSource()

由虚假的出版商

eventMessages

我从

获得SSE回复
.handle((p, h) -> reactiveSource()) 

跟踪日志显示.handle((p, h) -> Flux.just("foo", "bar")) POST处理程序已映射,并且正在调用curl localhost:8080/events 方法:

reactiveSource()

为什么?

1 个答案:

答案 0 :(得分:1)

原因似乎是WebFluxInboundEndpoint停止处理doHandle()中没有正文的POST请求,行

.map(body -> new HttpEntity<>(...)) 
如果没有请求正文内容,则永远不会执行

private Mono<Void> doHandle(ServerWebExchange exchange) {
    return extractRequestBody(exchange)
            .doOnSubscribe(s -> this.activeCount.incrementAndGet())
            .map(body -> new HttpEntity<>(body, exchange.getRequest().getHeaders()))
            .map(entity -> buildMessage(entity, exchange))
            .flatMap(requestMessage -> {
                if (this.expectReply) {
                    return sendAndReceiveMessageReactive(requestMessage)
                            .flatMap(replyMessage -> populateResponse(exchange, replyMessage));
                }
                else {
                    send(requestMessage);
                    return setStatusCode(exchange);
                }
            })
            .doOnTerminate(this.activeCount::decrementAndGet);

}

解决方法:调用方必须发送任何非空的请求正文才能使其正常工作,例如用-d传递的单引号就足够了:

curl -d ' http://localhost:8080/message/4

有了这样的请求,我的日志按预期包含传入的GenericMessage,并且/ events资源开始生成SSE。

2018-05-05 17:25:24.777 TRACE 40436 --- [ctor-http-nio-8] o.s.w.r.r.method.InvocableHandlerMethod  : Method [org.springframework.integration.webflux.inbound.WebFluxInboundEndpoint.handle] returned [MonoDefer]
2018-05-05 17:25:24.777 DEBUG 40436 --- [ctor-http-nio-8] o.s.w.r.r.method.InvocableHandlerMethod  : Response fully handled in controller method
2018-05-05 17:25:24.778  INFO 40436 --- [ctor-http-nio-8] o.s.integration.handler.LoggingHandler   : GenericMessage [payload=4, headers={http_requestMethod=POST, Accept=*/*, User-Agent=curl/7.49.1, http_requestUrl=http://localhost:8080/message/4, Host=localhost:8080, id=9a09294d-280a-af3b-0894-23597cf1cb5f, Content-Length=1, contentType=application/x-www-form-urlencoded, timestamp=1525533924778}]