Module bus.limiter

Class ResourceManager

java.lang.Object
org.miaixz.bus.limiter.metric.ResourceManager

public class ResourceManager extends Object
Manages resources and their associated limiting protections for a specific user or context. This class tracks the state of various resources, applying Limiting rules to control access and prevent overload.
Since:
Java 17+
Author:
Kimi Liu
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static void
    add(String resourceKey)
    Adds a resource key to the set of protected methods.
    static boolean
    contain(String resourceKey)
    Checks if a given resource key is currently registered as a protected method.
    boolean
    entry(String resourceKey, Limiting limiting)
    Attempts to gain entry to a resource, applying the specified limiting rules.
    boolean
    Checks if all managed resources within this ResourceManager are clear (i.e., their protection has expired and they have been removed).

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • ResourceManager

      public ResourceManager()
  • Method Details

    • contain

      public static boolean contain(String resourceKey)
      Checks if a given resource key is currently registered as a protected method.
      Parameters:
      resourceKey - The unique identifier of the resource.
      Returns:
      true if the resource is protected, false otherwise.
    • add

      public static void add(String resourceKey)
      Adds a resource key to the set of protected methods. This marks the resource as being under some form of limiting protection.
      Parameters:
      resourceKey - The unique identifier of the resource to add.
    • entry

      public boolean entry(String resourceKey, Limiting limiting) throws IllegalStateException
      Attempts to gain entry to a resource, applying the specified limiting rules. If the resource is not yet managed, a new ResourceManager.Protection instance is created. It checks for expiration and remaining allowance before granting access.
      Parameters:
      resourceKey - The unique identifier of the resource to access.
      limiting - The Limiting annotation defining the rules for this resource.
      Returns:
      true if access is allowed, false if access is denied due to limiting.
      Throws:
      IllegalStateException - if the state of the protection is invalid (though current implementation does not explicitly throw this).
    • isClear

      public boolean isClear()
      Checks if all managed resources within this ResourceManager are clear (i.e., their protection has expired and they have been removed). This method iterates through the managed resources and removes expired ones.
      Returns:
      true if all resources are clear and the map is empty, false otherwise.