All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.alibaba.rocketmq.tools.command.CommandUtil Maven / Gradle / Ivy

There is a newer version: 3.2.6
Show newest version
/**
 * 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.tools.command;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import com.alibaba.rocketmq.client.exception.MQBrokerException;
import com.alibaba.rocketmq.common.MixAll;
import com.alibaba.rocketmq.common.protocol.body.ClusterInfo;
import com.alibaba.rocketmq.common.protocol.route.BrokerData;
import com.alibaba.rocketmq.remoting.exception.RemotingConnectException;
import com.alibaba.rocketmq.remoting.exception.RemotingSendRequestException;
import com.alibaba.rocketmq.remoting.exception.RemotingTimeoutException;
import com.alibaba.rocketmq.tools.admin.MQAdminExt;


/**
 * 各个子命令的接口
 * 
 * @author shijia.wxr
 * @since 2013-7-25
 */
public class CommandUtil {
    public static Set fetchMasterAddrByClusterName(final MQAdminExt adminExt, final String clusterName)
            throws InterruptedException, RemotingConnectException, RemotingTimeoutException,
            RemotingSendRequestException, MQBrokerException {
        Set masterSet = new HashSet();

        ClusterInfo clusterInfoSerializeWrapper = adminExt.examineBrokerClusterInfo();

        Set brokerNameSet = clusterInfoSerializeWrapper.getClusterAddrTable().get(clusterName);

        if (brokerNameSet != null) {
            for (String brokerName : brokerNameSet) {
                BrokerData brokerData = clusterInfoSerializeWrapper.getBrokerAddrTable().get(brokerName);
                if (brokerData != null) {

                    String addr = brokerData.getBrokerAddrs().get(MixAll.MASTER_ID);
                    if (addr != null) {
                        masterSet.add(addr);
                    }
                }
            }
        }
        else {
            System.out
                .printf("[error] Make sure the specified clusterName exists or the nameserver which connected is correct.");
        }

        return masterSet;
    }


    public static Set fetchBrokerNameByClusterName(final MQAdminExt adminExt, final String clusterName)
            throws Exception {
        ClusterInfo clusterInfoSerializeWrapper = adminExt.examineBrokerClusterInfo();
        Set brokerNameSet = clusterInfoSerializeWrapper.getClusterAddrTable().get(clusterName);
        if (brokerNameSet.isEmpty()) {
            throw new Exception(
                "Make sure the specified clusterName exists or the nameserver which connected is correct.");
        }
        return brokerNameSet;
    }


    public static String fetchBrokerNameByAddr(final MQAdminExt adminExt, final String addr) throws Exception {
        ClusterInfo clusterInfoSerializeWrapper = adminExt.examineBrokerClusterInfo();
        HashMap brokerAddrTable =
                clusterInfoSerializeWrapper.getBrokerAddrTable();
        Iterator> it = brokerAddrTable.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry entry = it.next();
            HashMap brokerAddrs = entry.getValue().getBrokerAddrs();
            if (brokerAddrs.containsValue(addr))
                return entry.getKey();
        }
        throw new Exception(
            "Make sure the specified broker addr exists or the nameserver which connected is correct.");
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy