From ec2422418b604b159eeb3b89972b857df3da0e54 Mon Sep 17 00:00:00 2001 From: Oleg Zhurakousky Date: Tue, 30 Nov 2021 14:04:10 +0100 Subject: [PATCH] GH-2213 Add docs to clarify SpEL usage Resolves #2213 --- docs/src/main/asciidoc/preface.adoc | 31 ++++++++++--------- .../main/asciidoc/spring-cloud-stream.adoc | 8 ++--- 2 files changed, 20 insertions(+), 19 deletions(-) diff --git a/docs/src/main/asciidoc/preface.adoc b/docs/src/main/asciidoc/preface.adoc index e14de847d..259a18adb 100644 --- a/docs/src/main/asciidoc/preface.adoc +++ b/docs/src/main/asciidoc/preface.adoc @@ -154,23 +154,9 @@ You can also build and package your application into a boot jar (by using `./mvn Now you have a working (albeit very basic) Spring Cloud Stream application. -== What's New in 3.x? - - -[[spring-cloud-stream-preface-new-features]] -=== New Features and Enhancements - -- *Routing Function* - see <> for more details. -- *StreamBridge* - for dynamic destinations. See <> for more details. -- *Multiple bindings with functions* (multiple message handlers) - see <> for more details. -- *Functions with multiple inputs/outputs* (single function that can subscribe or target multiple destinations) - see <> for more details. -- *Native support for reactive programming* - since v3.0.0 we no longer distribute spring-cloud-stream-reactive modules and instead -relying on native reactive support provided by spring cloud function. For backward -compatibility you can still bring `spring-cloud-stream-reactive` from previous versions. - [[spring-cloud-stream-preface-notable-deprecations]] -=== Notable Deprecations +== Notable Deprecations - Annotation-based programming model. Basically the @EnableBInding, @StreamListener and all related annotations are now deprecated in favor of the functional programming model. See <> for more details. @@ -183,3 +169,18 @@ compatibility you can still bring `spring-cloud-stream-reactive` from previous v - The `BinderAwareChannelResolver` is deprecated in favor if providing `spring.cloud.stream.sendto.destination` property. This is primarily for function-based programming model. For StreamListener it would still be required and thus will stay until we deprecate and eventually discontinue StreamListener and annotation-based programming model. + +[[spel-and-streaming-data]] + +== Spring Expression Language (SpEL) in the context of Streaming data + +Throwout this reference manual you will encounter many features and examples where you can utilize Spring Expression Language (SpEL). It is important to understand certain limitations when it comes to using it. + +SpEL gives you access to the current Message as well as the Application Context you are running in. +However it is important to understand what type of data SpEL can see especially in the context of the incoming Message. +From the broker, the message arrives in a form of a byte[]. It is then transformed to a `Message` by the binders where as you can see the payload of the message maintains its raw form. The headers of the message are ``, where values are typically another primitive or a collection/array of primitives, hence Object. +That is because binder does not know the required input type as it has no access to the user code (function). So effectively binder delivered an envelope with the payload and some readable meta-data in the form of message headers, just like the letter delivered by mail. +This means that while accessing payload of the message is possible you will only have access to it as raw data (i.e., byte[]). And while it may be very common for developers to ask for ability to have SpEL access to fields of a payload object as concrete type (e.g., Foo, Bar etc), you can see how difficult or even impossible would it be to achieve. +Here is one example to demonstrate the problem; Imagine you have a routing expression to route to different functions based on payload type. This requirement would imply payload conversion from byte[] to a specific type and then applying the SpEL. However, in order to perform such conversion we would need to know the actual type to pass to converter and that comes from functions signature which we don’t know which one. A better approach to solve this requirement would be to pass the type information as message headers (e.g., `application/json;type=foo.bar.Baz` ). You’ll get a clear readable String value that could be accessed and evaluated in a year and easy to read SpEL expression. + +Additionally it is considered very bad practice to use payload for routing decisions, since the payload is considered to be privileged data - data only to be read by its final recipient. Again, using the mail delivery analogy you would not want the mailman to open your envelope and read the contents of the letter to make some delivery decisions. The same concept applies here, especially when it is relatively easy to include such information when generating a Message. It enforces certain level of discipline related to the design of data to be transmitted over the network and which pieces of such data can be considered as public and which are privileged. diff --git a/docs/src/main/asciidoc/spring-cloud-stream.adoc b/docs/src/main/asciidoc/spring-cloud-stream.adoc index d4f95285c..0f65e560f 100644 --- a/docs/src/main/asciidoc/spring-cloud-stream.adoc +++ b/docs/src/main/asciidoc/spring-cloud-stream.adoc @@ -2398,9 +2398,9 @@ public MessageSourceCustomizer sourceCustomizer() { These properties are exposed via `org.springframework.cloud.stream.binder.ProducerProperties` The following binding properties are available for output bindings only and must be prefixed with `spring.cloud.stream.bindings..producer.` -(for example, `spring.cloud.stream.bindings.func-out-0.producer.partitionKeyExpression=payload.id`). +(for example, `spring.cloud.stream.bindings.func-out-0.producer.partitionKeyExpression=headers.id`). -Default values can be set by using the prefix `spring.cloud.stream.default.producer` (for example, `spring.cloud.stream.default.producer.partitionKeyExpression=payload.id`). +Default values can be set by using the prefix `spring.cloud.stream.default.producer` (for example, `spring.cloud.stream.default.producer.partitionKeyExpression=headers.id`). autoStartup:: Signals if this consumer needs to be started automatically @@ -2719,14 +2719,14 @@ You can configure an output binding to send partitioned data by setting one and For example, the following is a valid and typical configuration: ---- -spring.cloud.stream.bindings.func-out-0.producer.partitionKeyExpression=payload.id +spring.cloud.stream.bindings.func-out-0.producer.partitionKeyExpression=headers.id spring.cloud.stream.bindings.func-out-0.producer.partitionCount=5 ---- Based on that example configuration, data is sent to the target partition by using the following logic. A partition key's value is calculated for each message sent to a partitioned output binding based on the `partitionKeyExpression`. -The `partitionKeyExpression` is a SpEL expression that is evaluated against the outbound message for extracting the partitioning key. +The `partitionKeyExpression` is a SpEL expression that is evaluated against the outbound message (in the preceding example it's the value of the `id` from message headers) for extracting the partitioning key. If a SpEL expression is not sufficient for your needs, you can instead calculate the partition key value by providing an implementation of `org.springframework.cloud.stream.binder.PartitionKeyExtractorStrategy` and configuring it as a bean (by using the `@Bean` annotation). If you have more then one bean of type `org.springframework.cloud.stream.binder.PartitionKeyExtractorStrategy` available in the Application Context, you can further filter it by specifying its name with the `partitionKeyExtractorName` property, as shown in the following example: