R
R
Rammstein0072021-01-14 22:56:52
Java
Rammstein007, 2021-01-14 22:56:52

How to send a batch of messages to kafka?

Now one message is transmitted how to send a bunch of messages? Below is the code that sends one message at a time.

public void sendMessages(List<EntryTestJson> msgList)
    {
        String topic = "test";
        try (Producer<String, String> producer = createProducer())
        {
            for (EntryTestJson message : msgList)
            {
                ProducerRecord<String, String> record = new ProducerRecord<>(topic, message.toString());
                producer.send(record, new RecordCallback());
                producer.flush();
                log.info("Отправлено сообщение: " + record.value());
            }
        } catch (Exception e)
        {
            log.error("Ошибка при отправке сообщения", e);
        }
    }

Answer the question

In order to leave comments, you need to log in

2 answer(s)
L
Loli E1ON, 2021-01-14
@E1ON

https://stackoverflow.com/questions/31275065/is-ka...

I
Igor Cherny, 2021-01-16
@freeg0r

there is no such approach for business logic, Kafka client behind the scenes itself generates message batches for sending, the boundary size and formation time of which you can change with configuration settings (batch.size, linger.ms) and which will be sent to the same partition. Given that partition selection can be based on a message key, manually composing packages (in business code) can create additional problems.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question