List of usage examples for io.netty.channel ChannelOption SO_KEEPALIVE
ChannelOption SO_KEEPALIVE
To view the source code for io.netty.channel ChannelOption SO_KEEPALIVE.
Click Source Link
From source file:com.celeral.netlet.benchmark.netty.BenchmarkTcpClient.java
License:Apache License
public static void main(String[] args) { String host = args[0];/* w ww .j ava2 s .com*/ int port = Integer.parseInt(args[1]); EventLoopGroup workerGroup = new NioEventLoopGroup(); try { Bootstrap b = new Bootstrap(); // (1) b.group(workerGroup); // (2) b.channel(NioSocketChannel.class); // (3) b.option(ChannelOption.SO_KEEPALIVE, true); // (4) b.handler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new BenchmarkTcpClient()); } }); // Start the client. ChannelFuture f = b.connect(host, port).sync(); // (5) // Wait until the connection is closed. f.channel().closeFuture().sync(); } catch (Exception e) { logger.error("", e); } finally { workerGroup.shutdownGracefully(); } }
From source file:com.celeral.netlet.benchmark.netty.EchoTcpServer.java
License:Apache License
public static void main(String[] args) throws Exception { int port;//from w w w. j a va2 s.c o m if (args.length > 0) { port = Integer.parseInt(args[0]); } else { port = 8080; } EventLoopGroup bossGroup = new NioEventLoopGroup(); EventLoopGroup workerGroup = new NioEventLoopGroup(); try { ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .childHandler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new EchoTcpServer()); } }).option(ChannelOption.SO_BACKLOG, 128).childOption(ChannelOption.SO_KEEPALIVE, true); ChannelFuture f = b.bind(port).sync(); // (7) f.channel().closeFuture().sync(); } finally { workerGroup.shutdownGracefully(); bossGroup.shutdownGracefully(); } }
From source file:com.chen.opensourceframework.netty.copy.DiscardServer.java
License:Apache License
public static void main(String[] args) throws Exception { EventLoopGroup bossGroup = new NioEventLoopGroup(); EventLoopGroup workerGroup = new NioEventLoopGroup(); try {/*from w w w .j av a 2s. com*/ ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .handler(new LoggingHandler(LogLevel.INFO)) .childHandler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) { ChannelPipeline p = ch.pipeline(); p.addLast(new DiscardServerHandler()); } }).option(ChannelOption.SO_BACKLOG, 128) // (5) .childOption(ChannelOption.SO_KEEPALIVE, true); // (6); ChannelFuture f = b.bind(PORT).sync(); f.channel().closeFuture().sync(); } finally { workerGroup.shutdownGracefully(); bossGroup.shutdownGracefully(); } }
From source file:com.chicm.cmraft.rpc.RpcClient.java
License:Apache License
private ChannelHandlerContext connectRemoteServer() throws InterruptedException { EventLoopGroup workerGroup = new NioEventLoopGroup(); try {//w w w. jav a2 s . c om ClientChannelHandler channelHandler = new ClientChannelHandler(listener); Bootstrap b = new Bootstrap(); b.group(workerGroup); b.channel(NioSocketChannel.class); b.option(ChannelOption.SO_KEEPALIVE, true); b.handler(channelHandler); ChannelFuture f = b.connect(getRemoteServer().getHost(), getRemoteServer().getPort()).sync(); LOG.debug("connected to: " + this.getRemoteServer()); return channelHandler.getCtx(); // Wait until the connection is closed. //f.channel().closeFuture().sync(); } finally { //workerGroup.shutdownGracefully(); } }
From source file:com.cloudera.livy.client.local.rpc.Rpc.java
License:Apache License
/** * Creates an RPC client for a server running on the given remote host and port. * * @param config RPC configuration data. * @param eloop Event loop for managing the connection. * @param host Host name or IP address to connect to. * @param port Port where server is listening. * @param clientId The client ID that identifies the connection. * @param secret Secret for authenticating the client with the server. * @param dispatcher Dispatcher used to handle RPC calls. * @return A future that can be used to monitor the creation of the RPC object. *//* ww w . ja v a 2s . c o m*/ public static Promise<Rpc> createClient(final LocalConf config, final EventLoopGroup eloop, String host, int port, final String clientId, final String secret, final RpcDispatcher dispatcher) throws Exception { int connectTimeoutMs = (int) config.getTimeAsMs(RPC_CLIENT_CONNECT_TIMEOUT); final ChannelFuture cf = new Bootstrap().group(eloop).handler(new ChannelInboundHandlerAdapter() { }).channel(NioSocketChannel.class).option(ChannelOption.SO_KEEPALIVE, true) .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, connectTimeoutMs).connect(host, port); final Promise<Rpc> promise = eloop.next().newPromise(); final AtomicReference<Rpc> rpc = new AtomicReference<Rpc>(); // Set up a timeout to undo everything. final Runnable timeoutTask = new Runnable() { @Override public void run() { promise.setFailure(new TimeoutException("Timed out waiting for RPC server connection.")); } }; final ScheduledFuture<?> timeoutFuture = eloop.schedule(timeoutTask, config.getTimeAsMs(RPC_CLIENT_HANDSHAKE_TIMEOUT), TimeUnit.MILLISECONDS); // The channel listener instantiates the Rpc instance when the connection is established, // and initiates the SASL handshake. cf.addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture cf) throws Exception { if (cf.isSuccess()) { SaslClientHandler saslHandler = new SaslClientHandler(config, clientId, promise, timeoutFuture, secret, dispatcher); Rpc rpc = createRpc(config, saslHandler, (SocketChannel) cf.channel(), eloop); saslHandler.rpc = rpc; saslHandler.sendHello(cf.channel()); } else { promise.setFailure(cf.cause()); } } }); // Handle cancellation of the promise. promise.addListener(new GenericFutureListener<Promise<Rpc>>() { @Override public void operationComplete(Promise<Rpc> p) { if (p.isCancelled()) { cf.cancel(true); } } }); return promise; }
From source file:com.cloudera.livy.client.local.rpc.RpcServer.java
License:Apache License
public RpcServer(LocalConf lconf) throws IOException, InterruptedException { this.config = lconf; this.group = new NioEventLoopGroup(this.config.getInt(RPC_MAX_THREADS), new ThreadFactoryBuilder().setNameFormat("RPC-Handler-%d").setDaemon(true).build()); this.channel = new ServerBootstrap().group(group).channel(NioServerSocketChannel.class) .childHandler(new ChannelInitializer<SocketChannel>() { @Override//from w w w. java 2s . c o m public void initChannel(SocketChannel ch) throws Exception { SaslServerHandler saslHandler = new SaslServerHandler(config); final Rpc newRpc = Rpc.createServer(saslHandler, config, ch, group); saslHandler.rpc = newRpc; Runnable cancelTask = new Runnable() { @Override public void run() { LOG.warn("Timed out waiting for hello from client."); newRpc.close(); } }; saslHandler.cancelTask = group.schedule(cancelTask, config.getTimeAsMs(RPC_CLIENT_HANDSHAKE_TIMEOUT), TimeUnit.MILLISECONDS); } }).option(ChannelOption.SO_BACKLOG, 1).option(ChannelOption.SO_REUSEADDR, true) .childOption(ChannelOption.SO_KEEPALIVE, true).bind(0).sync().channel(); this.port = ((InetSocketAddress) channel.localAddress()).getPort(); this.pendingClients = Maps.newConcurrentMap(); String address = config.get(RPC_SERVER_ADDRESS); if (address == null) { address = config.findLocalAddress(); } this.address = address; }
From source file:com.cloudera.livy.rsc.rpc.Rpc.java
License:Apache License
/** * Creates an RPC client for a server running on the given remote host and port. * * @param config RPC configuration data. * @param eloop Event loop for managing the connection. * @param host Host name or IP address to connect to. * @param port Port where server is listening. * @param clientId The client ID that identifies the connection. * @param secret Secret for authenticating the client with the server. * @param dispatcher Dispatcher used to handle RPC calls. * @return A future that can be used to monitor the creation of the RPC object. *//* www. java 2 s . co m*/ public static Promise<Rpc> createClient(final RSCConf config, final EventLoopGroup eloop, String host, int port, final String clientId, final String secret, final RpcDispatcher dispatcher) throws Exception { int connectTimeoutMs = (int) config.getTimeAsMs(RPC_CLIENT_CONNECT_TIMEOUT); final ChannelFuture cf = new Bootstrap().group(eloop).handler(new ChannelInboundHandlerAdapter() { }).channel(NioSocketChannel.class).option(ChannelOption.SO_KEEPALIVE, true) .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, connectTimeoutMs).connect(host, port); final Promise<Rpc> promise = eloop.next().newPromise(); final AtomicReference<Rpc> rpc = new AtomicReference<Rpc>(); // Set up a timeout to undo everything. final Runnable timeoutTask = new Runnable() { @Override public void run() { promise.setFailure(new TimeoutException("Timed out waiting for RPC server connection.")); } }; final ScheduledFuture<?> timeoutFuture = eloop.schedule(timeoutTask, config.getTimeAsMs(RPC_CLIENT_HANDSHAKE_TIMEOUT), TimeUnit.MILLISECONDS); // The channel listener instantiates the Rpc instance when the connection is established, // and initiates the SASL handshake. cf.addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture cf) throws Exception { if (cf.isSuccess()) { SaslClientHandler saslHandler = new SaslClientHandler(config, clientId, promise, timeoutFuture, secret, dispatcher); Rpc rpc = createRpc(config, saslHandler, (SocketChannel) cf.channel(), eloop); saslHandler.rpc = rpc; saslHandler.sendHello(cf.channel()); } else { promise.setFailure(cf.cause()); } } }); // Handle cancellation of the promise. promise.addListener(new GenericFutureListener<Promise<Rpc>>() { @Override public void operationComplete(Promise<Rpc> p) { if (p.isCancelled()) { cf.cancel(true); } } }); return promise; }
From source file:com.cloudera.livy.rsc.rpc.RpcServer.java
License:Apache License
public RpcServer(RSCConf lconf) throws IOException, InterruptedException { this.config = lconf; this.group = new NioEventLoopGroup(this.config.getInt(RPC_MAX_THREADS), Utils.newDaemonThreadFactory("RPC-Handler-%d")); this.channel = new ServerBootstrap().group(group).channel(NioServerSocketChannel.class) .childHandler(new ChannelInitializer<SocketChannel>() { @Override/* ww w .j a v a 2s. c o m*/ public void initChannel(SocketChannel ch) throws Exception { SaslServerHandler saslHandler = new SaslServerHandler(config); final Rpc newRpc = Rpc.createServer(saslHandler, config, ch, group); saslHandler.rpc = newRpc; Runnable cancelTask = new Runnable() { @Override public void run() { LOG.warn("Timed out waiting for hello from client."); newRpc.close(); } }; saslHandler.cancelTask = group.schedule(cancelTask, config.getTimeAsMs(RPC_CLIENT_HANDSHAKE_TIMEOUT), TimeUnit.MILLISECONDS); } }).option(ChannelOption.SO_BACKLOG, 1).option(ChannelOption.SO_REUSEADDR, true) .childOption(ChannelOption.SO_KEEPALIVE, true).bind(0).sync().channel(); this.port = ((InetSocketAddress) channel.localAddress()).getPort(); this.pendingClients = new ConcurrentHashMap<>(); String address = config.get(RPC_SERVER_ADDRESS); if (address == null) { address = config.findLocalAddress(); } this.address = address; }
From source file:com.codebroker.core.service.NettyNetService.java
License:Open Source License
@Override public void init(Object object) { logger.info("?Netty "); PropertiesWrapper propertiesWrapper = (PropertiesWrapper) object; int defaultValue = Runtime.getRuntime().availableProcessors() * 2; bossGroupNum = propertiesWrapper.getIntProperty(SystemEnvironment.NETTY_BOSS_GROUP_NUM, defaultValue); workerGroupNum = propertiesWrapper.getIntProperty(SystemEnvironment.NETTY_WORKER_GROUP_NUM, defaultValue); backlog = propertiesWrapper.getIntProperty(SystemEnvironment.NETTY_BACKLOG, BACKLOG); name = propertiesWrapper.getProperty(SystemEnvironment.NETTY_SERVER_NAME, "NETTY_SERVER"); int port = propertiesWrapper.getIntProperty(SystemEnvironment.TCP_PROT, D_PORT); Thread thread = new Thread(new Runnable() { public void run() { bootstrap = new ServerBootstrap(); bossGroup = new NioEventLoopGroup(bossGroupNum); workerGroup = new NioEventLoopGroup(workerGroupNum); bootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .option(ChannelOption.SO_BACKLOG, backlog) .option(ChannelOption.SO_REUSEADDR, Boolean.valueOf(true)) // .option(ChannelOption.TCP_NODELAY, // Boolean.valueOf(true)) // .option(ChannelOption.SO_KEEPALIVE, // Boolean.valueOf(true)) .childOption(ChannelOption.TCP_NODELAY, true).childOption(ChannelOption.SO_KEEPALIVE, true) .childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT) .childOption(ChannelOption.RCVBUF_ALLOCATOR, AdaptiveRecvByteBufAllocator.DEFAULT) .handler(new LoggingHandler(LogLevel.INFO)).childHandler(new NettyServerInitializer()); ChannelFuture f;/*from w w w . j a v a 2s . c o m*/ try { f = bootstrap.bind(port).sync(); f.channel().closeFuture().sync(); } catch (InterruptedException e) { e.printStackTrace(); } } }, "Netty-Start-Thread"); thread.start(); logger.info("?Netty ?"); super.setActive(); }
From source file:com.codnos.dbgp.internal.impl.DBGpEngineImpl.java
License:Apache License
private void connectToIde(EventLoopGroup workerGroup) throws InterruptedException { Bootstrap b = new Bootstrap(); b.group(workerGroup);/*from w w w . j a va 2 s.c o m*/ b.channel(NioSocketChannel.class); b.option(ChannelOption.SO_KEEPALIVE, true); b.handler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new DBGpInitHandler(new Init(debuggerEngine)), new DBGpCommandDecoder(), new DBGpResponseEncoder(), new BreakpointSetCommandHandler(debuggerEngine, argumentConfiguration), new BreakpointGetCommandHandler(debuggerEngine, argumentConfiguration), new BreakpointRemoveCommandHandler(debuggerEngine, argumentConfiguration), new BreakpointUpdateCommandHandler(debuggerEngine, argumentConfiguration), new StackDepthCommandHandler(debuggerEngine, argumentConfiguration), new RunCommandHandler(debuggerEngine, statusChangeHandlerFactory, argumentConfiguration), new StepOverCommandHandler(debuggerEngine, statusChangeHandlerFactory, argumentConfiguration), new StepIntoCommandHandler(debuggerEngine, statusChangeHandlerFactory, argumentConfiguration), new StepOutCommandHandler(debuggerEngine, statusChangeHandlerFactory, argumentConfiguration), new StackGetCommandHandler(debuggerEngine, argumentConfiguration), new ContextGetCommandHandler(debuggerEngine, argumentConfiguration), new StatusCommandHandler(debuggerEngine, argumentConfiguration), new BreakNowCommandHandler(debuggerEngine, argumentConfiguration), new EvalCommandHandler(debuggerEngine, argumentConfiguration)); } }); b.connect("localhost", port).sync(); }