Package net.spy.memcached.internal
Class OperationFuture<T>
java.lang.Object
net.spy.memcached.compat.SpyObject
net.spy.memcached.internal.AbstractListenableFuture<T,OperationCompletionListener>
net.spy.memcached.internal.OperationFuture<T>
- Type Parameters:
T
- Type of object returned from this future.
- All Implemented Interfaces:
Future<T>
,ListenableFuture<T,
OperationCompletionListener>
public class OperationFuture<T>
extends AbstractListenableFuture<T,OperationCompletionListener>
implements Future<T>
Managed future for operations.
From an OperationFuture, application code can determine if the status of a given Operation in an asynchronous manner.
If for example we needed to update the keys "user:
-
Nested Class Summary
Nested classes/interfaces inherited from interface java.util.concurrent.Future
Future.State
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate Long
private final String
private final CountDownLatch
private final AtomicReference
<T> private Operation
protected OperationStatus
private final long
-
Constructor Summary
ConstructorsConstructorDescriptionOperationFuture
(String k, CountDownLatch l, long opTimeout, ExecutorService service) Create an OperationFuture for a given async operation.OperationFuture
(String k, CountDownLatch l, AtomicReference<T> oref, long opTimeout, ExecutorService service) Create an OperationFuture for a given async operation. -
Method Summary
Modifier and TypeMethodDescriptionaddListener
(OperationCompletionListener listener) Add a listener to the future, which will be executed once the operation completes.boolean
cancel()
Cancel this operation, if possible.boolean
cancel
(boolean ign) Deprecated.get()
Get the results of the given operation.Get the results of the given operation.getCas()
Get the CAS for this operation.getKey()
Get the key for this operation.Get the current status of this operation.boolean
Whether or not the Operation associated with this OperationFuture has been canceled.boolean
isDone()
Whether or not the Operation is done and result can be retrieved with get().removeListener
(OperationCompletionListener listener) Remove a previously added listener from the future.void
set
(T o, OperationStatus s) Set the Operation associated with this OperationFuture.void
setCas
(long inCas) Set the key for this operation.void
Set the Operation associated with this OperationFuture.void
Signals that this future is complete.Methods inherited from class net.spy.memcached.internal.AbstractListenableFuture
addToListeners, executor, notifyListener, notifyListeners, notifyListeners, removeFromListeners
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface java.util.concurrent.Future
exceptionNow, resultNow, state
-
Field Details
-
latch
-
objRef
-
status
-
timeout
private final long timeout -
op
-
key
-
cas
-
-
Constructor Details
-
OperationFuture
Create an OperationFuture for a given async operation. This is intended for internal use only.- Parameters:
k
- the key for the operationl
- the latch to be used counting down the OperationFutureopTimeout
- the timeout within which the operation needs to be done
-
OperationFuture
public OperationFuture(String k, CountDownLatch l, AtomicReference<T> oref, long opTimeout, ExecutorService service) Create an OperationFuture for a given async operation. This is intended for internal use only.- Parameters:
k
- the key for the operationl
- the latch to be used counting down the OperationFutureoref
- an AtomicReference associated with the operationopTimeout
- the timeout within which the operation needs to be done
-
-
Method Details
-
cancel
public boolean cancel(boolean ign) Deprecated.Cancel this operation, if possible. -
cancel
public boolean cancel()Cancel this operation, if possible.- Returns:
- true if the operation has not yet been written to the network
-
get
Get the results of the given operation. As with the Future interface, this call will block until the results of the future operation has been received.- Specified by:
get
in interfaceFuture<T>
- Returns:
- the operation results of this OperationFuture
- Throws:
InterruptedException
ExecutionException
-
get
public T get(long duration, TimeUnit units) throws InterruptedException, TimeoutException, ExecutionException Get the results of the given operation. As with the Future interface, this call will block until the results of the future operation has been received.- Specified by:
get
in interfaceFuture<T>
- Parameters:
duration
- amount of time to waitunits
- unit of time to wait- Returns:
- the operation results of this OperationFuture
- Throws:
InterruptedException
TimeoutException
ExecutionException
-
getKey
Get the key for this operation.- Returns:
- the key for this operation
-
setCas
public void setCas(long inCas) Set the key for this operation.- Parameters:
inCas
- the CAS value
-
getCas
Get the CAS for this operation. The interrupted status of the current thread is cleared by this method. Inspect the returned OperationStatus to check whether an interruption has taken place.- Returns:
- the CAS for this operation or null if unsuccessful.
- Throws:
UnsupportedOperationException
- If this is for an ASCII protocol configured client.
-
getStatus
Get the current status of this operation. Note that the operation status may change as the operation is tried and potentially retried against the servers specified by the NodeLocator. The interrupted status of the current thread is cleared by this method. Inspect the returned OperationStatus to check whether an interruption has taken place.- Returns:
- OperationStatus
-
set
Set the Operation associated with this OperationFuture. This is intended for internal use only.- Parameters:
o
- the Operation object itselfs
- the OperationStatus associated with this operation
-
setOperation
Set the Operation associated with this OperationFuture. This is intended for internal use only.- Parameters:
to
- the Operation to set this OperationFuture to be tracking
-
isCancelled
public boolean isCancelled()Whether or not the Operation associated with this OperationFuture has been canceled. One scenario in which this can occur is if the connection is lost and the Operation has been sent over the network. In this case, the operation may or may not have reached the server before the connection was dropped.- Specified by:
isCancelled
in interfaceFuture<T>
- Returns:
- true if the Operation has been canceled
-
isDone
public boolean isDone()Whether or not the Operation is done and result can be retrieved with get(). The most common way to wait for this OperationFuture is to use the get() method which will block. This method allows one to check if it's complete without blocking. -
addListener
Description copied from interface:ListenableFuture
Add a listener to the future, which will be executed once the operation completes.- Specified by:
addListener
in interfaceListenableFuture<T,
OperationCompletionListener> - Parameters:
listener
- the listener which will be executed.- Returns:
- the current future to allow for object-chaining.
-
removeListener
Description copied from interface:ListenableFuture
Remove a previously added listener from the future.- Specified by:
removeListener
in interfaceListenableFuture<T,
OperationCompletionListener> - Parameters:
listener
- the previously added listener.- Returns:
- the current future to allow for object-chaining.
-
signalComplete
public void signalComplete()Signals that this future is complete.
-