Skip to content

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -734,6 +734,33 @@ project('cafe-dsl') {
}
}

project('webflux') {
description = 'Webflux DSL Sample'

apply plugin: 'org.springframework.boot'

dependencies {
compile 'org.springframework.boot:spring-boot-starter-integration'
compile "org.springframework.boot:spring-boot-starter-webflux"
compile "org.springframework.integration:spring-integration-core"
Copy link
Member

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 the spring-integration-webflux.

compile "org.springframework.integration:spring-integration-webflux"

testCompile 'org.springframework.boot:spring-boot-starter-test'
}
bootRun {
main = 'org.springframework.integration.samples.dsl.webflux.WebFluxApplication'
}

task run(type: JavaExec) {
main 'org.springframework.integration.samples.dsl.webflux.WebFluxApplication'
classpath = sourceSets.main.runtimeClasspath
}

tasks.withType(JavaExec) {
standardInput = System.in
}
}


project('jdbc') {
description = 'JDBC Basic Sample'
Expand Down
31 changes: 31 additions & 0 deletions dsl/webflux/README.md
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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has been fixed and 5.0.5 released.
We can remove this sentence altogether.


## 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"

59 changes: 59 additions & 0 deletions dsl/webflux/pom.xml
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())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, add this one before split() as well. This way we will have full Reactive flow.

.<TextNode, String>route(o -> o.asText(),
m -> m.defaultOutputToParentFlow()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need defaultOutputToParentFlow()?
Is there some incoming values we cannot map with those subFlows ?

.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))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we don't need to extract just payload - the WebFlux.inboundGateway() will do that for us properly.
On the other hand we don't need to wrap to the Flux.from() I believe.
How does it work if you only have .handle((p, h) -> reactiveSource()) ?

.get();
}

}
2 changes: 2 additions & 0 deletions dsl/webflux/src/main/resources/application.properties
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

New line in the end of each file.
You can configure your IDE to do that for your on file save.

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() {
}
Copy link
Member

Choose a reason for hiding this comment

The 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.
You can borrow some ideas how to test it from here: https://github.com/spring-projects/spring-integration/blob/master/spring-integration-webflux/src/test/java/org/springframework/integration/webflux/dsl/WebFluxDslTests.java

Although you can do it with the webEnvironment = WebEnvironment.RANDOM_PORT and WebTestClient.bindToServer().


}