Producing Message

We can use KafkaTemplate to produce the message directly. By default as of Kafka 2.4, it is using StickyPartitioner:

java - Kafka RoundRobin partitioner not distributing messages to 4 partitions - Stack Overflow

To change it back to RoundRobinPartitioner (randomly), we can set in ProducerConfig.PARTITIONER_CLASS_CONFIG

Example:

Configuration

@Configuration  
public class KafkaConfiguration {
	@Bean  
	public ProducerFactory<String, String> producerFactory() {  
	  return new DefaultKafkaProducerFactory<>(  
	    Map.of(  
	      ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9093",  
	      ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class,  
	      ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class,  
	      ProducerConfig.PARTITIONER_CLASS_CONFIG, RoundRobinPartitioner.class  
	    ));  
	}

	@Bean  
	public KafkaTemplate<String, String> kafkaTemplate() {  
	  return new KafkaTemplate<>(producerFactory());  
	}
}

Service

@Service
public class DefaultEventProducer {

  private final KafkaTemplate<String, String> kafkaTemplate;

  public DefaultEventProducer(KafkaTemplate<String, String> kafkaTemplate) {
    this.kafkaTemplate = kafkaTemplate;
  }

  public void sendMessage(String message) {
    new ProducerRecord<String, String>(TOPIC_NAME, message);
    kafkaTemplate.send(TOPIC_NAME, message);
  }
}