提交 e1a42e4e 编写于 作者: 释一 提交者: yukon

Fix random int bug in SelectMessageQueueByRandom

Author: 释一 <quanwei.lqw@alibaba-inc.com>

Closes #173 from swwlqw/master.
上级 81884c8d
...@@ -22,17 +22,12 @@ import org.apache.rocketmq.client.producer.MessageQueueSelector; ...@@ -22,17 +22,12 @@ import org.apache.rocketmq.client.producer.MessageQueueSelector;
import org.apache.rocketmq.common.message.Message; import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.common.message.MessageQueue; import org.apache.rocketmq.common.message.MessageQueue;
public class SelectMessageQueueByRandoom implements MessageQueueSelector { public class SelectMessageQueueByRandom implements MessageQueueSelector {
private Random random = new Random(System.currentTimeMillis()); private Random random = new Random(System.currentTimeMillis());
@Override @Override
public MessageQueue select(List<MessageQueue> mqs, Message msg, Object arg) { public MessageQueue select(List<MessageQueue> mqs, Message msg, Object arg) {
int value = random.nextInt(); int value = random.nextInt(mqs.size());
if (value < 0) {
value = Math.abs(value);
}
value = value % mqs.size();
return mqs.get(value); return mqs.get(value);
} }
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册