Skip to content

Commit 93eaead

Browse files
jbertramtabish121
authored andcommitted
ARTEMIS-5997 refactor SimpleAddressManager.getMatchingQueue
The getMatchingQueue method on SimpleAddressManager is overloaded, but only one of them is actually necessary. The three-parameter version is only used once and the method that uses it has already performed all the same work. Furthermore, the two-parameter version doesn't actually check the routing-type and the first if statement omits the wild check. This commit: - Removes the unnecessary version of getMatchingQueue - Adds missing checks to the remaining getMatchingQueue - Adds JavaDoc - Adds tests
1 parent 0202f63 commit 93eaead

13 files changed

Lines changed: 220 additions & 77 deletions

File tree

artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/broker/AMQPSessionCallback.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -832,12 +832,6 @@ public SimpleString getMatchingQueue(SimpleString address, RoutingType routingTy
832832
return serverSession.getMatchingQueue(address, routingType);
833833
}
834834

835-
public SimpleString getMatchingQueue(SimpleString address,
836-
SimpleString queueName,
837-
RoutingType routingType) throws Exception {
838-
return serverSession.getMatchingQueue(address, queueName, routingType);
839-
}
840-
841835
public AddressInfo getAddress(SimpleString address) {
842836
return serverSession.getAddress(address);
843837
}

artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/proton/DefaultSenderController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,7 @@ protected SimpleString getMatchingQueue(SimpleString queueName, SimpleString add
527527
throw new ActiveMQIllegalStateException("Queue: " + queueName + " filter mismatch [" + filter + "] is different than existing filter [" + result.getFilterString() + "]");
528528

529529
}
530-
return sessionSPI.getMatchingQueue(address, queueName, routingType);
530+
return queueName;
531531
}
532532
}
533533

artemis-server/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,12 @@
290290
<artifactId>mockserver-client-java</artifactId>
291291
<scope>test</scope>
292292
</dependency>
293+
<dependency>
294+
<groupId>org.hamcrest</groupId>
295+
<artifactId>hamcrest</artifactId>
296+
<version>${hamcrest.version}</version>
297+
<scope>test</scope>
298+
</dependency>
293299
</dependencies>
294300

295301
<build>

artemis-server/src/main/java/org/apache/activemq/artemis/core/postoffice/AddressManager.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,6 @@ public interface AddressManager {
5151

5252
SimpleString getMatchingQueue(SimpleString address, RoutingType routingType) throws Exception;
5353

54-
SimpleString getMatchingQueue(SimpleString address, SimpleString queueName, RoutingType routingType) throws Exception;
55-
5654
LocalQueueBinding findLocalBinding(long id);
5755

5856
void clear();

artemis-server/src/main/java/org/apache/activemq/artemis/core/postoffice/PostOffice.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,6 @@ default Queue findQueue(final long bindingID) {
149149

150150
SimpleString getMatchingQueue(SimpleString address, RoutingType routingType) throws Exception;
151151

152-
SimpleString getMatchingQueue(SimpleString address, SimpleString queueName, RoutingType routingType) throws Exception;
153-
154152
RoutingStatus route(Message message, boolean direct) throws Exception;
155153

156154
RoutingStatus route(Message message,

artemis-server/src/main/java/org/apache/activemq/artemis/core/postoffice/impl/PostOfficeImpl.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1538,13 +1538,6 @@ public SimpleString getMatchingQueue(SimpleString address, RoutingType routingTy
15381538
return addressManager.getMatchingQueue(address, routingType);
15391539
}
15401540

1541-
@Override
1542-
public SimpleString getMatchingQueue(SimpleString address,
1543-
SimpleString queueName,
1544-
RoutingType routingType) throws Exception {
1545-
return addressManager.getMatchingQueue(address, queueName, routingType);
1546-
}
1547-
15481541
@Override
15491542
public void sendQueueInfoToQueue(final SimpleString queueName, final SimpleString address) throws Exception {
15501543
// We send direct to the queue so we can send it to the same queue that is bound to the notifications address -

artemis-server/src/main/java/org/apache/activemq/artemis/core/postoffice/impl/SimpleAddressManager.java

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -75,17 +75,12 @@ public class SimpleAddressManager implements AddressManager {
7575

7676
protected final WildcardConfiguration wildcardConfiguration;
7777

78-
public SimpleAddressManager(final BindingsFactory bindingsFactory, final StorageManager storageManager,
79-
final MetricsManager metricsManager) {
80-
this(bindingsFactory, new WildcardConfiguration(), storageManager, metricsManager);
81-
}
82-
8378
public SimpleAddressManager(final BindingsFactory bindingsFactory,
8479
final WildcardConfiguration wildcardConfiguration,
8580
final StorageManager storageManager,
8681
final MetricsManager metricsManager) {
87-
this.wildcardConfiguration = wildcardConfiguration;
8882
this.bindingsFactory = bindingsFactory;
83+
this.wildcardConfiguration = wildcardConfiguration;
8984
this.storageManager = storageManager;
9085
this.metricsManager = metricsManager;
9186
}
@@ -190,37 +185,49 @@ public Collection<Binding> getDirectBindings(final SimpleString address) throws
190185
return Collections.unmodifiableCollection(outputList);
191186
}
192187

188+
/**
189+
* This method looks at the local queue bindings of an address to find one with a matching routing type that also is
190+
* not wild. A match with the same name as the input address is preferred.
191+
* <p>
192+
* If a match is found, the unique name of the corresponding binding is returned. If no match is found, the method
193+
* returns {@code null}.
194+
*
195+
* @param address the address to match against
196+
* @param routingType the routing type to use for matching the binding
197+
* @return the unique name of the matching binding, or {@code null} if no match is found
198+
* @throws Exception if an error occurs while attempting to find a matching binding
199+
*/
193200
@Override
194201
public SimpleString getMatchingQueue(final SimpleString address, RoutingType routingType) throws Exception {
195202
SimpleString realAddress = CompositeAddress.extractAddressName(address);
196-
Binding binding = getBinding(realAddress);
197203

198-
if (binding == null || !(binding instanceof LocalQueueBinding) || !binding.getAddress().equals(realAddress)) {
199-
Bindings bindings = mappings.get(realAddress);
200-
if (bindings != null) {
201-
for (Binding theBinding : bindings.getBindings()) {
202-
if (theBinding instanceof LocalQueueBinding && !wildcardConfiguration.isWild(theBinding.getUniqueName())) {
203-
binding = theBinding;
204-
break;
205-
}
204+
Binding potentialMatch = getBinding(realAddress);
205+
if (checkBindingForMatch(potentialMatch, realAddress, routingType)) {
206+
// a local queue binding with the same name as the input address is preferred
207+
return potentialMatch.getUniqueName();
208+
}
209+
210+
Bindings bindings = mappings.get(realAddress);
211+
if (bindings != null) {
212+
for (Binding b : bindings.getBindings()) {
213+
if (checkBindingForMatch(b, realAddress, routingType)) {
214+
return b.getUniqueName();
206215
}
207216
}
208217
}
209218

210-
return binding != null ? binding.getUniqueName() : null;
219+
return null;
211220
}
212221

213-
@Override
214-
public SimpleString getMatchingQueue(final SimpleString address,
215-
final SimpleString queueName,
216-
RoutingType routingType) throws Exception {
217-
SimpleString realAddress = CompositeAddress.extractAddressName(address);
218-
Binding binding = getBinding(queueName);
219-
220-
if (binding != null && !binding.getAddress().equals(realAddress) && !realAddress.toString().isEmpty()) {
221-
throw new IllegalStateException("queue belongs to address" + binding.getAddress());
222+
private boolean checkBindingForMatch(Binding binding, SimpleString address, RoutingType routingType) {
223+
if (binding != null &&
224+
binding instanceof LocalQueueBinding lqb &&
225+
lqb.getAddress().equals(address) &&
226+
lqb.getQueue().getRoutingType() == routingType &&
227+
!wildcardConfiguration.isWild(binding.getUniqueName())) {
228+
return true;
222229
}
223-
return binding != null ? binding.getUniqueName() : null;
230+
return false;
224231
}
225232

226233
@Override

artemis-server/src/main/java/org/apache/activemq/artemis/core/postoffice/impl/WildcardAddressManager.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,6 @@ public WildcardAddressManager(final BindingsFactory bindingsFactory,
4141
super(bindingsFactory, wildcardConfiguration, storageManager, metricsManager);
4242
}
4343

44-
public WildcardAddressManager(final BindingsFactory bindingsFactory,
45-
final StorageManager storageManager,
46-
final MetricsManager metricsManager) {
47-
super(bindingsFactory, storageManager, metricsManager);
48-
}
49-
5044
// publish, may be a new address that needs wildcard bindings added
5145
// won't contain a wildcard because we don't ever route to a wildcards at this time
5246
@Override

artemis-server/src/main/java/org/apache/activemq/artemis/core/server/ServerSession.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -497,10 +497,6 @@ void createSharedQueue(SimpleString address,
497497

498498
SimpleString getMatchingQueue(SimpleString address, RoutingType routingType) throws Exception;
499499

500-
SimpleString getMatchingQueue(SimpleString address,
501-
SimpleString queueName,
502-
RoutingType routingType) throws Exception;
503-
504500
AddressInfo getAddress(SimpleString address);
505501

506502
/**

artemis-server/src/main/java/org/apache/activemq/artemis/core/server/impl/ServerSessionImpl.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1935,7 +1935,7 @@ public AutoCreateResult checkAutoCreate(final QueueConfiguration queueConfig) th
19351935
Queue q = server.locateQueue(unPrefixedQueue);
19361936
if (q == null) {
19371937
// The queue doesn't exist.
1938-
if (!queueConfig.isFqqn() && server.getPostOffice().getMatchingQueue(unPrefixedAddress, queueConfig.getRoutingType()) != null) {
1938+
if (!queueConfig.isFqqn() && getMatchingQueue(unPrefixedAddress, queueConfig.getRoutingType()) != null) {
19391939
// The address has a local, non-wildcard queue with a different name, which is fine. Just ignore it.
19401940
result = AutoCreateResult.EXISTED;
19411941
} else if (addressSettings.isAutoCreateQueues() || queueConfig.isTemporary()) {
@@ -2226,13 +2226,6 @@ public SimpleString getMatchingQueue(SimpleString address, RoutingType routingTy
22262226
return server.getPostOffice().getMatchingQueue(address, routingType);
22272227
}
22282228

2229-
@Override
2230-
public SimpleString getMatchingQueue(SimpleString address,
2231-
SimpleString queueName,
2232-
RoutingType routingType) throws Exception {
2233-
return server.getPostOffice().getMatchingQueue(address, queueName, routingType);
2234-
}
2235-
22362229
@Override
22372230
public AddressInfo getAddress(SimpleString address) {
22382231
return server.getPostOffice().getAddressInfo(removePrefix(address));

0 commit comments

Comments
 (0)