From 89e0ab4271ca9e048682b2d32a22fe2fd68be168 Mon Sep 17 00:00:00 2001 From: Oleg Zhurakousky Date: Tue, 8 Oct 2024 09:33:01 +0200 Subject: [PATCH] Add documentation for using BiFunction --- .../programming-model.adoc | 34 ++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/docs/modules/ROOT/pages/spring-cloud-function/programming-model.adoc b/docs/modules/ROOT/pages/spring-cloud-function/programming-model.adoc index 818265dbf..d566386ba 100644 --- a/docs/modules/ROOT/pages/spring-cloud-function/programming-model.adoc +++ b/docs/modules/ROOT/pages/spring-cloud-function/programming-model.adoc @@ -125,7 +125,39 @@ public Supplier> someSupplier() { Function can also be written in imperative or reactive way, yet unlike Supplier and Consumer there are no special considerations for the implementor other then understanding that when used within frameworks such as https://spring.io/projects/spring-cloud-stream[Spring Cloud Stream] and others, reactive function is -invoked only once to pass a reference to the stream (Flux or Mono) and imperative is invoked once per event. +invoked only once to pass a reference to the stream (i.e., Flux or Mono) and imperative is invoked once per event. + +[source, java] +---- +public Function uppercase() { + . . . . +} +---- + +[[bifunction]] +=== BiFunction +In the event you need to receive some additional data (metadata) with your payload you can always make your function +signature to receive a Message which contains a map of headers containing such additional information. + +[source, java] +---- +public Function, String> uppercase() { + . . . . +} +---- + +To make your function signature a bit lighter and more POJO like there is another approach. You can use `BiFunction`. +[source, java] +---- +public BiFunction uppercase() { + . . . . +} +---- + +Given that a `Message` only contains two attributes (payload and headers) and `BiFunction` requiring two input parameters the framework will automatically recognise this paradigm and will extract payload from the `Message` passing it as a first argument and the map of headers as the second. +In this case your functions is also not coupled to Spring’s messaging API. +Keep in mind that BiFunction requires a strict signature where second argument *must* be a Map. +The same rule applies to `BiConsumer`. [[consumer]] === Consumer