Package org.copperengine.core.common
Class TicketPool
- java.lang.Object
-
- org.copperengine.core.common.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 aNotificationBroadcasterSupport
and settraceEnabled
totrue
and to allow monitoring wait times via JMXNotification
svoid
setTraceEnabled(boolean traceEnabled)
you must settraceEnabled
totrue
and set aNotificationBroadcasterSupport
to allow monitoring wait times via JMXNotification
sjava.lang.String
toString()
-
-
-
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 classjava.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()
-
setTraceEnabled
public void setTraceEnabled(boolean traceEnabled)
you must settraceEnabled
totrue
and set aNotificationBroadcasterSupport
to allow monitoring wait times via JMXNotification
s- Parameters:
traceEnabled
- boolean to be set if tracing via JMX is desired- See Also:
setNotificationBroadcasterSupport(NotificationBroadcasterSupport)
-
setNotificationBroadcasterSupport
public void setNotificationBroadcasterSupport(javax.management.NotificationBroadcasterSupport nbs)
you must set aNotificationBroadcasterSupport
and settraceEnabled
totrue
and to allow monitoring wait times via JMXNotification
s- Parameters:
nbs
- NotificationBroadcasterSupport to be set if monitoring wait via JMX is desired- See Also:
setTraceEnabled(boolean)
-
-