Future<Channel> channelFuture = channelPool.acquire(); channelFuture.addListener(future -> { if (future.isSuccess()) {
/** * Remove the {@link ChannelPool} from this {@link AbstractChannelPoolMap}. Returns {@code true} if removed, * {@code false} otherwise. * * Please note that {@code null} keys are not allowed. */ public final boolean remove(K key) { P pool = map.remove(checkNotNull(key, "key")); if (pool != null) { pool.close(); return true; } return false; }
private void release0(Channel channel, Promise<Void> promise) { if (protocolImpl == null) { // If protocolImpl is null that means the first connection failed to establish. Release it back to the // underlying connection pool. delegatePool.release(channel, promise); } else { protocolImpl.release(channel, promise); } }
public HttpReceive request(HttpSend httpSend, long timeout, TimeUnit timeUnit) { final HttpReceive httpReceive = new HttpReceive(); Future<Channel> fch = channelPool.acquire(); Channel channel = null; try { channel = fch.get(timeout, timeUnit); ChannelPipeline p = channel.pipeline(); p.addLast(new HttpClientHandler(httpSend, httpReceive)); final FullHttpRequest fullHttpRequest = convertRequest(httpSend); p.writeAndFlush(fullHttpRequest); channel.closeFuture().await(timeout, timeUnit); if (!httpReceive.getIsDone()) { httpReceive.setHaveError(true); httpReceive.setErrMsg("请求已经超时"); } } catch (Exception e) { if (log.isWarnEnabled()) { log.warn(e.getMessage(), e); } httpReceive.setHaveError(true) .setErrMsg(e.getMessage()) .setThrowable(e) .setIsDone(true); } finally { if (channel != null) { channelPool.release(channel); } } return httpReceive; }
@Override public void close() { try { setClosedFlag().await(); for (MultiplexedChannelRecord c : connections) { Future<Channel> f = c.getConnectionFuture(); f.await(); if (f.isSuccess()) { connectionPool.release(f.getNow()).await(); } } connectionPool.close(); } catch (InterruptedException ie) { Thread.currentThread().interrupt(); throw new RuntimeException(ie); } }
private void release0(Channel channel, Promise<Void> promise) { if (protocolImpl == null) { // If protocolImpl is null that means the first connection failed to establish. Release it back to the // underlying connection pool. simpleChannelPool.release(channel, promise); } else { protocolImpl.release(channel, promise); } }
@Override public void connect(final Downstream<? super T> downstream) throws Exception { channelPool.acquire().addListener(acquireFuture -> { if (acquireFuture.isSuccess()) { Channel channel = (Channel) acquireFuture.getNow(); if (channel.eventLoop().equals(execution.getEventLoop())) { send(downstream, channel); } else { channel.deregister().addListener(deregisterFuture -> execution.getEventLoop().register(channel).addListener(registerFuture -> { if (registerFuture.isSuccess()) { send(downstream, channel); } else { channel.close(); channelPool.release(channel); connectFailure(downstream, registerFuture.cause()); } }) ); } } else { connectFailure(downstream, acquireFuture.cause()); } }); }
@Override public void close() { try { setClosedFlag().await(); for (MultiplexedChannelRecord c : connections) { Future<Channel> f = c.getConnectionFuture(); f.await(); if (f.isSuccess()) { connectionPool.release(f.getNow()).await(); } } connectionPool.close(); } catch (InterruptedException ie) { Thread.currentThread().interrupt(); throw new RuntimeException(ie); } }
@Override public Future<Channel> acquire() { return delegate.acquire(); }
@Override public Future<Void> release(Channel channel, Promise<Void> promise) { return pool.release(channel, promise); }
/** * Remove the {@link ChannelPool} from this {@link AbstractChannelPoolMap}. Returns {@code true} if removed, * {@code false} otherwise. * * Please note that {@code null} keys are not allowed. */ public final boolean remove(K key) { P pool = map.remove(checkNotNull(key, "key")); if (pool != null) { pool.close(); return true; } return false; }
@Override public Future<Channel> acquire(Promise<Channel> promise) { return pool.acquire(promise); }
@Override public Future<Void> release(Channel channel) { return pool.release(channel); }
@Override public final P get(K key) { P pool = map.get(checkNotNull(key, "key")); if (pool == null) { pool = newPool(key); P old = map.putIfAbsent(key, pool); if (old != null) { // We need to destroy the newly created pool as we not use it. pool.close(); pool = old; } } return pool; } /**
@Override public Future<Channel> acquire(Promise<Channel> promise) { return pool.acquire(promise); }
@Override public Future<Void> release(Channel channel, Promise<Void> promise) { return pool.release(channel, promise); }
/** * Remove the {@link ChannelPool} from this {@link AbstractChannelPoolMap}. Returns {@code true} if removed, * {@code false} otherwise. * * Please note that {@code null} keys are not allowed. */ public final boolean remove(K key) { P pool = map.remove(checkNotNull(key, "key")); if (pool != null) { pool.close(); return true; } return false; }
@Override public Future<Channel> acquire(Promise<Channel> promise) { return delegatePool.acquire(promise); }
@Override public Future<Void> release(Channel channel, Promise<Void> promise) { return pool.release(channel, promise); }
@Override public final P get(K key) { P pool = map.get(checkNotNull(key, "key")); if (pool == null) { pool = newPool(key); P old = map.putIfAbsent(key, pool); if (old != null) { // We need to destroy the newly created pool as we not use it. pool.close(); pool = old; } } return pool; } /**