public final class ResourceThrottle extends Object
To use this class, it simply has to be instantiated. Any client code wishing to access a resource (let's call this resource "x()"), then the following code can be used when accessing the resource to hinder flooding:
ResourceThrottle throttle = .... // retrieve common resource throttle Object handle = throttle.checkout("Some user"); try { if (handle == null) { // report error that the resource is flooded. Don't use it! } else { // resource is not flooded x(); } } finally { throttle.checkin(handle); }
Modifier and Type | Field and Description |
---|---|
static Runnable |
DO_NOTHING |
protected org.apache.log4j.Logger |
mLogger |
static String |
NO_DESCRIPTION |
Constructor and Description |
---|
ResourceThrottle() |
Modifier and Type | Method and Description |
---|---|
void |
checkin(Object pHandle)
Check in the resource from the previously allocated handle.
|
Object |
checkout()
Attempt to acquire a resource.
|
Object |
checkout(String pWhat)
Attempt to acquire a resource, giving a description of what
the resource is being used for.
|
void |
doStartService() |
void |
doStopService() |
protected void |
failedCheckout(String pWhat) |
Collection |
getActiveResources()
Retrieves a list string representations of all active resources.
|
int |
getAvailableCapacity()
Retrieve the number of free resources that this throttle attempts to govern.
|
int |
getMaximumConcurrent()
Retrieve the maximum number of concurrent usages of a specific resource.
|
Runnable |
getOverloadAction() |
String |
getOverloadMessage()
Retrieve the message that clients can use when handling the case in which the
server has been overloaded.
|
ThrottleStatusCollector |
getStatusCollector() |
boolean |
isServiceEnabled()
Whether or not the service is enabled.
|
boolean |
isServiceRunning()
Whether or not the service is running.
|
void |
setMaximumConcurrent(int pMaxiumConcurrent) |
void |
setOverloadAction(Runnable pNewAction) |
void |
setOverloadMessage(String pOverloadMessage) |
void |
setServiceEnabled(boolean pServiceEnabled) |
void |
setStatusCollector(ThrottleStatusCollector pStatusCollector) |
public static String NO_DESCRIPTION
public static Runnable DO_NOTHING
protected org.apache.log4j.Logger mLogger
public void setMaximumConcurrent(int pMaxiumConcurrent)
public int getMaximumConcurrent()
public int getAvailableCapacity()
public void setOverloadMessage(String pOverloadMessage)
public String getOverloadMessage()
public Collection getActiveResources()
public Object checkout()
public Object checkout(String pWhat)
pWhat
- typically used to describe what the resource is being
used for. For example, in a web environment, the URI of the request
would be normal. In the case of a job scheduler, the name of a job.getActiveResources()
protected void failedCheckout(String pWhat)
public void checkin(Object pHandle)
public Runnable getOverloadAction()
public void setOverloadAction(Runnable pNewAction)
public ThrottleStatusCollector getStatusCollector()
public void setStatusCollector(ThrottleStatusCollector pStatusCollector)
public boolean isServiceRunning()
public boolean isServiceEnabled()
public void setServiceEnabled(boolean pServiceEnabled)
public void doStartService()
public void doStopService()
© 1998- 2018 Stibo DX