Breaking News: Grepper is joining You.com. Read the official announcement!
Check it out

Stopping a Consumer

Sumit Rawal answered on June 1, 2023 Popularity 1/10 Helpfulness 1/10

Contents


More Related Answers


Stopping a Consumer

0

In the previous lessons, we showed code for the Kafka consumer that ran an infinite poll loop. This brings up the question of how we can gracefully stop a consumer. The KafkaConsumer object exposes a method wakeup() that can be invoked from a different thread to stop the consumer. Note that wakeup() is the only method of the consumer object that can be invoked safely from another thread. When wakeup() is invoked on the consumer object, the consumer throws a WakeupException if the consumer is already waiting on the poll() method. If not, the exception is thrown the next time the consumer invokes the poll() method. As a developer, you don’t need to handle the WakeupException, but you must invoke close() on the consumer object in the finally block. Closing the consumer commits offsets and informs the broker that the consumer is leaving. The broker can then trigger a rebalance immediately rather than wait for a session timeout to assign the partitions owned by the exiting consumer to other consumers in the group.

The code widget below demonstrates how to write a consumer to gracefully exit the infinite poll loop. 

Popularity 1/10 Helpfulness 1/10 Language java
Source: Grepper
Tags: consumer java
Link to this answer
Share Copy Link
Contributed on Jun 01 2023
Sumit Rawal
0 Answers  Avg Quality 2/10


X

Continue with Google

By continuing, I agree that I have read and agree to Greppers's Terms of Service and Privacy Policy.
X
Grepper Account Login Required

Oops, You will need to install Grepper and log-in to perform this action.