List of usage examples for io.netty.bootstrap Bootstrap connect
public ChannelFuture connect()
From source file:Http2Client.java
License:Apache License
public Http2Client(final String host, final int port, final boolean ssl) throws Exception { this.host = host; this.port = port; this.ssl = ssl; // Configure SSL. if (ssl) {/*from w w w . j a v a2s . c o m*/ this.sslCtx = SslContext.newClientContext(null, InsecureTrustManagerFactory.INSTANCE, null, Arrays.asList(SelectedProtocol.HTTP_2.protocolName(), SelectedProtocol.HTTP_1_1.protocolName()), 0, 0); } else { this.sslCtx = null; } // XXX (dano): Http2Connection does not seem to be thread safe, use one thread only this.workerGroup = new NioEventLoopGroup(1); Http2ClientInitializer initializer = new Http2ClientInitializer(sslCtx); // Configure the client. Bootstrap b = new Bootstrap(); b.group(workerGroup); b.channel(NioSocketChannel.class); b.option(ChannelOption.SO_KEEPALIVE, true); b.remoteAddress(host, port); b.handler(initializer); // Start the client. this.channel = b.connect().syncUninterruptibly().channel(); System.out.println("Connected to [" + host + ':' + port + ']'); // Wait for the HTTP/2 upgrade to occur. this.connectionHandler = initializer.connectionHandler(); connectionHandler.awaitInitialization(); }
From source file:alluxio.network.connection.NettyChannelPool.java
License:Apache License
/** * Creates a netty channel instance.//from w ww . jav a 2 s . c o m * * @return the channel created * @throws IOException if it fails to create a channel */ @Override protected Channel createNewResource() throws IOException { Bootstrap bs; try { bs = mBootstrap.clone(); } catch (Exception e) { // No exception should happen here. throw Throwables.propagate(e); } try { ChannelFuture channelFuture = bs.connect().sync(); if (channelFuture.isSuccess()) { LOG.info("Created netty channel with netty bootstrap {}.", mBootstrap); return channelFuture.channel(); } else { LOG.error("Failed to create netty channel with netty bootstrap {} and error {}.", mBootstrap, channelFuture.cause().getMessage()); throw new IOException(channelFuture.cause()); } } catch (InterruptedException e) { throw new RuntimeException(e); } }
From source file:alluxio.network.netty.NettyChannelPool.java
License:Apache License
/** * Creates a netty channel instance./*from w w w. j av a 2s . co m*/ * * @return the channel created */ @Override protected Channel createNewResource() throws IOException { Bootstrap bs; bs = mBootstrap.clone(); try { ChannelFuture channelFuture = bs.connect().sync(); if (channelFuture.isSuccess()) { LOG.info("Created netty channel with netty bootstrap {}.", mBootstrap); return channelFuture.channel(); } else { LOG.error("Failed to create netty channel with netty bootstrap {} and error {}.", mBootstrap, channelFuture.cause().getMessage()); throw new UnavailableException(channelFuture.cause()); } } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw new CanceledException(e); } }
From source file:ccwihr.client.t2.Http2Client.java
License:Apache License
public static void main(String[] args) throws Exception { // // Configure SSL. // final SslContext sslCtx; // if (SSL) { // SslProvider provider = OpenSsl.isAlpnSupported() ? SslProvider.OPENSSL : SslProvider.JDK; // sslCtx = SslContextBuilder.forClient() // .sslProvider(provider) // /* NOTE: the cipher filter may not include all ciphers required by the HTTP/2 specification. // * Please refer to the HTTP/2 specification for cipher requirements. */ // .ciphers(Http2SecurityUtil.CIPHERS, SupportedCipherSuiteFilter.INSTANCE) // .trustManager(InsecureTrustManagerFactory.INSTANCE) // .applicationProtocolConfig(new ApplicationProtocolConfig( // Protocol.ALPN, // // NO_ADVERTISE is currently the only mode supported by both OpenSsl and JDK providers. // SelectorFailureBehavior.NO_ADVERTISE, // // ACCEPT is currently the only mode supported by both OpenSsl and JDK providers. // SelectedListenerFailureBehavior.ACCEPT, // ApplicationProtocolNames.HTTP_2, // ApplicationProtocolNames.HTTP_1_1)) // .build(); // } else { // sslCtx = null; // }//www . j av a 2 s.c o m EventLoopGroup workerGroup = new NioEventLoopGroup(); // Http2ClientInitializer initializer = new Http2ClientInitializer(sslCtx, Integer.MAX_VALUE); Http2ClientInitializer initializer = new Http2ClientInitializer(null, Integer.MAX_VALUE); try { // Configure the client. Bootstrap b = new Bootstrap(); b.group(workerGroup); b.channel(NioSocketChannel.class); b.option(ChannelOption.SO_KEEPALIVE, true); b.remoteAddress(HOST, PORT); b.handler(initializer); // Start the client. Channel channel = b.connect().syncUninterruptibly().channel(); System.out.println("Connected to [" + HOST + ':' + PORT + ']'); // Wait for the HTTP/2 upgrade to occur. Http2SettingsHandler http2SettingsHandler = initializer.settingsHandler(); http2SettingsHandler.awaitSettings(5, TimeUnit.SECONDS); HttpResponseHandler responseHandler = initializer.responseHandler(); int streamId = 3; HttpScheme scheme = SSL ? HttpScheme.HTTPS : HttpScheme.HTTP; AsciiString hostName = new AsciiString(HOST + ':' + PORT); System.err.println("Sending request(s)..."); if (URL != null) { // Create a simple GET request. FullHttpRequest request = new DefaultFullHttpRequest(HTTP_1_1, GET, URL); request.headers().add(HttpHeaderNames.HOST, hostName); request.headers().add(HttpConversionUtil.ExtensionHeaderNames.SCHEME.text(), scheme.name()); request.headers().add(HttpHeaderNames.ACCEPT_ENCODING, HttpHeaderValues.GZIP); request.headers().add(HttpHeaderNames.ACCEPT_ENCODING, HttpHeaderValues.DEFLATE); responseHandler.put(streamId, channel.writeAndFlush(request), channel.newPromise()); streamId += 2; } if (URL2 != null) { // Create a simple POST request with a body. FullHttpRequest request = new DefaultFullHttpRequest(HTTP_1_1, POST, URL2, Unpooled.copiedBuffer(URL2DATA.getBytes(CharsetUtil.UTF_8))); request.headers().add(HttpHeaderNames.HOST, hostName); request.headers().add(HttpConversionUtil.ExtensionHeaderNames.SCHEME.text(), scheme.name()); request.headers().add(HttpHeaderNames.ACCEPT_ENCODING, HttpHeaderValues.GZIP); request.headers().add(HttpHeaderNames.ACCEPT_ENCODING, HttpHeaderValues.DEFLATE); responseHandler.put(streamId, channel.writeAndFlush(request), channel.newPromise()); streamId += 2; } responseHandler.awaitResponses(5, TimeUnit.SECONDS); System.out.println("Finished HTTP/2 request(s)"); // Wait until the connection is closed. channel.close().syncUninterruptibly(); } finally { workerGroup.shutdownGracefully(); } }
From source file:club.lovety.xy.netty.test.UptimeClient.java
License:Apache License
static void connect(Bootstrap b) { b.connect().addListener(new ChannelFutureListener() { @Override/*from w w w.j a v a2 s .c o m*/ public void operationComplete(ChannelFuture future) throws Exception { if (future.cause() != null) { handler.startTime = -1; handler.println("Failed to connect: " + future.cause()); } } }); }
From source file:com.doctor.netty5.example.chatroom.ChatRoomClient.java
License:Apache License
public void start() throws InterruptedException { NioEventLoopGroup workersGroup = new NioEventLoopGroup(1); try {/*from w ww. j a va 2 s. c o m*/ Bootstrap bootstrap = new Bootstrap(); bootstrap.group(workersGroup).channel(NioSocketChannel.class).remoteAddress(host, port) .handler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast( new DelimiterBasedFrameDecoder(8049, true, Delimiters.lineDelimiter())); ch.pipeline().addLast(new StringDecoder(StandardCharsets.UTF_8)); ch.pipeline().addLast(new StringEncoder(StandardCharsets.UTF_8)); ch.pipeline().addLast(new ChatRoomClientHandler()); } }); ChannelFuture channelFuture = bootstrap.connect().sync(); channelFuture.channel().closeFuture().sync(); } finally { workersGroup.shutdownGracefully(); } }
From source file:com.doctor.netty5.example.echo.EchoClient.java
License:Apache License
public void start() throws InterruptedException { NioEventLoopGroup workersGroup = new NioEventLoopGroup(1); try {// w ww . j a v a2 s. c o m Bootstrap bootstrap = new Bootstrap(); bootstrap.group(workersGroup).channel(NioSocketChannel.class).remoteAddress(host, port) .handler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new LineBasedFrameDecoder(2048)); ch.pipeline().addLast(new StringDecoder(StandardCharsets.UTF_8)); ch.pipeline().addLast(new StringEncoder(StandardCharsets.UTF_8)); ch.pipeline().addLast(new EchoClientHandler()); } }); ChannelFuture channelFuture = bootstrap.connect().sync(); channelFuture.channel().closeFuture().sync(); } finally { workersGroup.shutdownGracefully(); } }
From source file:com.doctor.netty5.example.echo_object.EchoClient.java
License:Apache License
public void start() throws InterruptedException { NioEventLoopGroup workersGroup = new NioEventLoopGroup(1); try {/* ww w. j a v a 2 s. c om*/ Bootstrap bootstrap = new Bootstrap(); bootstrap.group(workersGroup).channel(NioSocketChannel.class).remoteAddress(host, port) .handler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new ObjectEncoder()); ch.pipeline().addLast(new ObjectDecoder( ClassResolvers.softCachingResolver(this.getClass().getClassLoader()))); ch.pipeline().addLast(new EchoClientHandler()); } }); ChannelFuture channelFuture = bootstrap.connect().sync(); channelFuture.channel().closeFuture().sync(); } finally { workersGroup.shutdownGracefully(); } }
From source file:com.doctor.netty5.example.factorial_algorithm.FactorialClient.java
License:Apache License
public void start() throws InterruptedException { NioEventLoopGroup workersGroup = new NioEventLoopGroup(1); try {/*from www. ja v a 2s . co m*/ Bootstrap bootstrap = new Bootstrap(); bootstrap.group(workersGroup).channel(NioSocketChannel.class).remoteAddress(host, port) .handler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new NumberEncoder()); ch.pipeline().addLast(new BigIntegerDecoder()); ch.pipeline().addLast(new FactorialClientHandler()); } }); ChannelFuture channelFuture = bootstrap.connect().sync(); channelFuture.channel().closeFuture().sync(); } finally { workersGroup.shutdownGracefully(); } }
From source file:com.fjn.helper.frameworkex.netty.v4.echotest.EchoClient.java
License:Apache License
public void start() throws Exception { EventLoopGroup group = new NioEventLoopGroup(); try {/* w ww . j av a2 s .com*/ Bootstrap b = new Bootstrap(); b.group(group); b.channel(NioSocketChannel.class); b.remoteAddress(new InetSocketAddress(host, port)); b.handler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new EchoClientHandler()); } }); ChannelFuture f = (ChannelFuture) b.connect().sync(); f.channel().closeFuture().sync(); } finally { group.shutdownGracefully().sync(); } }