diff --git a/core/spring-cloud-stream/src/main/java/org/springframework/cloud/stream/function/StandardBatchUtils.java b/core/spring-cloud-stream/src/main/java/org/springframework/cloud/stream/function/StandardBatchUtils.java new file mode 100644 index 000000000..178f2932d --- /dev/null +++ b/core/spring-cloud-stream/src/main/java/org/springframework/cloud/stream/function/StandardBatchUtils.java @@ -0,0 +1,61 @@ +/* + * Copyright 2024-2024 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. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.stream.function; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.springframework.messaging.Message; +import org.springframework.messaging.MessageHeaders; +import org.springframework.messaging.support.MessageBuilder; + +/** + * @author Oleg Zhurakousky + * @since 4.2 + */ +public class StandardBatchUtils { + + public static String BATCH_HEADERS = "scst_batchHeaders"; + + + public static class BatchMessageBuilder { + + private final List payloads = new ArrayList<>(); + + private final List> batchHeaders = new ArrayList<>(); + + private final Map headers = new HashMap<>(); + + public BatchMessageBuilder addMessage(Object payload, Map batchHeaders) { + this.payloads.add(payload); + this.batchHeaders.add(batchHeaders); + return this; + } + + public BatchMessageBuilder addHeader(String key, Object value) { + this.headers.put(key, value); + return this; + } + + public Message> build() { + this.headers.put(BATCH_HEADERS, this.batchHeaders); + return MessageBuilder.createMessage(payloads, new MessageHeaders(headers)); + } + } +} diff --git a/core/spring-cloud-stream/src/test/java/org/springframework/cloud/stream/function/StandardBatchUtilsTests.java b/core/spring-cloud-stream/src/test/java/org/springframework/cloud/stream/function/StandardBatchUtilsTests.java new file mode 100644 index 000000000..77c387cfa --- /dev/null +++ b/core/spring-cloud-stream/src/test/java/org/springframework/cloud/stream/function/StandardBatchUtilsTests.java @@ -0,0 +1,59 @@ +/* + * Copyright 2024-2024 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. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.stream.function; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.util.Collections; +import java.util.List; +import java.util.Map; + +import org.junit.jupiter.api.Test; +import org.springframework.cloud.stream.function.StandardBatchUtils.BatchMessageBuilder; +import org.springframework.messaging.Message; + +/** + * + */ +public class StandardBatchUtilsTests { + + @SuppressWarnings("unchecked") + @Test + public void testBatchMessageBuilder() { + BatchMessageBuilder builder = new BatchMessageBuilder(); + builder.addMessage("foo", Collections.singletonMap("fooKey", "fooValue")); + builder.addHeader("a", "a"); + builder.addMessage("bar", Collections.singletonMap("barKey", "barValue")); + builder.addMessage("baz", Collections.singletonMap("bazKey", "bazValue")); + + Message> batchMessage = builder.build(); + + List payloads = batchMessage.getPayload(); + assertThat(payloads.size()).isEqualTo(3); + + List> batchHeaders = (List>) batchMessage.getHeaders().get(StandardBatchUtils.BATCH_HEADERS); + assertThat(batchHeaders.size()).isEqualTo(3); + + assertThat(payloads.get(0)).isEqualTo("foo"); + assertThat(batchHeaders.get(0).get("fooKey")).isEqualTo("fooValue"); + + assertThat(payloads.get(1)).isEqualTo("bar"); + assertThat(batchHeaders.get(1).get("barKey")).isEqualTo("barValue"); + + assertThat(batchMessage.getHeaders().get("a")).isEqualTo("a"); + } +}