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

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

Introduction

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

Prototype

V getNow();

Source Link

Document

Return the result without blocking.

Usage

From source file:org.redisson.MapWriterPromise.java

License:Apache License

public MapWriterPromise(RFuture<R> f, final CommandAsyncExecutor commandExecutor, final MapWriterTask<R> task) {
    f.addListener(new FutureListener<R>() {
        @Override//www .  j  a  v  a 2  s.  c om
        public void operationComplete(final Future<R> future) throws Exception {
            if (!future.isSuccess()) {
                tryFailure(future.cause());
                return;
            }

            if (task.condition(future)) {
                commandExecutor.getConnectionManager().getExecutor().execute(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            task.execute();
                        } catch (Exception e) {
                            tryFailure(e);
                            return;
                        }
                        trySuccess(future.getNow());
                    }
                });
            } else {
                trySuccess(future.getNow());
            }
        }
    });
}

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  va 2  s  .  co  m
        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);//from   w  w  w .  j  ava2 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 w ww  .  j a  va2s .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;/*from w  w w . ja  v a 2s . com*/
    }

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

From source file:org.redisson.QueueTransferTask.java

License:Apache License

private void addListener(RFuture<Long> startTimeFuture) {
    startTimeFuture.addListener(new FutureListener<Long>() {
        @Override//from   ww  w .ja  v a 2 s  .  c o 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 {//from   w w w. ja va  2 s  . c om
        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());/*from w w w  .  j  a  v a  2s.c  o 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.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 ww  w  .  j  a v a  2s .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;
}

From source file:org.redisson.RedissonBlockingFairQueue.java

License:Apache License

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

    RFuture<Long> tryAcquireFuture = tryAcquireAsync();
    tryAcquireFuture.addListener(new FutureListener<Long>() {
        @Override//  ww w.j  a  va 2 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.pollAsync();
                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 {
                promise.trySuccess(null);
            }
        }
    });

    return promise;
}