Add Spring Batch Producer example to existing Kafka batch sample (#240)

* Add working batch-produce example in same style as existing sample app

* add documentation and alignment with existing project

* capitalization
This commit is contained in:
Steven PG
2023-02-09 17:58:41 -05:00
committed by GitHub
parent b07961aa1b
commit 547ca663c5
3 changed files with 34 additions and 1 deletions

View File

@@ -2,7 +2,8 @@
This is an example of a Spring Cloud Stream demonstrating the processing of record batches.
The application simply upper-cases the input records and sends them to another topic.
The application simply upper-cases the input records and sends them to another topic using two different
implementations. The `batch-produce` profile enables batch publishing.
=== Running the app:

View File

@@ -20,6 +20,8 @@ import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import org.springframework.beans.factory.annotation.Autowired;
@@ -31,6 +33,8 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Profile;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Component;
@SpringBootApplication
@@ -89,3 +93,16 @@ class Transactions extends Base {
}
}
@Component
@Profile("batch-produce")
class BatchProduce extends Base {
@Bean
Function<List<String>, List<Message<String>>> consumer() {
return list -> list.stream()
.map(string -> string.toUpperCase())
.map(uppercasedString -> MessageBuilder.withPayload(uppercasedString).build())
.collect(Collectors.toList());
}
}

View File

@@ -28,3 +28,18 @@ spring:
binder:
transaction:
transaction-id-prefix: batch-tx-
---
spring:
profiles: batch-produce
cloud:
stream:
bindings:
consumer-in-0:
destination: batch-in
group: batch-in
consumer:
batch-mode: true
consumer-out-0:
destination: batch-out