@Override
  public void execute(final CommandLine commandLine, final Options options) {
    DefaultMQAdminExt defaultMQAdminExt = new DefaultMQAdminExt();
    defaultMQAdminExt.setInstanceName(Long.toString(System.currentTimeMillis()));

    try {
      String topic = commandLine.getOptionValue('t').trim();
      String type = commandLine.getOptionValue('m').trim();

      if ("get".equals(type)) {
        // 获取顺序消息
        defaultMQAdminExt.start();
        String orderConf =
            defaultMQAdminExt.getKVConfig(NamesrvUtil.NAMESPACE_ORDER_TOPIC_CONFIG, topic);
        System.out.printf("get orderConf success. topic=[%s], orderConf=[%s] ", topic, orderConf);

        return;
      } else if ("put".equals(type)) {
        // 更新顺序消息
        defaultMQAdminExt.start();
        String orderConf = "";
        if (commandLine.hasOption('v')) {
          orderConf = commandLine.getOptionValue('v').trim();
        }
        if (UtilAll.isBlank(orderConf)) {
          throw new Exception("please set orderConf with option -v.");
        }

        defaultMQAdminExt.createOrUpdateOrderConf(topic, orderConf, true);
        System.out.printf(
            "update orderConf success. topic=[%s], orderConf=[%s]", topic, orderConf.toString());
        return;
      } else if ("delete".equals(type)) {
        // 删除顺序消息
        defaultMQAdminExt.start();
        defaultMQAdminExt.deleteKvConfig(NamesrvUtil.NAMESPACE_ORDER_TOPIC_CONFIG, topic);
        System.out.printf("delete orderConf success. topic=[%s]", topic);

        return;
      }

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

    defaultMQAdminExt.setInstanceName(Long.toString(System.currentTimeMillis()));

    long printInterval = 1;
    boolean enableInterval = commandLine.hasOption('i');

    if (enableInterval) {
      printInterval = Long.parseLong(commandLine.getOptionValue('i')) * 1000;
    }

    try {
      defaultMQAdminExt.start();

      do {
        if (commandLine.hasOption('m')) {
          this.printClusterMoreStats(defaultMQAdminExt);
        } else {
          this.printClusterBaseInfo(defaultMQAdminExt);
        }

        Thread.sleep(printInterval);

        System.out.println("");
      } while (enableInterval);
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      defaultMQAdminExt.shutdown();
    }
  }
  @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, RPCHook rpcHook) {
    DefaultMQAdminExt defaultMQAdminExt = new DefaultMQAdminExt(rpcHook);

    defaultMQAdminExt.setInstanceName(Long.toString(System.currentTimeMillis()));

    try {
      defaultMQAdminExt.start();

      String brokerAddr = commandLine.getOptionValue('b').trim();

      KVTable kvTable = defaultMQAdminExt.fetchBrokerRuntimeStats(brokerAddr);

      // 为了排序
      TreeMap<String, String> tmp = new TreeMap<String, String>();
      tmp.putAll(kvTable.getTable());

      Iterator<Entry<String, String>> it = tmp.entrySet().iterator();
      while (it.hasNext()) {
        Entry<String, String> next = it.next();
        System.out.printf("%-32s: %s\n", next.getKey(), next.getValue());
      }
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      defaultMQAdminExt.shutdown();
    }
  }
  @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();
    }
  }
  void queryByKey(final DefaultMQAdminExt admin, final String topic, final String key)
      throws MQClientException, InterruptedException {
    admin.start();

    QueryResult queryResult = admin.queryMessage(topic, key, 32, 0, Long.MAX_VALUE);
    System.out.printf(
        "%-50s %-4s  %s\n", //
        "#Message ID", //
        "#QID", //
        "#Offset");
    for (MessageExt msg : queryResult.getMessageList()) {
      System.out.printf("%-50s %-4d %d\n", msg.getMsgId(), msg.getQueueId(), msg.getQueueOffset());
    }
  }
  @Override
  public void execute(CommandLine commandLine, Options options, RPCHook rpcHook) {
    DefaultMQAdminExt defaultMQAdminExt = new DefaultMQAdminExt(rpcHook);
    defaultMQAdminExt.setInstanceName(Long.toString(System.currentTimeMillis()));

    try {
      boolean result = false;
      defaultMQAdminExt.start();
      if (commandLine.hasOption('b')) {
        String addr = commandLine.getOptionValue('b').trim();
        result = defaultMQAdminExt.cleanUnusedTopicByAddr(addr);

      } else {
        String cluster = commandLine.getOptionValue('c');
        if (null != cluster) cluster = cluster.trim();
        result = defaultMQAdminExt.cleanUnusedTopicByAddr(cluster);
      }
      System.out.println(result ? "success" : "false");
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      defaultMQAdminExt.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();
    }
  }