0
0
Kafkadevops~10 mins

Java consumer client in Kafka - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Java consumer client
Start Java Consumer
Configure Properties
Create KafkaConsumer
Subscribe to Topic
Poll for Records
Process Records
Commit Offsets
Repeat Poll or Exit
Close Consumer
This flow shows how a Java Kafka consumer starts, subscribes to a topic, polls messages, processes them, commits offsets, and closes.
Execution Sample
Kafka
Properties props = new Properties();
props.put("bootstrap.servers", "localhost:9092");
props.put("group.id", "test-group");
KafkaConsumer<String, String> consumer = new KafkaConsumer<>(props, new StringDeserializer(), new StringDeserializer());
consumer.subscribe(Collections.singletonList("my-topic"));
ConsumerRecords<String, String> records = consumer.poll(Duration.ofMillis(100));
This code sets up a Kafka consumer, subscribes to 'my-topic', and polls for messages.
Process Table
StepActionEvaluationResult
1Create Properties objectProperties emptyProperties with bootstrap.servers and group.id set
2Create KafkaConsumer with properties and deserializersKafkaConsumer instance createdConsumer ready to subscribe
3Subscribe to topic 'my-topic'Subscription successfulConsumer listens to 'my-topic'
4Poll for records with 100ms timeoutPoll sent to Kafka brokerConsumerRecords received (may be empty)
5Process each recordIterate over recordsRecords processed (e.g., print key/value)
6Commit offsetsOffsets committed synchronouslyKafka knows messages processed
7Repeat poll or exitLoop continues or stopsConsumer continues polling or closes
8Close consumerConsumer closedResources freed, connection closed
💡 Consumer closes after processing or on application shutdown
Status Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5After Step 6Final
propsempty{"bootstrap.servers":"localhost:9092","group.id":"test-group"}{"bootstrap.servers":"localhost:9092","group.id":"test-group"}{"bootstrap.servers":"localhost:9092","group.id":"test-group"}{"bootstrap.servers":"localhost:9092","group.id":"test-group"}{"bootstrap.servers":"localhost:9092","group.id":"test-group"}{"bootstrap.servers":"localhost:9092","group.id":"test-group"}unchanged
consumernullnullKafkaConsumer instanceKafkaConsumer subscribedKafkaConsumer polledKafkaConsumer processed recordsKafkaConsumer committed offsetsKafkaConsumer closed
recordsnullnullnullConsumerRecords (may be empty or contain messages)Processed recordsProcessed recordsProcessed recordsnull
Key Moments - 3 Insights
Why do we need to set 'group.id' in the properties?
The 'group.id' identifies the consumer group this client belongs to, which Kafka uses to manage message delivery and offset commits. See execution_table step 1 where properties are set.
What happens if the poll() call returns no records?
If poll() returns no records, the consumer waits and polls again in the loop. This is normal if no new messages are available. See execution_table step 4.
Why must we commit offsets after processing records?
Committing offsets tells Kafka which messages have been processed, so on restart the consumer doesn't reprocess them. See execution_table step 6.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4, what does the poll() method return?
AA ConsumerRecords object containing messages or empty
BA Properties object with configuration
CA KafkaConsumer instance
DA list of topic names
💡 Hint
Refer to execution_table row 4 under Result column
At which step is the consumer subscribed to the topic?
AStep 2
BStep 3
CStep 5
DStep 6
💡 Hint
Check execution_table row 3 Action and Result columns
If we omit committing offsets, what will likely happen on consumer restart?
AConsumer will skip messages already processed
BConsumer will reprocess messages from last committed offset
CConsumer will fail to connect to Kafka
DConsumer will automatically commit offsets
💡 Hint
See key_moments answer about offset commits and execution_table step 6
Concept Snapshot
Java Kafka Consumer Client:
- Configure Properties (bootstrap.servers, group.id)
- Create KafkaConsumer with deserializers
- Subscribe to topic(s)
- Poll for messages in a loop
- Process messages
- Commit offsets to track progress
- Close consumer on shutdown
Full Transcript
This visual execution trace shows how a Java Kafka consumer client works step-by-step. First, properties are configured with the Kafka server address and consumer group ID. Then, a KafkaConsumer instance is created using these properties and deserializers for keys and values. The consumer subscribes to a topic, for example 'my-topic'. It then polls the Kafka broker for new messages with a timeout. If messages are received, the consumer processes each record, such as printing the key and value. After processing, the consumer commits the offsets to Kafka to mark messages as consumed. This polling and processing loop continues until the consumer is closed, freeing resources and closing connections. Key points include the importance of group.id for consumer groups, handling empty polls, and committing offsets to avoid reprocessing messages on restart.