Writing to Kafka Topics
The setup for this tutorial is the same as for Listening to Kafka Topics. Please refer to its "Dependencies" section for required libraries.
The KafkaTemplate
Writing to a Kafka topic is pretty straight forward. You will need a KafkaTemplate that you can create as follows.
In a Spring managed class, add a new KafkaTemplate class variable:
private KafkaTemplate<Integer, String> template;
Instantiate this variable in a post-construct method:
@PostConstruct private void init() { Map<String, Object> senderProps = senderProps(); ProducerFactory<Integer, String> pf = new DefaultKafkaProducerFactory<Integer, String>(senderProps); template = new KafkaTemplate<>(pf); }
where senderProps()
is a method that looks like the following:
private Map<String, Object> senderProps() { Map<String, Object> props = new HashMap<>(); props.put(ProducerConfig.CLIENT_ID_CONFIG, "test.app.producer"); props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092"); props.put(ProducerConfig.RETRIES_CONFIG, 0); props.put(ProducerConfig.BATCH_SIZE_CONFIG, 16384); props.put(ProducerConfig.LINGER_MS_CONFIG, 1); props.put(ProducerConfig.BUFFER_MEMORY_CONFIG, 33554432); props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, IntegerSerializer.class); props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class); return props; }
Make sure that your client id is unique and that the bootstrap servers config points to your Kafka instance. Depending on your use-case you can change the configurations as needed.
Sending messages
Once this setup is complete, you can send messages to Kafka with one line like this:
template.send("topic", "msg");
where "topic" is the topic you want to send a message to, and "msg" is the message.