netty的启动过程是咋样的,就是那里启动了socket监听,咋处理请求的( 四 )
/*** Binder类继承SimpleChannelUpstreamHandler,SimpleChannelUpstreamHandler类主要处理上行消息的,*/ private final class Binder extends SimpleChannelUpstreamHandler { private final SocketAddress localAddress; private final Map\u0026lt;String, Object\u0026gt; childOptions = new HashMap\u0026lt;String, Object\u0026gt;(); private final DefaultChannelFuture bindFuture = new DefaultChannelFuture(null, false); Binder(SocketAddress localAddress) { this.localAddress = localAddress; } /*** *覆盖父类SimpleChannelUpstreamHandler中的channelOpen方法 */ @Override public void channelOpen( ChannelHandlerContext ctx, ChannelStateEvent evt) { try { evt.getChannel().getConfig().setPipelineFactory(getPipelineFactory()); // Split options into two categories: parent and child. Map\u0026lt;String, Object\u0026gt; allOptions = getOptions(); Map\u0026lt;String, Object\u0026gt; parentOptions = new HashMap\u0026lt;String, Object\u0026gt;(); for (Entry\u0026lt;String, Object\u0026gt; e: allOptions.entrySet()) { if (e.getKey().startsWith("child.")) { childOptions.put( e.getKey().substring(6), e.getValue()); } else if (!"pipelineFactory".equals(e.getKey())) { parentOptions.put(e.getKey(), e.getValue()); } } // Apply parent options. evt.getChannel().getConfig().setOptions(parentOptions); } finally { ctx.sendUpstream(evt); } evt.getChannel().bind(localAddress).addListener(new ChannelFutureListener() { public void operationComplete(ChannelFuture future) throws Exception { if (future.isSuccess()) { bindFuture.setSuccess(); } else { bindFuture.setFailure(future.getCause()); } } }); } @Override public void childChannelOpen( ChannelHandlerContext ctx, ChildChannelStateEvent e) throws Exception { // Apply child options. try { e.getChildChannel().getConfig().setOptions(childOptions); } catch (Throwable t) { fireExceptionCaught(e.getChildChannel(), t); } ctx.sendUpstream(e); } @Override public void exceptionCaught( ChannelHandlerContext ctx, ExceptionEvent e) throws Exception { bindFuture.setFailure(e.getCause()); ctx.sendUpstream(e); } }我们看到这个Bind类是extend SimpleChannelUpstreamHandler,这样问题就很明显了,channelOpen(ctx, evt)这个事件传递就传到了Bind类中的这个channelOpen方法。 我们顺着跟下去,会看到 evt.getChannel().bind(localAddress)这个方法。channel自然还是之前的NioSocketChannel,那么我们看这个bind方法做了哪些事情? 我们发现他调用了Channels.bind方法
推荐阅读
- 强寒潮来袭浙江启动低温雨雪冰冻灾害Ⅳ级应急响应
- 西藏航空一机长执飞过程中身体不适降落后送医去世
- 江苏省13市启动重污染天气黄色预警
- 兰州启动进口冷链食品监管总仓“出仓”须查验证明
- 朋友圈直播酒驾全过程?警方:当事人涉嫌酒驾接受调查
- “艺术无障碍平台”正式启动
- |共促和谐劳动关系 工会法律和集体协商技能竞赛启动
- 正点财经|
- |苏州·中国声学创新谷在常熟启动
- 大三学生准备日本留学过程中要不要准备考研
