public static Set<MessageQueue> topicRouteData2TopicSubscribeInfo( final String topic, final TopicRouteData route) { Set<MessageQueue> mqList = new HashSet<MessageQueue>(); List<QueueData> qds = route.getQueueDatas(); for (QueueData qd : qds) { if (PermName.isReadable(qd.getPerm())) { for (int i = 0; i < qd.getReadQueueNums(); i++) { MessageQueue mq = new MessageQueue(topic, qd.getBrokerName(), i); mqList.add(mq); } } } return mqList; }
@Override public String toString() { return "TopicConfig [topicName=" + topicName + ", readQueueNums=" + readQueueNums + ", writeQueueNums=" + writeQueueNums + ", perm=" + PermName.perm2String(perm) + ", topicFilterType=" + topicFilterType + ", topicSysFlag=" + topicSysFlag + ", order=" + order + "]"; }
public static TopicPublishInfo topicRouteData2TopicPublishInfo( final String topic, final TopicRouteData route) { TopicPublishInfo info = new TopicPublishInfo(); // 顺序消息 if (route.getOrderTopicConf() != null && route.getOrderTopicConf().length() > 0) { String[] brokers = route.getOrderTopicConf().split(";"); for (String broker : brokers) { String[] item = broker.split(":"); int nums = Integer.parseInt(item[1]); for (int i = 0; i < nums; i++) { MessageQueue mq = new MessageQueue(topic, item[0], i); info.getMessageQueueList().add(mq); } } info.setOrderTopic(true); } // 非顺序消息 else { List<QueueData> qds = route.getQueueDatas(); // 排序原因:即使没有配置顺序消息模式,默认队列的顺序同配置的一致。 Collections.sort(qds); for (QueueData qd : qds) { if (PermName.isWriteable(qd.getPerm())) { for (int i = 0; i < qd.getWriteQueueNums(); i++) { MessageQueue mq = new MessageQueue(topic, qd.getBrokerName(), i); info.getMessageQueueList().add(mq); } } } info.setOrderTopic(false); } return info; }
/** 发消息时,如果Topic不存在,尝试创建 */ public TopicConfig createTopicInSendMessageMethod( final String topic, final String defaultTopic, final String remoteAddress, final int clientDefaultTopicQueueNums) { TopicConfig topicConfig = null; boolean createNew = false; try { if (this.lockTopicConfigTable.tryLock(LockTimeoutMillis, TimeUnit.MILLISECONDS)) { try { topicConfig = this.topicConfigTable.get(topic); if (topicConfig != null) return topicConfig; TopicConfig defaultTopicConfig = this.topicConfigTable.get(defaultTopic); if (defaultTopicConfig != null) { if (PermName.isInherited(defaultTopicConfig.getPerm())) { topicConfig = new TopicConfig(topic); int queueNums = clientDefaultTopicQueueNums > defaultTopicConfig.getWriteQueueNums() ? defaultTopicConfig.getWriteQueueNums() : clientDefaultTopicQueueNums; if (queueNums < 0) { queueNums = 0; } topicConfig.setReadQueueNums(queueNums); topicConfig.setWriteQueueNums(queueNums); int perm = defaultTopicConfig.getPerm(); perm &= ~PermName.PERM_INHERIT; topicConfig.setPerm(perm); topicConfig.setTopicFilterType(defaultTopicConfig.getTopicFilterType()); } else { log.warn( "create new topic failed, because the default topic[" + defaultTopic + "] no perm, " + defaultTopicConfig.getPerm() + " producer: " + remoteAddress); } } else { log.warn( "create new topic failed, because the default topic[" + defaultTopic + "] not exist." + " producer: " + remoteAddress); } if (topicConfig != null) { log.info( "create new topic by default topic[" + defaultTopic + "], " + topicConfig + " producer: " + remoteAddress); this.topicConfigTable.put(topic, topicConfig); createNew = true; this.persist(); } } finally { this.lockTopicConfigTable.unlock(); } } } catch (InterruptedException e) { log.error("createTopicInSendMessageMethod exception", e); } if (createNew) { this.brokerController.registerBrokerAll(); } return topicConfig; }