List of usage examples for io.netty.channel ChannelFuture channel
Channel channel();
From source file:com.book.netty5.client.NettyClient.java
License:Apache License
public void connect(int port, String host) throws Exception { // ?NIO//from w w w .ja v a 2 s . c om 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 { ch.pipeline().addLast(new NettyMessageDecoder(1024 * 1024, 4, 4)); ch.pipeline().addLast("MessageEncoder", new NettyMessageEncoder()); ch.pipeline().addLast("readTimeoutHandler", new ReadTimeoutHandler(50)); ch.pipeline().addLast("LoginAuthHandler", new LoginAuthReqHandler()); ch.pipeline().addLast("HeartBeatHandler", new HeartBeatReqHandler()); } }); // ?? ChannelFuture future = b.connect(new InetSocketAddress(host, port), new InetSocketAddress(NettyConstant.LOCALIP, NettyConstant.LOCAL_PORT)).sync(); future.channel().closeFuture().sync(); } finally { // ???????? executor.execute(new Runnable() { public void run() { try { TimeUnit.SECONDS.sleep(1); try { connect(NettyConstant.PORT, NettyConstant.REMOTEIP);// ??? } catch (Exception e) { e.printStackTrace(); } } catch (InterruptedException e) { e.printStackTrace(); } } }); } }
From source file:com.bow.demo.module.netty.demo.echo.EchoClient.java
License:Apache License
public static void main(String[] args) throws Exception { // Configure SSL.git final SslContext sslCtx; if (SSL) {//from www . j a va 2 s . com 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).sync(); // Wait until the connection is closed. f.channel().closeFuture().sync(); } finally { // Shut down the event loop to terminate all threads. group.shutdownGracefully(); } }
From source file:com.bow.demo.module.netty.demo.echo.EchoServer.java
License:Apache License
public static void main(String[] args) throws Exception { // Configure SSL. final SslContext sslCtx; if (SSL) {/*w w w . j a v a 2 s .co m*/ SelfSignedCertificate ssc = new SelfSignedCertificate(); sslCtx = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).build(); } else { sslCtx = null; } // Configure the server. EventLoopGroup bossGroup = new NioEventLoopGroup(1); EventLoopGroup workerGroup = new NioEventLoopGroup(); try { ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .option(ChannelOption.SO_BACKLOG, 100).handler(new LoggingHandler(LogLevel.INFO)) .childHandler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { ChannelPipeline p = ch.pipeline(); if (sslCtx != null) { p.addLast(sslCtx.newHandler(ch.alloc())); } //p.addLast(new LoggingHandler(LogLevel.INFO)); p.addLast(new EchoServerHandler()); } }); // Start the server. ChannelFuture f = b.bind(PORT).sync(); // Wait until the server socket is closed. f.channel().closeFuture().sync(); } finally { // Shut down all event loops to terminate all threads. bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } }
From source file:com.caocao.nio.client.EchoClient.java
License:Apache License
public static void main(String[] args) throws Exception { // Configure the client. EventLoopGroup group = new NioEventLoopGroup(); try {/*from w w w .j a va 2s . c om*/ 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(); p.addLast(new MsgDecoder()); p.addLast(new MsgEncoder()); p.addLast(new EchoClientHandler()); } }); // Start the client. ChannelFuture f = b.connect("127.0.0.1", 80).sync(); // Wait until the connection is closed. f.channel().closeFuture().sync(); } finally { // Shut down the event loop to terminate all threads. group.shutdownGracefully(); } }
From source file:com.caocao.nio.server.NettyServer.java
License:Apache License
public void initAndStart() { System.out.println("port:" + port); // Configure the server. bossGroup = new NioEventLoopGroup(Runtime.getRuntime().availableProcessors() * 2); workerGroup = new NioEventLoopGroup(Runtime.getRuntime().availableProcessors() * 2); try {/*ww w. j a v a2s . c om*/ ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .option(ChannelOption.SO_BACKLOG, 128).option(ChannelOption.SO_KEEPALIVE, true) .option(ChannelOption.TCP_NODELAY, true).option(ChannelOption.SO_SNDBUF, 5 * 1024) .option(ChannelOption.SO_SNDBUF, 5 * 1024) .option(ChannelOption.RCVBUF_ALLOCATOR, new AdaptiveRecvByteBufAllocator(40, 64, 1024)) .option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT) .handler(new LoggingHandler(LogLevel.INFO)).childHandler(new CustomerInitializer()); // Start the server. ChannelFuture f = b.bind(port).sync(); // Wait until the server socket is closed. f.channel().closeFuture().sync(); } catch (InterruptedException e) { logger.error("netty??", e); } finally { // Shut down all event loops to terminate all threads. bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } }
From source file:com.caricah.iotracah.server.netty.channelgroup.IotChannelGroupFuture.java
License:Apache License
/** * Creates a new instance.//from w ww . j av a 2s. co m */ IotChannelGroupFuture(ChannelGroup group, Collection<ChannelFuture> futures, EventExecutor executor) { super(executor); if (group == null) { throw new NullPointerException("group"); } if (futures == null) { throw new NullPointerException("futures"); } this.group = group; Map<Channel, ChannelFuture> futureMap = new LinkedHashMap<Channel, ChannelFuture>(); for (ChannelFuture f : futures) { futureMap.put(f.channel(), f); } this.futures = Collections.unmodifiableMap(futureMap); for (ChannelFuture f : this.futures.values()) { f.addListener(childListener); } // Done on arrival? if (this.futures.isEmpty()) { setSuccess0(); } }
From source file:com.caricah.iotracah.server.netty.ServerImpl.java
License:Apache License
/** * The @link configure method is responsible for starting the implementation server processes. * The implementation should return once the server has started this allows * the launcher to maintain the life of the application. * * @throws UnRetriableException/* ww w . j a v a2 s.com*/ */ public void initiate() throws UnRetriableException { log.info(" configure : initiating the netty server."); try { int countOfAvailableProcessors = Runtime.getRuntime().availableProcessors() + 1; if (Epoll.isAvailable()) { bossEventLoopGroup = new EpollEventLoopGroup(2, getExecutorService()); workerEventLoopGroup = new EpollEventLoopGroup(countOfAvailableProcessors, getExecutorService()); } else { bossEventLoopGroup = new NioEventLoopGroup(2, getExecutorService()); workerEventLoopGroup = new NioEventLoopGroup(countOfAvailableProcessors, getExecutorService()); } //Initialize listener for TCP ServerBootstrap tcpBootstrap = new ServerBootstrap(); tcpBootstrap.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT); tcpBootstrap.childOption(ChannelOption.WRITE_BUFFER_HIGH_WATER_MARK, 32 * 1024); tcpBootstrap.childOption(ChannelOption.WRITE_BUFFER_LOW_WATER_MARK, 8 * 1024); tcpBootstrap = tcpBootstrap.group(bossEventLoopGroup, workerEventLoopGroup); if (Epoll.isAvailable()) { tcpBootstrap = tcpBootstrap.channel(EpollServerSocketChannel.class); } else { tcpBootstrap = tcpBootstrap.channel(NioServerSocketChannel.class); } tcpBootstrap = tcpBootstrap.handler(new LoggingHandler(LogLevel.INFO)) .childHandler(getServerInitializer(this, getConnectionTimeout())); ChannelFuture tcpChannelFuture = tcpBootstrap.bind(getTcpPort()).sync(); tcpChannel = tcpChannelFuture.channel(); if (isSslEnabled()) { //Initialize listener for SSL ServerBootstrap sslBootstrap = new ServerBootstrap(); sslBootstrap.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT); sslBootstrap.childOption(ChannelOption.WRITE_BUFFER_HIGH_WATER_MARK, 32 * 1024); sslBootstrap.childOption(ChannelOption.WRITE_BUFFER_LOW_WATER_MARK, 8 * 1024); sslBootstrap = sslBootstrap.group(bossEventLoopGroup, workerEventLoopGroup); if (Epoll.isAvailable()) { sslBootstrap = sslBootstrap.channel(EpollServerSocketChannel.class); } else { sslBootstrap = sslBootstrap.channel(NioServerSocketChannel.class); } sslBootstrap = sslBootstrap.handler(new LoggingHandler(LogLevel.INFO)) .childHandler(getServerInitializer(this, getConnectionTimeout(), getSslHandler())); ChannelFuture sslChannelFuture = sslBootstrap.bind(getSslPort()).sync(); sslChannel = sslChannelFuture.channel(); } } catch (InterruptedException e) { log.error(" configure : Initialization issues ", e); throw new UnRetriableException(e); } }
From source file:com.cat.netty.file.FileServer.java
License:Apache License
public void run(int port) throws Exception { EventLoopGroup bossGroup = new NioEventLoopGroup(); EventLoopGroup workerGroup = new NioEventLoopGroup(); try {/*from w w w . j a v a 2 s .c o m*/ ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .option(ChannelOption.SO_BACKLOG, 100).childHandler(new ChannelInitializer<SocketChannel>() { /* * (non-Javadoc) * * @see * io.netty.channel.ChannelInitializer#initChannel(io * .netty.channel.Channel) */ public void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new StringEncoder(CharsetUtil.UTF_8), new LineBasedFrameDecoder(1024), new StringDecoder(CharsetUtil.UTF_8), new FileServerHandler()); } }); ChannelFuture f = b.bind(port).sync(); System.out.println("Server start at port : " + port); f.channel().closeFuture().sync(); } finally { // ? bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } }
From source file:com.cdg.study.netty.discard.DiscardServer.java
License:Open Source License
public static void main(String[] args) throws Exception { EventLoopGroup bossGroup = new NioEventLoopGroup(1); // bossGroup 1? ?? EventLoopGroup workerGroup = new NioEventLoopGroup(); // default *2 ?? try {// ww w . j ava 2s .c o m ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .handler(new LoggingHandler(LogLevel.INFO)).childHandler(new DiscardServerHandler()); ChannelFuture f = b.bind(8010).sync(); System.err.println("Ready for 0.0.0.0:8010"); f.channel().closeFuture().sync(); } finally { workerGroup.shutdownGracefully(); bossGroup.shutdownGracefully(); } }
From source file:com.cdg.study.netty.echo.EchoServer.java
License:Open Source License
public static void main(String[] args) throws Exception { EventLoopGroup bossGroup = new NioEventLoopGroup(1); EventLoopGroup workerGroup = new NioEventLoopGroup(); try {//from w w w. j av a 2 s .co m ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .childHandler(new EchoServerHandler()); ChannelFuture f = b.bind(8011).sync(); f.channel().closeFuture().sync(); } finally { workerGroup.shutdownGracefully(); bossGroup.shutdownGracefully(); } }