+ * If a match is found, the unique name of the corresponding binding is returned. If no match is found, the method
+ * returns {@code null}.
+ *
+ * @param address the address to match against
+ * @param routingType the routing type to use for matching the binding
+ * @return the unique name of the matching binding, or {@code null} if no match is found
+ * @throws Exception if an error occurs while attempting to find a matching binding
+ */
@Override
public SimpleString getMatchingQueue(final SimpleString address, RoutingType routingType) throws Exception {
SimpleString realAddress = CompositeAddress.extractAddressName(address);
- Binding binding = getBinding(realAddress);
- if (binding == null || !(binding instanceof LocalQueueBinding) || !binding.getAddress().equals(realAddress)) {
- Bindings bindings = mappings.get(realAddress);
- if (bindings != null) {
- for (Binding theBinding : bindings.getBindings()) {
- if (theBinding instanceof LocalQueueBinding && !wildcardConfiguration.isWild(theBinding.getUniqueName())) {
- binding = theBinding;
- break;
- }
+ Binding potentialMatch = getBinding(realAddress);
+ if (checkBindingForMatch(potentialMatch, realAddress, routingType)) {
+ // a local queue binding with the same name as the input address is preferred
+ return potentialMatch.getUniqueName();
+ }
+
+ Bindings bindings = mappings.get(realAddress);
+ if (bindings != null) {
+ for (Binding b : bindings.getBindings()) {
+ if (checkBindingForMatch(b, realAddress, routingType)) {
+ return b.getUniqueName();
}
}
}
- return binding != null ? binding.getUniqueName() : null;
+ return null;
}
- @Override
- public SimpleString getMatchingQueue(final SimpleString address,
- final SimpleString queueName,
- RoutingType routingType) throws Exception {
- SimpleString realAddress = CompositeAddress.extractAddressName(address);
- Binding binding = getBinding(queueName);
-
- if (binding != null && !binding.getAddress().equals(realAddress) && !realAddress.toString().isEmpty()) {
- throw new IllegalStateException("queue belongs to address" + binding.getAddress());
+ private boolean checkBindingForMatch(Binding binding, SimpleString address, RoutingType routingType) {
+ if (binding != null &&
+ binding instanceof LocalQueueBinding lqb &&
+ lqb.getAddress().equals(address) &&
+ lqb.getQueue().getRoutingType() == routingType &&
+ !wildcardConfiguration.isWild(binding.getUniqueName())) {
+ return true;
}
- return binding != null ? binding.getUniqueName() : null;
+ return false;
}
@Override
diff --git a/artemis-server/src/main/java/org/apache/activemq/artemis/core/postoffice/impl/WildcardAddressManager.java b/artemis-server/src/main/java/org/apache/activemq/artemis/core/postoffice/impl/WildcardAddressManager.java
index d4797bc8e5a..7a3ab89ddeb 100644
--- a/artemis-server/src/main/java/org/apache/activemq/artemis/core/postoffice/impl/WildcardAddressManager.java
+++ b/artemis-server/src/main/java/org/apache/activemq/artemis/core/postoffice/impl/WildcardAddressManager.java
@@ -41,12 +41,6 @@ public WildcardAddressManager(final BindingsFactory bindingsFactory,
super(bindingsFactory, wildcardConfiguration, storageManager, metricsManager);
}
- public WildcardAddressManager(final BindingsFactory bindingsFactory,
- final StorageManager storageManager,
- final MetricsManager metricsManager) {
- super(bindingsFactory, storageManager, metricsManager);
- }
-
// publish, may be a new address that needs wildcard bindings added
// won't contain a wildcard because we don't ever route to a wildcards at this time
@Override
diff --git a/artemis-server/src/main/java/org/apache/activemq/artemis/core/server/ServerSession.java b/artemis-server/src/main/java/org/apache/activemq/artemis/core/server/ServerSession.java
index ec66ec2e7a4..623494986eb 100644
--- a/artemis-server/src/main/java/org/apache/activemq/artemis/core/server/ServerSession.java
+++ b/artemis-server/src/main/java/org/apache/activemq/artemis/core/server/ServerSession.java
@@ -497,10 +497,6 @@ void createSharedQueue(SimpleString address,
SimpleString getMatchingQueue(SimpleString address, RoutingType routingType) throws Exception;
- SimpleString getMatchingQueue(SimpleString address,
- SimpleString queueName,
- RoutingType routingType) throws Exception;
-
AddressInfo getAddress(SimpleString address);
/**
diff --git a/artemis-server/src/main/java/org/apache/activemq/artemis/core/server/impl/ServerSessionImpl.java b/artemis-server/src/main/java/org/apache/activemq/artemis/core/server/impl/ServerSessionImpl.java
index 4ec39ed1bfa..4a46113cef0 100644
--- a/artemis-server/src/main/java/org/apache/activemq/artemis/core/server/impl/ServerSessionImpl.java
+++ b/artemis-server/src/main/java/org/apache/activemq/artemis/core/server/impl/ServerSessionImpl.java
@@ -1935,7 +1935,7 @@ public AutoCreateResult checkAutoCreate(final QueueConfiguration queueConfig) th
Queue q = server.locateQueue(unPrefixedQueue);
if (q == null) {
// The queue doesn't exist.
- if (!queueConfig.isFqqn() && server.getPostOffice().getMatchingQueue(unPrefixedAddress, queueConfig.getRoutingType()) != null) {
+ if (!queueConfig.isFqqn() && getMatchingQueue(unPrefixedAddress, queueConfig.getRoutingType()) != null) {
// The address has a local, non-wildcard queue with a different name, which is fine. Just ignore it.
result = AutoCreateResult.EXISTED;
} else if (addressSettings.isAutoCreateQueues() || queueConfig.isTemporary()) {
@@ -2226,13 +2226,6 @@ public SimpleString getMatchingQueue(SimpleString address, RoutingType routingTy
return server.getPostOffice().getMatchingQueue(address, routingType);
}
- @Override
- public SimpleString getMatchingQueue(SimpleString address,
- SimpleString queueName,
- RoutingType routingType) throws Exception {
- return server.getPostOffice().getMatchingQueue(address, queueName, routingType);
- }
-
@Override
public AddressInfo getAddress(SimpleString address) {
return server.getPostOffice().getAddressInfo(removePrefix(address));
diff --git a/artemis-server/src/test/java/org/apache/activemq/artemis/core/postoffice/impl/AddressManagerGetMatchingQueueTest.java b/artemis-server/src/test/java/org/apache/activemq/artemis/core/postoffice/impl/AddressManagerGetMatchingQueueTest.java
new file mode 100644
index 00000000000..e823418e69d
--- /dev/null
+++ b/artemis-server/src/test/java/org/apache/activemq/artemis/core/postoffice/impl/AddressManagerGetMatchingQueueTest.java
@@ -0,0 +1,165 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.activemq.artemis.core.postoffice.impl;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.activemq.artemis.api.core.RoutingType;
+import org.apache.activemq.artemis.api.core.SimpleString;
+import org.apache.activemq.artemis.core.config.WildcardConfiguration;
+import org.apache.activemq.artemis.core.postoffice.Binding;
+import org.apache.activemq.artemis.core.postoffice.Bindings;
+import org.apache.activemq.artemis.core.postoffice.BindingsFactory;
+import org.apache.activemq.artemis.core.server.Queue;
+import org.junit.jupiter.api.Test;
+
+import static org.apache.activemq.artemis.utils.RandomUtil.randomUUIDSimpleString;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.anyOf;
+import static org.hamcrest.Matchers.is;
+import static org.junit.Assert.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.doAnswer;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+public class AddressManagerGetMatchingQueueTest {
+
+ @Test
+ public void testCorrectNameCorrectRoutingTypeSingleQueue() throws Exception {
+ SimpleString addressName = SimpleString.of("myAddress");
+ SimpleString queueName = randomUUIDSimpleString();
+ SimpleAddressManager am = getSimpleAddressManager();
+ am.addBinding(getLocalQueueBinding(addressName, queueName, RoutingType.ANYCAST));
+
+ assertEquals(queueName, am.getMatchingQueue(addressName, RoutingType.ANYCAST));
+ }
+
+ @Test
+ public void testCorrectNameCorrectRoutingTypeMultipleQueues() throws Exception {
+ SimpleString addressName = SimpleString.of("myAddress");
+ SimpleString queueName1 = randomUUIDSimpleString();
+ SimpleString queueName2 = randomUUIDSimpleString();
+ SimpleAddressManager am = getSimpleAddressManager();
+ am.addBinding(getLocalQueueBinding(addressName, queueName1, RoutingType.ANYCAST));
+ am.addBinding(getLocalQueueBinding(addressName, queueName2, RoutingType.ANYCAST));
+
+ assertThat(am.getMatchingQueue(addressName, RoutingType.ANYCAST), anyOf(is(queueName1), is(queueName2)));
+ }
+
+ @Test
+ public void testCorrectNameCorrectRoutingTypeMultipleQueuesMixedRoutingTypes() throws Exception {
+ SimpleString addressName = SimpleString.of("myAddress");
+ SimpleString queueName1 = randomUUIDSimpleString();
+ SimpleString queueName2 = randomUUIDSimpleString();
+ SimpleAddressManager am = getSimpleAddressManager();
+ am.addBinding(getLocalQueueBinding(addressName, queueName1, RoutingType.ANYCAST));
+ am.addBinding(getLocalQueueBinding(addressName, queueName2, RoutingType.MULTICAST));
+
+ assertEquals(queueName1, am.getMatchingQueue(addressName, RoutingType.ANYCAST));
+ }
+
+ @Test
+ public void testCorrectNameIncorrectRoutingTypeSingleQueue() throws Exception {
+ SimpleString addressName = SimpleString.of("myAddress");
+ SimpleAddressManager am = getSimpleAddressManager();
+ am.addBinding(getLocalQueueBinding(addressName, RoutingType.MULTICAST));
+
+ assertNull(am.getMatchingQueue(addressName, RoutingType.ANYCAST));
+ }
+
+ @Test
+ public void testCorrectNameIncorrectRoutingTypeMultipleQueues() throws Exception {
+ SimpleString addressName = SimpleString.of("myAddress");
+ SimpleAddressManager am = getSimpleAddressManager();
+ am.addBinding(getLocalQueueBinding(addressName, RoutingType.MULTICAST));
+ am.addBinding(getLocalQueueBinding(addressName, RoutingType.MULTICAST));
+
+ assertNull(am.getMatchingQueue(addressName, RoutingType.ANYCAST));
+ }
+
+ @Test
+ public void testIncorrectNameCorrectRoutingTypeSingleQueue() throws Exception {
+ SimpleAddressManager am = getSimpleAddressManager();
+ am.addBinding(getLocalQueueBinding(randomUUIDSimpleString(), RoutingType.ANYCAST));
+
+ assertNull(am.getMatchingQueue(randomUUIDSimpleString(), RoutingType.ANYCAST));
+ }
+
+ @Test
+ public void testIncorrectNameCorrectRoutingTypeMultipleQueues() throws Exception {
+ SimpleAddressManager am = getSimpleAddressManager();
+ am.addBinding(getLocalQueueBinding(randomUUIDSimpleString(), RoutingType.ANYCAST));
+ am.addBinding(getLocalQueueBinding(randomUUIDSimpleString(), RoutingType.ANYCAST));
+
+ assertNull(am.getMatchingQueue(randomUUIDSimpleString(), RoutingType.ANYCAST));
+ }
+
+ @Test
+ public void testIncorrectNameIncorrectRoutingTypeSingleQueue() throws Exception {
+ SimpleAddressManager am = getSimpleAddressManager();
+ am.addBinding(getLocalQueueBinding(randomUUIDSimpleString(), RoutingType.MULTICAST));
+
+ assertNull(am.getMatchingQueue(randomUUIDSimpleString(), RoutingType.ANYCAST));
+ }
+
+ @Test
+ public void testIncorrectNameIncorrectRoutingTypeMultipleQueues() throws Exception {
+ SimpleAddressManager am = getSimpleAddressManager();
+ am.addBinding(getLocalQueueBinding(randomUUIDSimpleString(), RoutingType.MULTICAST));
+ am.addBinding(getLocalQueueBinding(randomUUIDSimpleString(), RoutingType.MULTICAST));
+
+ assertNull(am.getMatchingQueue(randomUUIDSimpleString(), RoutingType.ANYCAST));
+ }
+
+ private static SimpleAddressManager getSimpleAddressManager() throws Exception {
+ return new SimpleAddressManager(getBindingsFactory(), new WildcardConfiguration(), null, null);
+ }
+
+ private static LocalQueueBinding getLocalQueueBinding(SimpleString addressName, RoutingType routingType) {
+ return getLocalQueueBinding(addressName, randomUUIDSimpleString(), routingType);
+ }
+
+ private static LocalQueueBinding getLocalQueueBinding(SimpleString addressName, SimpleString queueName, RoutingType routingType) {
+ return new LocalQueueBinding(addressName, getQueue(queueName, routingType), randomUUIDSimpleString());
+ }
+
+ private static BindingsFactory getBindingsFactory() throws Exception {
+ List