}
@Override
public void execute(CommandLine commandLine, Options options) {
DefaultMQAdminExt defaultMQAdminExt = new DefaultMQAdminExt();
defaultMQAdminExt.setInstanceName(Long.toString(System.currentTimeMillis()));
try {
String group = commandLine.getOptionValue("g").trim();
String topic = commandLine.getOptionValue("t").trim();
String timeStampStr = commandLine.getOptionValue("s").trim();
long timestamp = 0;
try {
// 直接输入 long 类型的 timestamp
timestamp = Long.valueOf(timeStampStr);
}
catch (NumberFormatException e) {
// 输入的为日期格式,精确到毫秒
timestamp = UtilAll.parseDate(timeStampStr, UtilAll.yyyy_MM_dd_HH_mm_ss_SSS).getTime();
}
boolean force = true;
if (commandLine.hasOption('f')) {
force = Boolean.valueOf(commandLine.getOptionValue("f").trim());
}
defaultMQAdminExt.start();
Map<MessageQueue, Long> offsetTable;
try {
offsetTable = defaultMQAdminExt.resetOffsetByTimestamp(topic, group, timestamp, force);
}
catch (MQClientException e) {
if (ResponseCode.CONSUMER_NOT_ONLINE == e.getResponseCode()) {
ResetOffsetByTimeOldCommand.resetOffset(defaultMQAdminExt, group, topic, timestamp,
force, timeStampStr);
return;
}
throw e;
}
System.out
.printf(
"rollback consumer offset by specified group[%s], topic[%s], force[%s], timestamp(string)[%s], timestamp(long)[%s]\n",
group, topic, force, timeStampStr, timestamp);
System.out.printf("%-40s %-40s %-40s\n",//
"#brokerName",//
"#queueId",//
"#offset");
Iterator<Map.Entry<MessageQueue, Long>> iterator = offsetTable.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<MessageQueue, Long> entry = iterator.next();
System.out.printf("%-40s %-40d %-40d\n",//
UtilAll.frontStringAtLeast(entry.getKey().getBrokerName(), 32),//
entry.getKey().getQueueId(),//
entry.getValue());
}
}
catch (Exception e) {
e.printStackTrace();
}
finally {
defaultMQAdminExt.shutdown();
}
}