List of usage examples for io.netty.channel ChannelFuture awaitUninterruptibly
@Override ChannelFuture awaitUninterruptibly();
From source file:org.apache.camel.component.netty4.SingleTCPNettyServerBootstrapFactory.java
License:Apache License
protected void startServerBootstrap() { // prefer using explicit configured thread pools EventLoopGroup bg = configuration.getBossGroup(); EventLoopGroup wg = configuration.getWorkerGroup(); if (bg == null) { // create new pool which we should shutdown when stopping as its not shared bossGroup = new NettyServerBossPoolBuilder().withBossCount(configuration.getBossCount()) .withName("NettyServerTCPBoss").build(); bg = bossGroup;//from www .ja v a2 s.c o m } if (wg == null) { // create new pool which we should shutdown when stopping as its not shared workerGroup = new NettyWorkerPoolBuilder().withWorkerCount(configuration.getWorkerCount()) .withName("NettyServerTCPWorker").build(); wg = workerGroup; } //channelFactory = new NioServerSocketChannelFactory(bg, wg); serverBootstrap = new ServerBootstrap(); serverBootstrap.group(bg, wg).channel(NioServerSocketChannel.class); serverBootstrap.childOption(ChannelOption.SO_KEEPALIVE, configuration.isKeepAlive()); serverBootstrap.childOption(ChannelOption.TCP_NODELAY, configuration.isTcpNoDelay()); serverBootstrap.option(ChannelOption.SO_REUSEADDR, configuration.isReuseAddress()); serverBootstrap.childOption(ChannelOption.SO_REUSEADDR, configuration.isReuseAddress()); serverBootstrap.childOption(ChannelOption.CONNECT_TIMEOUT_MILLIS, configuration.getConnectTimeout()); if (configuration.getBacklog() > 0) { serverBootstrap.option(ChannelOption.SO_BACKLOG, configuration.getBacklog()); } // TODO set any additional netty options and child options /*if (configuration.getOptions() != null) { for (Map.Entry<String, Object> entry : configuration.getOptions().entrySet()) { serverBootstrap.setOption(entry.getKey(), entry.getValue()); } }*/ // set the pipeline factory, which creates the pipeline for each newly created channels serverBootstrap.childHandler(pipelineFactory); LOG.debug("Created ServerBootstrap {}", serverBootstrap); LOG.info("ServerBootstrap binding to {}:{}", configuration.getHost(), configuration.getPort()); ChannelFuture channelFutrue = serverBootstrap .bind(new InetSocketAddress(configuration.getHost(), configuration.getPort())); channelFutrue.awaitUninterruptibly(); channel = channelFutrue.channel(); // to keep track of all channels in use allChannels.add(channel); }
From source file:org.apache.camel.component.netty4.SingleUDPNettyServerBootstrapFactory.java
License:Apache License
protected void startServerBootstrap() throws Exception { // create non-shared worker pool EventLoopGroup wg = configuration.getWorkerGroup(); if (wg == null) { // create new pool which we should shutdown when stopping as its not shared workerGroup = new NettyWorkerPoolBuilder().withWorkerCount(configuration.getWorkerCount()) .withName("NettyServerTCPWorker").build(); wg = workerGroup;//from ww w . jav a 2 s . com } Bootstrap bootstrap = new Bootstrap(); bootstrap.group(wg).channel(NioDatagramChannel.class); // We cannot set the child option here bootstrap.option(ChannelOption.SO_REUSEADDR, configuration.isReuseAddress()); bootstrap.option(ChannelOption.SO_SNDBUF, configuration.getSendBufferSize()); bootstrap.option(ChannelOption.SO_RCVBUF, configuration.getReceiveBufferSize()); bootstrap.option(ChannelOption.SO_BROADCAST, configuration.isBroadcast()); bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, configuration.getConnectTimeout()); // TODO need to find the right setting of below option // only set this if user has specified /* if (configuration.getReceiveBufferSizePredictor() > 0) { bootstrap.setOption("receiveBufferSizePredictorFactory", new FixedReceiveBufferSizePredictorFactory(configuration.getReceiveBufferSizePredictor())); }*/ if (configuration.getBacklog() > 0) { bootstrap.option(ChannelOption.SO_BACKLOG, configuration.getBacklog()); } //TODO need to check the additional netty options /* if (configuration.getOptions() != null) { for (Map.Entry<String, Object> entry : configuration.getOptions().entrySet()) { connectionlessBootstrap.setOption(entry.getKey(), entry.getValue()); } }*/ LOG.debug("Created ConnectionlessBootstrap {}", bootstrap); // set the pipeline factory, which creates the pipeline for each newly created channels bootstrap.handler(pipelineFactory); InetSocketAddress hostAddress = new InetSocketAddress(configuration.getHost(), configuration.getPort()); SubnetUtils multicastSubnet = new SubnetUtils(MULTICAST_SUBNET); if (multicastSubnet.getInfo().isInRange(configuration.getHost())) { ChannelFuture channelFuture = bootstrap.bind(hostAddress); channelFuture.awaitUninterruptibly(); channel = channelFuture.channel(); DatagramChannel datagramChannel = (DatagramChannel) channel; String networkInterface = configuration.getNetworkInterface() == null ? LOOPBACK_INTERFACE : configuration.getNetworkInterface(); multicastNetworkInterface = NetworkInterface.getByName(networkInterface); ObjectHelper.notNull(multicastNetworkInterface, "No network interface found for '" + networkInterface + "'."); LOG.info("ConnectionlessBootstrap joining {}:{} using network interface: {}", new Object[] { configuration.getHost(), configuration.getPort(), multicastNetworkInterface.getName() }); datagramChannel.joinGroup(hostAddress, multicastNetworkInterface).syncUninterruptibly(); allChannels.add(datagramChannel); } else { LOG.info("ConnectionlessBootstrap binding to {}:{}", configuration.getHost(), configuration.getPort()); ChannelFuture channelFuture = bootstrap.bind(hostAddress); channelFuture.awaitUninterruptibly(); channel = channelFuture.channel(); allChannels.add(channel); } }
From source file:org.apache.cassandra.transport.Server.java
License:Apache License
private void run() { // Configure the server. eventExecutorGroup = new RequestThreadPoolExecutor(); boolean hasEpoll = enableEpoll ? Epoll.isAvailable() : false; if (hasEpoll) { workerGroup = new EpollEventLoopGroup(); logger.info("Netty using native Epoll event loop"); } else {// w w w . j a v a 2 s . c o m workerGroup = new NioEventLoopGroup(); logger.info("Netty using Java NIO event loop"); } ServerBootstrap bootstrap = new ServerBootstrap().group(workerGroup) .channel(hasEpoll ? EpollServerSocketChannel.class : NioServerSocketChannel.class) .childOption(ChannelOption.TCP_NODELAY, true).childOption(ChannelOption.SO_LINGER, 0) .childOption(ChannelOption.SO_KEEPALIVE, DatabaseDescriptor.getRpcKeepAlive()) .childOption(ChannelOption.ALLOCATOR, CBUtil.allocator) .childOption(ChannelOption.WRITE_BUFFER_HIGH_WATER_MARK, 32 * 1024) .childOption(ChannelOption.WRITE_BUFFER_LOW_WATER_MARK, 8 * 1024); final EncryptionOptions.ClientEncryptionOptions clientEnc = DatabaseDescriptor.getClientEncryptionOptions(); if (clientEnc.enabled) { logger.info("Enabling encrypted CQL connections between client and server"); bootstrap.childHandler(new SecureInitializer(this, clientEnc)); } else { bootstrap.childHandler(new Initializer(this)); } // Bind and start to accept incoming connections. logger.info("Using Netty Version: {}", Version.identify().entrySet()); logger.info("Starting listening for CQL clients on {}...", socket); ChannelFuture bindFuture = bootstrap.bind(socket); if (!bindFuture.awaitUninterruptibly().isSuccess()) throw new IllegalStateException(String.format("Failed to bind port %d on %s.", socket.getPort(), socket.getAddress().getHostAddress())); connectionTracker.allChannels.add(bindFuture.channel()); isRunning.set(true); StorageService.instance.setRpcReady(true); }
From source file:org.apache.cassandra.transport.SimpleClient.java
License:Apache License
protected void establishConnection() throws IOException { // Configure the client. bootstrap = new Bootstrap().group(new NioEventLoopGroup()) .channel(io.netty.channel.socket.nio.NioSocketChannel.class) .option(ChannelOption.TCP_NODELAY, true); // Configure the pipeline factory. if (encryptionOptions.enabled) { bootstrap.handler(new SecureInitializer()); } else {//from w w w . ja v a2 s . co m bootstrap.handler(new Initializer()); } ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port)); // Wait until the connection attempt succeeds or fails. channel = future.awaitUninterruptibly().channel(); if (!future.isSuccess()) { bootstrap.group().shutdownGracefully(); throw new IOException("Connection Error", future.cause()); } }
From source file:org.apache.tajo.worker.Fetcher.java
License:Apache License
public FileChunk get() throws IOException { if (useLocalFile) { LOG.info("Get pseudo fetch from local host"); startTime = System.currentTimeMillis(); finishTime = System.currentTimeMillis(); state = TajoProtos.FetcherState.FETCH_FINISHED; return fileChunk; }/*from w ww. j a va 2 s . com*/ LOG.info("Get real fetch from remote host"); this.startTime = System.currentTimeMillis(); this.state = TajoProtos.FetcherState.FETCH_FETCHING; ChannelFuture future = null; try { future = bootstrap.clone().connect(new InetSocketAddress(host, port)) .addListener(ChannelFutureListener.CLOSE_ON_FAILURE); // Wait until the connection attempt succeeds or fails. Channel channel = future.awaitUninterruptibly().channel(); if (!future.isSuccess()) { state = TajoProtos.FetcherState.FETCH_FAILED; throw new IOException(future.cause()); } String query = uri.getPath() + (uri.getRawQuery() != null ? "?" + uri.getRawQuery() : ""); // Prepare the HTTP request. HttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, query); request.headers().set(HttpHeaders.Names.HOST, host); request.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.CLOSE); request.headers().set(HttpHeaders.Names.ACCEPT_ENCODING, HttpHeaders.Values.GZIP); LOG.info("Status: " + getState() + ", URI:" + uri); // Send the HTTP request. ChannelFuture channelFuture = channel.writeAndFlush(request); // Wait for the server to close the connection. channel.closeFuture().awaitUninterruptibly(); channelFuture.addListener(ChannelFutureListener.CLOSE); fileChunk.setLength(fileChunk.getFile().length()); return fileChunk; } finally { if (future != null) { // Close the channel to exit. future.channel().close(); } this.finishTime = System.currentTimeMillis(); LOG.info("Fetcher finished:" + (finishTime - startTime) + " ms, " + getState() + ", URI:" + uri); } }
From source file:org.apache.tajo.worker.LocalFetcher.java
License:Apache License
private List<FileChunk> getChunksForRangeShuffle(final PullServerParams params, final Path queryBaseDir) throws IOException { final List<FileChunk> fileChunks = new ArrayList<>(); if (state == FetcherState.FETCH_INIT) { final ChannelInitializer<Channel> initializer = new HttpClientChannelInitializer(); bootstrap.handler(initializer);/*www. j a va 2s . c om*/ } this.state = FetcherState.FETCH_META_FETCHING; ChannelFuture future = null; try { future = bootstrap.clone().connect(new InetSocketAddress(host, port)) .addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE); // Wait until the connection attempt succeeds or fails. Channel channel = future.awaitUninterruptibly().channel(); if (!future.isSuccess()) { endFetch(FetcherState.FETCH_FAILED); throw new IOException(future.cause()); } for (URI eachURI : createChunkMetaRequestURIs(host, port, params)) { String query = eachURI.getPath() + (eachURI.getRawQuery() != null ? "?" + eachURI.getRawQuery() : ""); HttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, query); request.headers().set(HttpHeaders.Names.HOST, host); request.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.CLOSE); request.headers().set(HttpHeaders.Names.ACCEPT_ENCODING, HttpHeaders.Values.GZIP); if (LOG.isDebugEnabled()) { LOG.debug("Status: " + getState() + ", URI:" + eachURI); } // Send the HTTP request. channel.writeAndFlush(request); } // Wait for the server to close the connection. throw exception if failed channel.closeFuture().syncUninterruptibly(); if (!state.equals(FetcherState.FETCH_META_FINISHED)) { endFetch(FetcherState.FETCH_FAILED); } else { state = FetcherState.FETCH_DATA_FETCHING; fileLen = fileNum = 0; for (FileChunkMeta eachMeta : chunkMetas) { Path outputPath = StorageUtil.concatPath(queryBaseDir, eachMeta.getTaskId(), "output"); if (!localDirAllocator.ifExists(outputPath.toString(), conf)) { LOG.warn("Range shuffle - file not exist. " + outputPath); continue; } Path path = localFileSystem .makeQualified(localDirAllocator.getLocalPathToRead(outputPath.toString(), conf)); File file = new File(URI.create(path.toUri() + "/output")); FileChunk chunk = new FileChunk(file, eachMeta.getStartOffset(), eachMeta.getLength()); chunk.setEbId(tableName); fileChunks.add(chunk); fileLen += chunk.length(); fileNum++; } endFetch(FetcherState.FETCH_DATA_FINISHED); } return fileChunks; } finally { if (future != null && future.channel().isOpen()) { // Close the channel to exit. future.channel().close().awaitUninterruptibly(); } } }
From source file:org.apache.tajo.worker.RemoteFetcher.java
License:Apache License
@Override public List<FileChunk> get() throws IOException { List<FileChunk> fileChunks = new ArrayList<>(); if (state == FetcherState.FETCH_INIT) { ChannelInitializer<Channel> initializer = new HttpClientChannelInitializer(fileChunk.getFile()); bootstrap.handler(initializer);/*from www . j a va 2s. c om*/ } this.startTime = System.currentTimeMillis(); this.state = FetcherState.FETCH_DATA_FETCHING; ChannelFuture future = null; try { future = bootstrap.clone().connect(new InetSocketAddress(host, port)) .addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE); // Wait until the connection attempt succeeds or fails. Channel channel = future.awaitUninterruptibly().channel(); if (!future.isSuccess()) { state = TajoProtos.FetcherState.FETCH_FAILED; throw new IOException(future.cause()); } String query = uri.getPath() + (uri.getRawQuery() != null ? "?" + uri.getRawQuery() : ""); // Prepare the HTTP request. HttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, query); request.headers().set(HttpHeaders.Names.HOST, host); request.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.CLOSE); request.headers().set(HttpHeaders.Names.ACCEPT_ENCODING, HttpHeaders.Values.GZIP); if (LOG.isDebugEnabled()) { LOG.debug("Status: " + getState() + ", URI:" + uri); } // Send the HTTP request. channel.writeAndFlush(request); // Wait for the server to close the connection. throw exception if failed channel.closeFuture().syncUninterruptibly(); fileChunk.setLength(fileChunk.getFile().length()); long start = 0; for (Long eachChunkLength : chunkLengths) { if (eachChunkLength == 0) continue; FileChunk chunk = new FileChunk(fileChunk.getFile(), start, eachChunkLength); chunk.setEbId(fileChunk.getEbId()); chunk.setFromRemote(true); fileChunks.add(chunk); start += eachChunkLength; } return fileChunks; } finally { if (future != null && future.channel().isOpen()) { // Close the channel to exit. future.channel().close().awaitUninterruptibly(); } this.finishTime = System.currentTimeMillis(); long elapsedMills = finishTime - startTime; String transferSpeed; if (elapsedMills > 1000) { long bytePerSec = (fileChunk.length() * 1000) / elapsedMills; transferSpeed = FileUtils.byteCountToDisplaySize(bytePerSec); } else { transferSpeed = FileUtils.byteCountToDisplaySize(Math.max(fileChunk.length(), 0)); } LOG.info(String.format("Fetcher :%d ms elapsed. %s/sec, len:%d, state:%s, URL:%s", elapsedMills, transferSpeed, fileChunk.length(), getState(), uri)); } }
From source file:org.elasticsearch.hadoop.transport.netty4.Netty4Utils.java
License:Apache License
public static void closeChannels(final Collection<Channel> channels) throws IOException { IOException closingExceptions = null; final List<ChannelFuture> futures = new ArrayList<>(); for (final Channel channel : channels) { try {/*from w ww.j av a 2 s. c om*/ if (channel != null && channel.isOpen()) { futures.add(channel.close()); } } catch (Exception e) { if (closingExceptions == null) { closingExceptions = new IOException("failed to close channels"); } closingExceptions.addSuppressed(e); } } for (final ChannelFuture future : futures) { future.awaitUninterruptibly(); } if (closingExceptions != null) { throw closingExceptions; } }
From source file:org.hornetq.amqp.test.minimalclient.SimpleAMQPConnector.java
License:Apache License
public AMQPClientConnection connect(String host, int port) throws Exception { SocketAddress remoteDestination = new InetSocketAddress(host, port); ChannelFuture future = bootstrap.connect(remoteDestination); future.awaitUninterruptibly(); AMQPClientSPI clientConnectionSPI = new AMQPClientSPI(future.channel()); final AMQPClientConnection connection = (AMQPClientConnection) ProtonClientConnectionFactory.getFactory() .createConnection(clientConnectionSPI, false); future.channel().pipeline().addLast(new ChannelDuplexHandler() { @Override/* w ww . j a va 2 s . c om*/ public void channelRead(final ChannelHandlerContext ctx, final Object msg) throws Exception { ByteBuf buffer = (ByteBuf) msg; connection.inputBuffer(buffer); } }); return connection; }
From source file:org.hornetq.core.remoting.impl.netty.NettyConnector.java
License:Apache License
public Connection createConnection() { if (channelClazz == null) { return null; }//from www . j a va2 s . com // HORNETQ-907 - strip off IPv6 scope-id (if necessary) SocketAddress remoteDestination = new InetSocketAddress(host, port); InetAddress inetAddress = ((InetSocketAddress) remoteDestination).getAddress(); if (inetAddress instanceof Inet6Address) { Inet6Address inet6Address = (Inet6Address) inetAddress; if (inet6Address.getScopeId() != 0) { try { remoteDestination = new InetSocketAddress(InetAddress.getByAddress(inet6Address.getAddress()), ((InetSocketAddress) remoteDestination).getPort()); } catch (UnknownHostException e) { throw new IllegalArgumentException(e.getMessage()); } } } HornetQClientLogger.LOGGER.debug("Remote destination: " + remoteDestination); ChannelFuture future; //port 0 does not work so only use local address if set if (localPort != 0) { SocketAddress localDestination; if (localAddress != null) { localDestination = new InetSocketAddress(localAddress, localPort); } else { localDestination = new InetSocketAddress(localPort); } future = bootstrap.connect(remoteDestination, localDestination); } else { future = bootstrap.connect(remoteDestination); } future.awaitUninterruptibly(); if (future.isSuccess()) { final Channel ch = future.channel(); SslHandler sslHandler = ch.pipeline().get(SslHandler.class); if (sslHandler != null) { Future<Channel> handshakeFuture = sslHandler.handshakeFuture(); if (handshakeFuture.awaitUninterruptibly(30000)) { if (handshakeFuture.isSuccess()) { ChannelPipeline channelPipeline = ch.pipeline(); HornetQChannelHandler channelHandler = channelPipeline.get(HornetQChannelHandler.class); channelHandler.active = true; } else { ch.close().awaitUninterruptibly(); HornetQClientLogger.LOGGER.errorCreatingNettyConnection(handshakeFuture.cause()); return null; } } else { //handshakeFuture.setFailure(new SSLException("Handshake was not completed in 30 seconds")); ch.close().awaitUninterruptibly(); return null; } } if (httpUpgradeEnabled) { // Send a HTTP GET + Upgrade request that will be handled by the http-upgrade handler. try { //get this first incase it removes itself HttpUpgradeHandler httpUpgradeHandler = (HttpUpgradeHandler) ch.pipeline().get("http-upgrade"); URI uri = new URI("http", null, host, port, null, null, null); HttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, uri.getRawPath()); request.headers().set(HttpHeaders.Names.HOST, host); request.headers().set(HttpHeaders.Names.UPGRADE, HORNETQ_REMOTING); request.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.UPGRADE); final String endpoint = ConfigurationHelper.getStringProperty( TransportConstants.HTTP_UPGRADE_ENDPOINT_PROP_NAME, null, configuration); if (endpoint != null) { request.headers().set(TransportConstants.HTTP_UPGRADE_ENDPOINT_PROP_NAME, endpoint); } // Get 16 bit nonce and base 64 encode it byte[] nonce = randomBytes(16); String key = base64(nonce); request.headers().set(SEC_HORNETQ_REMOTING_KEY, key); ch.attr(REMOTING_KEY).set(key); HornetQClientLogger.LOGGER.debugf("Sending HTTP request %s", request); // Send the HTTP request. ch.writeAndFlush(request); if (!httpUpgradeHandler.awaitHandshake()) { return null; } } catch (URISyntaxException e) { HornetQClientLogger.LOGGER.errorCreatingNettyConnection(e); return null; } } else { ChannelPipeline channelPipeline = ch.pipeline(); HornetQChannelHandler channelHandler = channelPipeline.get(HornetQChannelHandler.class); channelHandler.active = true; } // No acceptor on a client connection Listener connectionListener = new Listener(); NettyConnection conn = new NettyConnection(configuration, ch, connectionListener, !httpEnabled && batchDelay > 0, false); connectionListener.connectionCreated(null, conn, HornetQClient.DEFAULT_CORE_PROTOCOL); return conn; } else { Throwable t = future.cause(); if (t != null && !(t instanceof ConnectException)) { HornetQClientLogger.LOGGER.errorCreatingNettyConnection(future.cause()); } return null; } }