本文主要研究一下artemis的ResourceLimitSettings
activemq-artemis-2.11.0/artemis-server/src/main/java/org/apache/activemq/artemis/core/settings/impl/ResourceLimitSettings.java
public class ResourceLimitSettings implements Serializable, EncodingSupport {
private static final long serialVersionUID = -110638321333856932L;
public static final SimpleString DEFAULT_MATCH = null;
public static final Integer DEFAULT_MAX_CONNECTIONS = -1;
public static final Integer DEFAULT_MAX_QUEUES = -1;
SimpleString match = null;
Integer maxConnections = null;
Integer maxQueues = null;
//......
}
activemq-artemis-2.11.0/artemis-server/src/main/java/org/apache/activemq/artemis/core/server/impl/ActiveMQServerImpl.java
public class ActiveMQServerImpl implements ActiveMQServer {
//......
private void checkSessionLimit(String username) throws Exception {
if (configuration.getResourceLimitSettings() != null && configuration.getResourceLimitSettings().containsKey(username)) {
ResourceLimitSettings limits = configuration.getResourceLimitSettings().get(username);
if (limits.getMaxConnections() == -1) {
return;
} else if (limits.getMaxConnections() == 0 || getSessionCountForUser(username) >= limits.getMaxConnections()) {
throw ActiveMQMessageBundle.BUNDLE.sessionLimitReached(username, limits.getMaxConnections());
}
}
}
private int getSessionCountForUser(String username) {
int sessionCount = 0;
for (Entry<String, ServerSession> sessionEntry : sessions.entrySet()) {
if (sessionEntry.getValue().getUsername().equals(username)) {
sessionCount++;
}
}
return sessionCount;
}
public void checkQueueCreationLimit(String username) throws Exception {
if (configuration.getResourceLimitSettings() != null && configuration.getResourceLimitSettings().containsKey(username)) {
ResourceLimitSettings limits = configuration.getResourceLimitSettings().get(username);
if (limits.getMaxQueues() == -1) {
return;
} else if (limits.getMaxQueues() == 0 || getQueueCountForUser(username) >= limits.getMaxQueues()) {
throw ActiveMQMessageBundle.BUNDLE.queueLimitReached(username, limits.getMaxQueues());
}
}
}
public int getQueueCountForUser(String username) throws Exception {
Map<SimpleString, Binding> bindings = postOffice.getAllBindings();
int queuesForUser = 0;
for (Binding binding : bindings.values()) {
if (binding instanceof LocalQueueBinding && ((LocalQueueBinding) binding).getQueue().getUser().equals(SimpleString.toSimpleString(username))) {
queuesForUser++;
}
}
return queuesForUser;
}
//......
}
ResourceLimitSettings定义了maxConnections、maxQueues属性;artemis在createSession的时候会执行checkSessionLimit方法,在createQueue的时候会执行checkQueueCreationLimit方法