Search in sources :

Example 1 with WriteListener

use of com.aerospike.client.listener.WriteListener in project aerospike-client-java by aerospike.

the class TestAsyncQuery method asyncQuery.

@Test
public void asyncQuery() {
    final AtomicInteger count = new AtomicInteger();
    for (int i = 1; i <= size; i++) {
        final Key key = new Key(args.namespace, args.set, keyPrefix + i);
        Bin bin = new Bin(binName, i);
        client.put(eventLoop, new WriteListener() {

            public void onSuccess(final Key key) {
                if (count.incrementAndGet() == size) {
                    runQuery();
                }
            }

            public void onFailure(AerospikeException e) {
                setError(e);
                notifyComplete();
            }
        }, null, key, bin);
    }
    waitTillComplete();
}
Also used : AerospikeException(com.aerospike.client.AerospikeException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Bin(com.aerospike.client.Bin) WriteListener(com.aerospike.client.listener.WriteListener) Key(com.aerospike.client.Key) Test(org.junit.Test)

Example 2 with WriteListener

use of com.aerospike.client.listener.WriteListener in project aerospike-client-java by aerospike.

the class AsyncPutGet method runPutGetInline.

// Inline asynchronous put/get calls.
private void runPutGetInline(final AerospikeClient client, final EventLoop eventLoop, final Key key, final Bin bin) {
    console.info("Put: namespace=%s set=%s key=%s value=%s", key.namespace, key.setName, key.userKey, bin.value);
    client.put(eventLoop, new WriteListener() {

        public void onSuccess(final Key key) {
            try {
                // Write succeeded.  Now call read.
                console.info("Get: namespace=%s set=%s key=%s", key.namespace, key.setName, key.userKey);
                client.get(eventLoop, new RecordListener() {

                    public void onSuccess(final Key key, final Record record) {
                        validateBin(key, bin, record);
                        notifyComplete();
                    }

                    public void onFailure(AerospikeException e) {
                        console.error("Failed to get: namespace=%s set=%s key=%s exception=%s", key.namespace, key.setName, key.userKey, e.getMessage());
                        notifyComplete();
                    }
                }, policy, key);
            } catch (Exception e) {
                console.error("Failed to get: namespace=%s set=%s key=%s exception=%s", key.namespace, key.setName, key.userKey, e.getMessage());
            }
        }

        public void onFailure(AerospikeException e) {
            console.error("Failed to put: namespace=%s set=%s key=%s exception=%s", key.namespace, key.setName, key.userKey, e.getMessage());
            notifyComplete();
        }
    }, writePolicy, key, bin);
}
Also used : AerospikeException(com.aerospike.client.AerospikeException) RecordListener(com.aerospike.client.listener.RecordListener) Record(com.aerospike.client.Record) WriteListener(com.aerospike.client.listener.WriteListener) Key(com.aerospike.client.Key) IOException(java.io.IOException) ConnectException(java.net.ConnectException) AerospikeException(com.aerospike.client.AerospikeException)

Example 3 with WriteListener

use of com.aerospike.client.listener.WriteListener in project aerospike-client-java by aerospike.

the class AsyncQuery method runQueryExample.

private void runQueryExample(final AerospikeClient client, final EventLoop eventLoop, final String keyPrefix, final String binName, final int size) {
    console.info("Write " + size + " records.");
    final AtomicInteger count = new AtomicInteger();
    for (int i = 1; i <= size; i++) {
        final Key key = new Key(params.namespace, params.set, keyPrefix + i);
        Bin bin = new Bin(binName, i);
        client.put(eventLoop, new WriteListener() {

            public void onSuccess(final Key key) {
                if (count.incrementAndGet() == size) {
                    runQuery(client, eventLoop, binName);
                }
            }

            public void onFailure(AerospikeException e) {
                console.error("Failed to put: namespace=%s set=%s key=%s exception=%s", key.namespace, key.setName, key.userKey, e.getMessage());
                notifyComplete();
            }
        }, writePolicy, key, bin);
    }
}
Also used : AerospikeException(com.aerospike.client.AerospikeException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Bin(com.aerospike.client.Bin) WriteListener(com.aerospike.client.listener.WriteListener) Key(com.aerospike.client.Key)

Example 4 with WriteListener

use of com.aerospike.client.listener.WriteListener in project aerospike-client-java by aerospike.

the class TestAsyncPutGet method asyncPutGetInline.

@Test
public void asyncPutGetInline() {
    final Key key = new Key(args.namespace, args.set, "putgetkey1");
    final Bin bin = new Bin(binName, "value");
    client.put(eventLoop, new WriteListener() {

        public void onSuccess(final Key key) {
            try {
                // Write succeeded.  Now call read.
                client.get(eventLoop, new RecordListener() {

                    public void onSuccess(final Key key, final Record record) {
                        assertBinEqual(key, record, bin);
                        notifyComplete();
                    }

                    public void onFailure(AerospikeException e) {
                        setError(e);
                        notifyComplete();
                    }
                }, null, key);
            } catch (Exception e) {
                setError(e);
                notifyComplete();
            }
        }

        public void onFailure(AerospikeException e) {
            setError(e);
            notifyComplete();
        }
    }, null, key, bin);
    waitTillComplete();
}
Also used : AerospikeException(com.aerospike.client.AerospikeException) Bin(com.aerospike.client.Bin) RecordListener(com.aerospike.client.listener.RecordListener) Record(com.aerospike.client.Record) WriteListener(com.aerospike.client.listener.WriteListener) Key(com.aerospike.client.Key) IOException(java.io.IOException) ConnectException(java.net.ConnectException) AerospikeException(com.aerospike.client.AerospikeException) Test(org.junit.Test)

Aggregations

AerospikeException (com.aerospike.client.AerospikeException)4 Key (com.aerospike.client.Key)4 WriteListener (com.aerospike.client.listener.WriteListener)4 Bin (com.aerospike.client.Bin)3 Record (com.aerospike.client.Record)2 RecordListener (com.aerospike.client.listener.RecordListener)2 IOException (java.io.IOException)2 ConnectException (java.net.ConnectException)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 Test (org.junit.Test)2