Skip to content

Commit 89e0ab4

Browse files
committed
Add documentation for using BiFunction
1 parent b899cfc commit 89e0ab4

File tree

1 file changed

+33
-1
lines changed

1 file changed

+33
-1
lines changed

docs/modules/ROOT/pages/spring-cloud-function/programming-model.adoc

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,39 @@ public Supplier<Flux<String>> someSupplier() {
125125
Function can also be written in imperative or reactive way, yet unlike Supplier and Consumer there are
126126
no special considerations for the implementor other then understanding that when used within frameworks
127127
such as https://spring.io/projects/spring-cloud-stream[Spring Cloud Stream] and others, reactive function is
128-
invoked only once to pass a reference to the stream (Flux or Mono) and imperative is invoked once per event.
128+
invoked only once to pass a reference to the stream (i.e., Flux or Mono) and imperative is invoked once per event.
129+
130+
[source, java]
131+
----
132+
public Function<String, String> uppercase() {
133+
. . . .
134+
}
135+
----
136+
137+
[[bifunction]]
138+
=== BiFunction
139+
In the event you need to receive some additional data (metadata) with your payload you can always make your function
140+
signature to receive a Message which contains a map of headers containing such additional information.
141+
142+
[source, java]
143+
----
144+
public Function<Message<String>, String> uppercase() {
145+
. . . .
146+
}
147+
----
148+
149+
To make your function signature a bit lighter and more POJO like there is another approach. You can use `BiFunction`.
150+
[source, java]
151+
----
152+
public BiFunction<String, Map, String> uppercase() {
153+
. . . .
154+
}
155+
----
156+
157+
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.
158+
In this case your functions is also not coupled to Spring’s messaging API.
159+
Keep in mind that BiFunction requires a strict signature where second argument *must* be a Map.
160+
The same rule applies to `BiConsumer`.
129161

130162
[[consumer]]
131163
=== Consumer

0 commit comments

Comments
 (0)