本文主要研究一下artemis的ResourceLimitSettings

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;

   //......
}
  • ResourceLimitSettings定义了maxConnections、maxQueues属性

ActiveMQServerImpl

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;

   }

   //......
}
  • ActiveMQServerImpl的checkSessionLimit方法会获取指定username的ResourceLimitSettings,若getSessionCountForUser大于等于limits.getMaxConnections()或者limits.getMaxConnections()为0,则抛出ActiveMQMessageBundle.BUNDLE.sessionLimitReached;checkQueueCreationLimit方法会获取指定username的ResourceLimitSettings,若getQueueCountForUser大于等于limits.getMaxQueues()或者limits.getMaxQueues() == 0,则抛出ActiveMQMessageBundle.BUNDLE.queueLimitReached

小结

ResourceLimitSettings定义了maxConnections、maxQueues属性;artemis在createSession的时候会执行checkSessionLimit方法,在createQueue的时候会执行checkQueueCreationLimit方法

doc


codecraft
11.9k 声望2k 粉丝

当一个代码的工匠回首往事时,不因虚度年华而悔恨,也不因碌碌无为而羞愧,这样,当他老的时候,可以很自豪告诉世人,我曾经将代码注入生命去打造互联网的浪潮之巅,那是个很疯狂的时代,我在一波波的浪潮上留下...


引用和评论

0 条评论