@Override
  public boolean clear(ConsumerGroup consumerGroup) {
    DefaultMQAdminExt defaultMQAdminExt = new DefaultMQAdminExt();
    defaultMQAdminExt.setInstanceName(Helper.getInstanceName());
    try {
      defaultMQAdminExt.start();
      List<ConsumerGroupHosting> consumerGroupHostingList =
          consumerGroupMapper.queryHosting(
              consumerGroup.getId(), new int[] {Status.ACTIVE.getId()}, 0, 0, null);
      if (null != consumerGroupHostingList && !consumerGroupHostingList.isEmpty()) {
        for (ConsumerGroupHosting hosting : consumerGroupHostingList) {
          defaultMQAdminExt.deleteSubscriptionGroup(
              hosting.getBroker().getAddress(), consumerGroup.getGroupName());
        }
      } else {
        Set<String> masterSet =
            CommandUtil.fetchMasterAddrByClusterName(
                defaultMQAdminExt, consumerGroup.getClusterName());

        if (null != masterSet && !masterSet.isEmpty()) {
          for (String brokerAddress : masterSet) {
            defaultMQAdminExt.deleteSubscriptionGroup(
                brokerAddress, consumerGroup.getGroupName(), 15000L);
          }
        }
      }
    } catch (Exception e) {
      System.out.println("DELETE CONSUMER GROUP ON BROKER FAILED!" + e);
      return false;
    } finally {
      defaultMQAdminExt.shutdown();
    }

    return true;
  }
  @Override
  public boolean update(ConsumerGroup consumerGroup) {
    DefaultMQAdminExt defaultMQAdminExt = new DefaultMQAdminExt();
    defaultMQAdminExt.setInstanceName(Helper.getInstanceName());
    try {
      defaultMQAdminExt.start();
      SubscriptionGroupConfig subscriptionGroupConfig = wrap(consumerGroup);
      List<ConsumerGroupHosting> consumerGroupHostingList =
          consumerGroupMapper.queryHosting(
              consumerGroup.getId(), new int[] {Status.ACTIVE.getId()}, 0, 0, null);
      if (null != consumerGroupHostingList && !consumerGroupHostingList.isEmpty()) {
        for (ConsumerGroupHosting hosting : consumerGroupHostingList) {
          defaultMQAdminExt.createAndUpdateSubscriptionGroupConfig(
              hosting.getBroker().getAddress(), subscriptionGroupConfig);
        }
      } else {
        Set<String> masterSet =
            CommandUtil.fetchMasterAddrByClusterName(
                defaultMQAdminExt, consumerGroup.getClusterName());

        if (null != masterSet && !masterSet.isEmpty()) {
          for (String brokerAddress : masterSet) {
            defaultMQAdminExt.createAndUpdateSubscriptionGroupConfig(
                brokerAddress, subscriptionGroupConfig);
          }
        }
      }
    } catch (Exception e) {
      return false;
    } finally {
      defaultMQAdminExt.shutdown();
    }
    return true;
  }
  @Override
  public void execute(CommandLine commandLine, Options options) {
    DefaultMQAdminExt adminExt = new DefaultMQAdminExt();
    adminExt.setInstanceName(Long.toString(System.currentTimeMillis()));
    try {
      String topic = commandLine.getOptionValue('t').trim();

      if (commandLine.hasOption('c')) {
        String clusterName = commandLine.getOptionValue('c').trim();

        adminExt.start();
        // 删除 broker 上的 topic 信息
        Set<String> masterSet = CommandUtil.fetchMasterAddrByClusterName(adminExt, clusterName);
        adminExt.deleteTopicInBroker(masterSet, topic);
        System.out.printf("delete topic [%s] from cluster [%s] success.\n", topic, clusterName);

        // 删除 NameServer 上的 topic 信息
        Set<String> nameServerSet = null;
        if (commandLine.hasOption('n')) {
          String[] ns = commandLine.getOptionValue('n').trim().split(";");
          nameServerSet = new HashSet(Arrays.asList(ns));
        }
        adminExt.deleteTopicInNameServer(nameServerSet, topic);
        System.out.printf("delete topic [%s] from NameServer success.\n", topic);
        return;
      }

      ServerUtil.printCommandLineHelp("mqadmin " + this.commandName(), options);
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      adminExt.shutdown();
    }
  }
  @Override
  public void execute(CommandLine commandLine, Options options) {
    DefaultMQAdminExt adminExt = new DefaultMQAdminExt();
    adminExt.setInstanceName(Long.toString(System.currentTimeMillis()));
    try {
      // groupName
      String groupName = commandLine.getOptionValue('g').trim();

      if (commandLine.hasOption('b')) {
        String addr = commandLine.getOptionValue('b').trim();
        adminExt.start();

        adminExt.deleteSubscriptionGroup(addr, groupName);
        System.out.printf(
            "delete subscription group [%s] from broker [%s] success.\n", groupName, addr);

        return;
      } else if (commandLine.hasOption('c')) {
        String clusterName = commandLine.getOptionValue('c').trim();
        adminExt.start();

        Set<String> masterSet = CommandUtil.fetchMasterAddrByClusterName(adminExt, clusterName);
        for (String master : masterSet) {
          adminExt.deleteSubscriptionGroup(master, groupName);
          System.out.printf(
              "delete subscription group [%s] from broker [%s] in cluster [%s] success.\n",
              groupName, master, clusterName);
        }

        return;
      }

      ServerUtil.printCommandLineHelp("mqadmin " + this.commandName(), options);
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      adminExt.shutdown();
    }
  }
  @Override
  public void execute(final CommandLine commandLine, final Options options, RPCHook rpcHook) {
    DefaultMQAdminExt defaultMQAdminExt = new DefaultMQAdminExt(rpcHook);
    defaultMQAdminExt.setInstanceName(Long.toString(System.currentTimeMillis()));

    try {
      TopicConfig topicConfig = new TopicConfig();
      topicConfig.setReadQueueNums(8);
      topicConfig.setWriteQueueNums(8);
      topicConfig.setTopicName(commandLine.getOptionValue('t').trim());

      // readQueueNums
      if (commandLine.hasOption('r')) {
        topicConfig.setReadQueueNums(Integer.parseInt(commandLine.getOptionValue('r').trim()));
      }

      // writeQueueNums
      if (commandLine.hasOption('w')) {
        topicConfig.setWriteQueueNums(Integer.parseInt(commandLine.getOptionValue('w').trim()));
      }

      // perm
      if (commandLine.hasOption('p')) {
        topicConfig.setPerm(Integer.parseInt(commandLine.getOptionValue('p').trim()));
      }

      boolean isUnit = false;
      if (commandLine.hasOption('u')) {
        isUnit = Boolean.parseBoolean(commandLine.getOptionValue('u').trim());
      }

      boolean isCenterSync = false;
      if (commandLine.hasOption('s')) {
        isCenterSync = Boolean.parseBoolean(commandLine.getOptionValue('s').trim());
      }

      int topicCenterSync = TopicSysFlag.buildSysFlag(isUnit, isCenterSync);
      topicConfig.setTopicSysFlag(topicCenterSync);

      boolean isOrder = false;
      if (commandLine.hasOption('o')) {
        isOrder = Boolean.parseBoolean(commandLine.getOptionValue('o').trim());
      }
      topicConfig.setOrder(isOrder);

      if (commandLine.hasOption('b')) {
        String addr = commandLine.getOptionValue('b').trim();

        defaultMQAdminExt.start();
        defaultMQAdminExt.createAndUpdateTopicConfig(addr, topicConfig);

        if (isOrder) {
          String brokerName = CommandUtil.fetchBrokerNameByAddr(defaultMQAdminExt, addr);
          String orderConf = brokerName + ":" + topicConfig.getWriteQueueNums();
          defaultMQAdminExt.createOrUpdateOrderConf(topicConfig.getTopicName(), orderConf, false);
          System.out.println(
              String.format(
                  "set broker orderConf. isOrder=%s, orderConf=[%s]",
                  isOrder, orderConf.toString()));
        }
        System.out.printf("create topic to %s success.%n", addr);
        System.out.println(topicConfig);
        return;

      } else if (commandLine.hasOption('c')) {
        String clusterName = commandLine.getOptionValue('c').trim();

        defaultMQAdminExt.start();

        Set<String> masterSet =
            CommandUtil.fetchMasterAddrByClusterName(defaultMQAdminExt, clusterName);
        for (String addr : masterSet) {
          defaultMQAdminExt.createAndUpdateTopicConfig(addr, topicConfig);
          System.out.printf("create topic to %s success.%n", addr);
        }

        if (isOrder) {
          Set<String> brokerNameSet =
              CommandUtil.fetchBrokerNameByClusterName(defaultMQAdminExt, clusterName);
          StringBuilder orderConf = new StringBuilder();
          String splitor = "";
          for (String s : brokerNameSet) {
            orderConf.append(splitor).append(s).append(":").append(topicConfig.getWriteQueueNums());
            splitor = ";";
          }
          defaultMQAdminExt.createOrUpdateOrderConf(
              topicConfig.getTopicName(), orderConf.toString(), true);
          System.out.println(
              String.format(
                  "set cluster orderConf. isOrder=%s, orderConf=[%s]",
                  isOrder, orderConf.toString()));
        }

        System.out.println(topicConfig);
        return;
      }

      ServerUtil.printCommandLineHelp("mqadmin " + this.commandName(), options);
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      defaultMQAdminExt.shutdown();
    }
  }