-
Notifications
You must be signed in to change notification settings - Fork 2.5k
initial commit of WebFlux sample #225
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# WebFlux: Spring Integration Java DSL | ||
|
||
This sample demonstrates the usage of the WebFlux protocol adapter to split incoming messages to different routes and provide the results as SSE events. | ||
|
||
NOTE: at the time of this writing, [the WebFlux integration drops POST messages with empty request body](https://jira.spring.io/browse/INT-4462) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This has been fixed and |
||
|
||
## Run the Sample | ||
|
||
* You need Java 8 to run this sample, because it is based on Lambdas. | ||
* running the `WebFluxApplication` class from within STS (Right-click on | ||
Main class --> Run As --> Java Application) | ||
* or from the command line in the _webflux_ folder: | ||
|
||
$ mvn spring-boot:run | ||
|
||
## Interact with the Sample | ||
|
||
The sample expects messages containing a JSON array where possible items are `"latte macchiato""` or `"caffe"`. | ||
|
||
$ curl -v -d "[\"latte macchiato\", \"caffe\"]" -H "Content-Type: application/json" http://localhost:8080/messages | ||
|
||
To listen for SSE events: | ||
|
||
$ curl localhost:8080/events | ||
|
||
Whenever a message is processed, a corresponding event will be sent to the _/events_ resource. | ||
|
||
data:"latte macchiato" | ||
|
||
data:"caffe" | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>org.springframework.integration.samples</groupId> | ||
<artifactId>webflux</artifactId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
<packaging>jar</packaging> | ||
|
||
<name>webflux</name> | ||
<description>Demo project for Spring Integration Webflux</description> | ||
|
||
<parent> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-parent</artifactId> | ||
<version>2.0.1.RELEASE</version> | ||
<relativePath/> <!-- lookup parent from repository --> | ||
</parent> | ||
|
||
<properties> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> | ||
<java.version>1.8</java.version> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-integration</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-webflux</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.integration</groupId> | ||
<artifactId>spring-integration-webflux</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> | ||
|
||
|
||
</project> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* Copyright 2018 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.integration.samples.dsl.webflux; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.node.TextNode; | ||
import org.reactivestreams.Publisher; | ||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.http.HttpMethod; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.integration.dsl.IntegrationFlow; | ||
import org.springframework.integration.dsl.IntegrationFlows; | ||
import org.springframework.integration.dsl.channel.MessageChannels; | ||
import org.springframework.integration.webflux.dsl.WebFlux; | ||
import org.springframework.messaging.Message; | ||
import reactor.core.publisher.Flux; | ||
|
||
/** | ||
* @author Dietrich Schulten | ||
* @since 5.0.4 | ||
*/ | ||
@SpringBootApplication | ||
public class WebFluxApplication { | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(WebFluxApplication.class, args); | ||
} | ||
|
||
@Bean | ||
public Publisher<Message<JsonNode>> reactiveSource() { | ||
return IntegrationFlows. | ||
from(WebFlux.inboundChannelAdapter("/messages") | ||
.requestMapping(r -> r | ||
.methods(HttpMethod.POST) | ||
) | ||
.requestPayloadType(JsonNode.class) | ||
) | ||
.split() | ||
.channel(MessageChannels.flux()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please, add this one before |
||
.<TextNode, String>route(o -> o.asText(), | ||
m -> m.defaultOutputToParentFlow() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we need |
||
.subFlowMapping("latte macchiato", f -> f.handle((p, h) -> p)) | ||
.subFlowMapping("caffe", f -> f.handle((p, h) -> p))) | ||
.toReactivePublisher(); | ||
} | ||
|
||
|
||
@Bean | ||
public IntegrationFlow eventMessages() { | ||
return IntegrationFlows | ||
.from(WebFlux.inboundGateway("/events") | ||
.requestMapping(m -> m.produces(MediaType.TEXT_EVENT_STREAM_VALUE))) | ||
.handle((p, h) -> Flux.from(reactiveSource()) | ||
.map(Message::getPayload)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we don't need to extract just |
||
.get(); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
logging.level.org.springframework.web: DEBUG | ||
logging.level.org.springframework.integration: DEBUG | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. New line in the end of each file. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* Copyright 2014-2015 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.integration.samples.dsl.webflux; | ||
|
||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.test.context.junit4.SpringRunner; | ||
|
||
/** | ||
* @author Dietrich Schulten | ||
* @since 5.0.4 | ||
*/ | ||
@RunWith(SpringRunner.class) | ||
@SpringBootTest | ||
public class WebfluxApplicationTests { | ||
|
||
@Test | ||
public void contextLoads() { | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would be great to have some content in this test to be sure that our flow is correct. Although you can do it with the |
||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't need this dependency - it is polled by the
spring-boot-starter-integration
, as well as by thespring-integration-webflux
.