Use the Java client
Point a Java kafka-clients producer and consumer at monedula-sim-bridge with their default settings, idempotence included, and see them on the canvas.
On this page
The Java client needs no special settings for the bridge: the bootstrap address is the only change. Its defaults, an idempotent producer and a consumer that joins groups with the classic protocol, are what the bridge was tested with.
Before you start
- A paired bridge, from steps 1 and 2 of Connect your first app.
- A Java project with
org.apache.kafka:kafka-clientson the classpath.
Steps
1. Produce
import java.util.Properties;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.common.serialization.StringSerializer;
public class Produce {
public static void main(String[] args) throws Exception {
Properties props = new Properties();
props.put("bootstrap.servers", "127.0.0.1:19092");
props.put("client.id", "java-producer");
props.put("key.serializer", StringSerializer.class.getName());
props.put("value.serializer", StringSerializer.class.getName());
try (KafkaProducer<String, String> producer = new KafkaProducer<>(props)) {
for (int i = 0; i < 20; i++) {
producer.send(new ProducerRecord<>("payments", "k" + i, "payment " + i)).get();
}
}
}
}
client.id is optional; the canvas and the app list name each app by it, so a name of your own is easier to find than a generated one.
payments does not exist in a fresh free-play session. The producer’s first metadata request creates it, as a broker with automatic topic creation does: one partition, replication factor 3 or the number of live brokers if that is lower. The topic appears on the canvas, and the records follow.
2. Consume
import java.time.Duration;
import java.util.List;
import java.util.Properties;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.common.serialization.StringDeserializer;
public class Consume {
public static void main(String[] args) {
Properties props = new Properties();
props.put("bootstrap.servers", "127.0.0.1:19092");
props.put("group.id", "payments-reader");
props.put("auto.offset.reset", "earliest");
props.put("key.deserializer", StringDeserializer.class.getName());
props.put("value.deserializer", StringDeserializer.class.getName());
try (KafkaConsumer<String, String> consumer = new KafkaConsumer<>(props)) {
consumer.subscribe(List.of("payments"));
while (true) {
for (ConsumerRecord<String, String> r : consumer.poll(Duration.ofSeconds(1))) {
System.out.printf("%s-%d@%d %s=%s%n", r.topic(), r.partition(), r.offset(), r.key(), r.value());
}
}
}
}
}
The group payments-reader gets its own consumer lane with the app badge, and its committed offsets show on the canvas like a simulated group’s.
Settings to leave alone
transactional.idon some diskless setups: Nisshi and KafScale refuse it at initialization, and a diskless topic of Ursa, Inkless or the KIP-1163 draft refuses to be in a transaction. Elsewhere transactions work,read_committedconsumers included; see Limits for where they differ from Kafka.group.protocol=consumeron AutoMQ, Redpanda, KafScale, Nisshi and Ursa: these setups have no KIP-848 protocol and the consumer stops withUnsupportedVersionException. Elsewhere the consumer protocol works, with the broker’s 45 second session timeout (the client refusessession.timeout.msunder it).group.idof a group the simulator runs itself: it is refused. Pick a name of your own.
Expect it to be slower than a broker
The playground admits 200 records a second per app, with up to 3000 records saved for a burst, and answers the rest with a throttle time, which the producer honours. The simulated cluster advances one step per tick, so a send(...).get() waits for the canvas. See Limits.