Class TicketPool


  • public class TicketPool
    extends java.lang.Object
    Helper class that implements a semaphore to support a system overload mechanism.
    Before a request is to be accepted, a ticket must be obtained. If the maximum amount of tickets in the pool is reached, the request is delayed until another process has released its ticket. After obtaining a ticket, the system must release this ticket back to the TicketPool.
    • Constructor Summary

      Constructors 
      Constructor Description
      TicketPool​(int availableTickets)  
      TicketPool​(java.lang.String id, int availableTickets)  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      long availableTickets()  
      int getCapacity()  
      java.lang.String getId()  
      int getMaxTickets()  
      java.lang.String getName()  
      int getUsedTickets()  
      boolean isTraceEnabled()  
      void obtain()
      Obtain a ticket.
      void obtain​(int count)
      Obtain a given amount of tickets.
      void obtain​(int count, boolean force)  
      boolean obtain​(int count, boolean force, boolean noWait)  
      void release()
      Releases a ticket and notifies potentially waiting threads, that new tickets are in the pool.
      void release​(int count)
      Releases the given number of tickets and notifies potentially waiting threads, that new tickets are in the pool.
      void setCapacity​(int availableTickets)  
      void setNotificationBroadcasterSupport​(javax.management.NotificationBroadcasterSupport nbs)
      you must set a NotificationBroadcasterSupport and set traceEnabled to true and to allow monitoring wait times via JMX Notifications
      void setTraceEnabled​(boolean traceEnabled)
      you must set traceEnabled to true and set a NotificationBroadcasterSupport to allow monitoring wait times via JMX Notifications
      java.lang.String toString()  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Constructor Detail

      • TicketPool

        public TicketPool​(java.lang.String id,
                          int availableTickets)
      • TicketPool

        public TicketPool​(int availableTickets)
    • Method Detail

      • availableTickets

        public long availableTickets()
      • getUsedTickets

        public int getUsedTickets()
      • getMaxTickets

        public int getMaxTickets()
      • setCapacity

        public void setCapacity​(int availableTickets)
      • obtain

        public void obtain()
        Obtain a ticket.
      • obtain

        public void obtain​(int count)
                    throws java.lang.IllegalArgumentException
        Obtain a given amount of tickets. The thread is locked, until all tickets are obtained.
        Parameters:
        count - number of tickets to obtain. must be 0 > count < maxTickets
        Throws:
        java.lang.IllegalArgumentException - Thrown if, count is < 0 or count > maxTickets when forcing is disabled.
      • obtain

        public void obtain​(int count,
                           boolean force)
                    throws java.lang.IllegalArgumentException
        Throws:
        java.lang.IllegalArgumentException
      • obtain

        public boolean obtain​(int count,
                              boolean force,
                              boolean noWait)
                       throws java.lang.IllegalArgumentException
        Throws:
        java.lang.IllegalArgumentException
      • release

        public void release​(int count)
        Releases the given number of tickets and notifies potentially waiting threads, that new tickets are in the pool.
        Parameters:
        count - number of tickets to be released
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • release

        public void release()
        Releases a ticket and notifies potentially waiting threads, that new tickets are in the pool.
      • getName

        public java.lang.String getName()
      • getId

        public java.lang.String getId()
      • getCapacity

        public int getCapacity()
      • isTraceEnabled

        public boolean isTraceEnabled()
      • setNotificationBroadcasterSupport

        public void setNotificationBroadcasterSupport​(javax.management.NotificationBroadcasterSupport nbs)
        you must set a NotificationBroadcasterSupport and set traceEnabled to true and to allow monitoring wait times via JMX Notifications
        Parameters:
        nbs - NotificationBroadcasterSupport to be set if monitoring wait via JMX is desired
        See Also:
        setTraceEnabled(boolean)