From 2091a005c5723aed1ddb34c9eb96f5a32cd30a93 Mon Sep 17 00:00:00 2001 From: Oleg Zhurakousky Date: Wed, 18 Dec 2019 14:25:47 +0100 Subject: [PATCH] GH-1867 Updated documentation with web source Resolves #1867 --- .../main/asciidoc/spring-cloud-stream.adoc | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/docs/src/main/asciidoc/spring-cloud-stream.adoc b/docs/src/main/asciidoc/spring-cloud-stream.adoc index 094676891..fc4c66a62 100644 --- a/docs/src/main/asciidoc/spring-cloud-stream.adoc +++ b/docs/src/main/asciidoc/spring-cloud-stream.adoc @@ -593,9 +593,56 @@ Default: 1L. For example `--spring.cloud.stream.poller.fixed-delay=2000` sets the poller interval to poll every two seconds. +===== Foreign event-driven sources + +There are cases where the actual source of data may be coming from outside system that is not a binder. For example, the +source of the data may be a classic web endpoint. How do we bridge such source with the functional Supplier? + +Let's look at a simple example: + +[source, java] +---- +@SpringBootApplication +@Controller +public class WebSourceApplication { + + public static void main(String[] args) { + SpringApplication.run(WebSourceApplication.class); + } + + EmitterProcessor processor = EmitterProcessor.create(); + + @RequestMapping + @ResponseStatus(HttpStatus.ACCEPTED) + public void delegateToSupplier(@RequestBody String body) { + processor.onNext(body); + } + + @Bean + public Supplier> supplier() { + return () -> processor; + } +} +---- + +Here you see a standard MVC endpoint method called `delegateToSupplier` bound to the root web context and a `Supplier` bean. Note how `Supplier` bean returns `Flux` of `Strings`. +Yes we are benefiting from the reactive support (see <> for more details). Specifically +https://projectreactor.io/docs/core/release/api/reactor/core/publisher/EmitterProcessor.html[EmitterProcessor] which effectively serves as bridge +between the web and spring-cloud-stream. As you can see in the endpoint method, all we do is call `onNext(..)` operation of the `EmitterProcessor` +with the value of the HTTP request's body. From that point on the Supplier rules described earlier apply. + +You can now send message to spring-cloud-stream source as + +---- +curl -H "Content-Type: text/plain" -X POST -d "hello from the other side" http://localhost:8080/ +---- + +And while this example demonstrates bridging web endpoint with the Supplier of data that will be fed into spring-cloud-stream framework, +the approach can be used with other type of foreign sources. ===== Reactive Functions support + Since _Spring Cloud Function_ is build on top of https://projectreactor.io/[Project Reactor] there isn't much you need to do to benefit from reactive programming model while implementing `Supplier`, `Function` or `Consumer`.