设为首页 加入收藏

TOP

Java NIO框架--Netty4的简单示例(一)
2015-02-02 14:24:03 来源: 作者: 【 】 浏览:25
Tags:Java NIO 框架 --Netty4 简单 示例

简介


相比Netty3, Netty4有很多显著的变化:


NioEventLoopGroup 是一个处理I/O操作的多线程事件环。即为Netty4里的线程池,在3.x里,一个Channel是由ChannelFactory创建的,同时新创建的Channel会自动注册到一个隐藏的I/O线程。 4.0使用新的名为EventLoopGroup的接口来替换ChannelFactory,它由一个或多个EventLoop来构成。一个新的 Channel不会自动注册到EventLoopGroup,但用户可以显式调用EventLoopGroup.register()来注册。在Server端的Bootstrap参数中,有两个EventLoopGroup,第一个通常称为'boss',用于接收发来的连接请求。第二个称为'worker',,用于处理boss接受并且注册给worker的连接中的信息。


ChannelInitializer是一个特殊的handler,用于方便的配置用户自定义的handler实现,如代码中所示。在channelRegistered的生命周期中会触发用户复写的initChannel(C ch)方法,并且在调用后会讲自身从channelPipeline中移除。


代码示例


import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.serialization.ClassResolvers;
import io.netty.handler.codec.serialization.ObjectDecoder;
import io.netty.handler.codec.serialization.ObjectEncoder;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import io.netty.handler.ssl.SslContext;
import io.netty.handler.ssl.util.SelfSignedCertificate;
?
/**
?* DateTime: 2015年1月5日 上午9:56:10
?*
?*/
public class HelloWorldServer {
? ? static final boolean SSL = System.getProperty("ssl") != null;
? ? static final int PORT = Integer.parseInt(System.getProperty("port", "8007"));
?
? ? public static void main(String[] args) throws Exception {
? ? ? ? // Configure SSL.
? ? ? ? final SslContext sslCtx;
? ? ? ? if (SSL) {
? ? ? ? ? ? SelfSignedCertificate ssc = new SelfSignedCertificate();
? ? ? ? ? ? sslCtx = SslContext.newServerContext(ssc.certificate(), ssc.privateKey());
? ? ? ? } else {
? ? ? ? ? ? sslCtx = null;
? ? ? ? }
?
? ? ? ? // Configure the server.
? ? ? ? EventLoopGroup bossGroup = new NioEventLoopGroup(1);
? ? ? ? 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() {
? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? public void initChannel(SocketChannel ch) throws Exception {
? ? ? ? ? ? ? ? ? ? ChannelPipeline p = ch.pipeline();
? ? ? ? ? ? ? ? ? ? if (sslCtx != null) {
? ? ? ? ? ? ? ? ? ? ? ? p.addLast(sslCtx.newHandler(ch.alloc()));
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? p.addLast(new LoggingHandler(LogLevel.INFO));
? ? ? ? ? ? ? ? ? ? p.addLast(
? ? ? ? ? ? ? ? ? ? ? ? new ObjectEncoder(),
? ? ? ? ? ? ? ? ? ? ? ? new ObjectDecoder(ClassResolvers.cacheDisabled(null)),
? ? ? ? ? ? ? ? ? ? ? ? new HelloWorldServerHandler());
? ? ? ? ? ? ? ? }
? ? ? ? ? ? });
?
? ? ? ? ? ? // Start the server.
? ? ? ? ? ? ChannelFuture f = b.bind(PORT).sync();
?
? ? ? ? ? ? // Wait until the server socket is closed.
? ? ? ? ? ? f.channel().closeFuture().sync();
? ? ? ? } finally {
? ? ? ? ? ? // Shut down all event loops to terminate all threads.
? ? ? ? ? ? bossGroup.shutdownGracefully();
? ? ? ? ? ? workerGroup.shutdownGracefully();
? ? ? ? }
? ? }
}
?
class HelloWorldServerHandler exten

首页 上一页 1 2 3 下一页 尾页 1/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Python版简易计算器的实现 下一篇Java多线程--信号量(Semaphore)

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: