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:io.codis.nedis.PromiseConverter.java

License:Apache License

public static PromiseConverter<List<SortedSetEntry>> toSortedSetEntryList(EventExecutor executor) {
    return new PromiseConverter<List<SortedSetEntry>>(executor) {

        @Override/*from ww  w  . j av a2  s . c  o m*/
        public FutureListener<Object> newListener(final Promise<List<SortedSetEntry>> promise) {
            return new FutureListener<Object>() {

                @Override
                public void operationComplete(Future<Object> future) throws Exception {
                    if (future.isSuccess()) {
                        Object resp = future.getNow();
                        if (resp instanceof RedisResponseException) {
                            promise.tryFailure((RedisResponseException) resp);
                        } else {
                            @SuppressWarnings("unchecked")
                            List<byte[]> rawValueList = (List<byte[]>) resp;
                            List<SortedSetEntry> values = new ArrayList<>(rawValueList.size() / 2);
                            for (Iterator<byte[]> iter = rawValueList.iterator(); iter.hasNext();) {
                                values.add(new SortedSetEntry(iter.next(), bytesToDouble(iter.next())));
                            }
                            promise.trySuccess(values);
                        }
                    } else {
                        promise.tryFailure(future.cause());
                    }
                }
            };
        }
    };
}

From source file:io.codis.nedis.PromiseConverter.java

License:Apache License

public static PromiseConverter<List<Boolean>> toBooleanList(EventExecutor executor) {
    return new PromiseConverter<List<Boolean>>(executor) {

        @Override//from ww  w.  ja  va  2  s  .  c o m
        public FutureListener<Object> newListener(final Promise<List<Boolean>> promise) {
            return new FutureListener<Object>() {

                @Override
                public void operationComplete(Future<Object> future) throws Exception {
                    if (future.isSuccess()) {
                        Object resp = future.getNow();
                        if (resp instanceof RedisResponseException) {
                            promise.tryFailure((RedisResponseException) resp);
                        } else if (resp == RedisResponseDecoder.NULL_REPLY) {
                            promise.trySuccess(null);
                        } else {
                            @SuppressWarnings("unchecked")
                            List<Long> rawValueList = (List<Long>) resp;
                            List<Boolean> values = new ArrayList<>(rawValueList.size());
                            for (long l : rawValueList) {
                                values.add(l != 0L);
                            }
                            promise.trySuccess(values);
                        }
                    } else {
                        promise.tryFailure(future.cause());
                    }
                }
            };
        }
    };
}

From source file:io.codis.nedis.PromiseConverter.java

License:Apache License

public static PromiseConverter<List<Object>> toObjectList(EventExecutor executor) {
    return new PromiseConverter<List<Object>>(executor) {

        @Override// www  .j a  v a 2  s. c  o  m
        public FutureListener<Object> newListener(final Promise<List<Object>> promise) {
            return new FutureListener<Object>() {

                @SuppressWarnings("unchecked")
                @Override
                public void operationComplete(Future<Object> future) throws Exception {
                    if (future.isSuccess()) {
                        Object resp = future.getNow();
                        if (resp instanceof RedisResponseException) {
                            promise.tryFailure((RedisResponseException) resp);
                        } else if (resp == RedisResponseDecoder.NULL_REPLY) {
                            promise.trySuccess(null);
                        } else {
                            promise.trySuccess((List<Object>) resp);
                        }
                    } else {
                        promise.tryFailure(future.cause());
                    }
                }
            };
        }
    };
}

From source file:io.codis.nedis.TestNedis.java

License:Apache License

@Test
public void testBlockingCommands() throws InterruptedException {
    pool = NedisClientPoolBuilder.create().remoteAddress(new InetSocketAddress("127.0.0.1", PORT))
            .timeoutMs(100).exclusive(true).build();
    NedisClient client = NedisUtils.newPooledClient(pool);
    Future<List<byte[]>> brpopFuture = client.brpop(100, toBytes("foo"));
    Thread.sleep(1000);//from   w ww.j av  a  2s.  c  o m
    assertFalse(brpopFuture.isDone());
    client.lpush(toBytes("foo"), toBytes("bar"));
    List<byte[]> brpopResp = brpopFuture.sync().getNow();
    assertEquals(2, brpopResp.size());
    assertEquals("foo", bytesToString(brpopResp.get(0)));
    assertEquals("bar", bytesToString(brpopResp.get(1)));

    Future<List<byte[]>> blpopFuture = client.blpop(100, toBytes("a1"));
    Future<byte[]> brpoplpushFuture = client.brpoplpush(toBytes("a2"), toBytes("a1"), 100);
    Thread.sleep(1000);
    assertFalse(blpopFuture.isDone());
    assertFalse(brpoplpushFuture.isDone());
    client.lpush(toBytes("a2"), toBytes("b"));

    List<byte[]> blpopResp = blpopFuture.sync().getNow();
    assertEquals(2, blpopResp.size());
    assertEquals("a1", bytesToString(blpopResp.get(0)));
    assertEquals("b", bytesToString(blpopResp.get(1)));

    assertTrue(brpoplpushFuture.isDone());
    assertEquals("b", bytesToString(brpoplpushFuture.getNow()));
}

From source file:io.codis.nedis.TestNedis.java

License:Apache License

private void testTxn(NedisClient txnClient, NedisClient chkClient) throws InterruptedException {
    Future<Void> multiFuture = txnClient.multi();
    Future<Boolean> setFuture1 = txnClient.set(toBytes("k1"), toBytes("v1"));
    Future<Boolean> setFuture2 = txnClient.set(toBytes("k2"), toBytes("v2"));
    Thread.sleep(1000);/*w  w w .  j  a v a 2 s . c o m*/
    assertFalse(setFuture1.isDone());
    assertFalse(setFuture2.isDone());
    assertFalse(chkClient.exists(toBytes("k1")).sync().getNow().booleanValue());
    assertFalse(chkClient.exists(toBytes("k2")).sync().getNow().booleanValue());
    List<Object> execResult = txnClient.exec().sync().getNow();
    assertTrue(multiFuture.isDone());
    assertTrue(setFuture1.getNow().booleanValue());
    assertTrue(setFuture2.getNow().booleanValue());
    assertEquals(2, execResult.size());
    assertEquals("OK", execResult.get(0).toString());
    assertEquals("OK", execResult.get(1).toString());

    multiFuture = txnClient.multi();
    setFuture1 = txnClient.set(toBytes("k1"), toBytes("v3"));
    setFuture2 = txnClient.set(toBytes("k2"), toBytes("v4"));
    Thread.sleep(1000);
    assertFalse(setFuture1.isDone());
    assertFalse(setFuture2.isDone());
    assertEquals("v1", bytesToString(chkClient.get(toBytes("k1")).sync().getNow()));
    assertEquals("v2", bytesToString(chkClient.get(toBytes("k2")).sync().getNow()));
    txnClient.discard().sync();
    assertTrue(multiFuture.isDone());
    assertTrue(setFuture1.isDone());
    assertTrue(setFuture2.isDone());
    assertThat(setFuture1.cause(), is(instanceOf(TxnDiscardException.class)));
    assertThat(setFuture2.cause(), is(instanceOf(TxnDiscardException.class)));
    assertEquals("v1", bytesToString(chkClient.get(toBytes("k1")).sync().getNow()));
    assertEquals("v2", bytesToString(chkClient.get(toBytes("k2")).sync().getNow()));

    Future<Void> watchFuture = txnClient.watch(toBytes("k1"));
    multiFuture = txnClient.multi();
    setFuture1 = txnClient.set(toBytes("k1"), toBytes("v3"));
    execResult = txnClient.exec().sync().getNow();
    assertTrue(watchFuture.isDone());
    assertTrue(multiFuture.isDone());
    assertTrue(setFuture1.getNow().booleanValue());
    assertEquals(1, execResult.size());
    assertEquals("OK", execResult.get(0).toString());
    assertEquals("v3", bytesToString(chkClient.get(toBytes("k1")).sync().getNow()));

    txnClient.watch(toBytes("k1")).sync();
    multiFuture = txnClient.multi();
    setFuture1 = txnClient.set(toBytes("k1"), toBytes("v4"));
    assertTrue(chkClient.set(toBytes("k1"), toBytes("v1")).sync().getNow().booleanValue());
    execResult = txnClient.exec().sync().getNow();
    assertTrue(watchFuture.isDone());
    assertTrue(multiFuture.isDone());
    assertTrue(setFuture1.isDone());
    assertThat(setFuture1.cause(), is(instanceOf(TxnAbortException.class)));
    assertNull(execResult);
    assertEquals("v1", bytesToString(chkClient.get(toBytes("k1")).sync().getNow()));
}

From source file:io.gatling.http.client.impl.DefaultHttpClient.java

License:Apache License

private void sendTx(HttpTx tx, EventLoop eventLoop) {

    EventLoopResources resources = eventLoopResources(eventLoop);
    Request request = tx.request;
    HttpListener listener = tx.listener;
    RequestTimeout requestTimeout = tx.requestTimeout;

    // use a fresh channel for WebSocket
    Channel pooledChannel = request.getUri().isWebSocket() ? null : resources.channelPool.poll(tx.key);

    if (pooledChannel != null) {
        sendTxWithChannel(tx, pooledChannel);

    } else {/*from   w ww.j av a 2s. c  o  m*/
        resolveRemoteAddresses(request, eventLoop, listener, requestTimeout)
                .addListener((Future<List<InetSocketAddress>> whenRemoteAddresses) -> {
                    if (requestTimeout.isDone()) {
                        return;
                    }

                    if (whenRemoteAddresses.isSuccess()) {
                        List<InetSocketAddress> addresses = whenRemoteAddresses.getNow();

                        if (request.isHttp2Enabled()) {
                            String domain = tx.request.getUri().getHost();
                            Channel coalescedChannel = resources.channelPool
                                    .pollCoalescedChannel(tx.key.clientId, domain, addresses);
                            if (coalescedChannel != null) {
                                tx.listener.onProtocolAwareness(true);
                                sendTxWithChannel(tx, coalescedChannel);
                            } else {
                                sendTxWithNewChannel(tx, resources, eventLoop, addresses);
                            }
                        } else {
                            sendTxWithNewChannel(tx, resources, eventLoop, addresses);
                        }
                    }
                });
    }
}

From source file:io.gatling.http.client.impl.DefaultHttpClient.java

License:Apache License

private void sendHttp2Txs(List<HttpTx> txs, EventLoop eventLoop) {
    HttpTx tx = txs.get(0);/*from  ww  w . j a v  a2s  .c o  m*/

    EventLoopResources resources = eventLoopResources(eventLoop);
    Request request = tx.request;
    HttpListener listener = tx.listener;
    RequestTimeout requestTimeout = tx.requestTimeout;

    resolveRemoteAddresses(request, eventLoop, listener, requestTimeout)
            .addListener((Future<List<InetSocketAddress>> whenRemoteAddresses) -> {
                if (requestTimeout.isDone()) {
                    return;
                }

                if (whenRemoteAddresses.isSuccess()) {
                    List<InetSocketAddress> addresses = whenRemoteAddresses.getNow();

                    String domain = tx.request.getUri().getHost();
                    Channel coalescedChannel = resources.channelPool.pollCoalescedChannel(tx.key.clientId,
                            domain, addresses);
                    if (coalescedChannel != null) {
                        sendHttp2TxsWithChannel(txs, coalescedChannel);
                    } else {
                        sendHttp2TxsWithNewChannel(txs, resources, eventLoop, addresses);
                    }
                }
            });
}

From source file:io.gatling.http.client.impl.DefaultHttpClient.java

License:Apache License

private Future<List<InetSocketAddress>> resolveRemoteAddresses(Request request, EventLoop eventLoop,
        HttpListener listener, RequestTimeout requestTimeout) {
    if (!request.getUri().isSecured() && request.getProxyServer() instanceof HttpProxyServer) {
        // directly connect to proxy over clear HTTP
        InetSocketAddress remoteAddress = ((HttpProxyServer) request.getProxyServer()).getAddress();
        return ImmediateEventExecutor.INSTANCE.newSucceededFuture(singletonList(remoteAddress));
    } else {//from  w ww. j a va 2 s  .  c  o  m
        Promise<List<InetSocketAddress>> p = eventLoop.newPromise();

        request.getNameResolver().resolveAll(request.getUri().getHost(), eventLoop.newPromise())
                .addListener((Future<List<InetAddress>> whenAddresses) -> {
                    if (whenAddresses.isSuccess()) {
                        List<InetSocketAddress> remoteInetSocketAddresses = whenAddresses.getNow().stream().map(
                                address -> new InetSocketAddress(address, request.getUri().getExplicitPort()))
                                .collect(Collectors.toList());

                        p.setSuccess(remoteInetSocketAddresses);
                    } else {
                        if (!requestTimeout.isDone()) {
                            // only report if we haven't timed out
                            listener.onThrowable(whenAddresses.cause());
                        }
                        p.setFailure(whenAddresses.cause());
                    }
                });
        return p;
    }
}

From source file:io.gatling.http.client.impl.DefaultHttpClient.java

License:Apache License

private void sendTxWithNewChannel(HttpTx tx, EventLoopResources resources, EventLoop eventLoop,
        List<InetSocketAddress> addresses) {
    openNewChannel(tx.request, eventLoop, resources, addresses, tx.listener, tx.requestTimeout)
            .addListener((Future<Channel> whenNewChannel) -> {
                if (whenNewChannel.isSuccess()) {
                    Channel channel = whenNewChannel.getNow();
                    if (tx.requestTimeout.isDone()) {
                        channel.close();
                        return;
                    }//from  w  w  w .  jav  a  2 s .c o  m

                    channelGroup.add(channel);
                    resources.channelPool.register(channel, tx.key);

                    if (tx.request.getUri().isSecured()) {
                        LOGGER.debug("Installing SslHandler for {}", tx.request.getUri());
                        installSslHandler(tx, channel).addListener(f -> {
                            if (tx.requestTimeout.isDone() || !f.isSuccess()) {
                                channel.close();
                                return;
                            }

                            if (tx.request.isAlpnRequired()) {
                                LOGGER.debug("Installing Http2Handler for {}", tx.request.getUri());
                                installHttp2Handler(tx, channel, resources.channelPool).addListener(f2 -> {
                                    if (tx.requestTimeout.isDone() || !f2.isSuccess()) {
                                        channel.close();
                                        return;
                                    }
                                    sendTxWithChannel(tx, channel);
                                });

                            } else {
                                sendTxWithChannel(tx, channel);
                            }
                        });
                    } else {
                        sendTxWithChannel(tx, channel);
                    }
                }
            });
}

From source file:io.gatling.http.client.impl.DefaultHttpClient.java

License:Apache License

private void sendHttp2TxsWithNewChannel(List<HttpTx> txs, EventLoopResources resources, EventLoop eventLoop,
        List<InetSocketAddress> addresses) {
    HttpTx tx = txs.get(0);//ww w . j av  a2s  .com
    openNewChannel(tx.request, eventLoop, resources, addresses, tx.listener, tx.requestTimeout)
            .addListener((Future<Channel> whenNewChannel) -> {
                if (whenNewChannel.isSuccess()) {
                    Channel channel = whenNewChannel.getNow();
                    if (tx.requestTimeout.isDone()) {
                        channel.close();
                        return;
                    }

                    channelGroup.add(channel);
                    resources.channelPool.register(channel, tx.key);

                    LOGGER.debug("Installing SslHandler for {}", tx.request.getUri());
                    installSslHandler(tx, channel).addListener(f -> {
                        if (tx.requestTimeout.isDone() || !f.isSuccess()) {
                            channel.close();
                            return;
                        }
                        LOGGER.debug("Installing Http2Handler for {}", tx.request.getUri());
                        installHttp2Handler(tx, channel, resources.channelPool).addListener(f2 -> {
                            if (tx.requestTimeout.isDone() || !f2.isSuccess()) {
                                channel.close();
                                return;
                            }
                            sendHttp2TxsWithChannel(txs, channel);
                        });
                    });
                }
            });
}