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

@@ -17,11 +17,13 @@
package org.springframework.integration.jdbc;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.IntStream;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
@@ -29,6 +31,7 @@ import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase;
@@ -108,6 +111,38 @@ public class JdbcMessageHandlerIntegrationTests {
assertThat(foos.get(2).get("NAME")).isEqualTo("foo3");
}
@Test
public void testInsertBatchOfMessages() {
JdbcMessageHandler handler = new JdbcMessageHandler(jdbcTemplate,
"insert into foos (id, status, name) values (:id, 0, :payload)");
ExpressionEvaluatingSqlParameterSourceFactory sqlParameterSourceFactory =
new ExpressionEvaluatingSqlParameterSourceFactory();
sqlParameterSourceFactory.setParameterExpressions(Map.of("id", "headers.id", "payload", "payload"));
sqlParameterSourceFactory.setBeanFactory(mock(BeanFactory.class));
handler.setSqlParameterSourceFactory(sqlParameterSourceFactory);
handler.afterPropertiesSet();
List<GenericMessage<String>> payload =
IntStream.range(1, 4)
.mapToObj(i -> "Item" + i)
.map(GenericMessage::new)
.toList();
handler.handleMessage(new GenericMessage<>(payload));
List<Map<String, Object>> foos = jdbcTemplate.queryForList("SELECT * FROM FOOS ORDER BY NAME");
assertThat(foos.size()).isEqualTo(3);
assertThat(foos.get(0).get("NAME")).isEqualTo("Item1");
assertThat(foos.get(1).get("NAME")).isEqualTo("Item2");
assertThat(foos.get(2).get("NAME")).isEqualTo("Item3");
assertThat(foos.get(0).get("ID"))
.isNotEqualTo(foos.get(0).get("NAME"))
.isEqualTo(payload.get(0).getHeaders().getId().toString());
}
@Test
public void testInsertWithMessagePreparedStatementSetter() {
JdbcMessageHandler handler = new JdbcMessageHandler(jdbcTemplate,