聊聊artemis的ResourceLimitSettings

原创
2020/02/21 20:04
阅读数 129

本文主要研究一下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

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部