Skip to content

Commit b20a4c7

Browse files
authored
Replace active temp dests map with set (#2113)
For some reason ActiveMQConnection was using a map instead of a set to store the destinations, and just stored the exact same thing as the key and value. Furthermore, when checking if the map contained the destination a call was being made to containsValue() which requires iterating over the entire map. This commit replaced the Map with a Set which simplifies things and makes the contains() check constant. Also, the scope of the set was changed from public to package because it makes no sense to have the scope as public and should be limited to only classes in the same package.
1 parent 403ac4f commit b20a4c7

3 files changed

Lines changed: 12 additions & 23 deletions

File tree

activemq-client/src/main/java/org/apache/activemq/ActiveMQConnection.java

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.util.Iterator;
2525
import java.util.List;
2626
import java.util.Map;
27+
import java.util.Set;
2728
import java.util.concurrent.ConcurrentHashMap;
2829
import java.util.concurrent.ConcurrentMap;
2930
import java.util.concurrent.CopyOnWriteArrayList;
@@ -119,7 +120,7 @@ public class ActiveMQConnection implements Connection, TopicConnection, QueueCon
119120

120121
private static final Logger LOG = LoggerFactory.getLogger(ActiveMQConnection.class);
121122

122-
public final ConcurrentMap<ActiveMQTempDestination, ActiveMQTempDestination> activeTempDestinations = new ConcurrentHashMap<>();
123+
final Set<ActiveMQTempDestination> activeTempDestinations = ConcurrentHashMap.newKeySet();
123124

124125
protected boolean dispatchAsync=true;
125126
protected boolean alwaysSessionAsync = true;
@@ -2151,7 +2152,7 @@ protected ActiveMQTempDestination createTempDestination(boolean topic) throws JM
21512152
syncSendPacket(info);
21522153

21532154
dest.setConnection(this);
2154-
activeTempDestinations.put(dest, dest);
2155+
activeTempDestinations.add(dest);
21552156
return dest;
21562157
}
21572158

@@ -2187,7 +2188,7 @@ public boolean isDeleted(ActiveMQDestination dest) {
21872188
return false;
21882189
}
21892190

2190-
return !activeTempDestinations.containsValue(dest);
2191+
return !activeTempDestinations.contains(dest);
21912192
}
21922193

21932194
public boolean isCopyMessageOnSend() {
@@ -2575,21 +2576,17 @@ public void setRmIdFromConnectionId(boolean rmIdFromConnectionId) {
25752576
*/
25762577
public void cleanUpTempDestinations() {
25772578

2578-
if (this.activeTempDestinations == null || this.activeTempDestinations.isEmpty()) {
2579+
if (this.activeTempDestinations.isEmpty()) {
25792580
return;
25802581
}
25812582

2582-
Iterator<ConcurrentMap.Entry<ActiveMQTempDestination, ActiveMQTempDestination>> entries
2583-
= this.activeTempDestinations.entrySet().iterator();
2584-
while(entries.hasNext()) {
2585-
ConcurrentMap.Entry<ActiveMQTempDestination, ActiveMQTempDestination> entry = entries.next();
2583+
for (ActiveMQTempDestination dest : activeTempDestinations) {
25862584
try {
25872585
// Only delete this temp destination if it was created from this connection. The connection used
25882586
// for the advisory consumer may also have a reference to this temp destination.
2589-
ActiveMQTempDestination dest = entry.getValue();
25902587
String thisConnectionId = (info.getConnectionId() == null) ? "" : info.getConnectionId().toString();
25912588
if (dest.getConnectionId() != null && dest.getConnectionId().equals(thisConnectionId)) {
2592-
this.deleteTempDestination(entry.getValue());
2589+
this.deleteTempDestination(dest);
25932590
}
25942591
} catch (Exception ex) {
25952592
// the temp dest is in use so it can not be deleted.

activemq-client/src/main/java/org/apache/activemq/AdvisoryConsumer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ private void processDestinationInfo(DestinationInfo dinfo) {
100100
if (tempDest.getConnection() != null) {
101101
tempDest = (ActiveMQTempDestination) tempDest.createDestination(tempDest.getPhysicalName());
102102
}
103-
connection.activeTempDestinations.put(tempDest, tempDest);
103+
connection.activeTempDestinations.add(tempDest);
104104
} else if (dinfo.getOperationType() == DestinationInfo.REMOVE_OPERATION_TYPE) {
105105
connection.activeTempDestinations.remove(tempDest);
106106
}

activemq-unit-tests/src/test/java/org/apache/activemq/JmsTempDestinationTest.java

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -221,12 +221,8 @@ public void testPublishFailsForClosedConnection() throws Exception {
221221
connection.start();
222222

223223
final ActiveMQConnection activeMQConnection = (ActiveMQConnection) connection;
224-
assertTrue("creation advisory received in time with async dispatch", Wait.waitFor(new Wait.Condition() {
225-
@Override
226-
public boolean isSatisified() throws Exception {
227-
return activeMQConnection.activeTempDestinations.containsKey(queue);
228-
}
229-
}));
224+
assertTrue("creation advisory received in time with async dispatch",
225+
Wait.waitFor(() -> activeMQConnection.activeTempDestinations.contains(queue)));
230226

231227
// This message delivery should work since the temp connection is still
232228
// open.
@@ -268,12 +264,8 @@ public void testPublishFailsForDestroyedTempDestination() throws Exception {
268264
connection.start();
269265

270266
final ActiveMQConnection activeMQConnection = (ActiveMQConnection) connection;
271-
assertTrue("creation advisory received in time with async dispatch", Wait.waitFor(new Wait.Condition() {
272-
@Override
273-
public boolean isSatisified() throws Exception {
274-
return activeMQConnection.activeTempDestinations.containsKey(queue);
275-
}
276-
}));
267+
assertTrue("creation advisory received in time with async dispatch",
268+
Wait.waitFor((Wait.Condition) () -> activeMQConnection.activeTempDestinations.contains(queue)));
277269

278270
// This message delivery should work since the temp connection is still
279271
// open.

0 commit comments

Comments
 (0)