From 8410e1d3d25f8441973fec2427571f323e0b931b Mon Sep 17 00:00:00 2001 From: Dietrich Schulten Date: Sun, 6 May 2018 10:30:22 +0200 Subject: [PATCH 1/5] initial commit of WebFlux sample --- dsl/webflux/README.md | 31 ++++++++++ dsl/webflux/pom.xml | 59 +++++++++++++++++++ .../sample/webflux/WebFluxApplication.java | 55 +++++++++++++++++ .../src/main/resources/application.properties | 2 + .../webflux/WebfluxApplicationTests.java | 16 +++++ 5 files changed, 163 insertions(+) create mode 100644 dsl/webflux/README.md create mode 100644 dsl/webflux/pom.xml create mode 100644 dsl/webflux/src/main/java/de/escalon/sample/webflux/WebFluxApplication.java create mode 100644 dsl/webflux/src/main/resources/application.properties create mode 100644 dsl/webflux/src/test/java/de/escalon/sample/webflux/WebfluxApplicationTests.java diff --git a/dsl/webflux/README.md b/dsl/webflux/README.md new file mode 100644 index 000000000..fcf18a33b --- /dev/null +++ b/dsl/webflux/README.md @@ -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) + +## Run the Sample + +* You need Java 8 to run this sample, because it is based on Lambdas. +* running the `de.escalon.sample.webflux.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" + diff --git a/dsl/webflux/pom.xml b/dsl/webflux/pom.xml new file mode 100644 index 000000000..a3660baf8 --- /dev/null +++ b/dsl/webflux/pom.xml @@ -0,0 +1,59 @@ + + + 4.0.0 + + de.escalon.sample + webflux + 0.0.1-SNAPSHOT + jar + + webflux + Demo project for Spring Integration Webflux + + + org.springframework.boot + spring-boot-starter-parent + 2.0.1.RELEASE + + + + + UTF-8 + UTF-8 + 1.8 + + + + + org.springframework.boot + spring-boot-starter-integration + + + + org.springframework.boot + spring-boot-starter-webflux + + + org.springframework.integration + spring-integration-webflux + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + + diff --git a/dsl/webflux/src/main/java/de/escalon/sample/webflux/WebFluxApplication.java b/dsl/webflux/src/main/java/de/escalon/sample/webflux/WebFluxApplication.java new file mode 100644 index 000000000..589157917 --- /dev/null +++ b/dsl/webflux/src/main/java/de/escalon/sample/webflux/WebFluxApplication.java @@ -0,0 +1,55 @@ +package de.escalon.sample.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; + + +@SpringBootApplication +public class WebFluxApplication { + + public static void main(String[] args) { + SpringApplication.run(WebFluxApplication.class, args); + } + + @Bean + public Publisher> reactiveSource() { + return IntegrationFlows. + from(WebFlux.inboundChannelAdapter("/messages") + .requestMapping(r -> r + .methods(HttpMethod.POST) + ) + .requestPayloadType(JsonNode.class) + ) + .split() + .channel(MessageChannels.flux()) + .route(o -> o.asText(), + m -> m.defaultOutputToParentFlow() + .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)) + .get(); + } + +} diff --git a/dsl/webflux/src/main/resources/application.properties b/dsl/webflux/src/main/resources/application.properties new file mode 100644 index 000000000..ea33ef9be --- /dev/null +++ b/dsl/webflux/src/main/resources/application.properties @@ -0,0 +1,2 @@ +logging.level.org.springframework.web: DEBUG +logging.level.org.springframework.integration: DEBUG \ No newline at end of file diff --git a/dsl/webflux/src/test/java/de/escalon/sample/webflux/WebfluxApplicationTests.java b/dsl/webflux/src/test/java/de/escalon/sample/webflux/WebfluxApplicationTests.java new file mode 100644 index 000000000..6b29a0a58 --- /dev/null +++ b/dsl/webflux/src/test/java/de/escalon/sample/webflux/WebfluxApplicationTests.java @@ -0,0 +1,16 @@ +package de.escalon.sample.webflux; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +@RunWith(SpringRunner.class) +@SpringBootTest +public class WebfluxApplicationTests { + + @Test + public void contextLoads() { + } + +} From c23eb902a39203b81aa86edb1fddc30d439772c9 Mon Sep 17 00:00:00 2001 From: Dietrich Schulten Date: Sun, 6 May 2018 13:20:20 +0200 Subject: [PATCH 2/5] renamed src package --- dsl/webflux/README.md | 2 +- .../integration/samples/dsl}/webflux/WebFluxApplication.java | 2 +- .../samples/dsl}/webflux/WebfluxApplicationTests.java | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) rename dsl/webflux/src/main/java/{de/escalon/sample => org/springframework/integration/samples/dsl}/webflux/WebFluxApplication.java (97%) rename dsl/webflux/src/test/java/{de/escalon/sample => org/springframework/integration/samples/dsl}/webflux/WebfluxApplicationTests.java (83%) diff --git a/dsl/webflux/README.md b/dsl/webflux/README.md index fcf18a33b..1afe72f3f 100644 --- a/dsl/webflux/README.md +++ b/dsl/webflux/README.md @@ -7,7 +7,7 @@ NOTE: at the time of this writing, [the WebFlux integration drops POST messages ## Run the Sample * You need Java 8 to run this sample, because it is based on Lambdas. -* running the `de.escalon.sample.webflux.WebFluxApplication` class from within STS (Right-click on +* 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: diff --git a/dsl/webflux/src/main/java/de/escalon/sample/webflux/WebFluxApplication.java b/dsl/webflux/src/main/java/org/springframework/integration/samples/dsl/webflux/WebFluxApplication.java similarity index 97% rename from dsl/webflux/src/main/java/de/escalon/sample/webflux/WebFluxApplication.java rename to dsl/webflux/src/main/java/org/springframework/integration/samples/dsl/webflux/WebFluxApplication.java index 589157917..80d73fa91 100644 --- a/dsl/webflux/src/main/java/de/escalon/sample/webflux/WebFluxApplication.java +++ b/dsl/webflux/src/main/java/org/springframework/integration/samples/dsl/webflux/WebFluxApplication.java @@ -1,4 +1,4 @@ -package de.escalon.sample.webflux; +package org.springframework.integration.samples.dsl.webflux; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.node.TextNode; diff --git a/dsl/webflux/src/test/java/de/escalon/sample/webflux/WebfluxApplicationTests.java b/dsl/webflux/src/test/java/org/springframework/integration/samples/dsl/webflux/WebfluxApplicationTests.java similarity index 83% rename from dsl/webflux/src/test/java/de/escalon/sample/webflux/WebfluxApplicationTests.java rename to dsl/webflux/src/test/java/org/springframework/integration/samples/dsl/webflux/WebfluxApplicationTests.java index 6b29a0a58..c232f165e 100644 --- a/dsl/webflux/src/test/java/de/escalon/sample/webflux/WebfluxApplicationTests.java +++ b/dsl/webflux/src/test/java/org/springframework/integration/samples/dsl/webflux/WebfluxApplicationTests.java @@ -1,4 +1,4 @@ -package de.escalon.sample.webflux; +package org.springframework.integration.samples.dsl.webflux; import org.junit.Test; import org.junit.runner.RunWith; From 2d2a27d23c26e32d2b639f59df04ca27c554f587 Mon Sep 17 00:00:00 2001 From: Dietrich Schulten Date: Sun, 6 May 2018 13:35:37 +0200 Subject: [PATCH 3/5] added gradle entry for webflux sample --- build.gradle | 27 +++++++++++++++++++++++++++ dsl/webflux/pom.xml | 2 +- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/build.gradle b/build.gradle index a0a870844..6e2a550ae 100644 --- a/build.gradle +++ b/build.gradle @@ -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" + 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' diff --git a/dsl/webflux/pom.xml b/dsl/webflux/pom.xml index a3660baf8..cb0e407e6 100644 --- a/dsl/webflux/pom.xml +++ b/dsl/webflux/pom.xml @@ -3,7 +3,7 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 - de.escalon.sample + org.springframework.integration.samples webflux 0.0.1-SNAPSHOT jar From 44e684e5672a25d29938ed7933aec8e3f39ba547 Mon Sep 17 00:00:00 2001 From: Dietrich Schulten Date: Sun, 6 May 2018 13:50:52 +0200 Subject: [PATCH 4/5] copyright and license header --- .../dsl/webflux/WebFluxApplication.java | 21 ++++++++++++++++++- .../dsl/webflux/WebfluxApplicationTests.java | 20 ++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/dsl/webflux/src/main/java/org/springframework/integration/samples/dsl/webflux/WebFluxApplication.java b/dsl/webflux/src/main/java/org/springframework/integration/samples/dsl/webflux/WebFluxApplication.java index 80d73fa91..20d00ce8a 100644 --- a/dsl/webflux/src/main/java/org/springframework/integration/samples/dsl/webflux/WebFluxApplication.java +++ b/dsl/webflux/src/main/java/org/springframework/integration/samples/dsl/webflux/WebFluxApplication.java @@ -1,3 +1,19 @@ +/* + * 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 com.fasterxml.jackson.databind.JsonNode; @@ -15,7 +31,10 @@ import org.springframework.messaging.Message; import reactor.core.publisher.Flux; - +/** + * @author Dietrich Schulten + * @since 5.0.4 + */ @SpringBootApplication public class WebFluxApplication { diff --git a/dsl/webflux/src/test/java/org/springframework/integration/samples/dsl/webflux/WebfluxApplicationTests.java b/dsl/webflux/src/test/java/org/springframework/integration/samples/dsl/webflux/WebfluxApplicationTests.java index c232f165e..74332b286 100644 --- a/dsl/webflux/src/test/java/org/springframework/integration/samples/dsl/webflux/WebfluxApplicationTests.java +++ b/dsl/webflux/src/test/java/org/springframework/integration/samples/dsl/webflux/WebfluxApplicationTests.java @@ -1,3 +1,19 @@ +/* + * 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; @@ -5,6 +21,10 @@ 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 { From 64f4f64f50fac840ea30b524e56809c609aa8011 Mon Sep 17 00:00:00 2001 From: Dietrich Schulten Date: Wed, 23 May 2018 06:38:39 +0200 Subject: [PATCH 5/5] adjusted copyright year to 2018 --- .../integration/samples/dsl/webflux/WebFluxApplication.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dsl/webflux/src/main/java/org/springframework/integration/samples/dsl/webflux/WebFluxApplication.java b/dsl/webflux/src/main/java/org/springframework/integration/samples/dsl/webflux/WebFluxApplication.java index 20d00ce8a..27fb20355 100644 --- a/dsl/webflux/src/main/java/org/springframework/integration/samples/dsl/webflux/WebFluxApplication.java +++ b/dsl/webflux/src/main/java/org/springframework/integration/samples/dsl/webflux/WebFluxApplication.java @@ -1,5 +1,5 @@ /* - * Copyright 2014-2015 the original author or authors. + * 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.