简体   繁体   中英

How to delay consuming message in Kafka nodejs?

I am using NodeJS to consume message from Kafka, after received the message , I will bring it to create an index in Elasticsearch. This is my piece of code :

kafkaConsumer.on('message', function (message) {
    elasticClient.index({
        index: 'test',
        type: 'sample',
        body: message
    }, function (error, response) {
        if (error) {

            // Stop consuming message here

            console.log(error);
        }
        console.log(response);
    });
});

I want to make sure that the index must be created successfully before continue consuming next message, because I don't want any message to be lost.

Have a try consumer.pause()/resume() .
pause() Pause the consumer
resume() Resume the consumer

You can pause your consumer so that it won't subscribe and fetch new messages. Once you get response from elasticClient you can resume your kafka consumer.

kafkaConsumer.pause();
kafkaConsumer.resume();

Use it according to your setup.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM