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

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

Introduction

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

Prototype

boolean tryFailure(Throwable cause);

Source Link

Document

Marks this future as a failure and notifies all listeners.

Usage

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

License:Apache License

public static PromiseConverter<ScanResult<SortedSetEntry>> toSortedSetScanResult(EventExecutor executor) {
    return new PromiseConverter<ScanResult<SortedSetEntry>>(executor) {

        @Override//  w ww.  j  a  v  a2s.  co m
        public FutureListener<Object> newListener(final Promise<ScanResult<SortedSetEntry>> 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 {
                            List<Object> list = (List<Object>) resp;
                            byte[] cursor = (byte[]) list.get(0);
                            List<byte[]> rawValueList = (List<byte[]>) list.get(1);
                            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(new ScanResult<SortedSetEntry>(cursor, values));
                        }
                    } else {
                        promise.tryFailure(future.cause());
                    }
                }
            };
        }
    };
}

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

License:Apache License

public static PromiseConverter<Map<byte[], byte[]>> toMap(EventExecutor executor) {
    return new PromiseConverter<Map<byte[], byte[]>>(executor) {

        @Override/*from  w  w w.  j a  v  a  2s .  c o  m*/
        public FutureListener<Object> newListener(final Promise<Map<byte[], byte[]>> 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;
                            Map<byte[], byte[]> values = newBytesKeyMap();
                            for (Iterator<byte[]> iter = rawValueList.iterator(); iter.hasNext();) {
                                values.put(iter.next(), iter.next());
                            }
                            promise.trySuccess(values);
                        }
                    } else {
                        promise.tryFailure(future.cause());
                    }
                }
            };
        }
    };
}

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

License:Apache License

public static PromiseConverter<Set<byte[]>> toSet(EventExecutor executor) {
    return new PromiseConverter<Set<byte[]>>(executor) {

        @Override//from   w ww.ja va  2 s  . c om
        public FutureListener<Object> newListener(final Promise<Set<byte[]>> 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 {
                            Set<byte[]> values = newBytesSet();
                            values.addAll((List<byte[]>) resp);
                            promise.trySuccess(values);
                        }
                    } else {
                        promise.tryFailure(future.cause());
                    }
                }
            };
        }
    };
}

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  w  w w. j a v a  2  s .  co  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  www .j av a 2s  .  co 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//from   w  w w.java 2s . co 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.netty.example.ocsp.OcspClientExample.java

License:Apache License

private static ChannelInitializer<Channel> newClientHandler(final ReferenceCountedOpenSslContext context,
        final String host, final Promise<FullHttpResponse> promise) {

    return new ChannelInitializer<Channel>() {
        @Override// w w  w  . j a va 2  s . co m
        protected void initChannel(Channel ch) throws Exception {
            SslHandler sslHandler = context.newHandler(ch.alloc());
            ReferenceCountedOpenSslEngine engine = (ReferenceCountedOpenSslEngine) sslHandler.engine();

            ChannelPipeline pipeline = ch.pipeline();
            pipeline.addLast(sslHandler);
            pipeline.addLast(new ExampleOcspClientHandler(engine));

            pipeline.addLast(new HttpClientCodec());
            pipeline.addLast(new HttpObjectAggregator(1024 * 1024));
            pipeline.addLast(new HttpClientHandler(host, promise));
        }

        @Override
        public void channelInactive(ChannelHandlerContext ctx) throws Exception {
            if (!promise.isDone()) {
                promise.tryFailure(new IllegalStateException("Connection closed and Promise was not done."));
            }
            ctx.fireChannelInactive();
        }

        @Override
        public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
            if (!promise.tryFailure(cause)) {
                ctx.fireExceptionCaught(cause);
            }
        }
    };
}

From source file:io.vertx.core.dns.impl.fix.DnsNameResolver.java

License:Apache License

private boolean doResolveCached(String hostname, Promise<InetAddress> promise, DnsCache resolveCache) {
    final List<DnsCacheEntry> cachedEntries = resolveCache.get(hostname);
    if (cachedEntries == null) {
        return false;
    }// w  ww  .java 2 s  . c  om

    InetAddress address = null;
    Throwable cause = null;
    synchronized (cachedEntries) {
        final int numEntries = cachedEntries.size();
        assert numEntries > 0;

        if (cachedEntries.get(0).cause() != null) {
            cause = cachedEntries.get(0).cause();
        } else {
            // Find the first entry with the preferred address type.
            for (InternetProtocolFamily f : resolvedAddressTypes) {
                for (int i = 0; i < numEntries; i++) {
                    final DnsCacheEntry e = cachedEntries.get(i);
                    if (f.addressType().isInstance(e.address())) {
                        address = e.address();
                        break;
                    }
                }
            }
        }
    }

    if (address != null) {
        setSuccess(promise, address);
    } else if (cause != null) {
        if (!promise.tryFailure(cause)) {
            logger.warn("Failed to notify failure to a promise: {}", promise, cause);
        }
    } else {
        return false;
    }

    return true;
}

From source file:io.vertx.core.dns.impl.fix.DnsNameResolver.java

License:Apache License

private void doResolveUncached(String hostname, Promise<InetAddress> promise, DnsCache resolveCache,
        boolean trySearchDomain) {
    if (trySearchDomain) {
        Promise<InetAddress> original = promise;
        promise = new DefaultPromise<>(executor());
        FutureListener<InetAddress> globalListener = future -> {
            if (future.isSuccess()) {
                original.setSuccess(future.getNow());
            } else {
                FutureListener<InetAddress> sdListener = new FutureListener<InetAddress>() {
                    int count;

                    @Override/*from w w  w  . j  a  v  a 2 s.  c  om*/
                    public void operationComplete(Future<InetAddress> future) throws Exception {
                        if (future.isSuccess()) {
                            original.trySuccess(future.getNow());
                        } else {
                            if (count < searchDomains.size()) {
                                String searchDomain = searchDomains.get(count++);
                                Promise<InetAddress> p = new DefaultPromise<>(executor());
                                doResolveUncached(hostname + "." + searchDomain, p, resolveCache, false);
                                p.addListener(this);
                            } else {
                                original.tryFailure(future.cause());
                            }
                        }
                    }
                };
                future.addListener(sdListener);
            }
        };
        promise.addListener(globalListener);
    }
    if (searchDomains(hostname)) {
        promise.tryFailure(new UnknownHostException(hostname));
    } else {
        final DnsNameResolverContext<InetAddress> ctx = new DnsNameResolverContext<InetAddress>(this, hostname,
                promise, resolveCache) {
            @Override
            protected boolean finishResolve(Class<? extends InetAddress> addressType,
                    List<DnsCacheEntry> resolvedEntries) {

                final int numEntries = resolvedEntries.size();
                for (int i = 0; i < numEntries; i++) {
                    final InetAddress a = resolvedEntries.get(i).address();
                    if (addressType.isInstance(a)) {
                        setSuccess(promise(), a);
                        return true;
                    }
                }
                return false;
            }
        };

        ctx.resolve();
    }
}

From source file:io.vertx.core.dns.impl.fix.DnsNameResolver.java

License:Apache License

private boolean doResolveAllCached(String hostname, Promise<List<InetAddress>> promise, DnsCache resolveCache) {
    final List<DnsCacheEntry> cachedEntries = resolveCache.get(hostname);
    if (cachedEntries == null) {
        return false;
    }//w  w w  .j  a  v a 2  s . c o  m

    List<InetAddress> result = null;
    Throwable cause = null;
    synchronized (cachedEntries) {
        final int numEntries = cachedEntries.size();
        assert numEntries > 0;

        if (cachedEntries.get(0).cause() != null) {
            cause = cachedEntries.get(0).cause();
        } else {
            for (InternetProtocolFamily f : resolvedAddressTypes) {
                for (int i = 0; i < numEntries; i++) {
                    final DnsCacheEntry e = cachedEntries.get(i);
                    if (f.addressType().isInstance(e.address())) {
                        if (result == null) {
                            result = new ArrayList<InetAddress>(numEntries);
                        }
                        result.add(e.address());
                    }
                }
            }
        }
    }

    if (result != null) {
        promise.trySuccess(result);
    } else if (cause != null) {
        promise.tryFailure(cause);
    } else {
        return false;
    }

    return true;
}