GH-3797: Improve batch processing in the framework (#3820)

* GH-3797: Improve batch processing in the framework

Fixes https://github.com/spring-projects/spring-integration/issues/3797

* Handle `Message` items of the `Iterable` payload properly in the `JdbcMessageHandler`.
Otherwise, they've been wrapped into an extra `Message`
* Produce a single message with a `Collection<Message<?>>` payload in the `AggregatingMessageHandler`
when the `getOutputProcessor()` is not an instance of `SimpleMessageGroupProcessor`
* Mention these changes in docs
* Point to the error handling sample from docs

* * Fix language in docs

Co-authored-by: Gary Russell <grussell@vmware.com>

Co-authored-by: Gary Russell <grussell@vmware.com>
This commit is contained in:
Artem Bilan
2022-06-07 10:14:53 -04:00
committed by GitHub
parent 4f49038e17
commit e855f13d8e
8 changed files with 100 additions and 35 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -57,14 +57,14 @@ import org.springframework.util.LinkedCaseInsensitiveMap;
* properties can be referred to by name in the query string, e.g.
*
* <pre class="code">
* INSERT INTO FOOS (MESSAGE_ID, PAYLOAD) VALUES (:headers[id], :payload)
* INSERT INTO ITEMS (MESSAGE_ID, PAYLOAD) VALUES (:headers[id], :payload)
* </pre>
*
* <p>
* When a message payload is an instance of {@link Iterable}, a
* {@link NamedParameterJdbcOperations#batchUpdate(String, SqlParameterSource[])} is performed, where each
* {@link SqlParameterSource} instance is based on items wrapped into an internal {@link Message} implementation with
* headers from the request message.
* headers from the request message. The item is wrapped only if it is not a {@link Message} already.
* <p>
* When a {@link #preparedStatementSetter} is configured, it is applied for each item in the appropriate
* {@link JdbcOperations#batchUpdate(String, BatchPreparedStatementSetter)} function.
@@ -219,19 +219,7 @@ public class JdbcMessageHandler extends AbstractMessageHandler {
if (message.getPayload() instanceof Iterable) {
Stream<? extends Message<?>> messageStream =
StreamSupport.stream(((Iterable<?>) message.getPayload()).spliterator(), false)
.map(payload -> new Message<Object>() {
@Override
public Object getPayload() {
return payload;
}
@Override
public MessageHeaders getHeaders() {
return message.getHeaders();
}
});
.map(payload -> payloadToMessage(payload, message.getHeaders()));
int[] updates;
@@ -288,4 +276,24 @@ public class JdbcMessageHandler extends AbstractMessageHandler {
}
}
private static Message<?> payloadToMessage(Object payload, MessageHeaders messageHeaders) {
if (payload instanceof Message) {
return (Message<?>) payload;
}
return new Message<>() {
@Override
public Object getPayload() {
return payload;
}
@Override
public MessageHeaders getHeaders() {
return messageHeaders;
}
};
}
}