androidkaifa1 opened a new issue #2170:
URL: https://github.com/apache/rocketmq/issues/2170
public List<RegisterBrokerResult> registerBrokerAll(
final String clusterName,
final String brokerAddr,
final String brokerName,
final long brokerId,
final String haServerAddr,
final TopicConfigSerializeWrapper topicConfigWrapper,
final List<String> filterServerList,
final boolean oneway,
final int timeoutMills,
final boolean compressed) {
// 初始化一个List,存放每个NameServer注册结果的
<font color="red"> // 多线程 会有并发问题吧
final List<RegisterBrokerResult> registerBrokerResultList =
Lists.newArrayList();
</font>
// 获取 NameServer 地址列表
List<String> nameServerAddressList =
this.remotingClient.getNameServerAddressList();
if (nameServerAddressList != null && nameServerAddressList.size() >
0) {
// 构建请求头,在请求头里面放很多的信息,比如说 BrokerId 和 BrokerName
final RegisterBrokerRequestHeader requestHeader = new
RegisterBrokerRequestHeader();
requestHeader.setBrokerAddr(brokerAddr);
requestHeader.setBrokerId(brokerId);
requestHeader.setBrokerName(brokerName);
requestHeader.setClusterName(clusterName);
requestHeader.setHaServerAddr(haServerAddr);
requestHeader.setCompressed(compressed);
// 构建请求体,包含一些配置
RegisterBrokerBody requestBody = new RegisterBrokerBody();
requestBody.setTopicConfigSerializeWrapper(topicConfigWrapper);
requestBody.setFilterServerList(filterServerList);
final byte[] body = requestBody.encode(compressed);
final int bodyCrc32 = UtilAll.crc32(body);
requestHeader.setBodyCrc32(bodyCrc32);
// 使用CountDownLatch同步计数器,保证注册完全部的 NameServer之后才往下走,
// 执行其他逻辑
final CountDownLatch countDownLatch = new
CountDownLatch(nameServerAddressList.size());
// 遍历NameServer 地址列表,使用线程池去注册
for (final String namesrvAddr : nameServerAddressList) {
brokerOuterExecutor.execute(new Runnable() {
@Override
public void run() {
try {
// 调用 registerBroker 真正执行注册
RegisterBrokerResult result =
registerBroker(namesrvAddr,oneway, timeoutMills,requestHeader,body);
**if (result != null) {
// 注册成功结果放到一个List里去
registerBrokerResultList.add(result);
}**
log.info("register broker[{}]to name server {}
OK", brokerId, namesrvAddr);
} catch (Exception e) {
log.warn("registerBroker Exception, {}",
namesrvAddr, e);
} finally {
// 注册完,执行 countDownLatch.countDown(); 同步计数器 -1
countDownLatch.countDown();
}
}
});
}
try {
// 等待所有 NameServer 都注册完,才返回注册结果
countDownLatch.await(timeoutMills, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
}
}
return registerBrokerResultList;
}
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
[email protected]