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:gash.router.global.edges.GlobalEdgeMonitor.java
License:Apache License
private Channel connectToChannel(String host, int port) { Bootstrap b = new Bootstrap(); NioEventLoopGroup nioEventLoopGroup = new NioEventLoopGroup(); GlobalInit globalInit = new GlobalInit(server, false); try {// w w w . j a va 2 s . c o m b.group(nioEventLoopGroup).channel(NioSocketChannel.class).handler(globalInit); b.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 10000); b.option(ChannelOption.TCP_NODELAY, true); b.option(ChannelOption.SO_KEEPALIVE, true); // Make the connection attempt. } catch (Exception e) { logger.error("Could not connect to the host " + host); return null; } return b.connect(host, port).syncUninterruptibly().channel(); }
From source file:gash.router.server.communication.CommConnection.java
License:Apache License
/** * abstraction of notification in the communication * /* www . j a va 2s . co m*/ * @param listener */ /*public void addListener(CommListener listener) { CommHandler handler = connect().pipeline().get(CommHandler.class); if (handler != null) handler.addListener(listener); }*/ private void init() { System.out.println("--> initializing connection to " + host + ":" + port); // the queue to support client-side surging //outbound = new LinkedBlockingDeque<CommandMessage>(); group = new NioEventLoopGroup(); try { CommInit si = new CommInit(false); Bootstrap b = new Bootstrap(); b.group(group).channel(NioSocketChannel.class).handler(si); b.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 100000); b.option(ChannelOption.TCP_NODELAY, true); b.option(ChannelOption.SO_KEEPALIVE, true); // Make the connection attempt. channel = b.connect(host, port).syncUninterruptibly(); // want to monitor the connection to the server s.t. if we loose the // connection, we can try to re-establish it. ClientClosedListener ccl = new ClientClosedListener(this); channel.channel().closeFuture().addListener(ccl); System.out.println(channel.channel().localAddress() + " -> open: " + channel.channel().isOpen() + ", write: " + channel.channel().isWritable() + ", reg: " + channel.channel().isRegistered()); } catch (Throwable ex) { logger.error("failed to initialize the client connection");//, ex); //ex.printStackTrace(); } // start outbound message processor //worker = new CommWorker(this); //worker.setDaemon(true); //worker.start(); }
From source file:gash.router.server.edges.EdgeMonitor.java
License:Apache License
private Channel connectToChannel(String host, int port) { Bootstrap b = new Bootstrap(); NioEventLoopGroup nioEventLoopGroup = new NioEventLoopGroup(); WorkInit workInit = new WorkInit(state, false); try {/*from w w w . j a v a 2s .c om*/ b.group(nioEventLoopGroup).channel(NioSocketChannel.class).handler(workInit); b.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 10000); b.option(ChannelOption.TCP_NODELAY, true); b.option(ChannelOption.SO_KEEPALIVE, true); // Make the connection attempt. } catch (Exception e) { logger.error("Could not connect to the host " + host); return null; } return b.connect(host, port).syncUninterruptibly().channel(); }
From source file:github.com.cp149.netty.server.NettyappenderServer.java
License:Apache License
public void run() throws Exception { try {/*from w w w . ja va 2 s .co m*/ bootstrap = new ServerBootstrap(); final EventExecutorGroup executor = new DefaultEventExecutorGroup(4); bootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .childOption(ChannelOption.SO_KEEPALIVE, true).childOption(ChannelOption.TCP_NODELAY, true) .childOption(ChannelOption.SO_RCVBUF, 65535).childOption(ChannelOption.SO_SNDBUF, 2048) .childOption(ChannelOption.SO_REUSEADDR, true) //reuse address .childOption(ChannelOption.ALLOCATOR, new PooledByteBufAllocator(false))// heap buf 's better .childHandler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { // ch.pipeline().addLast( new MarshallingEncoder(MarshallUtil.createProvider())); // ch.pipeline().addLast(new CompatibleObjectDecoder()); // ch.pipeline().addLast(new ObjectEncoder(), // new ObjectDecoder(ClassResolvers.cacheDisabled(null))); ch.pipeline().addLast(new MarshallingDecoder(MarshallUtil.createUnProvider())); ch.pipeline().addLast(executor, new NettyappenderServerHandler()); // } }); bootstrap.bind(port).sync(); } finally { } // bootstrap = new ServerBootstrap(new // NioServerSocketChannelFactory(Executors.newFixedThreadPool(4), // Executors.newFixedThreadPool(4))); // final ExecutionHandler executionHandler = new ExecutionHandler(new // OrderedMemoryAwareThreadPoolExecutor(4, 1024 * 1024 * 300, 1024 * // 1024 * 300 * 2)); // bootstrap.setOption("tcpNoDelay", true); // bootstrap.setOption("keepAlive", true); // // bootstrap.setOption("writeBufferHighWaterMark", 100 * 64 * 1024); // // bootstrap.setOption("sendBufferSize", 1048576); // bootstrap.setOption("receiveBufferSize", 1048576*10 ); // // // Set up the pipeline factory. // bootstrap.setPipelineFactory(new ChannelPipelineFactory() { // public ChannelPipeline getPipeline() throws Exception { // return Channels.pipeline(executionHandler, new // MarshallingDecoder(createProvider(createMarshallerFactory(), // createMarshallingConfig())), // new NettyappenderServerHandler()); // } // }); // // Bind and start to accept incoming connections. // bootstrap.bind(new InetSocketAddress(port)); // LoggerFactory.getLogger(this.getClass()).info("start server at" + // port); }
From source file:groovyx.gpars.remote.netty.NettyClient.java
License:Apache License
/** * Creates client that connect to server on specified host and port. * @param host the host where server listens on * @param port the port that server listens on */// ww w . j a va2 s.c o m public NettyClient(LocalHost localHost, String host, int port, ConnectListener connectListener) { workerGroup = new NioEventLoopGroup(); bootstrap = new Bootstrap(); bootstrap.group(workerGroup).channel(NioSocketChannel.class) .handler(new NettyChannelInitializer(localHost, connectListener)) .option(ChannelOption.SO_KEEPALIVE, true).option(ChannelOption.TCP_NODELAY, true) .remoteAddress(host, port); }
From source file:groovyx.gpars.remote.netty.NettyServer.java
License:Apache License
/** * Creates a server listening on specified address. *//*from w w w . j a va 2 s .co m*/ public NettyServer(LocalHost localHost, String address, int port, ConnectListener connectListener) { bossGroup = new NioEventLoopGroup(); workerGroup = new NioEventLoopGroup(); bootstrap = new ServerBootstrap(); bootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .childHandler(new NettyChannelInitializer(localHost, connectListener)) .childOption(ChannelOption.TCP_NODELAY, true).childOption(ChannelOption.SO_KEEPALIVE, true) .localAddress(new InetSocketAddress(address, port)); }
From source file:hivemall.mix.client.MixClient.java
License:Open Source License
private void configureBootstrap(Bootstrap b, EventLoopGroup workerGroup, NodeInfo server) throws SSLException, InterruptedException { // Configure SSL. final SslContext sslCtx; if (ssl) {//from w w w . ja v a 2s. c om sslCtx = SslContext.newClientContext(InsecureTrustManagerFactory.INSTANCE); } else { sslCtx = null; } b.group(workerGroup); b.option(ChannelOption.SO_KEEPALIVE, true); b.option(ChannelOption.TCP_NODELAY, true); b.channel(NioSocketChannel.class); b.handler(new MixClientInitializer(msgHandler, sslCtx)); SocketAddress remoteAddr = server.getSocketAddress(); ChannelFuture channelFuture = b.connect(remoteAddr).sync(); Channel channel = channelFuture.channel(); channelMap.put(server, channel); }
From source file:hivemall.mix.server.MixServer.java
License:Open Source License
private void acceptConnections(@Nonnull MixServerInitializer initializer, int port) throws InterruptedException { final EventLoopGroup bossGroup = new NioEventLoopGroup(1); final EventLoopGroup workerGroup = new NioEventLoopGroup(); try {//from ww w .ja v a2 s .c o m ServerBootstrap b = new ServerBootstrap(); b.option(ChannelOption.SO_KEEPALIVE, true); b.group(bossGroup, workerGroup); b.channel(NioServerSocketChannel.class); b.handler(new LoggingHandler(LogLevel.INFO)); b.childHandler(initializer); // Bind and start to accept incoming connections. ChannelFuture f = b.bind(port).sync(); this.state = ServerState.RUNNING; // 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 { this.state = ServerState.STOPPING; workerGroup.shutdownGracefully(); bossGroup.shutdownGracefully(); } }
From source file:http.HTTPClient.java
License:Open Source License
public HTTPClient(boolean ssl, String host, int port) throws Exception { try {/*from ww w . ja v a 2 s. c o m*/ final SslContext sslCtx; if (ssl) { SslProvider provider = OpenSsl.isAlpnSupported() ? SslProvider.OPENSSL : SslProvider.JDK; sslCtx = SslContextBuilder.forClient().sslProvider(provider) .ciphers(Http2SecurityUtil.CIPHERS, SupportedCipherSuiteFilter.INSTANCE) .trustManager(InsecureTrustManagerFactory.INSTANCE) .applicationProtocolConfig(new ApplicationProtocolConfig(Protocol.ALPN, // NO_ADVERTISE is currently the only mode supported by both OpenSsl and JDK providers. SelectorFailureBehavior.NO_ADVERTISE, // ACCEPT is currently the only mode supported by both OpenSsl and JDK providers. SelectedListenerFailureBehavior.ACCEPT, ApplicationProtocolNames.HTTP_2, ApplicationProtocolNames.HTTP_1_1)) .build(); } else { sslCtx = null; } workerGroup = new NioEventLoopGroup(); HTTPClientInitializer initializer = new HTTPClientInitializer(sslCtx, Integer.MAX_VALUE); // Configure the client. Bootstrap b = new Bootstrap(); b.group(workerGroup); b.channel(NioSocketChannel.class); b.option(ChannelOption.SO_KEEPALIVE, true); b.remoteAddress(host, port); b.handler(initializer); // Start the client. channel = b.connect().syncUninterruptibly().channel(); log.info("Connected to [" + host + ':' + port + ']'); // Wait for the HTTP/2 upgrade to occur. // HTTPSettingsHandler http2SettingsHandler = initializer.settingsHandler(); // http2SettingsHandler.awaitSettings(TestUtil.HTTP2_RESPONSE_TIME_OUT, TestUtil.HTTP2_RESPONSE_TIME_UNIT); // responseHandler = initializer.responseHandler(); scheme = ssl ? HttpScheme.HTTPS : HttpScheme.HTTP; hostName = new AsciiString(host + ':' + port); } catch (Exception ex) { log.error("Error while initializing http2 client " + ex); this.close(); } }
From source file:http.HTTPClient2.java
License:Open Source License
public HTTPClient2(boolean ssl, String host, int port) throws Exception { try {/* w w w. java 2 s . com*/ final SslContext sslCtx; if (ssl) { SslProvider provider = OpenSsl.isAlpnSupported() ? SslProvider.OPENSSL : SslProvider.JDK; sslCtx = SslContextBuilder.forClient().sslProvider(provider) .ciphers(Http2SecurityUtil.CIPHERS, SupportedCipherSuiteFilter.INSTANCE) .trustManager(InsecureTrustManagerFactory.INSTANCE) .applicationProtocolConfig(new ApplicationProtocolConfig(Protocol.ALPN, // NO_ADVERTISE is currently the only mode supported by both OpenSsl and JDK providers. SelectorFailureBehavior.NO_ADVERTISE, // ACCEPT is currently the only mode supported by both OpenSsl and JDK providers. SelectedListenerFailureBehavior.ACCEPT, ApplicationProtocolNames.HTTP_2, ApplicationProtocolNames.HTTP_1_1)) .build(); } else { sslCtx = null; } workerGroup = new NioEventLoopGroup(); HTTPClientInitializer initializer = new HTTPClientInitializer(sslCtx, Integer.MAX_VALUE); // Configure the client. Bootstrap b = new Bootstrap(); b.group(workerGroup); b.channel(NioSocketChannel.class); b.option(ChannelOption.SO_KEEPALIVE, true); b.remoteAddress(host, port); b.handler(initializer); // Start the client. channel = b.connect().syncUninterruptibly().channel(); log.info("Connected to [" + host + ':' + port + ']'); // Wait for the HTTP/2 upgrade to occur. HTTPSettingsHandler http2SettingsHandler = initializer.settingsHandler(); http2SettingsHandler.awaitSettings(TestUtil.HTTP2_RESPONSE_TIME_OUT, TestUtil.HTTP2_RESPONSE_TIME_UNIT); responseHandler = initializer.responseHandler(); scheme = ssl ? HttpScheme.HTTPS : HttpScheme.HTTP; hostName = new AsciiString(host + ':' + port); } catch (Exception ex) { log.error("Error while initializing http2 client " + ex); this.close(); } }