- 浏览: 778335 次
- 性别:
- 来自: 深圳
文章分类
最新评论
-
萨琳娜啊:
Java读源码之Netty深入剖析网盘地址:https://p ...
Netty源码学习-FileRegion -
飞天奔月:
写得有趣 ^_^
那一年你定义了一个接口 -
GoldRoger:
第二个方法很好
java-判断一个自然数是否是某个数的平方。当然不能使用开方运算 -
bylijinnan:
<script>alert("close ...
自己动手实现Java Validation -
paul920531:
39行有个bug:"int j=new Random ...
java-蓄水池抽样-要求从N个元素中随机的抽取k个元素,其中N无法确定
本文是阅读以下两篇文章时:
http://seeallhearall.blogspot.com/2012/05/netty-tutorial-part-1-introduction-to.html
http://seeallhearall.blogspot.com/2012/06/netty-tutorial-part-15-on-channel.html
我的一些笔记
=================笔记1
static class ServerDateHandler extends SimpleChannelHandler { Random random = new Random(System.nanoTime()); public void messageReceived(ChannelHandlerContext ctx,MessageEvent e) throws Exception { Date date = (Date)e.getMessage(); // Here's the REALLY important business service at the end of the pipeline long newTime = (date.getTime() + random.nextInt()); Date newDate = new Date(newTime); slog("Hey Guys ! I got a date ! [" + date + "] and I modified it to [" + newDate + "]"); // Send back the reponse Channel channel = e.getChannel(); ChannelFuture channelFuture = Channels.future(e.getChannel()); ChannelEvent responseEvent = new DownstreamMessageEvent(channel, channelFuture, newDate, channel.getRemoteAddress()); ctx.sendDownstream(responseEvent); // But still send it upstream because there might be another handler super.messageReceived(ctx, e); } }
为什么不直接这样:ctx.getChannel().write(newDate)?
作者是这样解释的:
If we wrote to the Channel directly, it would start from the "top" of the pipeline and might be pushed through handlers that are not indented to be called with the return Date.
不是很理解,看看源码:
e.getChannel().write最终调用DefaultChannelPipeline,从最后一个ChannelHandler(tail)往前,
消息会经过所有的DownstreamHandler:
public void sendDownstream(ChannelEvent e) { DefaultChannelHandlerContext tail = getActualDownstreamContext(this.tail); if (tail == null) { try { getSink().eventSunk(this, e); return; } catch (Throwable t) { notifyHandlerException(e, t); return; } } sendDownstream(tail, e); }
ctx.sendDownstream则只从当前的ChannelHandler往前走,也就是,
只有在当前ChannelHandler之前的ChannelHandler,才会经过:
public void sendDownstream(ChannelEvent e) { DefaultChannelHandlerContext prev = getActualDownstreamContext(this.prev); if (prev == null) { try { getSink().eventSunk(DefaultChannelPipeline.this, e); } catch (Throwable t) { notifyHandlerException(e, t); } } else { DefaultChannelPipeline.this.sendDownstream(prev, e); } }
这样可以避免消息穿过不必要的ChannelHandler,当然,如果清楚地知道,
那些“不必要的ChannelHandler”不会修改消息,
那直接e.getChannel().write也未尝不可
=================笔记2
a Channel provides the interface to connect and write to the destination represented by the Channel. No read ? you might ask ? Nope. Remember, it's like the asynchronous getWidgetCountmethod mentioned above. There's no return value.
Channel没有read方法?那怎么读取消息?
原来,读取到的消息,都封装在MessageEvent里面了,
它帮你读取好了,再fireMessageReceived通知你处理——这就是所谓的“异步”。详见NioWorker的read
=================笔记3
In the server, there is one boss thread allocated per listening socket. For example, if you opened two server ports such as 80 and 443, you will have two boss threads. In the client, there is only one boss thread*
=================笔记4
Please note that this decoder must be used with a proper FrameDecoder such as DelimiterBasedFrameDecoder if you are using a stream-based transport such as TCP/IP. // Decoders pipeline.addLast("frameDecoder", new DelimiterBasedFrameDecoder(80, Delimiters.lineDelimiter())); pipeline.addLast("stringDecoder", new StringDecoder(CharsetUtil.UTF_8));
为什么 StringDecoder要和FrameDecoder在一起使用?
因为任何消息的到来,刚开始都是byte[],如何进行“分段”( segment the byte stream,
分段方案有很多,例如length+content、或者delimiter等等),是第一个ChannelHandler的工作。
StringDecoder并没有“分段”的功能,它没有extends FrameDecoder
因此要在它前面安排一个FrameDecoder
=================笔记5
自定义delimiter:
ChannelBuffer delimiter = ChannelBuffers.wrappedBuffer("THEBIG-D".getBytes())
=================笔记6
In the server, there is one boss thread allocated per listening socket. In the client, there is only one boss thread* 1.The boss thread can be released when there is no work to do and is created lazily, but it may be more efficient to pool a small number of threads than create a new one when required and destroying it when idle. 2.It is possible that one might want to create several different channel factories and rather than giving each one their own boss pool, they can all share one.
为什么client只需要一个boss thread但仍然采用Excutor?
除了作者说的两个原因之外,我认为还有个原因是出于扩展性的考虑,允许你传入不同的Executor
=================笔记7
The Worker Threads: Worker threads perform all the asynchronous I/O. They are not general purpose threads and developers should take precautions not to assign unrelated tasks to threads in this pool which may cause the threads to block, be rendered unable to perform their real work which in turn may cause deadlocks and an untold number of performance issues.
第一次看这段话不理解,现在看来作者的意思应该是:worker thread是专门为IO而生,且是异步的。开发者不应该把其他工作交给worker thread运行
=================笔记8
Once you're done with a ChannelFactory, be sure to callreleaseExternalResources() on the factory. This will ensure that all its resources are released.
要记得调用 ChannelFactory的releaseExternalResources来释放资源:
bootstrap.releaseExternalResources(); public class Bootstrap...{ public void releaseExternalResources() { ChannelFactory factory = this.factory; if (factory != null) { factory.releaseExternalResources(); } } }
发表评论
-
TCP的TIME-WAIT
2014-04-23 16:35 1168原文连接:http://vincent.bernat.im/e ... -
《TCPIP详解卷1》学习-拥塞避免
2014-01-15 15:16 159拥塞避免算法、 ... -
Netty源码学习-HTTP-tunnel
2014-01-14 18:19 4256Netty关于HTTP tunnel的说明: http://d ... -
Netty源码学习-FileRegion
2013-12-31 17:17 5577今天看org.jboss.netty.example.http ... -
Netty源码学习-HttpChunkAggregator-HttpRequestEncoder-HttpResponseDecoder
2013-12-27 16:10 4004今天看Netty如何实现一个Http Server org.j ... -
Netty源码学习-ReadTimeoutHandler
2013-12-26 17:53 3770ReadTimeoutHandler的实现思 ... -
Netty源码学习-ChannelHandler
2013-12-25 18:12 1594一般来说,“有状态”的ChannelHandler不应 ... -
Netty源码学习-ServerBootstrap启动及事件处理过程
2013-12-19 20:11 10696Netty是采用了Reactor模式的多线程版本,建议先看下面 ... -
Netty源码学习-Java-NIO-Reactor
2013-12-19 18:21 4853Netty里面采用了NIO-based Reactor Pat ... -
Netty源码学习-ReplayingDecoder
2013-12-13 20:21 4225ReplayingDecoder是FrameDecoder的子 ... -
Netty源码学习-DefaultChannelPipeline2
2013-12-11 15:47 1249Netty3的API http://docs.jboss.or ... -
Netty源码学习-CompositeChannelBuffer
2013-12-06 15:54 2727CompositeChannelBuffer体现了Netty的 ... -
Netty源码学习-DelimiterBasedFrameDecoder
2013-12-05 18:36 9486看DelimiterBasedFrameDecoder的AP ... -
Netty源码学习-ObjectEncoder和ObjectDecoder
2013-12-05 16:06 4953Netty中传递对象的思路很直观: Netty中数据的传递是基 ... -
Netty源码学习-LengthFieldBasedFrameDecoder
2013-12-05 15:20 7242先看看LengthFieldBasedFrameDecoder ... -
Netty源码学习-FrameDecoder
2013-11-28 18:38 3870Netty 3.x的user guide里FrameDecod ... -
Netty源码学习-DefaultChannelPipeline
2013-11-27 17:00 2185package com.ljn.channel; /** ...
相关推荐
netty学习笔记
Netty学习笔记_Springboot实现自定义协议.docx Netty学习笔记_Springboot实现自定义协议.docx Netty学习笔记_Springboot实现自定义协议.docx
Netty基础,用于学习Netty,参考黑马程序员的netty教程
netty学习笔记
netty入门到精通学习笔记和demo
技术分享:Netty全面学习笔记(非常详细具体,排版很好)
Netty学习笔记, 包括代码学习, 源码分析.基于netty4.1, 用于记录Netty学习过程的各 种信息和心得
NULL 博文链接:https://bijian1013.iteye.com/blog/2340636
Netty全套学习资源(包括源码、笔记、学习文档等)
最近在做WEB 开发中用到了netty技术,在网上收集了很多资料,通过实际项目的应用,整理出了该文档,供朋友们相互学习(仅供参考)
NULL 博文链接:https://bijian1013.iteye.com/blog/2340639
NULL 博文链接:https://bijian1013.iteye.com/blog/2340632
NULL 博文链接:https://bijian1013.iteye.com/blog/2340638
NULL 博文链接:https://bijian1013.iteye.com/blog/2340634
NULL 博文链接:https://bijian1013.iteye.com/blog/2340631
对于学习netty做的个人笔记,基本上涵盖了netty的基本结构,原理,以及重要的组件知识。
netty 学习之路 项目笔记 项目说明 1、所谓BIO编程,就是使用JDK1.4之前的api进行编程,在这里我们以ServerSocket和Socket为例进行讲解, 编写一个时间服务的C/S架构应用。 2、client可以发送请求指令"GET CURRENT ...
集合、NIO、Netty、Thread、MySql、Hive、HBase、Kafka、Spark、Fink等学习笔记