List of usage examples for io.netty.channel.pool ChannelPool acquire
Future<Channel> acquire();
From source file:com.heliosapm.streams.metrichub.HubManager.java
License:Apache License
public RequestCompletion evaluate(final QueryContext queryContext, final RequestBuilder requestBuilder, final String expression) { try {/* www . ja v a2 s .c o m*/ final JsonGenerator jg = requestBuilder.renderHeader(); final ChannelPool pool = channelPool(); final Channel channel = pool.acquire().get(); final CompletableFuture<RequestCompletion> completionFuture = new CompletableFuture<RequestCompletion>(); evaluate(queryContext, expression).flush().consume(lmt -> { final RequestCompletion rc = new RequestCompletion(queryContext.getTimeout(), pool); if (channel.pipeline().get("completion") != null) { try { channel.pipeline().remove("completion"); } catch (Exception ex) { } } channel.pipeline().addLast("completion", rc); completionFuture.complete(rc); final ByteBuf bb = requestBuilder.merge(jg, lmt); if (log.isDebugEnabled()) log.debug("CREQUEST:\n{}", bb.toString(UTF8)); final HttpRequest httpRequest = buildHttpRequest(bb); log.info("Flushed request. Size: {} bytes", bb.readableBytes()); channel.writeAndFlush(httpRequest); }); return completionFuture.get(); } catch (Exception ex) { throw new RuntimeException("eval error", ex); } }
From source file:com.ibasco.agql.core.transport.NettyPooledTransport.java
License:Open Source License
/** * <p>Acquires a {@link Channel} from the {@link ChannelPool}</p> * * @param message/*from www. ja v a2 s .co m*/ * An {@link AbstractRequest} that will be used as the lookup reference for the {@link * io.netty.channel.pool.ChannelPoolMap} key * * @return A {@link CompletableFuture} containing the acquired {@link Channel} */ @Override public CompletableFuture<Channel> getChannel(M message) { final CompletableFuture<Channel> channelFuture = new CompletableFuture<>(); //Retrieve our channel pool based on the message final ChannelPool pool = poolMap.get(message); log.debug("Acquiring channel from pool '{}' for message : {}", pool, message); //Acquire a channel from the pool and listen for completion pool.acquire().addListener((Future<Channel> future) -> { if (future.isSuccess()) { log.debug("Successfully acquired Channel from pool"); Channel channel = future.get(); channel.attr(ChannelAttributes.CHANNEL_POOL).set(pool); channelFuture.complete(channel); } else { log.debug("Failed to acquire Channel from Pool"); channelFuture.completeExceptionally(new ConnectException(future.cause())); } }); return channelFuture; }
From source file:org.eclipse.californium.elements.tcp.TcpClientConnector.java
License:Open Source License
@Override public void send(final RawData msg) { if (msg == null) { throw new NullPointerException("Message must not be null"); }//from w ww . j a va 2s .c om if (msg.isMulticast()) { LOGGER.warn("TcpConnector drops {} bytes to multicast {}:{}", msg.getSize(), msg.getAddress(), msg.getPort()); msg.onError(new MulticastNotSupportedException("TCP doesn't support multicast!")); return; } if (workerGroup == null) { msg.onError(new IllegalStateException("TCP client connector not running!")); return; } InetSocketAddress addressKey = msg.getInetSocketAddress(); final boolean connected = poolMap.contains(addressKey); final EndpointContextMatcher endpointMatcher = getEndpointContextMatcher(); /* check, if a new connection should be established */ if (endpointMatcher != null && !connected && !endpointMatcher.isToBeSent(msg.getEndpointContext(), null)) { LOGGER.warn("TcpConnector drops {} bytes to new {}:{}", msg.getSize(), msg.getAddress(), msg.getPort()); msg.onError(new EndpointMismatchException("no connection")); return; } if (!connected) { msg.onConnecting(); } final ChannelPool channelPool = poolMap.get(addressKey); Future<Channel> acquire = channelPool.acquire(); acquire.addListener(new GenericFutureListener<Future<Channel>>() { @Override public void operationComplete(Future<Channel> future) throws Exception { Throwable cause = null; if (future.isSuccess()) { Channel channel = future.getNow(); try { send(channel, endpointMatcher, msg); } catch (Throwable t) { cause = t; } finally { try { channelPool.release(channel); } catch (RejectedExecutionException e) { LOGGER.debug("{}", e.getMessage()); } } } else if (future.isCancelled()) { cause = new CancellationException(); } else { cause = future.cause(); } if (cause != null) { if (cause instanceof ConnectTimeoutException) { LOGGER.warn("{}", cause.getMessage()); } else if (cause instanceof CancellationException) { LOGGER.debug("{}", cause.getMessage()); } else { LOGGER.warn("Unable to open connection to {}", msg.getAddress(), future.cause()); } msg.onError(future.cause()); } } }); }
From source file:reactor.ipc.netty.tcp.TcpClient.java
License:Open Source License
/** * @param handler//from w w w .jav a 2 s.c o m * @param address * @param secure * @param onSetup * * @return a new Mono to connect on subscribe */ @SuppressWarnings("unchecked") protected Mono<NettyContext> newHandler( BiFunction<? super NettyInbound, ? super NettyOutbound, ? extends Publisher<Void>> handler, InetSocketAddress address, boolean secure, Consumer<? super Channel> onSetup) { final BiFunction<? super NettyInbound, ? super NettyOutbound, ? extends Publisher<Void>> targetHandler = null == handler ? ChannelOperations.noopHandler() : handler; return Mono.create(sink -> { ChannelPool pool = options.getPool(address); ContextHandler<SocketChannel> contextHandler = doHandler(targetHandler, sink, secure, pool, onSetup); if (pool == null) { Bootstrap b = options.get(address); b.handler(contextHandler); contextHandler.setFuture(b.connect()); } else { contextHandler.setFuture(pool.acquire()); } }); }