Akhil
Akhil

Reputation: 1254

Creating new Kafka Producer object for every request

I am developing a rest service which will publish event to kafka (using java client) on the basic of topic, different topic can have different no. of brokers. I'm creating Producer for each and every request to send message into Kafka. I feel this is inefficient and can be achieved in more manageable by maintaining a pool of producers, will it really help? Please provide suggestions.

Upvotes: 1

Views: 3187

Answers (1)

Matthias J. Sax
Matthias J. Sax

Reputation: 62285

You can just use a single producer and write to different topics (https://kafka.apache.org/090/javadoc/index.html?org/apache/kafka/clients/producer/KafkaProducer.html). The class ProducerRecord offers a constructor that allows to specify the topic a record should be written to (https://kafka.apache.org/090/javadoc/index.html?org/apache/kafka/clients/producer/ProducerRecord.html)

Properties props = new Properties();
props.put("bootstrap.servers", "localhost:4242");
props.put("acks", "all");
props.put("retries", 0);
props.put("batch.size", 16384);
props.put("linger.ms", 1);
props.put("buffer.memory", 33554432);
props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");

Producer<String, String> producer = new KafkaProducer<>(props);
for(int i = 0; i < 100; i++)
    producer.send(new ProducerRecord<String, String>("my-topic", Integer.toString(i), Integer.toString(i)));

producer.close();

Upvotes: 1

Related Questions