I saw and implemented the method KafkaTemplate.send(TOPIC,message) with default partitioner class.
But here, I am not passing keys. I have a simple custom partitioner class and I also wanna send to kafka server like KafkaTemplate(TOPIC,key,message) where in producerConfig I set my customPartitioner class for partitioning.
I saw this Will send(Topic, Key, Message) method of KafkaTemplate calls Partition method if I provide custom Partitioner? but I didn't get it fully.
- my simple customPartitioner class:
public class CustomPartitionar implements Partitioner {
private PartitionMapper newMapper;
public CustomPartitionar(){
newMapper = new PartitionMapper();
}
@Override
public void configure(Map<String, ?> configs) {
}
@Override
public int partition(String topic, Object key, byte[] keyBytes, Object value, byte[] valueBytes,Cluster cluster) {
int partition = 0;
String userName = (String) key;
// Find the id of current user based on the username from another mapper class
Integer userId = newMapper.findUserId(userName);
// If the userId not found, default partition is 0
if (userId != null) {
partition = userId;
}
return partition;
}
@Override
public void close() {
}
}
- added this class to producerFactory:
config.put(ProducerConfig.PARTITIONER_CLASS_CONFIG, CustomPartitionar.class);
- actually my key will be get from "message.getReceiver()" and topic will be get from "message.getTopic()" so my messages will go to desired topic and partition belongs to that user/group..so I just wanna send like:
KafkaTemplate.send(message.getTopic(),message.getReceiver(),message)
can this be possible in a simple way or am I missing something?