Esempio n. 1
0
  public static void main(String[] args) {
    System.setProperty(
        RemotingCommand.RemotingVersionKey, Integer.toString(MQVersion.CurrentVersion));

    try {
      initLogback();
      switch (args.length) {
        case 0:
          printHelp();
          break;
        case 2:
          if (args[0].equals("help")) {
            SubCommand cmd = findSubCommand(args[1]);
            if (cmd != null) {
              Options options = ServerUtil.buildCommandlineOptions(new Options());
              options = cmd.buildCommandlineOptions(options);
              if (options != null) {
                ServerUtil.printCommandLineHelp("mqadmin " + cmd.commandName(), options);
              }
            } else {
              System.out.println("The sub command \'" + args[1] + "\' not exist.");
            }
            break;
          }
        case 1:
        default:
          SubCommand cmd = findSubCommand(args[0]);
          if (cmd != null) {
            // 将main中的args转化为子命令的args(去除第一个参数)
            String[] subargs = parseSubArgs(args);

            // 解析命令行
            Options options = ServerUtil.buildCommandlineOptions(new Options());
            final CommandLine commandLine =
                ServerUtil.parseCmdLine(
                    "mqadmin " + cmd.commandName(),
                    subargs,
                    cmd.buildCommandlineOptions(options),
                    new PosixParser());
            if (null == commandLine) {
              System.exit(-1);
              return;
            }

            if (commandLine.hasOption('n')) {
              String namesrvAddr = commandLine.getOptionValue('n');
              System.setProperty(MixAll.NAMESRV_ADDR_PROPERTY, namesrvAddr);
            }

            cmd.execute(commandLine, options);
          } else {
            System.out.println("The sub command \'" + args[0] + "\' not exist.");
          }
          break;
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
  @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(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(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();
    }
  }