public class Window<K,R,P> extends Object
| Constructor and Description |
|---|
Window(int size)
Creates a new window with the specified max window size.
|
Window(int size,
ScheduledExecutorService executor,
long monitorInterval,
WindowListener<K,R,P> listener)
Creates a new window with the specified max window size.
|
Window(int size,
ScheduledExecutorService executor,
long monitorInterval,
WindowListener<K,R,P> listener,
String monitorThreadName)
Creates a new window with the specified max window size.
|
| Modifier and Type | Method and Description |
|---|---|
boolean |
abortPendingOffers()
Aborts all current callers/threads waiting for a pending offer to be
accepted by the window.
|
void |
addListener(WindowListener<K,R,P> listener)
Adds a new WindowListener if and only if it isn't already present.
|
WindowFuture<K,R,P> |
cancel(K key)
Cancels (completes) a request.
|
List<WindowFuture<K,R,P>> |
cancelAll()
Cancels (completes) all requests.
|
List<WindowFuture<K,R,P>> |
cancelAllExpired()
Cancels (completes) all expired requests.
|
WindowFuture<K,R,P> |
complete(K key,
P response)
Successfully completes a request by setting the response on the associated future.
|
boolean |
containsKey(K key)
Returns true if and only if a future with this key exists in this window.
|
Map<K,WindowFuture<K,R,P>> |
createSortedSnapshot()
Creates an ordered snapshot of the requests in this window.
|
void |
destroy()
Destroy this window by freeing all resources associated with it.
|
WindowFuture<K,R,P> |
fail(K key,
Throwable t)
Fails (completes) a request by setting the cause of the failure on the associated future.
|
List<WindowFuture<K,R,P>> |
failAll(Throwable t)
Fails (completes) all requests by setting the same cause of the failure
on all associated futures.
|
WindowFuture<K,R,P> |
get(K key)
Gets the a future by its key.
|
int |
getFreeSize()
Gets the current number of request that would be accepted by this
window without blocking.
|
int |
getMaxSize()
Gets the max size of the window.
|
int |
getPendingOfferCount()
Gets the current number of callers/threads that are waiting for a pending
offer to be accepted.
|
int |
getSize()
Gets the current number of requests in the window.
|
WindowFuture |
offer(K key,
R request,
long offerTimeoutMillis)
Offers a request for acceptance, waiting for the specified amount of time
in case it could not immediately accepted.
|
WindowFuture |
offer(K key,
R request,
long offerTimeoutMillis,
long expireTimeoutMillis)
Offers a request for acceptance, waiting for the specified amount of time
in case it could not immediately accepted.
|
WindowFuture |
offer(K key,
R request,
long offerTimeoutMillis,
long expireTimeoutMillis,
boolean callerWaitingHint)
Offers a request for acceptance, waiting for the specified amount of time
in case it could not immediately accepted.
|
void |
removeListener(WindowListener<K,R,P> listener)
Removes a WindowListener if it is present.
|
boolean |
startMonitor()
Starts the monitor if this Window has an executor.
|
void |
stopMonitor()
Stops the monitor if its running.
|
public Window(int size)
size - The maximum number of requests permitted to
be outstanding (unacknowledged) at a given time. Must be > 0.public Window(int size,
ScheduledExecutorService executor,
long monitorInterval,
WindowListener<K,R,P> listener)
size - The maximum number of requests permitted to
be outstanding (unacknowledged) at a given time. Must be > 0.executor - The scheduled executor service to execute
recurring tasks (such as expiration of requests).monitorInterval - The number of milliseconds between executions of
monitoring tasks.listener - A listener to send window events topublic Window(int size,
ScheduledExecutorService executor,
long monitorInterval,
WindowListener<K,R,P> listener,
String monitorThreadName)
size - The maximum number of requests permitted to
be outstanding (unacknowledged) at a given time. Must be > 0.executor - The scheduled executor service to execute
recurring tasks (such as expiration of requests).monitorInterval - The number of milliseconds between executions of
monitoring tasks.listener - A listener to send window events tomonitorThreadName - The thread name we'll change to when a monitor
run is executed. Null if no name change is required.public int getMaxSize()
public int getSize()
public int getFreeSize()
public boolean containsKey(K key)
key - The key for the futurepublic WindowFuture<K,R,P> get(K key)
key - The key for the requestpublic void addListener(WindowListener<K,R,P> listener)
listener - The listener to addpublic void removeListener(WindowListener<K,R,P> listener)
listener - The listener to removepublic void destroy()
public boolean startMonitor()
public void stopMonitor()
public Map<K,WindowFuture<K,R,P>> createSortedSnapshot()
public WindowFuture offer(K key, R request, long offerTimeoutMillis) throws DuplicateKeyException, OfferTimeoutException, InterruptedException
key - The key for the request. A protocol's sequence number is a
good choice.request - The request to offerofferTimeoutMillis - The amount of time (in milliseconds) to wait
for the offer to be accepted.DuplicateKeyException - Thrown if the key already existsPendingOfferAbortedException - Thrown if the offer could not be
immediately accepted and the caller/thread was waiting, but
the abortPendingOffers() method was called in the meantime.OfferTimeoutException - Thrown if the offer could not be accepted
within the specified amount of time.InterruptedException - Thrown if the calling thread is interrupted
while waiting to acquire the internal lock.public WindowFuture offer(K key, R request, long offerTimeoutMillis, long expireTimeoutMillis) throws DuplicateKeyException, OfferTimeoutException, InterruptedException
key - The key for the request. A protocol's sequence number is a
good choice.request - The request to offerofferTimeoutMillis - The amount of time (in milliseconds) to wait
for the offer to be accepted.expireTimeoutMillis - The amount of time (in milliseconds) that a
request will be set to expire after acceptance. A value < 1 is
assumed to be an infinite expiration (request never expires).
Requests are not automatically expired unless monitoring was enabled
during construction of this window.DuplicateKeyException - Thrown if the key already existsPendingOfferAbortedException - Thrown if the offer could not be
immediately accepted and the caller/thread was waiting, but
the abortPendingOffers() method was called in the meantime.OfferTimeoutException - Thrown if the offer could not be accepted
within the specified amount of time.InterruptedException - Thrown if the calling thread is interrupted
while waiting to acquire the internal lock.public WindowFuture offer(K key, R request, long offerTimeoutMillis, long expireTimeoutMillis, boolean callerWaitingHint) throws DuplicateKeyException, OfferTimeoutException, PendingOfferAbortedException, InterruptedException
key - The key for the request. A protocol's sequence number is a
good choice.request - The request to offerofferTimeoutMillis - The amount of time (in milliseconds) to wait
for the offer to be accepted.expireTimeoutMillis - The amount of time (in milliseconds) that a
request will be set to expire after acceptance. A value < 1 is
assumed to be an infinite expiration (request never expires).
Requests are not automatically expired unless monitoring was enabled
during construction of this window.callerWaitingHint - If true the "caller state hint" of the
future will be set to "WAITING" during construction. This generally
does not affect any internal processing by this window, but allows
callers to hint they plan on calling "await()" on the future.DuplicateKeyException - Thrown if the key already existsPendingOfferAbortedException - Thrown if the offer could not be
immediately accepted and the caller/thread was waiting, but
the abortPendingOffers() method was called in the meantime.OfferTimeoutException - Thrown if the offer could not be accepted
within the specified amount of time.InterruptedException - Thrown if the calling thread is interrupted
while waiting to acquire the internal lock.public int getPendingOfferCount()
public boolean abortPendingOffers()
throws InterruptedException
InterruptedException - Thrown if the calling thread was interrupted
while waiting to obtain the window lock.public WindowFuture<K,R,P> complete(K key, P response) throws InterruptedException
key - The key for the original requestresponse - The response to set on the associated future. Null
responses are not accepted (use cancel()) instead.InterruptedException - Thrown if the calling thread is interrupted
and we're currently waiting to acquire the internal "windowLock".public WindowFuture<K,R,P> fail(K key, Throwable t) throws InterruptedException
key - The key for the original requestt - The throwable to set as the failure cause on the associated future.
Null values are not accepted (use cancel()) instead.InterruptedException - Thrown if the calling thread is interrupted
and we're currently waiting to acquire the internal "windowLock".public List<WindowFuture<K,R,P>> failAll(Throwable t) throws InterruptedException
t - The throwable to set as the failure cause on all associated futures.
Null values are not accepted (use cancelAll()) instead.InterruptedException - Thrown if the calling thread is interrupted
and we're currently waiting to acquire the internal "windowLock".public WindowFuture<K,R,P> cancel(K key) throws InterruptedException
key - The key for the original requestInterruptedException - Thrown if the calling thread is interrupted
and we're currently waiting to acquire the internal "windowLock".public List<WindowFuture<K,R,P>> cancelAll()
InterruptedException - Thrown if the calling thread is interrupted
and we're currently waiting to acquire the internal "windowLock".public List<WindowFuture<K,R,P>> cancelAllExpired()
InterruptedException - Thrown if the calling thread is interrupted
and we're currently waiting to acquire the internal "windowLock".Copyright © 2012-2015 Cloudhopper by Twitter. All Rights Reserved.