}
@Override
public void execute(CommandLine commandLine, Options options) {
DefaultMQAdminExt defaultMQAdminExt = new DefaultMQAdminExt();
defaultMQAdminExt.setInstanceName(Long.toString(System.currentTimeMillis()));
try {
defaultMQAdminExt.start();
// 查询特定consumer
if (commandLine.hasOption('g')) {
String consumerGroup = commandLine.getOptionValue('g').trim();
ConsumeStats consumeStats = defaultMQAdminExt.examineConsumeStats(consumerGroup);
List<MessageQueue> mqList = new LinkedList<MessageQueue>();
mqList.addAll(consumeStats.getOffsetTable().keySet());
Collections.sort(mqList);
System.out.printf("%-32s %-32s %-4s %-20s %-20s %s\n",//
"#Topic",//
"#Broker Name",//
"#QID",//
"#Broker Offset",//
"#Consumer Offset",//
"#Diff" //
);
long diffTotal = 0L;
for (MessageQueue mq : mqList) {
OffsetWrapper offsetWrapper = consumeStats.getOffsetTable().get(mq);
long diff = offsetWrapper.getBrokerOffset() - offsetWrapper.getConsumerOffset();
diffTotal += diff;
System.out.printf("%-32s %-32s %-4d %-20d %-20d %d\n",//
UtilAll.frontStringAtLeast(mq.getTopic(), 32),//
UtilAll.frontStringAtLeast(mq.getBrokerName(), 32),//
mq.getQueueId(),//
offsetWrapper.getBrokerOffset(),//
offsetWrapper.getConsumerOffset(),//
diff //
);
}
System.out.println("");
System.out.printf("Consume TPS: %d\n", consumeStats.getConsumeTps());
System.out.printf("Diff Total: %d\n", diffTotal);
}
// 查询全部
else {
System.out.printf("%-32s %-6s %-24s %-5s %-14s %-7s %s\n",//
"#Group",//
"#Count",//
"#Version",//
"#Type",//
"#Model",//
"#TPS",//
"#Diff Total"//
);
List<GroupConsumeInfo> groupConsumeInfoList = new LinkedList<GroupConsumeInfo>();
TopicList topicList = defaultMQAdminExt.fetchAllTopicList();
for (String topic : topicList.getTopicList()) {
if (topic.startsWith(MixAll.RETRY_GROUP_TOPIC_PREFIX)) {
String consumerGroup = topic.substring(MixAll.RETRY_GROUP_TOPIC_PREFIX.length());
try {
ConsumeStats consumeStats = null;
try {
consumeStats = defaultMQAdminExt.examineConsumeStats(consumerGroup);
}
catch (Exception e) {
log.warn("examineConsumeStats exception, " + consumerGroup, e);
}
ConsumerConnection cc = null;
try {
cc = defaultMQAdminExt.examineConsumerConnectionInfo(consumerGroup);
}
catch (Exception e) {
log.warn("examineConsumerConnectionInfo exception, " + consumerGroup, e);
}
GroupConsumeInfo groupConsumeInfo = new GroupConsumeInfo();
groupConsumeInfo.setGroup(consumerGroup);
if (consumeStats != null) {
groupConsumeInfo.setConsumeTps((int) consumeStats.getConsumeTps());
groupConsumeInfo.setDiffTotal(consumeStats.computeTotalDiff());
}
if (cc != null) {
groupConsumeInfo.setCount(cc.getConnectionSet().size());
groupConsumeInfo.setMessageModel(cc.getMessageModel());
groupConsumeInfo.setConsumeType(cc.getConsumeType());
groupConsumeInfo.setVersion(cc.computeMinVersion());
}
groupConsumeInfoList.add(groupConsumeInfo);
}
catch (Exception e) {
log.warn("examineConsumeStats or examineConsumerConnectionInfo exception, "
+ consumerGroup, e);
}
}
}
Collections.sort(groupConsumeInfoList);
for (GroupConsumeInfo info : groupConsumeInfoList) {
System.out.printf("%-32s %-6d %-24s %-5s %-14s %-7d %d\n",//
UtilAll.frontStringAtLeast(info.getGroup(), 32),//
info.getCount(),//
info.versionDesc(),//
info.consumeTypeDesc(),//
info.messageModelDesc(),//
info.getConsumeTps(),//
info.getDiffTotal()//
);
}
}
}
catch (Exception e) {
e.printStackTrace();
}
finally {
defaultMQAdminExt.shutdown();
}
}