List of usage examples for io.netty.util.concurrent Future addListener
Future<V> addListener(GenericFutureListener<? extends Future<? super V>> listener);
From source file:com.linecorp.armeria.client.circuitbreaker.CircuitBreakerRemoteInvokerTest.java
License:Apache License
@SuppressWarnings("unchecked") private static <T> Future<T> mockFuture() { Future<T> future = (Future<T>) mock(Future.class); when(future.addListener(any())).then(invoc -> { GenericFutureListener<Future<T>> listener = invoc.getArgumentAt(0, GenericFutureListener.class); listener.operationComplete(future); return future; });/*from w ww . j a v a2 s. co m*/ return future; }
From source file:com.linecorp.armeria.client.endpoint.dns.DnsEndpointGroup.java
License:Apache License
private void sendQueries() { if (stopped) { return;/*from w ww .j a va2s . c om*/ } final Future<List<DnsRecord>> future; final int numQuestions = questions.size(); if (numQuestions == 1) { // Simple case of single query final DnsQuestion question = questions.get(0); logger.debug("{} Sending a DNS query", logPrefix); future = resolver.resolveAll(question); } else { // Multiple queries logger.debug("{} Sending DNS queries", logPrefix); @SuppressWarnings("unchecked") final Promise<List<DnsRecord>> aggregatedPromise = eventLoop.newPromise(); final FutureListener<List<DnsRecord>> listener = new FutureListener<List<DnsRecord>>() { private final List<DnsRecord> records = new ArrayList<>(); private int remaining = numQuestions; @Nullable private List<Throwable> causes; @Override public void operationComplete(Future<List<DnsRecord>> future) throws Exception { if (future.isSuccess()) { final List<DnsRecord> records = future.getNow(); this.records.addAll(records); } else { if (causes == null) { causes = new ArrayList<>(numQuestions); } causes.add(future.cause()); } if (--remaining == 0) { if (!records.isEmpty()) { aggregatedPromise.setSuccess(records); } else { final Throwable aggregatedCause; if (causes == null) { aggregatedCause = new EndpointGroupException("empty result returned by DNS server"); } else { aggregatedCause = new EndpointGroupException("failed to receive DNS records"); for (Throwable c : causes) { aggregatedCause.addSuppressed(c); } } aggregatedPromise.setFailure(aggregatedCause); } } } }; questions.forEach(q -> resolver.resolveAll(q).addListener(listener)); future = aggregatedPromise; } attemptsSoFar++; future.addListener(this::onDnsRecords); }
From source file:com.linecorp.armeria.client.http.HttpClientDelegate.java
License:Apache License
@Override public HttpResponse execute(ClientRequestContext ctx, HttpRequest req) throws Exception { final Endpoint endpoint = ctx.endpoint().resolve().withDefaultPort(ctx.sessionProtocol().defaultPort()); autoFillHeaders(ctx, endpoint, req); final PoolKey poolKey = new PoolKey(InetSocketAddress.createUnresolved(endpoint.host(), endpoint.port()), ctx.sessionProtocol());//from w w w . j ava 2s . c o m final EventLoop eventLoop = ctx.eventLoop(); final Future<Channel> channelFuture = pool(eventLoop).acquire(poolKey); final DecodedHttpResponse res = new DecodedHttpResponse(eventLoop); if (channelFuture.isDone()) { if (channelFuture.isSuccess()) { Channel ch = channelFuture.getNow(); invoke0(ch, ctx, req, res, poolKey); } else { res.close(channelFuture.cause()); } } else { channelFuture.addListener((Future<Channel> future) -> { if (future.isSuccess()) { Channel ch = future.getNow(); invoke0(ch, ctx, req, res, poolKey); } else { res.close(channelFuture.cause()); } }); } return res; }
From source file:com.linecorp.armeria.client.HttpClientDelegate.java
License:Apache License
@Override public HttpResponse execute(ClientRequestContext ctx, HttpRequest req) throws Exception { final Endpoint endpoint = ctx.endpoint().resolve(ctx).withDefaultPort(ctx.sessionProtocol().defaultPort()); autoFillHeaders(ctx, endpoint, req); if (!sanitizePath(req)) { req.abort();/*from w w w. j ava 2s. c om*/ return HttpResponse.ofFailure(new IllegalArgumentException("invalid path: " + req.path())); } final EventLoop eventLoop = ctx.eventLoop(); final PoolKey poolKey = new PoolKey(endpoint.host(), endpoint.ipAddr(), endpoint.port(), ctx.sessionProtocol()); final Future<Channel> channelFuture = factory.pool(eventLoop).acquire(poolKey); final DecodedHttpResponse res = new DecodedHttpResponse(eventLoop); if (channelFuture.isDone()) { if (channelFuture.isSuccess()) { final Channel ch = channelFuture.getNow(); invoke0(ch, ctx, req, res, poolKey); } else { res.close(channelFuture.cause()); } } else { channelFuture.addListener((Future<Channel> future) -> { if (future.isSuccess()) { final Channel ch = future.getNow(); invoke0(ch, ctx, req, res, poolKey); } else { res.close(channelFuture.cause()); } }); } return res; }
From source file:com.linecorp.armeria.client.HttpRemoteInvoker.java
License:Apache License
@Override public <T> Future<T> invoke(EventLoop eventLoop, URI uri, ClientOptions options, ClientCodec codec, Method method, Object[] args) throws Exception { requireNonNull(uri, "uri"); requireNonNull(options, "options"); requireNonNull(codec, "codec"); requireNonNull(method, "method"); final Scheme scheme = Scheme.parse(uri.getScheme()); final SessionProtocol sessionProtocol = validateSessionProtocol(scheme.sessionProtocol()); final InetSocketAddress remoteAddress = convertToSocketAddress(uri, sessionProtocol.isTls()); final PoolKey poolKey = new PoolKey(remoteAddress, sessionProtocol); final Future<Channel> channelFuture = pool(eventLoop).acquire(poolKey); final Promise<T> resultPromise = eventLoop.newPromise(); codec.prepareRequest(method, args, resultPromise); if (channelFuture.isSuccess()) { Channel ch = channelFuture.getNow(); invoke0(codec, ch, method, args, options, resultPromise, poolKey); } else {// ww w. j a v a 2 s. com channelFuture.addListener((Future<Channel> future) -> { if (future.isSuccess()) { Channel ch = future.getNow(); invoke0(codec, ch, method, args, options, resultPromise, poolKey); } else { resultPromise.setFailure(channelFuture.cause()); } }); } return resultPromise; }
From source file:com.linecorp.armeria.client.pool.DefaultKeyedChannelPool.java
License:Apache License
private Future<Channel> acquireHealthyFromPoolOrNew(final K key, final Promise<Channel> promise) { final Deque<Channel> queue = pool.get(key); final Channel ch = queue == null ? null : queue.poll(); if (ch == null) { Future<Channel> f = channelFactory.apply(key); if (f.isDone()) { notifyConnect(key, f, promise); } else {//from w w w .jav a2 s . c om f.addListener((Future<Channel> future) -> notifyConnect(key, future, promise)); } return promise; } EventLoop loop = ch.eventLoop(); if (loop.inEventLoop()) { doHealthCheck(key, ch, promise); } else { loop.execute(() -> doHealthCheck(key, ch, promise)); } return promise; }
From source file:com.linecorp.armeria.client.pool.DefaultKeyedChannelPool.java
License:Apache License
private void doHealthCheck(final K key, final Channel ch, final Promise<Channel> promise) { assert ch.eventLoop().inEventLoop(); Future<Boolean> f = healthCheck.isHealthy(ch); if (f.isDone()) { notifyHealthCheck(key, f, ch, promise); } else {/* w ww.jav a2 s. c o m*/ f.addListener((FutureListener<Boolean>) future -> notifyHealthCheck(key, future, ch, promise)); } }
From source file:com.linecorp.armeria.client.pool.DefaultKeyedChannelPool.java
License:Apache License
private void doHealthCheckOnRelease(K key, final Channel channel, final Promise<Void> promise) throws Exception { final Future<Boolean> f = healthCheck.isHealthy(channel); if (f.isDone()) { releaseAndOfferIfHealthy(key, channel, promise, f); } else {//from ww w .j a va2s. co m f.addListener(future -> releaseAndOfferIfHealthy(key, channel, promise, f)); } }
From source file:com.linecorp.armeria.client.tracing.TracingRemoteInvoker.java
License:Apache License
@Override public final <T> Future<T> invoke(EventLoop eventLoop, URI uri, ClientOptions options, ClientCodec codec, Method method, Object[] args) throws Exception { // create new request adapter to catch generated spanId final InternalClientRequestAdapter requestAdapter = new InternalClientRequestAdapter( Endpoint.create(uri.getAuthority(), 0, 0), method.getName()); final Span span = clientInterceptor.openSpan(requestAdapter); // new client options with trace data final ClientOptions traceAwareOptions = putTraceData(options, requestAdapter.getSpanId()); if (span == null) { // skip tracing return super.invoke(eventLoop, uri, traceAwareOptions, codec, method, args); }//from w ww .j av a 2 s . c o m // The actual remote invocation is done asynchronously. // So we have to clear the span from current thread. clientInterceptor.clearSpan(); Future<T> result = null; try { result = super.invoke(eventLoop, uri, traceAwareOptions, codec, method, args); result.addListener(future -> clientInterceptor.closeSpan(span, createResponseAdapter(uri, options, codec, method, args, future))); } finally { if (result == null) { clientInterceptor.closeSpan(span, createResponseAdapter(uri, options, codec, method, args, null)); } } return result; }
From source file:com.linecorp.armeria.common.tracing.TracingTestBase.java
License:Apache License
@SuppressWarnings("unchecked") public static <T> Future<T> mockFuture() { Future<T> future = (Future<T>) mock(Future.class); when(future.addListener(any())).then(invoc -> { GenericFutureListener<Future<T>> listener = invoc.getArgumentAt(0, GenericFutureListener.class); listener.operationComplete(future); return future; });/*from w w w. j av a2s . c o m*/ return future; }