use of org.apache.flink.shaded.netty4.io.netty.channel.EventLoopGroup in project java-in-action by xinghalo.
the class SubReqClient method connect.
public void connect(int port, String host) throws Exception {
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
protected void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new ObjectDecoder(1024, ClassResolvers.cacheDisabled(this.getClass().getClassLoader())));
ch.pipeline().addLast(new ObjectEncoder());
ch.pipeline().addLast(new SubReqClientHandler());
}
});
ChannelFuture f = b.connect(host, port).sync();
f.channel().closeFuture().sync();
} finally {
group.shutdownGracefully().sync();
}
}
use of org.apache.flink.shaded.netty4.io.netty.channel.EventLoopGroup in project benchmark by seelunzi.
the class WebsocketChatServer method run.
public void run() throws Exception {
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).childHandler(new WebsocketChatServerInitializer()).option(ChannelOption.SO_BACKLOG, 128).childOption(ChannelOption.SO_KEEPALIVE, true);
System.out.println("WebsocketChatServer 启动了");
// 绑定端口,开始接收进来的连接
ChannelFuture f = b.bind(port).sync();
// 等待服务器 socket 关闭 。
// 在这个例子中,这不会发生,但你可以优雅地关闭你的服务器。
f.channel().closeFuture().sync();
} finally {
workerGroup.shutdownGracefully();
bossGroup.shutdownGracefully();
System.out.println("WebsocketChatServer 关闭了");
}
}
use of org.apache.flink.shaded.netty4.io.netty.channel.EventLoopGroup in project rskj by rsksmart.
the class UDPServer method startUDPServer.
public void startUDPServer() throws InterruptedException {
logger.info("Discovery UDPListener started");
EventLoopGroup group = new NioEventLoopGroup(1);
while (!shutdown) {
Bootstrap bootstrap = this.createBootstrap(group);
channel = bootstrap.bind(address, port).sync().channel();
channel.closeFuture().sync();
logger.warn("UDP channel closed. Recreating after 5 sec pause...");
TimeUnit.SECONDS.sleep(5);
}
group.shutdownGracefully().sync();
}
use of org.apache.flink.shaded.netty4.io.netty.channel.EventLoopGroup in project rskj by rsksmart.
the class PeerServerImpl method start.
public void start(InetAddress host, int port) {
// TODO review listening use
listening = true;
EventLoopGroup bossGroup = new NioEventLoopGroup(1);
EventLoopGroup workerGroup = new NioEventLoopGroup();
EthereumChannelInitializer ethereumChannelInitializer = ethereumChannelInitializerFactory.newInstance("");
ethereumListener.trace("Listening on port " + port);
try {
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup);
b.channel(NioServerSocketChannel.class);
b.option(ChannelOption.SO_KEEPALIVE, true);
b.option(ChannelOption.MESSAGE_SIZE_ESTIMATOR, DefaultMessageSizeEstimator.DEFAULT);
b.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, config.peerConnectionTimeout());
b.handler(new LoggingHandler());
b.childHandler(ethereumChannelInitializer);
// Start the client.
logger.info("Listening for incoming connections, port: [{}] ", port);
logger.info("NodeId: [{}] ", Hex.toHexString(config.nodeId()));
ChannelFuture f = b.bind(port).sync();
// Wait until the connection is closed.
f.channel().closeFuture().sync();
logger.debug("Connection is closed");
// TODO review listening use
listening = false;
} catch (Exception e) {
logger.debug("Exception: {} ({})", e.getMessage(), e.getClass().getName());
throw new Error("Server Disconnected");
} finally {
workerGroup.shutdownGracefully();
}
}
use of org.apache.flink.shaded.netty4.io.netty.channel.EventLoopGroup in project swift by luastar.
the class HttpServer method start.
public void start() {
// 设置线程名称
ThreadFactoryBuilder threadFactoryBuilder = new ThreadFactoryBuilder();
EventLoopGroup bossGroup = new NioEventLoopGroup(HttpConstant.SWIFT_BOSS_THREADS, threadFactoryBuilder.setNameFormat("boss-group-%d").build());
EventLoopGroup workerGroup = new NioEventLoopGroup(HttpConstant.SWIFT_WORKER_THREADS, threadFactoryBuilder.setNameFormat("worker-group-%d").build());
try {
ServerBootstrap bootstrap = new ServerBootstrap();
bootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).handler(new LoggingHandler(LogLevel.INFO)).childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
if (ssl) {
SelfSignedCertificate ssc = new SelfSignedCertificate();
SslContext sslContext = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).build();
pipeline.addLast(sslContext.newHandler(ch.alloc()));
}
// 超时处理
pipeline.addLast(new IdleStateHandler(0, 0, HttpConstant.SWIFT_TIMEOUT));
// http request decode and response encode
pipeline.addLast(new HttpServerCodec());
// 将消息头和体聚合成FullHttpRequest和FullHttpResponse
pipeline.addLast(new HttpObjectAggregator(HttpConstant.SWIFT_MAX_CONTENT_LENGTH));
// 压缩处理
pipeline.addLast(new HttpContentCompressor(HttpConstant.SWIFT_COMPRESSION_LEVEL));
// 自定义http服务
pipeline.addLast(new HttpChannelHandler(handlerMapping));
}
});
Channel channel = bootstrap.bind(port).sync().channel();
channel.closeFuture().sync();
} catch (Exception e) {
logger.error(e.getMessage(), e);
} finally {
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
Aggregations