Skip to content

Commit 8cc3079

Browse files
committed
docs: document batching producer
1 parent 6c8e63e commit 8cc3079

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

src/main/java/no/sysco/middleware/workshop/kafka/producer/ProducerWithoutBatchingApp.java renamed to src/main/java/no/sysco/middleware/workshop/kafka/producer/ProducerOneByOneApp.java

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,21 @@
99
import java.io.IOException;
1010
import java.util.Properties;
1111

12-
public class ProducerWithoutBatchingApp {
12+
/**
13+
* Creates a Producer to send messages to Kafka one by one, without Batching, or holding messages
14+
* in memory.
15+
*
16+
* This is not recommended if you are trying to achieve high throughput, as it will send each
17+
* message individually.
18+
*
19+
* It will reuse the same topic, or create it.
20+
*/
21+
public class ProducerOneByOneApp {
1322

1423
private final KafkaProducer<String, String> kafkaProducer;
1524

16-
private ProducerWithoutBatchingApp() {
25+
private ProducerOneByOneApp() {
26+
// Setting Producer configs
1727
final Properties producerConfigs = new Properties();
1828
producerConfigs.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, CommonProperties.BOOTSTRAP_SERVERS);
1929
producerConfigs.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
@@ -30,9 +40,13 @@ private void sendRecord() {
3040
kafkaProducer.flush();
3141
}
3242

43+
/**
44+
* Run Producer application
45+
*/
3346
public static void main(String[] args) throws IOException {
34-
final ProducerWithoutBatchingApp producerWithAckApp = new ProducerWithoutBatchingApp();
47+
final ProducerOneByOneApp producerWithAckApp = new ProducerOneByOneApp();
3548
producerWithAckApp.sendRecord();
49+
3650
System.out.println("Press ENTER to exit the system");
3751
System.in.read();
3852
}

src/main/java/no/sysco/middleware/workshop/kafka/producer/ProducerWithBatchingApp.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@
1111
import java.util.Properties;
1212
import java.util.stream.IntStream;
1313

14+
/**
15+
* Creates a Producer to send messages that will be batched first by size: trying to hold 100KB
16+
* in memory before sending to Kafka, and then if this is taking too long, it will wait for 10 sec.
17+
* for this. It will send the batch every 10 secs if no 100KB are batched.
18+
*/
1419
public class ProducerWithBatchingApp {
1520

1621
private final KafkaProducer<String, String> kafkaProducer;
@@ -30,11 +35,12 @@ private ProducerWithBatchingApp() {
3035
private void sendRecord() {
3136
final ProducerRecord<String, String> record =
3237
new ProducerRecord<>("simple-topic", "record");
33-
kafkaProducer.send(record, (metadata, exception) -> {
34-
System.out.println("Ack received");
35-
});
38+
kafkaProducer.send(record, (metadata, exception) -> System.out.println("Ack received"));
3639
}
3740

41+
/**
42+
* Run Producer Application
43+
*/
3844
public static void main(String[] args) throws IOException {
3945
final ProducerWithBatchingApp producerWithAckApp = new ProducerWithBatchingApp();
4046
IntStream.range(0, 1000).forEach(ignored -> producerWithAckApp.sendRecord());

0 commit comments

Comments
 (0)