Example usage for io.netty.util.concurrent Promise setSuccess

List of usage examples for io.netty.util.concurrent Promise setSuccess

Introduction

In this page you can find the example usage for io.netty.util.concurrent Promise setSuccess.

Prototype

Promise<V> setSuccess(V result);

Source Link

Document

Marks this future as a success and notifies all listeners.

Usage

From source file:org.redisson.RedissonList.java

License:Apache License

@Override
public Future<Boolean> addAllAsync(final Collection<? extends V> c) {
    if (c.isEmpty()) {
        return connectionManager.getGroup().next().newSucceededFuture(false);
    }/*from w  w w. j  a  v  a  2 s. c o  m*/
    return connectionManager.writeAsync(getName(), new AsyncOperation<Object, Boolean>() {
        @Override
        public void execute(final Promise<Boolean> promise, RedisAsyncConnection<Object, Object> async) {
            async.rpush((Object) getName(), c.toArray())
                    .addListener(new OperationListener<Object, Boolean, Object>(promise, async, this) {
                        @Override
                        public void onOperationComplete(Future<Object> future) throws Exception {
                            promise.setSuccess(true);
                        }
                    });
        }
    });
}

From source file:org.redisson.RedissonMap.java

License:Apache License

@Override
public V putIfAbsent(final K key, final V value) {
    //        while (true) {
    //            Boolean res = connection.hsetnx(getName(), key, value);
    //            if (!res) {
    //                V result = (V) connection.hget(getName(), key);
    //                if (result != null) {
    //                    return result;
    //                }
    //            } else {
    //                return null;
    //            }
    //        }//from w  w w  . j  a  va2 s  .  c  o m

    return connectionManager.write(getName(), new AsyncOperation<V, V>() {
        @Override
        public void execute(final Promise<V> promise, final RedisAsyncConnection<Object, V> async) {
            final AsyncOperation<V, V> timeoutCallback = this;
            async.hsetnx(getName(), key, value)
                    .addListener(new OperationListener<V, V, Boolean>(promise, async, timeoutCallback) {
                        @Override
                        public void onOperationComplete(Future<Boolean> future) throws Exception {
                            if (future.get()) {
                                promise.setSuccess(null);
                                return;
                            }

                            async.hget(getName(), key).addListener(
                                    new OperationListener<V, V, V>(promise, async, timeoutCallback) {
                                        @Override
                                        public void onOperationComplete(Future<V> future) throws Exception {
                                            V prev = future.get();
                                            if (prev != null) {
                                                promise.setSuccess(prev);
                                            } else {
                                                timeoutCallback.execute(promise, async);
                                            }
                                        }
                                    });
                        }
                    });
        }
    });
}

From source file:org.redisson.RedissonMap.java

License:Apache License

private void putAsync(final K key, final V value, final Promise<V> promise,
        final RedisAsyncConnection<Object, V> async, final AsyncOperation<V, V> timeoutCallback) {
    async.watch(getName()).addListener(new OperationListener<V, V, String>(promise, async, timeoutCallback) {
        @Override/*from  w w w  .j  a  va2  s. c  o  m*/
        public void onOperationComplete(Future<String> future) throws Exception {

            async.hget(getName(), key)
                    .addListener(new OperationListener<V, V, V>(promise, async, timeoutCallback) {
                        @Override
                        public void onOperationComplete(Future<V> future) throws Exception {

                            final V prev = future.get();
                            async.multi().addListener(
                                    new OperationListener<V, V, String>(promise, async, timeoutCallback) {
                                        @Override
                                        public void onOperationComplete(Future<String> future)
                                                throws Exception {

                                            async.hset(getName(), key, value)
                                                    .addListener(new OperationListener<V, V, Boolean>(promise,
                                                            async, timeoutCallback) {
                                                        @Override
                                                        public void onOperationComplete(Future<Boolean> future)
                                                                throws Exception {

                                                            async.exec().addListener(
                                                                    new OperationListener<V, V, List<Object>>(
                                                                            promise, async, timeoutCallback) {
                                                                        @Override
                                                                        public void onOperationComplete(
                                                                                Future<List<Object>> future)
                                                                                throws Exception {

                                                                            if (future.get().size() == 1) {
                                                                                promise.setSuccess(prev);
                                                                            } else {
                                                                                timeoutCallback.execute(promise,
                                                                                        async);
                                                                            }
                                                                        }
                                                                    });
                                                        }
                                                    });
                                        }
                                    });
                        }
                    });
        }

    });
}

From source file:org.redisson.RedissonMap.java

License:Apache License

private void removeAsync(final K key, final Promise<V> promise, final RedisAsyncConnection<Object, V> async,
        final AsyncOperation<V, V> timeoutCallback) {
    async.watch(getName()).addListener(new OperationListener<V, V, String>(promise, async, timeoutCallback) {
        @Override//from www  .  ja va  2  s  .c  o  m
        public void onOperationComplete(Future<String> future) throws Exception {

            async.hget(getName(), key)
                    .addListener(new OperationListener<V, V, V>(promise, async, timeoutCallback) {
                        @Override
                        public void onOperationComplete(Future<V> future) throws Exception {
                            final V prev = future.get();

                            async.multi().addListener(
                                    new OperationListener<V, V, String>(promise, async, timeoutCallback) {
                                        @Override
                                        public void onOperationComplete(Future<String> future)
                                                throws Exception {

                                            async.hdel(getName(), key)
                                                    .addListener(new OperationListener<V, V, Long>(promise,
                                                            async, timeoutCallback) {
                                                        @Override
                                                        public void onOperationComplete(Future<Long> future)
                                                                throws Exception {

                                                            async.exec().addListener(
                                                                    new OperationListener<V, V, List<Object>>(
                                                                            promise, async, timeoutCallback) {
                                                                        @Override
                                                                        public void onOperationComplete(
                                                                                Future<List<Object>> future)
                                                                                throws Exception {

                                                                            if (future.get().size() == 1) {
                                                                                promise.setSuccess(prev);
                                                                            } else {
                                                                                timeoutCallback.execute(promise,
                                                                                        async);
                                                                            }
                                                                        }
                                                                    });

                                                        }
                                                    });
                                        }
                                    });
                        }
                    });
        }
    });
}

From source file:org.redisson.RedissonObject.java

License:Apache License

public Future<Boolean> renameAsync(final String newName) {
    return connectionManager.writeAsync(getName(), new AsyncOperation<Object, Boolean>() {
        @Override//  w  w  w.j ava  2  s.  c om
        public void execute(final Promise<Boolean> promise, RedisAsyncConnection<Object, Object> async) {
            async.rename(getName(), newName)
                    .addListener(new OperationListener<Object, Boolean, String>(promise, async, this) {
                        @Override
                        public void onOperationComplete(Future<String> future) throws Exception {
                            promise.setSuccess("OK".equals(future.get()));
                        }
                    });
        }
    });
}

From source file:org.redisson.RedissonObject.java

License:Apache License

public Future<Boolean> deleteAsync() {
    return connectionManager.writeAsync(getName(), new AsyncOperation<Object, Boolean>() {
        @Override//from  w  ww.  j  a v a  2s.com
        public void execute(final Promise<Boolean> promise, RedisAsyncConnection<Object, Object> async) {
            async.del(getName())
                    .addListener(new OperationListener<Object, Boolean, Number>(promise, async, this) {
                        @Override
                        public void onOperationComplete(Future<Number> future) throws Exception {
                            promise.setSuccess(future.get().byteValue() == 1);
                        }
                    });
        }
    });
}

From source file:org.redisson.RedissonSet.java

License:Apache License

@Override
public Future<Boolean> addAsync(final V e) {
    return connectionManager.writeAsync(getName(), new AsyncOperation<V, Boolean>() {
        @Override//  w ww. j a v  a 2s .  co m
        public void execute(final Promise<Boolean> promise, RedisAsyncConnection<Object, V> async) {
            async.sadd(getName(), e).addListener(new OperationListener<V, Boolean, Long>(promise, async, this) {
                @Override
                public void onOperationComplete(Future<Long> future) throws Exception {
                    promise.setSuccess(future.get() > 0);
                }
            });
        }
    });
}

From source file:org.redisson.RedissonSet.java

License:Apache License

@Override
public Future<Boolean> removeAsync(final V e) {
    return connectionManager.writeAsync(getName(), new AsyncOperation<V, Boolean>() {
        @Override/*  w  w  w.  java 2 s.  co m*/
        public void execute(final Promise<Boolean> promise, RedisAsyncConnection<Object, V> async) {
            async.srem(getName(), e).addListener(new OperationListener<V, Boolean, Long>(promise, async, this) {
                @Override
                public void onOperationComplete(Future<Long> future) throws Exception {
                    promise.setSuccess(future.get() > 0);
                }
            });
        }
    });
}

From source file:org.redisson.RedissonSortedSet.java

License:Apache License

public Future<Boolean> addAsync(final V value) {
    EventLoop loop = connectionManager.getGroup().next();
    final Promise<Boolean> promise = loop.newPromise();

    loop.execute(new Runnable() {
        @Override/* w w  w .j a v a 2 s.c  om*/
        public void run() {
            try {
                boolean result = add(value);
                promise.setSuccess(result);
            } catch (Exception e) {
                promise.setFailure(e);
            }
        }
    });

    return promise;
}

From source file:org.redisson.RedissonSortedSet.java

License:Apache License

@Override
public Future<Boolean> removeAsync(final V value) {
    EventLoop loop = connectionManager.getGroup().next();
    final Promise<Boolean> promise = loop.newPromise();

    loop.execute(new Runnable() {
        @Override/*from w w w.j a  v a  2 s  .c  o m*/
        public void run() {
            try {
                boolean result = remove(value);
                promise.setSuccess(result);
            } catch (Exception e) {
                promise.setFailure(e);
            }
        }
    });

    return promise;
}