Skip to content

Commit 4e541f6

Browse files
authored
Replace active temp dests map with set (#2113) (#2115)
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. (cherry picked from commit b20a4c7)
1 parent ac2f60a commit 4e541f6

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;
@@ -118,7 +119,7 @@ public class ActiveMQConnection implements Connection, TopicConnection, QueueCon
118119

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

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

123124
protected boolean dispatchAsync=true;
124125
protected boolean alwaysSessionAsync = true;
@@ -2128,7 +2129,7 @@ protected ActiveMQTempDestination createTempDestination(boolean topic) throws JM
21282129
syncSendPacket(info);
21292130

21302131
dest.setConnection(this);
2131-
activeTempDestinations.put(dest, dest);
2132+
activeTempDestinations.add(dest);
21322133
return dest;
21332134
}
21342135

@@ -2164,7 +2165,7 @@ public boolean isDeleted(ActiveMQDestination dest) {
21642165
return false;
21652166
}
21662167

2167-
return !activeTempDestinations.containsValue(dest);
2168+
return !activeTempDestinations.contains(dest);
21682169
}
21692170

21702171
public boolean isCopyMessageOnSend() {
@@ -2527,21 +2528,17 @@ public void setRmIdFromConnectionId(boolean rmIdFromConnectionId) {
25272528
*/
25282529
public void cleanUpTempDestinations() {
25292530

2530-
if (this.activeTempDestinations == null || this.activeTempDestinations.isEmpty()) {
2531+
if (this.activeTempDestinations.isEmpty()) {
25312532
return;
25322533
}
25332534

2534-
Iterator<ConcurrentMap.Entry<ActiveMQTempDestination, ActiveMQTempDestination>> entries
2535-
= this.activeTempDestinations.entrySet().iterator();
2536-
while(entries.hasNext()) {
2537-
ConcurrentMap.Entry<ActiveMQTempDestination, ActiveMQTempDestination> entry = entries.next();
2535+
for (ActiveMQTempDestination dest : activeTempDestinations) {
25382536
try {
25392537
// Only delete this temp destination if it was created from this connection. The connection used
25402538
// for the advisory consumer may also have a reference to this temp destination.
2541-
ActiveMQTempDestination dest = entry.getValue();
25422539
String thisConnectionId = (info.getConnectionId() == null) ? "" : info.getConnectionId().toString();
25432540
if (dest.getConnectionId() != null && dest.getConnectionId().equals(thisConnectionId)) {
2544-
this.deleteTempDestination(entry.getValue());
2541+
this.deleteTempDestination(dest);
25452542
}
25462543
} catch (Exception ex) {
25472544
// 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
@@ -220,12 +220,8 @@ public void testPublishFailsForClosedConnection() throws Exception {
220220
connection.start();
221221

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

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

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

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

0 commit comments

Comments
 (0)