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:
java.util.concurrent.Future<T>
,ListenableFuture<T,OperationCompletionListener>
public class OperationFuture<T> extends AbstractListenableFuture<T,OperationCompletionListener> implements java.util.concurrent.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:
:name", "user: :friendlist" because later in the method we were going to verify the change occurred as expected interacting with the user, we can fire multiple IO operations simultaneously with this concept.
-
-
Constructor Summary
Constructors Constructor Description OperationFuture(java.lang.String k, java.util.concurrent.CountDownLatch l, long opTimeout, java.util.concurrent.ExecutorService service)
Create an OperationFuture for a given async operation.OperationFuture(java.lang.String k, java.util.concurrent.CountDownLatch l, java.util.concurrent.atomic.AtomicReference<T> oref, long opTimeout, java.util.concurrent.ExecutorService service)
Create an OperationFuture for a given async operation.
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description OperationFuture<T>
addListener(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.T
get()
Get the results of the given operation.T
get(long duration, java.util.concurrent.TimeUnit units)
Get the results of the given operation.java.lang.Long
getCas()
Get the CAS for this operation.java.lang.String
getKey()
Get the key for this operation.OperationStatus
getStatus()
Get the current status of this operation.boolean
isCancelled()
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().OperationFuture<T>
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
setOperation(Operation to)
Set the Operation associated with this OperationFuture.void
signalComplete()
Signals that this future is complete.-
Methods inherited from class net.spy.memcached.internal.AbstractListenableFuture
addToListeners, executor, notifyListener, notifyListeners, notifyListeners, removeFromListeners
-
-
-
-
Field Detail
-
latch
private final java.util.concurrent.CountDownLatch latch
-
objRef
private final java.util.concurrent.atomic.AtomicReference<T> objRef
-
status
protected OperationStatus status
-
timeout
private final long timeout
-
op
private Operation op
-
key
private final java.lang.String key
-
cas
private java.lang.Long cas
-
-
Constructor Detail
-
OperationFuture
public OperationFuture(java.lang.String k, java.util.concurrent.CountDownLatch l, long opTimeout, java.util.concurrent.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 OperationFutureopTimeout
- the timeout within which the operation needs to be done
-
OperationFuture
public OperationFuture(java.lang.String k, java.util.concurrent.CountDownLatch l, java.util.concurrent.atomic.AtomicReference<T> oref, long opTimeout, java.util.concurrent.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 Detail
-
cancel
public boolean cancel(boolean ign)
Deprecated.Cancel this operation, if possible.- Specified by:
cancel
in interfacejava.util.concurrent.Future<T>
- Parameters:
ign
- not used- Returns:
- true if the operation has not yet been written to the network
-
cancel
public boolean cancel()
Cancel this operation, if possible.- Returns:
- true if the operation has not yet been written to the network
-
get
public T get() throws java.lang.InterruptedException, java.util.concurrent.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 interfacejava.util.concurrent.Future<T>
- Returns:
- the operation results of this OperationFuture
- Throws:
java.lang.InterruptedException
java.util.concurrent.ExecutionException
-
get
public T get(long duration, java.util.concurrent.TimeUnit units) throws java.lang.InterruptedException, java.util.concurrent.TimeoutException, java.util.concurrent.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 interfacejava.util.concurrent.Future<T>
- Parameters:
duration
- amount of time to waitunits
- unit of time to wait- Returns:
- the operation results of this OperationFuture
- Throws:
java.lang.InterruptedException
java.util.concurrent.TimeoutException
java.util.concurrent.ExecutionException
-
getKey
public java.lang.String 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
public java.lang.Long 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:
java.lang.UnsupportedOperationException
- If this is for an ASCII protocol configured client.
-
getStatus
public OperationStatus 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
public void set(T o, OperationStatus s)
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
public void setOperation(Operation to)
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 interfacejava.util.concurrent.Future<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.- Specified by:
isDone
in interfacejava.util.concurrent.Future<T>
- Returns:
- true if the Operation is done
-
addListener
public OperationFuture<T> addListener(OperationCompletionListener listener)
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
public OperationFuture<T> removeListener(OperationCompletionListener listener)
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.
-
-