zifengmo commented on issue #21557:
URL: https://github.com/apache/pulsar/issues/21557#issuecomment-1818118000

   > BTW, the messages will be packaged as a batched message if you enabled 
`enableBatching`(default value is `true`)
   
   The code of the producer builder:
   
   private Producer getProducer(String topic) throws PulsarClientException {
           Producer producer = producerMap.get(topic);
           if (producer != null) {
               return producer;
           }
           producerCreationLock.lock();
           try {
               producer = producerMap.get(topic);
               if (producer == null) {
                   producer = pulsarClient.newProducer()
                           .sendTimeout(3, TimeUnit.SECONDS)
                           .enableBatching(false)
                           .topic(topic)
                           
.messageRoutingMode(MessageRoutingMode.CustomPartition).messageRouter(new 
MessageRouter() {
                               @Override
                               public int choosePartition(Message<?> message, 
TopicMetadata metadata) {
                                   int topicPartitionNum = 
metadata.numPartitions();
                                   int keyHashCode = 
message.getKey().hashCode();
                                   if (keyHashCode == Integer.MIN_VALUE) {
                                       keyHashCode = topicPartitionNum - 1;
                                   }
                                   return Math.abs(keyHashCode) % 
topicPartitionNum;
                               }
                           }).create();
                   producerMap.put(topic, producer);
               }
           } finally {
               producerCreationLock.unlock();
           }
           return producer;
       }


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to