diff --git a/artemis-server/src/main/java/org/apache/activemq/artemis/core/server/impl/QueueImpl.java b/artemis-server/src/main/java/org/apache/activemq/artemis/core/server/impl/QueueImpl.java index c2cfdef03d..8a0cda04a9 100644 --- a/artemis-server/src/main/java/org/apache/activemq/artemis/core/server/impl/QueueImpl.java +++ b/artemis-server/src/main/java/org/apache/activemq/artemis/core/server/impl/QueueImpl.java @@ -214,6 +214,8 @@ public class QueueImpl implements Queue { // We cache the consumers here since we don't want to include the redistributor + private final AtomicInteger consumersCount = new AtomicInteger(); + private final Set consumerSet = new HashSet<>(); private final Map groups = new HashMap<>(); @@ -807,7 +809,9 @@ public class QueueImpl implements Queue { consumerList.add(new ConsumerHolder(consumer)); - consumerSet.add(consumer); + if (consumerSet.add(consumer)) { + consumersCount.incrementAndGet(); + } if (refCountForConsumers != null) { refCountForConsumers.increment(); @@ -837,7 +841,9 @@ public class QueueImpl implements Queue { pos = consumerList.size() - 1; } - consumerSet.remove(consumer); + if (consumerSet.remove(consumer)) { + consumersCount.decrementAndGet(); + } LinkedList groupsToRemove = null; @@ -924,8 +930,8 @@ public class QueueImpl implements Queue { } @Override - public synchronized int getConsumerCount() { - return consumerSet.size(); + public int getConsumerCount() { + return consumersCount.get(); } @Override @@ -1011,16 +1017,14 @@ public class QueueImpl implements Queue { @Override public long getMessageCount() { - synchronized (this) { - if (pageSubscription != null) { - // messageReferences will have depaged messages which we need to discount from the counter as they are - // counted on the pageSubscription as well - return messageReferences.size() + getScheduledCount() + - deliveringCount.get() + - pageSubscription.getMessageCount(); - } else { - return messageReferences.size() + getScheduledCount() + deliveringCount.get(); - } + if (pageSubscription != null) { + // messageReferences will have depaged messages which we need to discount from the counter as they are + // counted on the pageSubscription as well + return messageReferences.size() + getScheduledCount() + + deliveringCount.get() + + pageSubscription.getMessageCount(); + } else { + return messageReferences.size() + getScheduledCount() + deliveringCount.get(); } }