diff --git a/artemis-server/src/main/java/org/apache/activemq/artemis/core/server/ActiveMQMessageBundle.java b/artemis-server/src/main/java/org/apache/activemq/artemis/core/server/ActiveMQMessageBundle.java index 3ae3eb9a0e..6a375156f1 100644 --- a/artemis-server/src/main/java/org/apache/activemq/artemis/core/server/ActiveMQMessageBundle.java +++ b/artemis-server/src/main/java/org/apache/activemq/artemis/core/server/ActiveMQMessageBundle.java @@ -363,7 +363,7 @@ public interface ActiveMQMessageBundle { ActiveMQSessionCreationException sessionLimitReached(String username, int limit); @Message(id = 229111, value = "Too many queues created by user ''{0}''. Queues allowed: {1}.", format = Message.Format.MESSAGE_FORMAT) - ActiveMQSessionCreationException queueLimitReached(String username, int limit); + ActiveMQSecurityException queueLimitReached(String username, int limit); @Message(id = 229112, value = "Cannot set MBeanServer during startup or while started") IllegalStateException cannotSetMBeanserver(); diff --git a/artemis-server/src/main/java/org/apache/activemq/artemis/core/server/impl/ActiveMQServerImpl.java b/artemis-server/src/main/java/org/apache/activemq/artemis/core/server/impl/ActiveMQServerImpl.java index cfc95f6068..5f831229d6 100644 --- a/artemis-server/src/main/java/org/apache/activemq/artemis/core/server/impl/ActiveMQServerImpl.java +++ b/artemis-server/src/main/java/org/apache/activemq/artemis/core/server/impl/ActiveMQServerImpl.java @@ -1744,16 +1744,22 @@ public class ActiveMQServerImpl implements ActiveMQServer { } public int getQueueCountForUser(String username) throws Exception { - int queuesForUser = 0; + SimpleString userNameSimpleString = SimpleString.toSimpleString(username); - for (Binding binding : iterableOf(postOffice.getAllBindings())) { - if (binding instanceof LocalQueueBinding && ((LocalQueueBinding) binding).getQueue().getUser().equals(SimpleString.toSimpleString(username))) { - queuesForUser++; + AtomicInteger bindingsCount = new AtomicInteger(0); + postOffice.getAllBindings().forEach((b) -> { + if (b instanceof LocalQueueBinding) { + LocalQueueBinding l = (LocalQueueBinding) b; + SimpleString user = l.getQueue().getUser(); + if (user != null) { + if (user.equals(userNameSimpleString)) { + bindingsCount.incrementAndGet(); + } + } } - } - - return queuesForUser; + }); + return bindingsCount.get(); } protected ServerSessionImpl internalCreateSession(String name, diff --git a/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/server/ResourceLimitTest.java b/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/server/ResourceLimitTest.java index e506fadff1..0c041297c4 100644 --- a/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/server/ResourceLimitTest.java +++ b/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/server/ResourceLimitTest.java @@ -19,6 +19,7 @@ package org.apache.activemq.artemis.tests.integration.server; import java.util.HashSet; import java.util.Set; +import org.apache.activemq.artemis.api.core.ActiveMQSecurityException; import org.apache.activemq.artemis.api.core.ActiveMQSessionCreationException; import org.apache.activemq.artemis.api.core.QueueConfiguration; import org.apache.activemq.artemis.api.core.RoutingType; @@ -104,7 +105,7 @@ public class ResourceLimitTest extends ActiveMQTestBase { try { clientSession.createQueue(new QueueConfiguration("anotherQueue").setAddress("address").setRoutingType(RoutingType.ANYCAST).setDurable(false)); } catch (Exception e) { - assertTrue(e instanceof ActiveMQSessionCreationException); + assertTrue(e instanceof ActiveMQSecurityException); } clientSession.deleteQueue("queue"); @@ -114,13 +115,13 @@ public class ResourceLimitTest extends ActiveMQTestBase { try { clientSession.createQueue(new QueueConfiguration("anotherQueue").setAddress("address").setRoutingType(RoutingType.ANYCAST).setDurable(false)); } catch (Exception e) { - assertTrue(e instanceof ActiveMQSessionCreationException); + assertTrue(e instanceof ActiveMQSecurityException); } try { clientSession.createSharedQueue(new QueueConfiguration("anotherQueue").setAddress("address").setDurable(false)); } catch (Exception e) { - assertTrue(e instanceof ActiveMQSessionCreationException); + assertTrue(e instanceof ActiveMQSecurityException); } } } diff --git a/tests/smoke-tests/pom.xml b/tests/smoke-tests/pom.xml index 3257936b5e..5511fd1ede 100644 --- a/tests/smoke-tests/pom.xml +++ b/tests/smoke-tests/pom.xml @@ -739,7 +739,24 @@ ${basedir}/target/classes/servers/brokerConnectMirrorSecurityB - + + + + test-compile + createBrokerMaxQueueResourceTest + + create + + + false + A + A + true + ${basedir}/target/MaxQueueResourceTest + ${basedir}/target/classes/servers/MaxQueueResourceTest + + + org.apache.activemq.tests diff --git a/tests/smoke-tests/src/main/resources/servers/MaxQueueResourceTest/artemis-roles.properties b/tests/smoke-tests/src/main/resources/servers/MaxQueueResourceTest/artemis-roles.properties new file mode 100644 index 0000000000..1093896974 --- /dev/null +++ b/tests/smoke-tests/src/main/resources/servers/MaxQueueResourceTest/artemis-roles.properties @@ -0,0 +1,18 @@ +## --------------------------------------------------------------------------- +## 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. +## --------------------------------------------------------------------------- + +amq=admin,johnopenwire,johnamqp,johncore \ No newline at end of file diff --git a/tests/smoke-tests/src/main/resources/servers/MaxQueueResourceTest/artemis-users.properties b/tests/smoke-tests/src/main/resources/servers/MaxQueueResourceTest/artemis-users.properties new file mode 100644 index 0000000000..96737e1130 --- /dev/null +++ b/tests/smoke-tests/src/main/resources/servers/MaxQueueResourceTest/artemis-users.properties @@ -0,0 +1,21 @@ +## --------------------------------------------------------------------------- +## 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. +## --------------------------------------------------------------------------- + +johnamqp = doe +johnopenwire = doe +johncore = doe +admin = admin \ No newline at end of file diff --git a/tests/smoke-tests/src/main/resources/servers/MaxQueueResourceTest/broker.xml b/tests/smoke-tests/src/main/resources/servers/MaxQueueResourceTest/broker.xml new file mode 100644 index 0000000000..10dfa941e5 --- /dev/null +++ b/tests/smoke-tests/src/main/resources/servers/MaxQueueResourceTest/broker.xml @@ -0,0 +1,246 @@ + + + + + + + + 0.0.0.0 + + + + false + + + NIO + + data/paging + + data/bindings + + data/journal + + data/large-messages + + true + + 2 + + 10 + + 4096 + + 10M + + + 40000 + + + + 1 + + + + + + + + + + + + + + + + + + + + + 5000 + + + 90 + + + true + + 120000 + + 60000 + + HALT + + + 40000 + + + + tcp://0.0.0.0:61616?tcpSendBufferSize=1048576;tcpReceiveBufferSize=1048576;amqpMinLargeMessageSize=102400;protocols=CORE,AMQP,STOMP,HORNETQ,MQTT,OPENWIRE;useEpoll=true;amqpCredits=1000;amqpLowCredits=300;amqpDuplicateDetection=true + + + + + 3 + + + + 5 + + + 3 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + DLQ + ExpiryQueue + 0 + + -1 + 10 + PAGE + true + true + true + true + + + + DLQ + ExpiryQueue + 0 + + -1 + 10 + PAGE + true + true + true + true + + + + +
+ + + +
+
+ + + +
+
+ + + +
+
+ + + +
+ +
+ +
+ +
+ + + + +
+
diff --git a/tests/smoke-tests/src/test/java/org/apache/activemq/artemis/tests/smoke/resourcetest/MaxQueueResourceTest.java b/tests/smoke-tests/src/test/java/org/apache/activemq/artemis/tests/smoke/resourcetest/MaxQueueResourceTest.java new file mode 100644 index 0000000000..b732c03d0b --- /dev/null +++ b/tests/smoke-tests/src/test/java/org/apache/activemq/artemis/tests/smoke/resourcetest/MaxQueueResourceTest.java @@ -0,0 +1,87 @@ +/* + * 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.tests.smoke.resourcetest; + +import javax.jms.Connection; +import javax.jms.ConnectionFactory; +import javax.jms.JMSSecurityException; +import javax.jms.MessageConsumer; +import javax.jms.MessageProducer; +import javax.jms.Session; +import javax.jms.Topic; + +import org.apache.activemq.artemis.tests.smoke.common.SmokeTestBase; +import org.apache.activemq.artemis.tests.util.CFUtil; +import org.apache.activemq.artemis.util.ServerUtil; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +public class MaxQueueResourceTest extends SmokeTestBase { + + public static final String SERVER_NAME_A = "MaxQueueResourceTest"; + + @Before + public void before() throws Exception { + startServer(SERVER_NAME_A, 0, 0); + ServerUtil.waitForServerToStart(0, "admin", "admin", 30000); + } + + @Test + public void testMaxQueue() throws Throwable { + // We call the three protocols in sequence here for two reasons: + // 1st: to actually test each protocol + // 2nd: Having more users creating stuff, makes the test more challenging (just in case) + // + // Notice that each protocol will concatenate the protocol name to the user and the clientID, + // which has been prepared by the server used on this test. + internalMaxQueue("core"); + internalMaxQueue("openwire"); + internalMaxQueue("amqp"); + } + + private void internalMaxQueue(String protocol) throws Throwable { + ConnectionFactory cfA = CFUtil.createConnectionFactory(protocol, "tcp://localhost:61616"); + + + try (Connection connectionA = cfA.createConnection("john" + protocol, "doe")) { + connectionA.setClientID("c1" + protocol); + Session sessionA = connectionA.createSession(false, Session.AUTO_ACKNOWLEDGE); + Topic topic = sessionA.createTopic("myTopic"); + MessageConsumer consumer1 = sessionA.createDurableSubscriber(topic, "t1"); + MessageConsumer consumer2 = sessionA.createDurableSubscriber(topic, "t2"); + MessageConsumer consumer3 = sessionA.createDurableSubscriber(topic, "t3"); + Exception exception = null; + MessageConsumer consumer4 = null; + + try { + consumer4 = sessionA.createDurableSubscriber(topic, "t4"); + } catch (JMSSecurityException e) { + exception = e; + } + Assert.assertNull(consumer4); + Assert.assertNotNull(exception); + MessageProducer producerA = sessionA.createProducer(topic); + for (int i = 0; i < 10; i++) { + producerA.send(sessionA.createTextMessage("toB")); + } + + } + } + +}