@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 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(); } }
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 { final String topic = commandLine.getOptionValue('t').trim(); final String key = commandLine.getOptionValue('k').trim(); this.queryByKey(defaultMQAdminExt, topic, key); } catch (Exception e) { e.printStackTrace(); } finally { defaultMQAdminExt.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 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 SubscriptionGroupConfig getGroupConfig( DefaultMQAdminExt defaultMQAdminExt, String groupName) { try { ConsumerConnection consumerConnection = defaultMQAdminExt.examineConsumerConnectionInfo(groupName); if (null != consumerConnection) { for (Connection connection : consumerConnection.getConnectionSet()) { String clientId = connection.getClientId(); if (null != clientId && !clientId.isEmpty()) { ConsumerRunningInfo info = defaultMQAdminExt.getConsumerRunningInfo(groupName, clientId, true); if (null != info) { return null; } } } } } catch (Exception e) { System.out.println("try to get SubscriptionGroupConfig failed! group :" + groupName + e); } return null; }
@Override public Set<String> getGroups(DefaultMQAdminExt defaultMQAdminExt) { Set<String> consumerGroups = new HashSet<>(); int flag = 0; while (flag++ < 5) { try { Set<String> topics = defaultMQAdminExt.fetchAllTopicList().getTopicList(); for (String topic : topics) { if (topic.startsWith(MixAll.RETRY_GROUP_TOPIC_PREFIX) || topic.startsWith(MixAll.DLQ_GROUP_TOPIC_PREFIX)) consumerGroups.add( topic.startsWith(MixAll.RETRY_GROUP_TOPIC_PREFIX) ? topic.replace(MixAll.RETRY_GROUP_TOPIC_PREFIX, "") : topic.replace(MixAll.DLQ_GROUP_TOPIC_PREFIX, "")); } break; } catch (Exception e) { e.printStackTrace(); } } return consumerGroups; }
@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(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(); } }
private void printClusterBaseInfo(final DefaultMQAdminExt defaultMQAdminExt) throws RemotingConnectException, RemotingTimeoutException, RemotingSendRequestException, InterruptedException, MQBrokerException { ClusterInfo clusterInfoSerializeWrapper = defaultMQAdminExt.examineBrokerClusterInfo(); System.out.printf( "%-16s %-32s %-4s %-22s %-22s %11s %11s\n", // "#Cluster Name", // "#Broker Name", // "#BID", // "#Addr", // "#Version", // "#InTPS", // "#OutTPS" // ); Iterator<Map.Entry<String, Set<String>>> itCluster = clusterInfoSerializeWrapper.getClusterAddrTable().entrySet().iterator(); while (itCluster.hasNext()) { Map.Entry<String, Set<String>> next = itCluster.next(); String clusterName = next.getKey(); TreeSet<String> brokerNameSet = new TreeSet<String>(); brokerNameSet.addAll(next.getValue()); for (String brokerName : brokerNameSet) { BrokerData brokerData = clusterInfoSerializeWrapper.getBrokerAddrTable().get(brokerName); if (brokerData != null) { Iterator<Map.Entry<Long, String>> itAddr = brokerData.getBrokerAddrs().entrySet().iterator(); while (itAddr.hasNext()) { Map.Entry<Long, String> next1 = itAddr.next(); double in = 0; double out = 0; String version = ""; try { KVTable kvTable = defaultMQAdminExt.fetchBrokerRuntimeStats(next1.getValue()); String putTps = kvTable.getTable().get("putTps"); String getTransferedTps = kvTable.getTable().get("getTransferedTps"); version = kvTable.getTable().get("brokerVersionDesc"); { String[] tpss = putTps.split(" "); if (tpss != null && tpss.length > 0) { in = Double.parseDouble(tpss[0]); } } { String[] tpss = getTransferedTps.split(" "); if (tpss != null && tpss.length > 0) { out = Double.parseDouble(tpss[0]); } } } catch (Exception e) { } System.out.printf( "%-16s %-32s %-4s %-22s %-22s %11.2f %11.2f\n", // clusterName, // brokerName, // next1.getKey().longValue(), // next1.getValue(), // version, // in, // out // ); } } } if (itCluster.hasNext()) { System.out.println(""); } } }
private void printClusterMoreStats(final DefaultMQAdminExt defaultMQAdminExt) throws RemotingConnectException, RemotingTimeoutException, RemotingSendRequestException, InterruptedException, MQBrokerException { ClusterInfo clusterInfoSerializeWrapper = defaultMQAdminExt.examineBrokerClusterInfo(); System.out.printf( "%-16s %-32s %14s %14s %14s %14s\n", // "#Cluster Name", // "#Broker Name", // "#InTotalYest", // "#OutTotalYest", // "#InTotalToday", // "#OutTotalToday" // ); Iterator<Map.Entry<String, Set<String>>> itCluster = clusterInfoSerializeWrapper.getClusterAddrTable().entrySet().iterator(); while (itCluster.hasNext()) { Map.Entry<String, Set<String>> next = itCluster.next(); String clusterName = next.getKey(); TreeSet<String> brokerNameSet = new TreeSet<String>(); brokerNameSet.addAll(next.getValue()); for (String brokerName : brokerNameSet) { BrokerData brokerData = clusterInfoSerializeWrapper.getBrokerAddrTable().get(brokerName); if (brokerData != null) { Iterator<Map.Entry<Long, String>> itAddr = brokerData.getBrokerAddrs().entrySet().iterator(); while (itAddr.hasNext()) { Map.Entry<Long, String> next1 = itAddr.next(); long InTotalYest = 0; long OutTotalYest = 0; long InTotalToday = 0; long OutTotalToday = 0; try { KVTable kvTable = defaultMQAdminExt.fetchBrokerRuntimeStats(next1.getValue()); String msgPutTotalYesterdayMorning = kvTable.getTable().get("msgPutTotalYesterdayMorning"); String msgPutTotalTodayMorning = kvTable.getTable().get("msgPutTotalTodayMorning"); String msgPutTotalTodayNow = kvTable.getTable().get("msgPutTotalTodayNow"); String msgGetTotalYesterdayMorning = kvTable.getTable().get("msgGetTotalYesterdayMorning"); String msgGetTotalTodayMorning = kvTable.getTable().get("msgGetTotalTodayMorning"); String msgGetTotalTodayNow = kvTable.getTable().get("msgGetTotalTodayNow"); InTotalYest = Long.parseLong(msgPutTotalTodayMorning) - Long.parseLong(msgPutTotalYesterdayMorning); OutTotalYest = Long.parseLong(msgGetTotalTodayMorning) - Long.parseLong(msgGetTotalYesterdayMorning); InTotalToday = Long.parseLong(msgPutTotalTodayNow) - Long.parseLong(msgPutTotalTodayMorning); OutTotalToday = Long.parseLong(msgGetTotalTodayNow) - Long.parseLong(msgGetTotalTodayMorning); } catch (Exception e) { } System.out.printf( "%-16s %-32s %14d %14d %14d %14d\n", // clusterName, // brokerName, // InTotalYest, // OutTotalYest, // InTotalToday, // OutTotalToday // ); } } } if (itCluster.hasNext()) { System.out.println(""); } } }
@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(); } }