Skip to content

Commit 5d881b2

Browse files
committed
GH-2994, GH-2986 Add documentation explaining batch type conversion
1 parent 03dc940 commit 5d881b2

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

docs/modules/ROOT/pages/spring-cloud-stream/producing-and-consuming-messages.adoc

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -795,6 +795,53 @@ public Function<List<Person>, Person> findFirstPerson() {
795795
}
796796
----
797797

798+
==== Batch Type Conversion
799+
Similarly to type conversion for a single message consumer, batching requires every message in the batch to be converted to the requested type. For instance, in the previous example, this type is `Person`.
800+
801+
It is also important to understand that the headers for each message in a batch are provided separately in the `MessageHeaders` of the Message representing the entire batch. These Messages and their corresponding batched headers are created by the respective binders, and their structure may differ. Therefore, you should refer to the binder documentation to understand the structure of batched headers. For Kafka and Rabbit, you can search for `amqp_batchedHeaders` and `kafka_batchConvertedHeaders`, respectively.
802+
803+
In short, if you have a message representing a batch with 5 payloads, the same message will contain a set of headers where each header corresponds to a payload with the same index.
804+
805+
However, what happens if a particular payload fails to convert? In a single message scenario, we simply return null and invoke your method with the unconverted message, which either results in an exception or allows you to handle the raw message, depending on your function's signature.
806+
807+
In the case of batch processing, things are a bit more complex. Returning null for an unconverted payload effectively reduces the batch size. For example, if the original batch contained 5 messages, and 2 failed to convert, the converted batch will only contain 3 messages. This might be acceptable, but what about the corresponding batched headers? There will still be 5 headers, as they were created when the binder formed the initial batch. This discrepancy makes it difficult to correlate the headers with their corresponding payloads.
808+
809+
To address this issue, we provide the MessageConverterHelper interface.
810+
811+
[source, java]
812+
----
813+
public interface MessageConverterHelper {
814+
815+
/**
816+
* This method will be called by the framework in cases when a message failed to convert.
817+
* It allows you to signal to the framework if such failure should be considered fatal or not.
818+
*
819+
* @param message failed message
820+
* @return true if conversion failure must be considered fatal.
821+
*/
822+
default boolean shouldFailIfCantConvert(Message<?> message) {
823+
return false;
824+
}
825+
826+
/**
827+
* This method will be called by the framework in cases when a single message within batch of messages failed to convert.
828+
* It provides a place for providing post-processing logic before message converter returns.
829+
*
830+
* @param message failed message.
831+
* @param index index of failed message within the batch
832+
*/
833+
default void postProcessBatchMessageOnFailure(Message<?> message, int index) {
834+
}
835+
}
836+
----
837+
838+
If implemented, this interface is called by the framework’s message converter logic to perform post-processing on the batched message when a particular payload cannot be converted.
839+
840+
The default implementations for Kafka and Rabbit automatically remove the corresponding batch headers to maintain the correlation between batched payloads and their headers. However, you can provide your own implementation and register it as a bean if you need to add custom behavior for such cases.
841+
842+
Additionally, the interface offers a method that allows for more deterministic handling of conversion failures. By default, this method returns `false`, but you can customize the implementation if you prefer to fail the entire process when a conversion error occurs.
843+
844+
798845
[[batch-producers]]
799846
=== Batch Producers
800847

0 commit comments

Comments
 (0)