List of usage examples for io.netty.channel ChannelFuture channel
Channel channel();
From source file:com.example.discard.DiscardServerMain.java
License:Apache License
public void run() throws Exception { final NioEventLoopGroup bossGroup = new NioEventLoopGroup();//1 final NioEventLoopGroup workerGroup = new NioEventLoopGroup(); try (AutoCloseable ignore = new ShutDownGracefully(bossGroup, workerGroup)) { final ServerBootstrap bootstrap = new ServerBootstrap();//2 bootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)//3 .childHandler(new ChannelInitializer<SocketChannel>() {//4 @Override/*from w w w .ja va 2 s . c o m*/ protected void initChannel(final SocketChannel ch) { ch.pipeline().addLast(new DiscardServerHandler()); } }).option(ChannelOption.SO_BACKLOG, 128)//5 .childOption(ChannelOption.SO_KEEPALIVE, true); //6 final ChannelFuture channelFuture = bootstrap.bind(port).sync();//7 channelFuture.channel().closeFuture().sync(); } }
From source file:com.example.pojo.server.ServerTimeServerHandler.java
License:Apache License
@Override public void channelActive(final ChannelHandlerContext ctx) { final ServerTime serverTime = new ServerTime(offset); log.info("channel active: {}", serverTime); final ChannelFuture channelFuture = ctx.writeAndFlush(serverTime); channelFuture.addListener((ChannelFuture future) -> { log.info("close channel."); future.channel().closeFuture(); });/*ww w . ja v a 2 s .co m*/ }
From source file:com.example.server.ServerMain.java
License:Apache License
public void run() throws InterruptedException { final NioEventLoopGroup parentGroup = new NioEventLoopGroup(); final NioEventLoopGroup workerGroup = new NioEventLoopGroup(); try (final GracefulShutdown ignored = GracefulShutdown.gracefulShutdown(parentGroup::shutdownGracefully, workerGroup::shutdownGracefully)) { final ServerBootstrap bootstrap = new ServerBootstrap(); bootstrap.group(parentGroup, workerGroup).channel(NioServerSocketChannel.class) .childHandler(ServerChannelInitializationConfigurer.channelInitializer()) .option(ChannelOption.SO_BACKLOG, 128).childOption(ChannelOption.SO_KEEPALIVE, true); log.info("server start"); final ChannelFuture channelFuture = bootstrap.bind(port).sync(); channelFuture.channel().closeFuture().sync(); }/*w ww. j ava 2 s . co m*/ }
From source file:com.example.spring.boot.netty.TcpServer.java
License:Apache License
public void bind() { EventLoopGroup bossGroup = new NioEventLoopGroup(); EventLoopGroup workerGroup = new NioEventLoopGroup(); try {//www . j a v a 2s. co m ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .option(ChannelOption.SO_BACKLOG, 65535).handler(new LoggingHandler(LogLevel.INFO)) .childHandler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { ChannelPipeline p = ch.pipeline(); //p.addLast(new LoggingHandler(LogLevel.INFO)); p.addLast(new LineBasedFrameDecoder(1024)); p.addLast(new StringDecoder()); p.addLast(TCP_SERVER_HANDLER); } }); // Start the server. ChannelFuture f = b.bind(port).sync(); // Wait until the server socket is closed. f.channel().closeFuture().sync(); } catch (Throwable e) { e.printStackTrace(); } finally { // Shut down all event loops to terminate all threads. bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } }
From source file:com.farsunset.cim.sdk.android.CIMConnectorManager.java
License:Apache License
public void connect(final String host, final int port) { if (!isNetworkConnected(context)) { Intent intent = new Intent(); intent.setAction(CIMConstant.IntentAction.ACTION_CONNECTION_FAILED); intent.putExtra(Exception.class.getName(), NetworkDisabledException.class.getSimpleName()); context.sendBroadcast(intent);/* w w w . j a v a 2s . co m*/ return; } if (isConnected() || !semaphore.tryAcquire()) { return; } executor.execute(new Runnable() { @Override public void run() { final InetSocketAddress remoteAddress = new InetSocketAddress(host, port); bootstrap.connect(remoteAddress).addListener(new GenericFutureListener<ChannelFuture>() { @Override public void operationComplete(ChannelFuture future) { semaphore.release(); future.removeListener(this); if (!future.isSuccess() && future.cause() != null) { handleConnectFailure(future.cause(), remoteAddress); } if (future.isSuccess()) { channel = future.channel(); } } }); } }); }
From source file:com.farsunset.cim.sdk.client.CIMConnectorManager.java
License:Apache License
public void connect(final String host, final int port) { if (isConnected() || !semaphore.tryAcquire()) { return;//from ww w . j a va 2 s. c o m } executor.execute(new Runnable() { @Override public void run() { logger.info("****************CIM? " + host + ":" + port + "......"); final InetSocketAddress remoteAddress = new InetSocketAddress(host, port); bootstrap.connect(remoteAddress).addListener(new GenericFutureListener<ChannelFuture>() { @Override public void operationComplete(ChannelFuture future) throws Exception { semaphore.release(); future.removeListener(this); if (!future.isSuccess() && future.cause() != null) { handleConnectFailure(future.cause(), remoteAddress); } if (future.isSuccess()) { channel = future.channel(); } } }); } }); }
From source file:com.feihong.newzxclient.tcp.NettyClient.java
License:Apache License
@SuppressWarnings("unchecked") public void connect() throws Exception { mGroup = new NioEventLoopGroup(); Bootstrap b = new Bootstrap(); b.group(mGroup).channel(NioSocketChannel.class).option(ChannelOption.TCP_NODELAY, true) .handler(new ChannelInitializer<SocketChannel>() { @Override/*from w w w . j a v a 2 s . c om*/ public void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast("decoder", new IntLengthDecoder()); ch.pipeline().addLast(new NettyClientHandler()); } }); ChannelFuture future = b.connect(host, port).sync(); future.addListeners(new ChannelFutureListener() { public void operationComplete(final ChannelFuture future) throws Exception { requestLogin(); } }); mChannel = future.channel(); mHandler = mChannel.pipeline().get(NettyClientHandler.class); }
From source file:com.fjn.helper.frameworkex.netty.v4.discardserver.DiscardServer.java
License:Apache License
public void run() throws Exception { EventLoopGroup bossGroup = new NioEventLoopGroup(); // (1) EventLoopGroup workerGroup = new NioEventLoopGroup(); try {/* w w w. j a v a 2 s .c om*/ ServerBootstrap b = new ServerBootstrap(); // (2) b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) // (3) .childHandler(new ChannelInitializer<SocketChannel>() { // (4) @Override public void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new DiscardServerHandler()); } }).option(ChannelOption.SO_BACKLOG, 128) // (5) .childOption(ChannelOption.SO_KEEPALIVE, true); // (6) // Bind and start to accept incoming connections. ChannelFuture f = b.bind(port).sync(); // (7) // Wait until the server socket is closed. // In this example, this does not happen, but you can do that to // gracefully // shut down your server. f.channel().closeFuture().sync(); } finally { workerGroup.shutdownGracefully(); bossGroup.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 {/*from ww w . j a v a2 s .c om*/ 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(); } }
From source file:com.fjn.helper.frameworkex.netty.v4.echotest.EchoServer.java
License:Apache License
public void start() throws Exception { EventLoopGroup group = new NioEventLoopGroup(); try {/* www . jav a 2s . c om*/ // Bootstraps the server ServerBootstrap b = new ServerBootstrap(); // Specifies NIO transport, local socket address b.group(group); b.childGroup(); b.channel(NioServerSocketChannel.class); b.localAddress(new InetSocketAddress(port)); // Adds handler to channel pipeline b.childHandler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new EchoServerHandler()); } }); // Binds server, waits for server to close, and releases resources ChannelFuture f = b.bind().sync(); System.out.println(EchoServer.class.getName() + " started and listen on " + f.channel().localAddress()); f.channel().closeFuture().sync(); } finally { group.shutdownGracefully().sync(); } }