Example usage for io.netty.util.concurrent Future isSuccess

List of usage examples for io.netty.util.concurrent Future isSuccess

Introduction

In this page you can find the example usage for io.netty.util.concurrent Future isSuccess.

Prototype

boolean isSuccess();

Source Link

Document

Returns true if and only if the I/O operation was completed successfully.

Usage

From source file:org.redisson.misc.ConnectionPool.java

License:Apache License

private void initConnections(final ClientConnectionsEntry entry, final Promise<Void> initPromise,
        boolean checkFreezed) {
    int minimumIdleSize = getMinimumIdleSize(entry);

    if (minimumIdleSize == 0 || (checkFreezed && entry.isFreezed())) {
        initPromise.setSuccess(null);//w ww.  j  a v a2  s  .c  om
        return;
    }

    final AtomicInteger initializedConnections = new AtomicInteger(minimumIdleSize);
    for (int i = 0; i < minimumIdleSize; i++) {
        if ((checkFreezed && entry.isFreezed()) || !tryAcquireConnection(entry)) {
            Throwable cause = new RedisConnectionException(
                    "Can't init enough connections amount! " + initializedConnections.get() + " from "
                            + minimumIdleSize + " was initialized. Server: " + entry.getClient().getAddr());
            initPromise.tryFailure(cause);
            return;
        }

        Future<T> promise = connectTo(entry);
        promise.addListener(new FutureListener<T>() {
            @Override
            public void operationComplete(Future<T> future) throws Exception {
                if (future.isSuccess()) {
                    T conn = future.getNow();
                    releaseConnection(entry, conn);
                }
                releaseConnection(entry);
                if (!future.isSuccess()) {
                    Throwable cause = new RedisConnectionException(
                            "Can't init enough connections amount! from " + entry.getClient().getAddr());
                    initPromise.tryFailure(cause);
                    return;
                }

                if (initializedConnections.decrementAndGet() == 0) {
                    initPromise.setSuccess(null);
                }
            }
        });
    }
}

From source file:org.redisson.misc.ConnectionPool.java

License:Apache License

private Future<T> connectTo(final ClientConnectionsEntry entry) {
    T conn = poll(entry);/*ww  w.ja va  2 s.  co m*/
    if (conn != null) {
        if (!conn.isActive()) {
            return promiseFailure(entry, conn);
        }

        return promiseSuccessful(entry, conn);
    }

    final Promise<T> promise = connectionManager.newPromise();
    Future<T> connFuture = connect(entry);
    connFuture.addListener(new FutureListener<T>() {
        @Override
        public void operationComplete(Future<T> future) throws Exception {
            if (!future.isSuccess()) {
                releaseConnection(entry);

                promiseFailure(entry, promise, future.cause());
                return;
            }

            T conn = future.getNow();
            if (!conn.isActive()) {
                promiseFailure(entry, promise, conn);
                return;
            }

            promiseSuccessful(entry, promise, conn);
        }
    });
    return promise;
}

From source file:org.redisson.misc.ConnectionPool.java

License:Apache License

private void scheduleCheck(final ClientConnectionsEntry entry) {

    connectionManager.getConnectionEventsHub().fireDisconnect(entry.getClient().getAddr());

    connectionManager.newTimeout(new TimerTask() {
        @Override/*from www.j a  v  a 2  s.  c o m*/
        public void run(Timeout timeout) throws Exception {
            if (entry.getFreezeReason() != FreezeReason.RECONNECT || !entry.isFreezed()) {
                return;
            }

            Future<RedisConnection> connectionFuture = entry.getClient().connectAsync();
            connectionFuture.addListener(new FutureListener<RedisConnection>() {
                @Override
                public void operationComplete(Future<RedisConnection> future) throws Exception {
                    if (entry.getFreezeReason() != FreezeReason.RECONNECT || !entry.isFreezed()) {
                        return;
                    }

                    if (!future.isSuccess()) {
                        scheduleCheck(entry);
                        return;
                    }
                    final RedisConnection c = future.getNow();
                    if (!c.isActive()) {
                        c.closeAsync();
                        scheduleCheck(entry);
                        return;
                    }

                    Future<String> f = c.asyncWithTimeout(null, RedisCommands.PING);
                    f.addListener(new FutureListener<String>() {
                        @Override
                        public void operationComplete(Future<String> future) throws Exception {
                            try {
                                if (entry.getFreezeReason() != FreezeReason.RECONNECT || !entry.isFreezed()) {
                                    return;
                                }

                                if (future.isSuccess() && "PONG".equals(future.getNow())) {
                                    entry.resetFailedAttempts();
                                    Promise<Void> promise = connectionManager.newPromise();
                                    promise.addListener(new FutureListener<Void>() {
                                        @Override
                                        public void operationComplete(Future<Void> future) throws Exception {
                                            if (entry.getNodeType() == NodeType.SLAVE) {
                                                masterSlaveEntry.slaveUp(
                                                        entry.getClient().getAddr().getHostName(),
                                                        entry.getClient().getAddr().getPort(),
                                                        FreezeReason.RECONNECT);
                                            } else {
                                                synchronized (entry) {
                                                    if (entry.getFreezeReason() == FreezeReason.RECONNECT) {
                                                        entry.setFreezed(false);
                                                        entry.setFreezeReason(null);
                                                    }
                                                }
                                            }
                                        }
                                    });
                                    initConnections(entry, promise, false);
                                } else {
                                    scheduleCheck(entry);
                                }
                            } finally {
                                c.closeAsync();
                            }
                        }
                    });
                }
            });
        }
    }, config.getReconnectionTimeout(), TimeUnit.MILLISECONDS);
}

From source file:org.redisson.misc.TransferListener.java

License:Apache License

@Override
public void operationComplete(Future<T> future) throws Exception {
    if (!future.isSuccess()) {
        promise.tryFailure(future.cause());
        return;//w w w. ja  v  a2  s .  c om
    }

    promise.trySuccess(future.getNow());
}

From source file:org.redisson.OperationListener.java

License:Apache License

protected boolean isBreak(RedisAsyncConnection<Object, V> async, Promise<V> promise, Future<F> future) {
    if (!future.isSuccess()) {
        promise.setFailure(future.cause());
        return true;
    }/*from  w  w  w  . j  a v  a  2 s  .c o m*/
    if (promise.isCancelled()) {
        if (async.isMultiMode()) {
            async.discard();
        }
        return true;
    }

    return false;
}

From source file:org.redisson.QueueTransferTask.java

License:Apache License

private void addListener(RFuture<Long> startTimeFuture) {
    startTimeFuture.addListener(new FutureListener<Long>() {
        @Override/*w  w  w  . ja  v  a 2 s  .  co m*/
        public void operationComplete(io.netty.util.concurrent.Future<Long> future) throws Exception {
            if (!future.isSuccess()) {
                if (future.cause() instanceof RedissonShutdownException) {
                    return;
                }
                log.error(future.cause().getMessage(), future.cause());
                scheduleTask(System.currentTimeMillis() + 5 * 1000L);
                return;
            }

            if (future.getNow() != null) {
                scheduleTask(future.getNow());
            }
        }
    });
}

From source file:org.redisson.reactive.NettyFuturePublisher.java

License:Apache License

@Override
public void subscribe(final Subscriber<? super T> subscriber) {
    try {//  w  w w . j a v a  2 s  .c  o  m
        subscriber.onSubscribe(new ReactiveSubscription<T>(this, subscriber) {

            @Override
            public void request(long elements) {
                Action.checkRequest(elements);
                if (isComplete())
                    return;

                that.addListener(new FutureListener<T>() {
                    @Override
                    public void operationComplete(Future<T> future) throws Exception {
                        if (!future.isSuccess()) {
                            subscriber.onError(future.cause());
                            return;
                        }

                        if (future.getNow() != null) {
                            subscriber.onNext(future.getNow());
                        }
                        onComplete();
                    }
                });
            }
        });
    } catch (Throwable throwable) {
        Exceptions.throwIfFatal(throwable);
        subscriber.onError(throwable);
    }
}

From source file:org.redisson.RedisNodes.java

License:Apache License

@Override
public boolean pingAll() {
    List<RedisClientEntry> clients = new ArrayList<RedisClientEntry>(connectionManager.getClients());
    final Map<RedisConnection, RFuture<String>> result = new ConcurrentHashMap<RedisConnection, RFuture<String>>(
            clients.size());/*  w  w w  .ja v a  2 s .co  m*/
    final CountDownLatch latch = new CountDownLatch(clients.size());
    for (RedisClientEntry entry : clients) {
        RFuture<RedisConnection> f = entry.getClient().connectAsync();
        f.addListener(new FutureListener<RedisConnection>() {
            @Override
            public void operationComplete(Future<RedisConnection> future) throws Exception {
                if (future.isSuccess()) {
                    final RedisConnection c = future.getNow();
                    RPromise<RedisConnection> connectionFuture = connectionManager.newPromise();
                    connectionManager.getConnectListener().onConnect(connectionFuture, c, null,
                            connectionManager.getConfig());
                    connectionFuture.addListener(new FutureListener<RedisConnection>() {
                        @Override
                        public void operationComplete(Future<RedisConnection> future) throws Exception {
                            RFuture<String> r = c.async(connectionManager.getConfig().getPingTimeout(),
                                    RedisCommands.PING);
                            result.put(c, r);
                            latch.countDown();
                        }
                    });
                } else {
                    latch.countDown();
                }
            }
        });
    }

    long time = System.currentTimeMillis();
    try {
        latch.await();
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
    }

    if (System.currentTimeMillis() - time >= connectionManager.getConfig().getConnectTimeout()) {
        for (Entry<RedisConnection, RFuture<String>> entry : result.entrySet()) {
            entry.getKey().closeAsync();
        }
        return false;
    }

    time = System.currentTimeMillis();
    boolean res = true;
    for (Entry<RedisConnection, RFuture<String>> entry : result.entrySet()) {
        RFuture<String> f = entry.getValue();
        f.awaitUninterruptibly();
        if (!"PONG".equals(f.getNow())) {
            res = false;
        }
        entry.getKey().closeAsync();
    }

    // true and no futures missed during client connection
    return res && result.size() == clients.size();
}

From source file:org.redisson.RedissonBinaryStream.java

License:Apache License

private void write(final byte[] value, final RPromise<Void> result, final int chunkSize, final int i) {
    final int len = Math.min(value.length - i * chunkSize, chunkSize);
    byte[] bytes = Arrays.copyOfRange(value, i * chunkSize, i * chunkSize + len);
    writeAsync(bytes).addListener(new FutureListener<Void>() {
        @Override//from  w  w w  . j ava  2  s .  c o  m
        public void operationComplete(Future<Void> future) throws Exception {
            if (!future.isSuccess()) {
                result.tryFailure(future.cause());
                return;
            }

            int j = i + 1;
            if (j * chunkSize > value.length) {
                result.trySuccess(null);
            } else {
                write(value, result, chunkSize, j);
            }
        }
    });
}

From source file:org.redisson.RedissonBlockingFairQueue.java

License:Apache License

@Override
public RFuture<V> takeAsync() {
    final RPromise<V> promise = newPromise();

    RFuture<Long> tryAcquireFuture = tryAcquireAsync();
    tryAcquireFuture.addListener(new FutureListener<Long>() {
        @Override//from   w w w  .  j ava2 s.  c  o  m
        public void operationComplete(Future<Long> future) throws Exception {
            if (!future.isSuccess()) {
                promise.tryFailure(future.cause());
                return;
            }

            final Long currentTimeout = future.getNow();
            if (currentTimeout == null) {
                final RFuture<V> pollFuture = RedissonBlockingFairQueue.super.takeAsync();
                pollFuture.addListener(new FutureListener<V>() {
                    @Override
                    public void operationComplete(Future<V> future) throws Exception {
                        if (!future.isSuccess()) {
                            promise.tryFailure(future.cause());
                            return;
                        }

                        promise.trySuccess(future.getNow());
                    }
                });
            } else {
                final RFuture<RedissonLockEntry> subscribeFuture = subscribe();
                final AtomicReference<Timeout> futureRef = new AtomicReference<Timeout>();
                subscribeFuture.addListener(new FutureListener<RedissonLockEntry>() {
                    @Override
                    public void operationComplete(Future<RedissonLockEntry> future) throws Exception {
                        if (!future.isSuccess()) {
                            promise.tryFailure(future.cause());
                            return;
                        }

                        if (futureRef.get() != null) {
                            futureRef.get().cancel();
                        }

                        tryTakeAsync(subscribeFuture, promise);
                    }
                });
            }
        }
    });

    return promise;
}