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.zhucode.longio.transport.netty.NettyConnector.java
License:Open Source License
private void runOneRawSocketServer(int port, Dispatcher dispatcher, ProtocolType pt) { ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup);/* w w w . j a v a2 s .co m*/ b.channel(NioServerSocketChannel.class); b.childHandler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast("decoder", new LengthFieldBasedFrameDecoder(65536, 0, 2, 0, 2)); ch.pipeline().addLast("encoder", new LengthFieldPrepender(2, false)); ch.pipeline().addLast(new IdleStateHandler(6000, 3000, 0)); ch.pipeline().addLast(new RawSocketHandler(NettyConnector.this, dispatcher, callbackDispatcher, getProtocolParser(pt))); } }); b.option(ChannelOption.SO_BACKLOG, 4096); b.childOption(ChannelOption.SO_KEEPALIVE, true); ChannelFuture f; try { f = b.bind(port).sync(); f.channel().closeFuture().sync(); } catch (Exception e) { e.printStackTrace(); } }
From source file:com.ztesoft.zsmart.zmq.remoting.netty.NettyRemotingServer.java
License:Apache License
@Override public void start() { this.defaultEventExecutorGroup = new DefaultEventExecutorGroup(// nettyServerConfig.getServerWorkThreads(), // new ThreadFactory() { private AtomicInteger threadIndex = new AtomicInteger(0); @Override//from ww w.ja v a 2 s . co m public Thread newThread(Runnable r) { return new Thread(r, "NettyServerWorkerThread_" + this.threadIndex.incrementAndGet()); } }); ServerBootstrap childHandler = // this.serverBootstrap.group(this.eventLoopGroupBoss, this.eventLoopGroupWorker) .channel(NioServerSocketChannel.class) // .option(ChannelOption.SO_BACKLOG, 1024) // .option(ChannelOption.SO_REUSEADDR, true) // .option(ChannelOption.SO_KEEPALIVE, false) // .childOption(ChannelOption.TCP_NODELAY, true) // .option(ChannelOption.SO_SNDBUF, nettyServerConfig.getServerSocketSndBufSize()) // .option(ChannelOption.SO_RCVBUF, nettyServerConfig.getServerSocketRcvBufSize()) // .localAddress(new InetSocketAddress(this.nettyServerConfig.getListenPort())) .childHandler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast( // defaultEventExecutorGroup, // new NettyEncoder(), // new NettyDecoder(), // new IdleStateHandler(0, 0, nettyServerConfig.getServerChannelMaxIdleTimeSeconds()), // new NettyConnetManageHandler(), // new NettyServerHandler()); } }); if (nettyServerConfig.isServerPooledByteBufAllocatorEnable()) { // ???? childHandler.childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT); } try { ChannelFuture sync = this.serverBootstrap.bind().sync(); InetSocketAddress addr = (InetSocketAddress) sync.channel().localAddress(); this.port = addr.getPort(); } catch (InterruptedException e1) { throw new RuntimeException("this.serverBootstrap.bind().sync() InterruptedException", e1); } if (this.channelEventListener != null) { this.nettyEventExecuter.start(); } // ?1?? this.timer.scheduleAtFixedRate(new TimerTask() { @Override public void run() { try { NettyRemotingServer.this.scanResponseTable(); } catch (Exception e) { log.error("scanResponseTable exception", e); } } }, 1000 * 3, 1000); }
From source file:controlspy3.ControlSpy3.java
public void run() { EventLoopGroup bossGroup = new NioEventLoopGroup(); EventLoopGroup workerGroup = new NioEventLoopGroup(); try {//from ww w . j a v a 2 s . c om // Server ServerBootstrap boots = new ServerBootstrap(); boots.group(bossGroup, workerGroup); boots.channel(NioServerSocketChannel.class); boots.childHandler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new TimeEncoder(), new SpyAsServer()); if (ALGO == false) { System.out.println("se salio "); } System.out.println("Cliente conectado!"); } }).option(ChannelOption.SO_BACKLOG, 128).childOption(ChannelOption.SO_KEEPALIVE, true); //SERVER // Bind and start to accept incoming connections. ChannelFuture future_ch = boots.bind(port).sync(); //SERVER // Wait until the server socket is closed. Server future_ch.channel().closeFuture().sync(); } catch (InterruptedException ex) { } finally { workerGroup.shutdownGracefully(); bossGroup.shutdownGracefully(); } }
From source file:darks.grid.network.GridMessageClient.java
License:Apache License
@Override public boolean initialize() { super.initialize(); try {/*from w ww . ja va 2 s . c om*/ log.info("Initialize message client."); NetworkConfig config = GridRuntime.config().getNetworkConfig(); int workerNum = config.getClientWorkerThreadNumber(); workerGroup = new NioEventLoopGroup(workerNum, ThreadUtils.getThreadFactory()); bootstrap = new Bootstrap(); bootstrap.group(workerGroup).channel(NioSocketChannel.class) .option(ChannelOption.TCP_NODELAY, config.isTcpNodelay()) .option(ChannelOption.SO_KEEPALIVE, config.isTcpKeepAlive()) .option(ChannelOption.ALLOCATOR, new PooledByteBufAllocator(true)) // .option(ChannelOption.SO_TIMEOUT, config.getRecvTimeout()) .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, config.getConnectTimeout()) .option(ChannelOption.SO_SNDBUF, config.getTcpSendBufferSize()) .option(ChannelOption.SO_RCVBUF, config.getTcpRecvBufferSize()); bootstrap.handler(newChannelHandler()); return true; } catch (Exception e) { log.error(e.getMessage(), e); return false; } }
From source file:darks.grid.network.GridMessageServer.java
License:Apache License
@Override public boolean initialize() { try {//ww w .j a v a 2 s . c o m NetworkConfig config = GridRuntime.config().getNetworkConfig(); int bossNum = Runtime.getRuntime().availableProcessors() * config.getServerBossThreadDelta(); int workerNum = config.getServerWorkerThreadNumber(); bossGroup = new NioEventLoopGroup(bossNum); workerGroup = new NioEventLoopGroup(workerNum); super.initialize(); bootstrap = new ServerBootstrap(); bootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .option(ChannelOption.TCP_NODELAY, config.isTcpNodelay()) .option(ChannelOption.SO_KEEPALIVE, config.isTcpKeepAlive()) .option(ChannelOption.ALLOCATOR, new PooledByteBufAllocator(true)) // .option(ChannelOption.SO_TIMEOUT, config.getRecvTimeout()) .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, config.getConnectTimeout()) .option(ChannelOption.SO_REUSEADDR, config.isTcpReuseAddr()) // .option(ChannelOption.SO_BACKLOG, config.getTcpBacklog()) .option(ChannelOption.SO_SNDBUF, config.getTcpSendBufferSize()) .option(ChannelOption.SO_RCVBUF, config.getTcpRecvBufferSize()) .childOption(ChannelOption.TCP_NODELAY, config.isTcpNodelay()) .childOption(ChannelOption.SO_KEEPALIVE, config.isTcpKeepAlive()) .childOption(ChannelOption.ALLOCATOR, new PooledByteBufAllocator(true)) .childOption(ChannelOption.SO_TIMEOUT, config.getRecvTimeout()) .childOption(ChannelOption.CONNECT_TIMEOUT_MILLIS, config.getConnectTimeout()) .childOption(ChannelOption.SO_REUSEADDR, config.isTcpReuseAddr()) .childOption(ChannelOption.SO_BACKLOG, config.getTcpBacklog()) .childOption(ChannelOption.SO_SNDBUF, config.getTcpSendBufferSize()) .childOption(ChannelOption.SO_RCVBUF, config.getTcpRecvBufferSize()); bootstrap.childHandler(newChannelHandler()); return true; } catch (Exception e) { log.error(e.getMessage(), e); return false; } }
From source file:dbseer.middleware.server.MiddlewareServer.java
License:Apache License
public void run() throws Exception { // basic log info. Log.info(String.format("Listening port = %d", port)); Log.info(String.format("DB log dir = %s", dbLogPath)); Log.info(String.format("System log dir = %s", sysLogPath)); // print server info. for (Server s : servers.values()) { s.printLogInfo();//from ww w .j a va 2 s .c o m // test MySQL/MariaDB connection using JDBC before we start anything. if (!s.testConnection()) { throw new Exception("Unable to connect to the MySQL server with the given credential."); } else if (!s.testMonitoringDir()) { throw new Exception("Specified monitoring directory and script do not exist."); } } // open named pipe. File checkPipeFile = new File(this.namedPipePath); if (checkPipeFile == null || !checkPipeFile.exists() || checkPipeFile.isDirectory()) { throw new Exception("Cannot open the named pipe for communication with dbseerroute. " + "You must run Maxscale with dbseerroute with correct named pipe first."); } namedPipeFile = new RandomAccessFile(this.namedPipePath, "rwd"); if (namedPipeFile == null) { throw new Exception("Cannot open the named pipe for communication with dbseerroute. " + "You must run Maxscale with dbseerroute with correct named pipe first."); } // attach shutdown hook. MiddlewareServerShutdown shutdownThread = new MiddlewareServerShutdown(this); Runtime.getRuntime().addShutdownHook(shutdownThread); // let's start accepting connections. EventLoopGroup bossGroup = new NioEventLoopGroup(1); EventLoopGroup workerGroup = new NioEventLoopGroup(4); final MiddlewareServer server = this; try { ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .option(ChannelOption.SO_BACKLOG, 128) .option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT) .childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT) .childOption(ChannelOption.SO_KEEPALIVE, true) .handler(new MiddlewareServerConnectionHandler(server)) .childHandler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { ChannelPipeline p = ch.pipeline(); p.addLast(new IdleStateHandler(10, 0, 0)); p.addLast(ZlibCodecFactory.newZlibEncoder(ZlibWrapper.ZLIB)); p.addLast(ZlibCodecFactory.newZlibDecoder(ZlibWrapper.ZLIB)); p.addLast(new MiddlewarePacketDecoder()); p.addLast(new MiddlewarePacketEncoder()); p.addLast(new MiddlewareServerHandler(server)); // p.addLast(new MiddlewarePacketDecoder(), new MiddlewareServerHandler(server)); } }); Log.info("Middleware is now accepting connections."); // bind and start accepting connections. ChannelFuture cf = b.bind(port).sync(); // shutdown the server. if (cf != null) { cf.channel().closeFuture().sync(); } } catch (Exception e) { Log.error(e.getMessage()); } finally { bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); if (tailerExecutor != null && !tailerExecutor.isShutdown()) { tailerExecutor.shutdown(); } } }
From source file:de.felix_klauke.pegasus.client.network.NettyClient.java
License:Apache License
/** * Start the Netty Client and connect it to the server. *///from w w w .j a v a 2 s . com public void start() { logger.info("Starting netty Client..."); bootstrap.channel(NioSocketChannel.class); bootstrap.group(new NioEventLoopGroup()); bootstrap.handler(channelInitializer); bootstrap.option(ChannelOption.SO_KEEPALIVE, true); logger.info("NettyClient is configured. Client will be connect now..."); try { ChannelFuture future = bootstrap.connect("felix-klauke.de", 27816).sync(); channel = future.channel(); logger.info("NettyClient is connected. Blocking Thread with the Client now..."); future.channel().closeFuture(); } catch (InterruptedException e) { e.printStackTrace(); } }
From source file:de.felix_klauke.pegasus.server.network.NettyServer.java
License:Apache License
/** * Start the most epic Server ever./*from ww w . j a v a 2 s . c o m*/ */ public void start() { logger.info("Starting Netty Server..."); serverBootstrap.channel(NioServerSocketChannel.class); serverBootstrap.group(new NioEventLoopGroup(), new NioEventLoopGroup()); serverBootstrap.childOption(ChannelOption.SO_KEEPALIVE, true); serverBootstrap.childHandler(channelInitializer); logger.info("NettyServer is configured. Server will be binded now..."); ChannelFuture future = serverBootstrap.bind(27816); logger.info("Nettyserver is bound. Blocking Thread with the Server now..."); try { future.sync().channel().closeFuture().sync(); } catch (InterruptedException e) { e.printStackTrace(); } }
From source file:de.jpaw.bonaparte.netty.testServer.TestServer.java
License:Apache License
public void run() throws Exception { // Configure the server. EventLoopGroup bossGroup = new NioEventLoopGroup(3); EventLoopGroup workerGroup = new NioEventLoopGroup(6); try {/*from w w w. j a va 2s . co m*/ ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .option(ChannelOption.SO_BACKLOG, 100).localAddress(new InetSocketAddress(port)) .childOption(ChannelOption.TCP_NODELAY, true).childOption(ChannelOption.SO_KEEPALIVE, true) .handler(new LoggingHandler(LogLevel.INFO)) .childHandler(new BonaparteNettyPipelineFactory(1000, new TestServerHandler(), null, 1)); // Start the server. ChannelFuture f = b.bind().sync(); // Wait until the server socket is closed. f.channel().closeFuture().sync(); } catch (Exception e) { System.out.println("Exception " + e + " occured"); } finally { // Shut down all event loops to terminate all threads. bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } }
From source file:de.pvsnp.chat.api.connector.ChatClient.java
public void connect() { EventLoopGroup workerGroup = new NioEventLoopGroup(); // (2) try {/*from ww w. j a v a2 s . c o m*/ Bootstrap bootstrap = new Bootstrap(); // (3) bootstrap.group(workerGroup); // (4) bootstrap.channel(NioSocketChannel.class); // (5) bootstrap.option(ChannelOption.SO_KEEPALIVE, true); // (6) bootstrap.handler(new ChannelInitializer<SocketChannel>() { // (7) @Override protected void initChannel(SocketChannel channel) throws Exception { System.out.println("Verbindung zum Server hergestellt!"); channel.pipeline().addLast(new StringEncoder(Charset.forName("UTF-8")), // (2) new LineBasedFrameDecoder(1024), // (3) new StringDecoder(Charset.forName("UTF-8")), // (2) new EchoClientHandler()); c = channel; } }); bootstrap.connect(ip, port).sync().channel().closeFuture().sync(); // (8) } catch (Exception ex) { ex.printStackTrace(); } finally { workerGroup.shutdownGracefully(); // (9) } }