use of org.apache.flink.shaded.netty4.io.netty.channel.nio.NioEventLoopGroup in project BRFS by zhangnianli.
the class NettyMessageServer method bind.
public void bind(int port) throws Exception {
// 配置服务端的NIO线程组
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).option(ChannelOption.SO_BACKLOG, 100).handler(new LoggingHandler(LogLevel.INFO)).childHandler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) {
ch.pipeline().addLast("http-decoder", new HttpRequestDecoder());
ch.pipeline().addLast("http-servercodec", new HttpServerCodec());
// 定义缓冲数据量
ch.pipeline().addLast("http-aggegator", new HttpObjectAggregator(1024 * 1024 * 64));
// ch.pipeline().addLast(new ProtobufVarint32FrameDecoder());
// ch.pipeline().addLast(
// new ProtobufDecoder(NettyMessageProto.NettyMessageReqRes.getDefaultInstance()));
// ch.pipeline().addLast(new ProtobufVarint32LengthFieldPrepender());
// ch.pipeline().addLast(new ProtobufEncoder());
// ch.pipeline().addLast("http-chunked",new ChunkedWriteHandler());
ch.pipeline().addLast(new NettyMessageServerHandler());
ch.pipeline().addLast("http-responseencoder", new HttpResponseEncoder());
}
});
// 绑定端口,同步等待成功
ChannelFuture f = b.bind(port).sync();
System.out.println("init start");
// 等待服务端监听端口关闭
f.channel().closeFuture().sync();
} finally {
// 优雅退出,释放线程池资源
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
use of org.apache.flink.shaded.netty4.io.netty.channel.nio.NioEventLoopGroup in project carbondata by apache.
the class NonSecureDictionaryClient method startClient.
/**
* start dictionary client
*
* @param address
* @param port
*/
@Override
public void startClient(String secretKey, String address, int port, boolean encryptSecureServer) {
LOGGER.audit("Starting client on " + address + " " + port);
long start = System.currentTimeMillis();
// Create an Event with 1 thread.
workerGroup = new NioEventLoopGroup(1);
Bootstrap clientBootstrap = new Bootstrap();
clientBootstrap.group(workerGroup).channel(NioSocketChannel.class).handler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
// Based on length provided at header, it collects all packets
pipeline.addLast("LengthDecoder", new LengthFieldBasedFrameDecoder(1048576, 0, 2, 0, 2));
pipeline.addLast("NonSecureDictionaryClientHandler", nonSecureDictionaryClientHandler);
}
});
clientBootstrap.connect(new InetSocketAddress(address, port));
LOGGER.info("Dictionary client Started, Total time spent : " + (System.currentTimeMillis() - start));
}
use of org.apache.flink.shaded.netty4.io.netty.channel.nio.NioEventLoopGroup in project carbondata by apache.
the class SecureDictionaryServer method startServer.
/**
* start dictionary server
*/
@Override
public void startServer() {
LOGGER.info("Starting Dictionary Server in Secure Mode");
secureDictionaryServerHandler = new SecureDictionaryServerHandler();
String workerThreads = CarbonProperties.getInstance().getProperty(CarbonCommonConstants.DICTIONARY_WORKER_THREADS, CarbonCommonConstants.DICTIONARY_WORKER_THREADS_DEFAULT);
boss = new NioEventLoopGroup(1);
worker = new NioEventLoopGroup(Integer.parseInt(workerThreads));
// Configure the server.
bindToPort();
}
use of org.apache.flink.shaded.netty4.io.netty.channel.nio.NioEventLoopGroup in project java-tron by tronprotocol.
the class UDPListener method start.
public void start() throws Exception {
NioEventLoopGroup group = new NioEventLoopGroup(1);
try {
discoveryExecutor = new DiscoveryExecutor(nodeManager);
discoveryExecutor.start();
while (!shutdown) {
Bootstrap b = new Bootstrap();
b.group(group).channel(NioDatagramChannel.class).handler(new ChannelInitializer<NioDatagramChannel>() {
@Override
public void initChannel(NioDatagramChannel ch) throws Exception {
ch.pipeline().addLast(stats.udp);
ch.pipeline().addLast(new ProtobufVarint32LengthFieldPrepender());
ch.pipeline().addLast(new ProtobufVarint32FrameDecoder());
ch.pipeline().addLast(new PacketDecoder());
MessageHandler messageHandler = new MessageHandler(ch, nodeManager);
nodeManager.setMessageSender(messageHandler);
ch.pipeline().addLast(messageHandler);
}
});
channel = b.bind(port).sync().channel();
logger.info("Discovery UDPListener started, bind port {}", port);
channel.closeFuture().sync();
if (shutdown) {
logger.info("Shutdown discovery UDPListener");
break;
}
logger.warn("UDP channel closed. Recreating after 5 sec pause...");
Thread.sleep(5000);
}
} catch (Exception e) {
if (e instanceof BindException && e.getMessage().contains("Address already in use")) {
logger.error("Port " + port + " is busy. Check if another instance is running with the same port.");
} else {
logger.error("Can't start discover: ", e);
}
} finally {
group.shutdownGracefully().sync();
}
}
use of org.apache.flink.shaded.netty4.io.netty.channel.nio.NioEventLoopGroup in project java-tron by tronprotocol.
the class PeerServer method start.
public void start(int port) {
bossGroup = new NioEventLoopGroup(1);
workerGroup = new NioEventLoopGroup();
tronChannelInitializer = ctx.getBean(TronChannelInitializer.class, "");
tronChannelInitializer.setNodeImpl(p2pNode);
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, this.args.getNodeConnectionTimeout());
b.handler(new LoggingHandler());
b.childHandler(tronChannelInitializer);
// Start the client.
logger.info("Listening for incoming connections, port: [{}] ", port);
logger.info("NodeId: [{}] ", Hex.toHexString(this.args.getMyKey().getNodeId()));
channelFuture = b.bind(port).sync();
listening = true;
// Wait until the connection is closed.
channelFuture.channel().closeFuture().sync();
logger.debug("Connection is closed");
} catch (Exception e) {
logger.debug("Exception: {} ({})", e.getMessage(), e.getClass().getName());
throw new Error("Server Disconnected");
} finally {
workerGroup.shutdownGracefully();
bossGroup.shutdownGracefully();
listening = false;
}
}
Aggregations