List of usage examples for io.netty.channel.nio NioEventLoopGroup NioEventLoopGroup
public NioEventLoopGroup()
From source file:com.ctrip.xpipe.redis.console.health.netty.EchoClient.java
License:Apache License
public static void main(String[] args) throws Exception { // Configure SSL.git final SslContext sslCtx; if (SSL) {/*from w w w. ja va2 s .c o m*/ sslCtx = SslContextBuilder.forClient().trustManager(InsecureTrustManagerFactory.INSTANCE).build(); } else { sslCtx = null; } // Configure the client. EventLoopGroup group = new NioEventLoopGroup(); try { Bootstrap b = new Bootstrap(); b.group(group).channel(NioSocketChannel.class).option(ChannelOption.TCP_NODELAY, true) .handler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { ChannelPipeline p = ch.pipeline(); if (sslCtx != null) { p.addLast(sslCtx.newHandler(ch.alloc(), HOST, PORT)); } // p.addLast(new LoggingHandler(LogLevel.INFO)); p.addLast(new EchoClientHandler()); } }); // Start the client. ChannelFuture f = b.connect(HOST, PORT); Channel c = f.channel(); while (true) { ByteBuf buf = Unpooled.buffer(8); buf.writeLong(System.nanoTime()); c.writeAndFlush(buf); Thread.sleep(1000); } } finally { // Shut down the event loop to terminate all threads. group.shutdownGracefully(); } }
From source file:com.datastax.driver.core.NettyOptionsTest.java
License:Apache License
private void should_invoke_netty_options_hooks(int hosts, int coreConnections) throws Exception { NettyOptions nettyOptions = mock(NettyOptions.class, CALLS_REAL_METHODS.get()); EventLoopGroup eventLoopGroup = new NioEventLoopGroup(); Timer timer = new HashedWheelTimer(); doReturn(eventLoopGroup).when(nettyOptions).eventLoopGroup(any(ThreadFactory.class)); doReturn(timer).when(nettyOptions).timer(any(ThreadFactory.class)); final ChannelHandler handler = mock(ChannelHandler.class); doAnswer(new Answer() { @Override/*from w ww. j ava 2 s . com*/ public Object answer(InvocationOnMock invocation) throws Throwable { SocketChannel channel = (SocketChannel) invocation.getArguments()[0]; channel.pipeline().addLast("test-handler", handler); return null; } }).when(nettyOptions).afterChannelInitialized(any(SocketChannel.class)); Cluster cluster = register(Cluster.builder().addContactPoints(getContactPoints().get(0)) .withPort(ccm().getBinaryPort()).withPoolingOptions(new PoolingOptions() .setConnectionsPerHost(HostDistance.LOCAL, coreConnections, coreConnections)) .withNettyOptions(nettyOptions).build()); // when cluster.connect();// force session creation to populate pools int expectedNumberOfCalls = TestUtils.numberOfLocalCoreConnections(cluster) * hosts + 1; // If the driver supports a more recent protocol version than C*, the negotiation at startup // will open 1 extra connection. if (!ProtocolVersion.NEWEST_SUPPORTED.isSupportedBy(TestUtils.findHost(cluster, 1))) expectedNumberOfCalls += 1; cluster.close(); // then verify(nettyOptions, times(1)).eventLoopGroup(any(ThreadFactory.class)); verify(nettyOptions, times(1)).channelClass(); verify(nettyOptions, times(1)).timer(any(ThreadFactory.class)); // per-connection hooks will be called coreConnections * hosts + 1 times: // the extra call is for the control connection verify(nettyOptions, times(expectedNumberOfCalls)).afterBootstrapInitialized(any(Bootstrap.class)); verify(nettyOptions, times(expectedNumberOfCalls)).afterChannelInitialized(any(SocketChannel.class)); verify(handler, times(expectedNumberOfCalls)).handlerAdded(any(ChannelHandlerContext.class)); verify(handler, times(expectedNumberOfCalls)).handlerRemoved(any(ChannelHandlerContext.class)); verify(nettyOptions, times(1)).onClusterClose(eventLoopGroup); verify(nettyOptions, times(1)).onClusterClose(timer); verifyNoMoreInteractions(nettyOptions); }
From source file:com.digisky.innerproxy.testclient.HttpSnoopClient.java
License:Apache License
public static void main(String[] args) throws Exception { uri = new URI(URL); String scheme = uri.getScheme() == null ? "http" : uri.getScheme(); int port = uri.getPort(); if (port == -1) { if ("http".equalsIgnoreCase(scheme)) { port = 80;/*from w w w . j ava 2 s.c o m*/ } else if ("https".equalsIgnoreCase(scheme)) { port = 443; } } if (!"http".equalsIgnoreCase(scheme) && !"https".equalsIgnoreCase(scheme)) { System.err.println("Only HTTP(S) is supported."); return; } // Configure SSL context if necessary. final boolean ssl = "https".equalsIgnoreCase(scheme); final SslContext sslCtx; if (ssl) { sslCtx = SslContext.newClientContext(InsecureTrustManagerFactory.INSTANCE); } else { sslCtx = null; } // Configure the client. EventLoopGroup group = new NioEventLoopGroup(); try { Bootstrap b = new Bootstrap(); b.group(group).channel(NioSocketChannel.class).handler(new HttpSnoopClientInitializer(sslCtx)); // Make the connection attempt. // Channel ch = b.connect(host, port).sync().channel(); TestTimer.add("main()::85"); //test(ch, "username_register", R_username_register.j); //test(ch, "normal_login", R_normal_login.j_username); //test(ch, "normal_login", R_normal_login.j_openid); //test(ch, "normal_login", R_normal_login.j_telphone); //test(ch, "normal_login", R_normal_login.j_devid); //test(ch, "normal_login", R_normal_login.j_email); //test(ch, "email_bind", R_email_bind.j); //test(ch, "telphone_bind", R_telphone_bind.j); //test(ch, "req_telphone_vcode", R_req_telphone_vcode.j); //test(ch, "auth", R_auth.j); //test(ch, "telphone_register", R_register.j_telphone_register); //test(ch, "email_register", R_register.j_email_register); //test(ch, "req_register_mode", R_req_register_mode.j); //test(ch, "identify_exist", R_identify_exist.j); } finally { group.shutdownGracefully(); } }
From source file:com.digisky.outerproxy.testclient.HttpSnoopClient.java
License:Apache License
public static void main(String[] args) throws Exception { uri = new URI(URL); String scheme = uri.getScheme() == null ? "http" : uri.getScheme(); int port = uri.getPort(); if (port == -1) { if ("http".equalsIgnoreCase(scheme)) { port = 80;/*w ww.j a v a 2 s .c o m*/ } else if ("https".equalsIgnoreCase(scheme)) { port = 443; } } if (!"http".equalsIgnoreCase(scheme) && !"https".equalsIgnoreCase(scheme)) { System.err.println("Only HTTP(S) is supported."); return; } // Configure SSL context if necessary. final boolean ssl = "https".equalsIgnoreCase(scheme); final SslContext sslCtx; if (ssl) { sslCtx = SslContext.newClientContext(InsecureTrustManagerFactory.INSTANCE); } else { sslCtx = null; } // Configure the client. EventLoopGroup group = new NioEventLoopGroup(); try { Bootstrap b = new Bootstrap(); b.group(group).channel(NioSocketChannel.class).handler(new HttpSnoopClientInitializer(sslCtx)); // Make the connection attempt. Channel ch = b.connect("192.168.60.163", 8089).sync().channel(); TestTimer.add("main()::85"); //test(ch, "username_register", R_username_register.j); //test(ch, "normal_login", R_normal_login.j_username); //test(ch, "normal_login", R_normal_login.j_openid); //test(ch, "normal_login", R_normal_login.j_telphone); //test(ch, "normal_login", R_normal_login.j_devid); //test(ch, "normal_login", R_normal_login.j_email); //test(ch, "email_bind", R_email_bind.j); //test(ch, "telphone_bind", R_telphone_bind.j); //test(ch, "req_telphone_vcode", R_req_telphone_vcode.j); //test(ch, "auth", R_auth.j); //test(ch, "telphone_register", R_register.j_telphone_register); //test(ch, "email_register", R_register.j_email_register); //test(ch, "req_register_mode", R_req_register_mode.j); //test(ch, "identify_exist", R_identify_exist.j); test(ch, "third_login", R_identify_exist.j); } finally { group.shutdownGracefully(); } }
From source file:com.digisky.stresstest.HttpSnoopClient.java
License:Apache License
public static void main(String[] args) throws Exception { uri = new URI(URL); String scheme = uri.getScheme() == null ? "http" : uri.getScheme(); String host = uri.getHost() == null ? "127.0.0.1" : uri.getHost(); int port = uri.getPort(); if (port == -1) { if ("http".equalsIgnoreCase(scheme)) { port = 80;/* w w w . j av a2 s .co m*/ } else if ("https".equalsIgnoreCase(scheme)) { port = 443; } } if (!"http".equalsIgnoreCase(scheme) && !"https".equalsIgnoreCase(scheme)) { System.err.println("Only HTTP(S) is supported."); return; } // Configure SSL context if necessary. final boolean ssl = "https".equalsIgnoreCase(scheme); final SslContext sslCtx; if (ssl) { sslCtx = SslContext.newClientContext(InsecureTrustManagerFactory.INSTANCE); } else { sslCtx = null; } // Configure the client. EventLoopGroup group = new NioEventLoopGroup(); try { Bootstrap b = new Bootstrap(); b.group(group).channel(NioSocketChannel.class).handler(new HttpSnoopClientInitializer(sslCtx)); // Make the connection attempt. System.out.println("try to connect!"); ChannelFuture f = b.connect(host, port); f.addListener(new ChannelFutureListener() { public void operationComplete(ChannelFuture future) { // Perform post-closure operation // ... System.out.println("complete!"); if (!future.isSuccess()) { // You might get a NullPointerException here because the future // might not be completed yet. future.cause().printStackTrace(); } else { System.out.println("ok!!!!"); } } }); } finally { group.shutdownGracefully(); } }
From source file:com.dingwang.netty.client.DiscardClient.java
License:Open Source License
public void run() { EventLoopGroup workerGroup = new NioEventLoopGroup(); //BootStrapServerBootstrap,???channel?channel Bootstrap b = new Bootstrap(); //?EventLoopGroup?bossworkder //??boss// www . ja va2 s . c o m b.group(workerGroup); //NioServerSocketChannelNioSocketChannel,channel b.channel(NioSocketChannel.class); //??ServerBootstrap?childOption()SocketChannelchannel b.option(ChannelOption.SO_KEEPALIVE, true); b.handler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new TimeDecoder(), new TimeClientHandler()); } }); ChannelFuture f; try { //connect()bind() f = b.connect(host, port).sync(); f.channel().writeAndFlush("dddddd"); f.channel().closeFuture().sync(); } catch (InterruptedException e) { e.printStackTrace(); } finally { workerGroup.shutdownGracefully(); } }
From source file:com.dingwang.rpc.client.RpcClient.java
License:Open Source License
public RpcResponse send(RpcRequest request) throws Exception { EventLoopGroup group = new NioEventLoopGroup(); try {//from ww w.j a v a 2 s.co m Bootstrap bootstrap = new Bootstrap(); bootstrap.group(group).channel(NioSocketChannel.class).handler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel channel) throws Exception { channel.pipeline().addLast(new RpcEncoder(RpcRequest.class)) // RPC ??? .addLast(new RpcDecoder(RpcResponse.class)) // RPC ???? .addLast(RpcClient.this); // RpcClient ?? RPC } }).option(ChannelOption.SO_KEEPALIVE, true); ChannelFuture future = bootstrap.connect(host, port).sync(); future.channel().writeAndFlush(request).sync(); synchronized (obj) { obj.wait(); // ? } if (response != null) { future.channel().closeFuture().sync(); } return response; } finally { group.shutdownGracefully(); } }
From source file:com.dingwang.rpc.server.RpcServer.java
License:Open Source License
@Override public void afterPropertiesSet() throws Exception { EventLoopGroup bossGroup = new NioEventLoopGroup(); EventLoopGroup workerGroup = new NioEventLoopGroup(); try {/*from w w w . ja va 2 s. com*/ ServerBootstrap bootstrap = new ServerBootstrap(); bootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .childHandler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel channel) throws Exception { channel.pipeline().addLast(new RpcDecoder(RpcRequest.class)) // RPC ?? .addLast(new RpcEncoder(RpcResponse.class)) // RPC ??? // .addLast(new ProviderProxy()); // ? RPC .addLast(new RpcHandler(handlerMap)); } }).option(ChannelOption.SO_BACKLOG, 128).childOption(ChannelOption.SO_KEEPALIVE, true); String[] array = serverAddress.split(":"); String host = array[0]; int port = Integer.parseInt(array[1]); ChannelFuture future = bootstrap.bind(host, port).sync(); LOGGER.debug("server started on port {}", port); if (serviceRegistry != null) { serviceRegistry.register(serverAddress); // ?? } future.channel().closeFuture().sync(); } finally { workerGroup.shutdownGracefully(); bossGroup.shutdownGracefully(); } }
From source file:com.dinstone.netty.Client.java
License:Apache License
public static void main(String[] args) throws IOException, InterruptedException { Bootstrap b = new Bootstrap(); b.group(new NioEventLoopGroup()).channel(NioSocketChannel.class) .handler(new ChannelInitializer<NioSocketChannel>() { @Override/* w ww. j a v a 2s . c o m*/ protected void initChannel(NioSocketChannel ch) throws Exception { ch.pipeline().addLast("dd", new ChannelHandlerAdapter() { /** * {@inheritDoc} * * @see io.netty.channel.ChannelHandlerAdapter#exceptionCaught(io.netty.channel.ChannelHandlerContext, * java.lang.Throwable) */ @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { System.out.println("error: "); cause.printStackTrace(); } }); } }); b.connect("localhost", 8090).addListener(new ChannelFutureListener() { public void operationComplete(ChannelFuture future) throws Exception { if (future.isSuccess()) { future.channel().write(Unpooled.buffer().writeBytes("123".getBytes())); future.channel().flush(); } } }); }
From source file:com.dinstone.rpc.netty.client.NettyConnector.java
License:Apache License
public NettyConnector(Configuration config) { workerGroup = new NioEventLoopGroup(); boot = new Bootstrap().group(workerGroup).channel(NioSocketChannel.class); boot.option(ChannelOption.SO_KEEPALIVE, true); boot.handler(new ChannelInitializer<SocketChannel>() { @Override//ww w . j a va 2s.c o m public void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new RpcProtocolDecoder(false)); ch.pipeline().addLast(new RpcProtocolEncoder(false)); ch.pipeline().addLast(new NettyClientHandler()); } }); String host = config.get(Constants.SERVICE_HOST, Constants.DEFAULT_SERVICE_HOST); int port = config.getInt(Constants.SERVICE_PORT, 7777); boot.remoteAddress(new InetSocketAddress(host, port)); }