如何从Http集成流程创建Spring Reactor Flux?

如何从Http集成流程创建Spring Reactor Flux?,第1张

如何从Http集成流程创建Spring Reactor Flux?

这对我很好:

@SpringBootApplication@RestControllerpublic class SpringIntegrationSseDemoApplication {    public static void main(String[] args) {        SpringApplication.run(SpringIntegrationSseDemoApplication.class, args);    }    @Bean    public Publisher<Message<String>> httpReactiveSource() {        return IntegrationFlows.     from(Http.inboundChannelAdapter("/message/{id}")  .requestMapping(r -> r          .methods(HttpMethod.POST)  )  .payloadexpression("#pathVariables.id")     )     .channel(MessageChannels.queue())     .toReactivePublisher();    }    @GetMapping(value = "/events", produces = MediaType.TEXT_EVENT_STREAM_VALUE)    public Flux<String> eventMessages() {        return Flux.from(httpReactiveSource())     .map(Message::getPayload);    }}

我在POM中有以下依赖关系:

<parent>    <groupId>org.springframework.boot</groupId>    <artifactId>spring-boot-starter-parent</artifactId>    <version>2.0.0.BUILD-SNAPSHOT</version>    <relativePath/> <!-- lookup parent from repository --></parent><dependencies>    <dependency>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-starter-integration</artifactId>    </dependency>    <dependency>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-starter-web</artifactId>    </dependency>    <dependency>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-starter-webflux</artifactId>    </dependency>    <dependency>        <groupId>org.springframework.integration</groupId>        <artifactId>spring-integration-http</artifactId>    </dependency>    <dependency>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-starter-test</artifactId>        <scope>test</scope>    </dependency></dependencies><build>    <plugins>        <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId>        </plugin>    </plugins></build>

我运行该应用程序,并有两个终端

curl http://localhost:8080/events

听上交所。

在第二个中,我执行以下 *** 作:

curl -X POST http://localhost:8080/message/foocurl -X POST http://localhost:8080/message/barcurl -X POST http://localhost:8080/message/666

因此,第一个终端的响应如下:

data:foodata:bardata:666

注意,我们不需要

spring-boot-starter-webflux
依赖。将
Flux
定期对MVC的Servlet容器上证所效果很好。

Spring
Integration也将很快支持WebFlux:https
://jira.spring.io/browse/INT-4300 。因此,您将可以在此处进行以下配置:

   IntegrationFlows    .from(Http.inboundReactiveGateway("/sse")  .requestMapping(m -> m.produces(MediaType.TEXT_EVENT_STREAM_VALUE)))

并且仅完全依赖WebFlux,而没有任何Servlet容器依赖性。



欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5083293.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-16
下一篇 2022-11-16

发表评论

登录后才能评论

评论列表(0条)

保存