Class SemaphoreValve

  • All Implemented Interfaces:
    javax.management.MBeanRegistration, Contained, JmxEnabled, Lifecycle, Valve

    public class SemaphoreValve
    extends ValveBase

    Implementation of a Valve that limits concurrency.

    This Valve may be attached to any Container, depending on the granularity of the concurrency control you wish to perform. Note that internally, some async requests may require multiple serial requests to complete what - to the user - appears as a single request.

    • Field Detail

      • semaphore

        protected java.util.concurrent.Semaphore semaphore
        Semaphore.
      • concurrency

        protected int concurrency
        Concurrency level of the semaphore.
      • fairness

        protected boolean fairness
        Fairness of the semaphore.
      • block

        protected boolean block
        Block until a permit is available.
      • interruptible

        protected boolean interruptible
        Block interruptibly until a permit is available.
      • highConcurrencyStatus

        protected int highConcurrencyStatus
        High concurrency status. This status code is returned as an error if concurrency is too high.
    • Constructor Detail

      • SemaphoreValve

        public SemaphoreValve()
        Construct a new SemaphoreValve with async support enabled.
    • Method Detail

      • getConcurrency

        public int getConcurrency()
        Return the concurrency level of the semaphore.
        Returns:
        the concurrency level
      • setConcurrency

        public void setConcurrency​(int concurrency)
        Set the concurrency level of the semaphore.
        Parameters:
        concurrency - the concurrency level
      • getFairness

        public boolean getFairness()
        Return the fairness setting of the semaphore.
        Returns:
        true if the semaphore uses fair ordering
      • setFairness

        public void setFairness​(boolean fairness)
        Set the fairness setting of the semaphore.
        Parameters:
        fairness - true if the semaphore should use fair ordering
      • getBlock

        public boolean getBlock()
        Return whether the valve blocks until a permit is available.
        Returns:
        true if the valve blocks waiting for a permit
      • setBlock

        public void setBlock​(boolean block)
        Set whether the valve blocks until a permit is available.
        Parameters:
        block - true if the valve should block waiting for a permit
      • getInterruptible

        public boolean getInterruptible()
        Return whether the valve blocks interruptibly.
        Returns:
        true if the valve blocks interruptibly
      • setInterruptible

        public void setInterruptible​(boolean interruptible)
        Set whether the valve blocks interruptibly.
        Parameters:
        interruptible - true if the valve should block interruptibly
      • getHighConcurrencyStatus

        public int getHighConcurrencyStatus()
        Return the HTTP status code returned when concurrency is too high.
        Returns:
        the status code, or -1 if no error is returned
      • setHighConcurrencyStatus

        public void setHighConcurrencyStatus​(int highConcurrencyStatus)
        Set the HTTP status code returned when concurrency is too high.
        Parameters:
        highConcurrencyStatus - the status code to return
      • invoke

        public void invoke​(Request request,
                           Response response)
                    throws java.io.IOException,
                           ServletException
        Do concurrency control on the request using the semaphore.
        Parameters:
        request - The servlet request to be processed
        response - The servlet response to be created
        Throws:
        java.io.IOException - if an input/output error occurs
        ServletException - if a servlet error occurs
      • controlConcurrency

        public boolean controlConcurrency​(Request request,
                                          Response response)
        Subclass friendly method to add conditions.
        Parameters:
        request - The Servlet request
        response - The Servlet response
        Returns:
        true if the concurrency control should occur on this request
      • permitDenied

        public void permitDenied​(Request request,
                                 Response response)
                          throws java.io.IOException,
                                 ServletException
        Subclass friendly method to add error handling when a permit isn't granted.
        Parameters:
        request - The Servlet request
        response - The Servlet response
        Throws:
        java.io.IOException - Error writing output
        ServletException - Other error