public interface Bucket
Use following links for further details:
Modifier and Type | Method and Description |
---|---|
void |
addTokens(long tokensToAdd)
Add tokensToAdd to bucket.
|
AsyncBucket |
asAsync()
Returns asynchronous view of this bucket.
|
AsyncScheduledBucket |
asAsyncScheduler()
Returns asynchronous view of this bucket that allows to use bucket as async scheduler.
|
BlockingBucket |
asScheduler()
Returns the
BlockingBucket view of this bucket, that provides operations which are able to block caller thread. |
BucketState |
createSnapshot()
Creates the copy of internal state.
|
long |
getAvailableTokens()
Returns amount of available tokens in this bucket.
|
boolean |
isAsyncModeSupported()
Describes whether or not this bucket supports asynchronous mode.
|
void |
replaceConfiguration(BucketConfiguration newConfiguration)
Replaces configuration of this bucket instance.
|
Bucket |
toListenable(BucketListener listener)
Returns new copy of this bucket instance decorated by
listener . |
boolean |
tryConsume(long numTokens)
Tries to consume a specified number of tokens from this bucket.
|
ConsumptionProbe |
tryConsumeAndReturnRemaining(long numTokens)
Tries to consume a specified number of tokens from this bucket.
|
long |
tryConsumeAsMuchAsPossible()
Tries to consume as much tokens from this bucket as available at the moment of invocation.
|
long |
tryConsumeAsMuchAsPossible(long limit)
Tries to consume as much tokens from bucket as available in the bucket at the moment of invocation,
but tokens which should be consumed is limited by
limit . |
BlockingBucket asScheduler()
BlockingBucket
view of this bucket, that provides operations which are able to block caller thread.boolean isAsyncModeSupported()
If asynchronous mode is not supported any attempt to call asAsync()
will fail with UnsupportedOperationException
AsyncBucket asAsync()
If asynchronous mode is not supported by particular extension behind this bucket,
then any attempt to call this method will fail with UnsupportedOperationException
.
UnsupportedOperationException
- if particular extension behind the bucket does not support asynchronous mode.AsyncScheduledBucket asAsyncScheduler()
If asynchronous mode is not supported by particular extension behind this bucket,
then any attempt to call this method will fail with UnsupportedOperationException
.
UnsupportedOperationException
- if particular extension behind the bucket does not support asynchronous mode.boolean tryConsume(long numTokens)
numTokens
- The number of tokens to consume from the bucket, must be a positive number.true
if the tokens were consumed, false
otherwise.ConsumptionProbe tryConsumeAndReturnRemaining(long numTokens)
numTokens
- The number of tokens to consume from the bucket, must be a positive number.ConsumptionProbe
which describes both result of consumption and tokens remaining in the bucket after consumption.long tryConsumeAsMuchAsPossible()
long tryConsumeAsMuchAsPossible(long limit)
limit
.limit
- maximum number of tokens to consume, should be positive.void addTokens(long tokensToAdd)
newTokens = Math.min(capacity, currentTokens + tokensToAdd)in other words resulted number of tokens never exceeds capacity independent of tokensToAdd.
Bucket wallet;
...
if(wallet.tryConsume(50)) {// get 50 cents from wallet
try {
buyCocaCola();
} catch(NoCocaColaException e) {
// return money to wallet
wallet.addTokens(50);
}
};
tokensToAdd
- number of tokens to addlong getAvailableTokens()
This method designed to be used only for monitoring and testing, you should never use this method for business cases, because available tokens can be changed by concurrent transactions for case of multithreaded/multi-process environment.
void replaceConfiguration(BucketConfiguration newConfiguration)
Rules of reconfiguration:
IncompatibleConfigurationException
will be thrown.
In other words you must not try to reduce or increase count of bandwidths,
it is impossible to create bucket with one bandwidth and reconfigure to use two bandwidths and vice-versa.
newConfiguration
- the new configuration for this bucketIncompatibleConfigurationException
- if newConfiguration
incompatible with previous configurationBucketState createSnapshot()
This method is designed to be used only for monitoring and testing, you should never use this method for business cases.
Bucket toListenable(BucketListener listener)
listener
.
See javadocs for BucketListener
in order to understand semantic of listener.listener
- the listener of bucket events.listener
Copyright © 2018. All rights reserved.