netty的启动过程是咋样的,就是那里启动了socket监听,咋处理请求的
从EchoServer来看Server的调用流程echoServer是一个简单的输出服务,以下是官方example包下面的示例代码:public class EchoServer { private final int port; public EchoServer(int port) { this.port = port; } public void run() { // 创建一个ServerBootstrap启动工具类 // 构造方法里传入NioServerSocketChannelFactory ServerBootstrap bootstrap = new ServerBootstrap( //构建一个创建NioServerSocketChannel的工厂类NioServerSocketChannelFactory new NioServerSocketChannelFactory( Executors.newCachedThreadPool(), Executors.newCachedThreadPool())); // Set up the ChannelPipelineFactory.pipeline主要负责管理channel和netty之间消息的传递 bootstrap.setPipelineFactory(new ChannelPipelineFactory() { public ChannelPipeline getPipeline() throws Exception { //传入echoServerHandler return Channels.pipeline(new EchoServerHandler()); } }); // Bind and start to accept incoming connections. bootstrap.bind(new InetSocketAddress(port)); } //启动main函数 public static void main(String args) throws Exception { int port; if (args.length \u0026gt; 0) { port = Integer.parseInt(args); } else { port = 8080; } new EchoServer(port).run(); }}从上面的示例代码中我们看到了netty的代码非常精简优雅,但是如果不用netty,我们怎么实现一个EchoServer呢? public class EchoServer { public void serve(int port) throws IOException { // 创建一个ServerSocketChannel ServerSocketChannel serverChannel = ServerSocketChannel.open(); ServerSocket ss = serverChannel.socket(); InetSocketAddress address = new InetSocketAddress(port); ss.bind(address); // 设置为费阻塞模式 serverChannel.configureBlocking(false); // 创建一个selector Selector selector = Selector.open(); // serverChannel注册监听OP_ACCEPT事件 serverChannel.register(selector, SelectionKey.OP_ACCEPT); while (true) { try { selector.select(); } catch (IOException ex) { ex.printStackTrace(); // handle in a proper way break; } Set readyKeys = selector.selectedKeys(); Iterator iterator = readyKeys.iterator(); while (iterator.hasNext()) { SelectionKey key = (SelectionKey) iterator.next(); iterator.remove(); try { if (key.isAcceptable()) { ServerSocketChannel server = (ServerSocketChannel) key .channel(); SocketChannel client = server.accept(); System.out .println("Accepted connection from " + client); client.configureBlocking(false); client.register(selector, SelectionKey.OP_WRITE | SelectionKey.OP_READ, ByteBuffer.allocate(100)); } if (key.isReadable()) { SocketChannel client = (SocketChannel) key.channel(); ByteBuffer output = (ByteBuffer) key.attachment(); client.read(output); } if (key.isWritable()) { SocketChannel client = (SocketChannel) key.channel(); ByteBuffer output = (ByteBuffer) key.attachment(); output.flip(); client.write(output); output.compact(); } } catch (IOException ex) { key.cancel(); try { key.channel().close(); } catch (IOException cex) { } } } } } public static void main(String args) throws IOException { new EchoServer().serve(8080); }}
推荐阅读
- 强寒潮来袭浙江启动低温雨雪冰冻灾害Ⅳ级应急响应
- 西藏航空一机长执飞过程中身体不适降落后送医去世
- 江苏省13市启动重污染天气黄色预警
- 兰州启动进口冷链食品监管总仓“出仓”须查验证明
- 朋友圈直播酒驾全过程?警方:当事人涉嫌酒驾接受调查
- “艺术无障碍平台”正式启动
- |共促和谐劳动关系 工会法律和集体协商技能竞赛启动
- 正点财经|
- |苏州·中国声学创新谷在常熟启动
- 大三学生准备日本留学过程中要不要准备考研
