protected Client createClient(String targetIP, int targetPort, int connectTimeout, String key) throws Exception { if (connectTimeout < 1000) { bootstrap.setOption("connectTimeoutMillis", 1000); } else { bootstrap.setOption("connectTimeoutMillis", connectTimeout); } NettyClientHandler handler = new NettyClientHandler(this, key); bootstrap.setPipelineFactory(new NettyClientPipelineFactory(handler)); ChannelFuture future = bootstrap.connect(new InetSocketAddress(targetIP, targetPort)); future.awaitUninterruptibly(connectTimeout); if (!future.isDone()) { LOGGER.error("Create connection to " + targetIP + ":" + targetPort + " timeout!"); throw new Exception("Create connection to " + targetIP + ":" + targetPort + " timeout!"); } if (future.isCancelled()) { LOGGER.error("Create connection to " + targetIP + ":" + targetPort + " cancelled by user!"); throw new Exception( "Create connection to " + targetIP + ":" + targetPort + " cancelled by user!"); } if (!future.isSuccess()) { LOGGER.error( "Create connection to " + targetIP + ":" + targetPort + " error", future.getCause()); throw new Exception( "Create connection to " + targetIP + ":" + targetPort + " error", future.getCause()); } NettyClient client = new NettyClient(future, key, connectTimeout); handler.setClient(client); return client; }
protected Client createClient(String targetIP, int targetPort, int connectTimeout, String key) throws Exception { Bootstrap bootstrap = new Bootstrap(); bootstrap .group(workerGroup) .channel(NioSocketChannel.class) .option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT) .option( ChannelOption.TCP_NODELAY, Boolean.parseBoolean(System.getProperty("nfs.rpc.tcp.nodelay", "true"))) .option( ChannelOption.SO_REUSEADDR, Boolean.parseBoolean(System.getProperty("nfs.rpc.tcp.reuseaddress", "true"))); if (connectTimeout < 1000) { bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 1000); } else { bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, connectTimeout); } final NettyClientHandler handler = new NettyClientHandler(this, key); bootstrap.handler( new ChannelInitializer<SocketChannel>() { protected void initChannel(SocketChannel channel) throws Exception { ChannelPipeline pipeline = channel.pipeline(); pipeline.addLast("decoder", new NettyProtocolDecoder()); pipeline.addLast("encoder", new NettyProtocolEncoder()); pipeline.addLast("handler", handler); } }); ChannelFuture future = bootstrap.connect(new InetSocketAddress(targetIP, targetPort)).sync(); future.awaitUninterruptibly(connectTimeout); if (!future.isDone()) { LOGGER.error("Create connection to " + targetIP + ":" + targetPort + " timeout!"); throw new Exception("Create connection to " + targetIP + ":" + targetPort + " timeout!"); } if (future.isCancelled()) { LOGGER.error("Create connection to " + targetIP + ":" + targetPort + " cancelled by user!"); throw new Exception( "Create connection to " + targetIP + ":" + targetPort + " cancelled by user!"); } if (!future.isSuccess()) { LOGGER.error( "Create connection to " + targetIP + ":" + targetPort + " error", future.cause()); throw new Exception( "Create connection to " + targetIP + ":" + targetPort + " error", future.cause()); } NettyClient client = new NettyClient(future, key, connectTimeout); handler.setClient(client); return client; }