In addition, the librares also support a range of asynchronous methods that can be used to store, update and retrieve values without having to explicitly wait for a response.
The asynchronous methods use a Future object or its appropriate implementation which is returned by the initial method call for the operation. The communication with the Couchbase server will be handled by the client libraries in the background so that the main program loop can continue. You can recover the status of the operation by using a method to check the status on the returned Future object. For example, rather than synchronously getting a key, an asynchronous call might look like this:
GetFuture getOp = client.asyncGet("someKey");A list of the asynchronous methods are shown in Table 4.3, “Java Client Library Asynchronous Method Summary”.
Table 4.3. Java Client Library Asynchronous Method Summary
| Method | Title |
|---|---|
client.add(key, expiry, value) | Add a value with the specified key that does not already exist |
client.add(key, expiry, value, persistto) | Add a value using the specified key and observe it being persisted on master and more node(s). |
client.add(key, expiry, value, persistto, replicateto) | Add a value using the specified key and observe it being persisted on master and more node(s) and being replicated to one or more node(s). |
client.add(key, expiry, value, replicateto) | Add a value using the specified key and observe it being replicated to one or more node(s). |
client.add(key, expiry, value, transcoder) | Add a value that does not already exist using custom transcoder |
client.asyncCAS(key, casunique, value) | Asynchronously compare and set a value |
client.asyncCAS(key, casunique, expiry, value, transcoder) | Asynchronously compare and set a value with custom transcoder and expiry |
client.asyncCAS(key, casunique, value, transcoder) | Asynchronously compare and set a value with custom transcoder |
client.asyncDecr(key, offset) | Asynchronously decrement the value of an existing key |
client.asyncGetAndTouch(key, expiry) | Asynchronously get a value and update the expiration time for a given key |
client.asyncGetAndTouch(key, expiry, transcoder) | Asynchronously get a value and update the expiration time for a given key using a custom transcoder |
client.asyncGet(key) | Asynchronously get a single key |
client.asyncGetBulk(keycollection) | Asynchronously get multiple keys |
client.asyncGetBulk(keyn) | Asynchronously get multiple keys |
client.asyncGetBulk(transcoder, keyn) | Asynchronously get multiple keys using a custom transcoder |
client.asyncGetBulk(keycollection, transcoder) | Asynchronously get multiple keys using a custom transcoder |
client.asyncGet(key, transcoder) | Asynchronously get a single key using a custom transcoder |
client.asyncGetLock(key [, getl-expiry ]) | Asynchronously get a lock. |
client.asyncGetLock(key [, getl-expiry ], transcoder) | Asynchronously get a lock with transcoder. |
client.asyncGets(key) | Asynchronously get single key value with CAS value |
client.asyncGets(key, transcoder) | Asynchronously get single key value with CAS value using custom transcoder |
client.asyncIncr(key, offset) | Asynchronously increment the value of an existing key |
client.delete(key) | Delete the specified key |
client.getAndLock(key [, getl-expiry ]) | Get and lock Asynchronously |
client.prepend(casunique, key, value) | Prepend a value to an existing key using the default transcoder |
client.prepend(casunique, key, value, transcoder) | Prepend a value to an existing key using a custom transcoder |
client.replace(key, expiry, value) | Update an existing key with a new value |
client.replace(key, expiry, value, persistto) | Replace a value using the specified key and observe it being persisted on master and more node(s). |
client.replace(key, expiry, value, persistto, replicateto) | Replace a value using the specified key and observe it being persisted on master and more node(s) and being replicated to one or more node(s). |
client.replace(key, expiry, value, replicateto) | Replace a value using the specified key and observe it being replicated to one or more node(s). |
client.replace(key, expiry, value, transcoder) | Update an existing key with a new value using a custom transcoder |
client.set(key, expiry, value) | Store a value using the specified key |
client.set(key, expiry, value, persistto) | Store a value using the specified key and observe it being persisted on master and more node(s). |
client.set(key, expiry, value, persistto, replicateto) | Store a value using the specified key and observe it being persisted on master and more node(s) and being replicated to one or more node(s). |
client.set(key, expiry, value, replicateto) | Store a value using the specified key and observe it being replicated to one or more node(s). |
client.set(key, expiry, value, transcoder) | Store a value using the specified key and a custom transcoder. |
client.touch(key, expiry) | Update the expiry time of an item |
This will populate the Future object GetFuture
with the response from the server. The Future object class is
defined
here.
The primary methods are:
cancel()
Attempts to Cancel the operation if the operation has not already been completed.
get()
Waits for the operation to complete. Gets the object returned by the operation as if the method was synchronous rather than asynchronous.
get(timeout, TimeUnit)
Gets the object waiting for a maximum time specified by
timeout and the corresponding
TimeUnit.
isDone()
The operation has been completed successfully.
For example, you can use the timeout method to obtain the value or cancel the operation:
GetFuture getOp = client.asyncGet("someKey"); Object myObj; try { myObj = getOp.get(5, TimeUnit.SECONDS); } catch(TimeoutException e) { getOp.cancel(false); }
Alternatively, you can do a blocking wait for the response by
using the get() method:
Object myObj; myObj = getOp.get();