From b421d48c476e74a8c7bb8129979df1dc0cb5a5a5 Mon Sep 17 00:00:00 2001 From: Zhanhui Li Date: Wed, 4 Jan 2017 15:20:29 +0800 Subject: [PATCH] Fix https://issues.apache.org/jira/browse/ROCKETMQ-25 --- .../org/apache/rocketmq/client/impl/MQAdminImpl.java | 11 ++++++++++- .../command/message/QueryMsgByKeySubCommand.java | 2 +- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/client/src/main/java/org/apache/rocketmq/client/impl/MQAdminImpl.java b/client/src/main/java/org/apache/rocketmq/client/impl/MQAdminImpl.java index fb948b77..983e5157 100644 --- a/client/src/main/java/org/apache/rocketmq/client/impl/MQAdminImpl.java +++ b/client/src/main/java/org/apache/rocketmq/client/impl/MQAdminImpl.java @@ -23,6 +23,9 @@ import java.util.List; import java.util.Set; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; +import java.util.concurrent.locks.ReadWriteLock; +import java.util.concurrent.locks.ReentrantReadWriteLock; + import org.apache.rocketmq.client.QueryResult; import org.apache.rocketmq.client.exception.MQBrokerException; import org.apache.rocketmq.client.exception.MQClientException; @@ -285,6 +288,7 @@ public class MQAdminImpl { if (!brokerAddrs.isEmpty()) { final CountDownLatch countDownLatch = new CountDownLatch(brokerAddrs.size()); final List queryResultList = new LinkedList(); + final ReadWriteLock lock = new ReentrantReadWriteLock(false); for (String addr : brokerAddrs) { try { @@ -318,7 +322,12 @@ public class MQAdminImpl { MessageDecoder.decodes(ByteBuffer.wrap(response.getBody()), true); QueryResult qr = new QueryResult(responseHeader.getIndexLastUpdateTimestamp(), wrappers); - queryResultList.add(qr); + try { + lock.writeLock().lock(); + queryResultList.add(qr); + } finally { + lock.writeLock().unlock(); + } break; } default: diff --git a/tools/src/main/java/org/apache/rocketmq/tools/command/message/QueryMsgByKeySubCommand.java b/tools/src/main/java/org/apache/rocketmq/tools/command/message/QueryMsgByKeySubCommand.java index 77dc6c41..bdc5f520 100644 --- a/tools/src/main/java/org/apache/rocketmq/tools/command/message/QueryMsgByKeySubCommand.java +++ b/tools/src/main/java/org/apache/rocketmq/tools/command/message/QueryMsgByKeySubCommand.java @@ -69,7 +69,7 @@ public class QueryMsgByKeySubCommand implements SubCommand { } } - void queryByKey(final DefaultMQAdminExt admin, final String topic, final String key) + private void queryByKey(final DefaultMQAdminExt admin, final String topic, final String key) throws MQClientException, InterruptedException { admin.start(); -- GitLab