Jaskey commented on a change in pull request #149: [ROCKETMQ-136]Provide a handy message queue producer for order message sharding URL: https://github.com/apache/incubator-rocketmq/pull/149#discussion_r140944583
########## File path: client/src/main/java/org/apache/rocketmq/client/producer/selector/SelectMessageQueueByConsistentHash.java ########## @@ -0,0 +1,94 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.rocketmq.client.producer.selector; + +import org.apache.rocketmq.client.producer.MessageQueueSelector; +import org.apache.rocketmq.common.consistenthash.ConsistentHashRouter; +import org.apache.rocketmq.common.consistenthash.Node; +import org.apache.rocketmq.common.message.Message; +import org.apache.rocketmq.common.message.MessageQueue; + +import java.util.ArrayList; +import java.util.List; + +public class SelectMessageQueueByConsistentHash implements MessageQueueSelector { + + private final ConsistentHashRouter<MQNode> consistentHashRouter = new ConsistentHashRouter<MQNode>(null, 0); + private volatile List<MessageQueue> lastMqs = new ArrayList<MessageQueue>(); + private final int virtualNodeNum; + + public SelectMessageQueueByConsistentHash() { + this.virtualNodeNum = 10; + } + + public SelectMessageQueueByConsistentHash(int virtualNodeNum) { + this.virtualNodeNum = virtualNodeNum; + } + + @Override + public MessageQueue select(List<MessageQueue> mqs, Message msg, Object arg) { + if (arg == null) { + throw new NullPointerException(); + } + synchronized (consistentHashRouter) { + rehash(mqs); + return consistentHashRouter.routeNode(arg.toString()).mq; + } + } + + private void rehash(List<MessageQueue> mqs) { Review comment: Yes we can, but I don't think it will have explicit better performance since , which just reduce two inner for loop in memory operation. However, maybe somebody may consider it is more easy to read. What do you think @zhouxinyu @shroman ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services