/** * Copyright (C) 2010-2013 Alibaba Group Holding Limited * * Licensed 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 com.alibaba.rocketmq.client.impl.producer; import java.util.ArrayList; import java.util.List; import java.util.concurrent.atomic.AtomicInteger; import com.alibaba.rocketmq.common.message.MessageQueue; /** * 发布Topic用到的路由信息 * * @author shijia.wxr<vintage.wang@gmail.com> * @since 2013-7-24 */ public class TopicPublishInfo { private boolean orderTopic = false; private List<MessageQueue> messageQueueList = new ArrayList<MessageQueue>(); private AtomicInteger sendWhichQueue = new AtomicInteger(0); public boolean isOrderTopic() { return orderTopic; } public boolean ok() { return null != this.messageQueueList && !this.messageQueueList.isEmpty(); } public void setOrderTopic(boolean orderTopic) { this.orderTopic = orderTopic; } public List<MessageQueue> getMessageQueueList() { return messageQueueList; } public void setMessageQueueList(List<MessageQueue> messageQueueList) { this.messageQueueList = messageQueueList; } public AtomicInteger getSendWhichQueue() { return sendWhichQueue; } public void setSendWhichQueue(AtomicInteger sendWhichQueue) { this.sendWhichQueue = sendWhichQueue; } /** * 如果lastBrokerName不为null,则寻找与其不同的MessageQueue */ public MessageQueue selectOneMessageQueue(final String lastBrokerName) { if (lastBrokerName != null) { int index = this.sendWhichQueue.getAndIncrement(); for (int i = 0; i < this.messageQueueList.size(); i++) { int pos = Math.abs(index++) % this.messageQueueList.size(); MessageQueue mq = this.messageQueueList.get(pos); if (!mq.getBrokerName().equals(lastBrokerName)) { return mq; } } return null; } else { int index = this.sendWhichQueue.getAndIncrement(); int pos = Math.abs(index) % this.messageQueueList.size(); return this.messageQueueList.get(pos); } } @Override public String toString() { return "TopicPublishInfo [orderTopic=" + orderTopic + ", messageQueueList=" + messageQueueList + ", sendWhichQueue=" + sendWhichQueue + "]"; } }