Netty ã®åºæ¬
ããæ°æ¥ãNettyã«ã¤ãã¦èª¿ã¹ã¦ããã®ã§ç解ã§ãããã¨ãæ¸ãã¾ãã
JBoss Netty
Netty 㯠Java ã§éåæãã¤ãã³ãé§åã®ãããã¯ã¼ã¯ã¢ããªãä½ãããã®ãã¬ã¼ã ã¯ã¼ã¯ã§ããNetty ã使ãã¨æ©ãã¦ç°¡åã«ãã¤ããã©ã¼ãã³ã¹ããã¤ã¹ã±ã¼ã«ã§ã¡ã³ããã³ã¹æ§ããããã®ãä½ãã¾ããããã¨ãåãã®å
¨é¨ä¹ãã§ãã
- ãªããã¤ããã©ã¼ãã³ã¹ããã¤ã¹ã±ã¼ã«ãï¼
Netty 㯠Java NIO(New I/O)ãã©ãããã¦ãã¦ããã³ããããã³ã°ãªIOæä½ãã§ãã¾ãããã®ããã1ã¤ã®ã³ãã¯ã·ã§ã³ã«ãã£ã¨1ã¹ã¬ãããå²ãå½ã¦ãå¿ è¦ããªãããå¹çã®ãããªã½ã¼ã¹æ¶è²»ããã¾ããå¾æ¥ã®ããããã³ã°ãªOIO(Old I/O)ããµãã¼ããã¦ãããå ããªå¤æ´ã§å¥½ããªæ¹ã使ãã¾ããã¾ããNIOã®è¤éãªByte BufferãChannelBufferã¨ãããªãã¸ã§ã¯ãã«æ½è±¡åããä¸å¿ è¦ãªã³ãã¼ãçºçããªããªã©ã®å·¥å¤«ãããã¦ããããã§ãã
- ãªãã¡ã³ããã³ã¹æ§ããããï¼
å¦çãã¬ã¤ã¤ã¼ãã¨ã«åé¢ã§ããããã§ããæå·åã®å±¤ããã¸ãã¹ãã¸ãã¯ã®å±¤ãªã©ãã®å±¤ã ããæ°ã«ããã°ããããã«ä½ãã¾ãã
Handler 㨠ChannelPipeline
Handler 㨠ChannelPipelineããã®ï¼ã¤ã Netty ã®åºç¤ã§ããããããç解ããã°å¤§ä½okã§ããä»ã®äºã¯å¤§ãããã¨ã§ã¯ããã¾ããã
å³ã®äººãã¼ã¯ã Handler ã表ãããã®Handlerãããã¤ãé
ç½®ãããã®ã ChannelPipeline ã§ããä¸æ¹åã®æµããUpStreamã¨è¨ãã¾ãããè¦ã¯åä¿¡ã§ããä¸æ¹åã¯DownStreamã§ãéä¿¡ã«å½ããã¾ããServerã¢ããªã±ã¼ã·ã§ã³ãªããªã¯ã¨ã¹ããåãåã£ã¦ã¬ã¹ãã³ã¹ãè¿ãã¨æãã®ã§ãUpStreamã®å¾ã«DownStreamãèµ·ããã¾ããClientã¢ããªã±ã¼ã·ã§ã³ãªãããªã¯ã¨ã¹ããæãã¦ã¬ã¹ãã³ã¹ãåãåãã®ã§ãDownStreamã®å¾ã«UpStreamã§ããã
æµãã¦ãããã®ã¯ãã¼ã¿ãã¨ããã¨å°ãéãã¾ããã¤ãã³ãã§ãã"ãã¼ã¿ãåä¿¡ãã"ã"Openãã"ã"Closeãã"ããªã©ã®ã¤ãã³ããæµãã¾ãããã¼ã¿ãåä¿¡ããæã®ã¤ãã³ãããã¯ãã¼ã¿ãåãåºããã®ã§ãã¾ãã¯ãã¼ã¿ãæµãã¦ããã¨èãã¦ãããã¨æãã¾ãã
æåã«ã¤ãã³ããåãåã£ãæä¸å±¤ã®Handler 㯠ã¤ãã³ãã«å
¥ã£ã¦ãããã¼ã¿ãdecodeãããªã©ã®å¦çããã¦ä¸ã¤ä¸ã®Handler ã«å¦çå¾ã®ãã¼ã¿ããã£ä»ããã¤ãã³ãã渡ãã¾ãããã®ããã«Handlerãã¨ã«å¦çãåãããã¨ã«ãã£ã¦ãä¸ã¤ä¸ã¤ã®Handlerã¯ç¬ç«ãã¦èªåã®ä»äºã«å°å¿µã§ãã¾ããç¬ç«ãã¦ããã¨ã¯è¨ã£ã¦ããeconde/decodeå¦çãªã©ãèããã¨åããããã«å¤ãã®å ´åãä¸ä¸ã®Handlerã«ä¾åæ§ãæã£ã¦ã¯ãã¾ããã
次ã®Handlerã«å¦çã渡ãã¨ããããã«åãã¹ã¬ããã§æ¬¡ã®Handlerãå¦çãå§ããããã§ã¯ããã¾ãããåHandlerã¯ä¸ä¸ã®é çªéãã«å¦çã¯ãããã®ã§ãããHandlerãã¨ã«ã¹ã¬ãããã¼ã«ããå²ãå½ã¦ãããã¹ã¬ããã§å¦çãããã¾ãã
ä¸çªä¸ã®Handlerã¾ã§ã¤ãã³ãã渡ãããããã«ä¸ã«æ¸¡ããã¨ããã¤ãã³ãã¯åã«ç¡è¦ããã¾ããServer ã§ã¯å¤ãã®å ´åãä¸çªä¸ã®Handlerããªã¯ã¨ã¹ããå¦çãã¦ã¬ã¹ãã³ã¹ãè¿ãå¦çãè¡ããDownStreamãå§ã¾ãã¾ãã
å
ç¨ã®å³ã§ã¯ç°¡ç¥ãã¦Pipelineã«ã¯UpStreamã¨DownStreamã®ï¼ã¤ã®ã©ã¤ã³ãããããã«æ¸ãã¾ããããæ£ç¢ºã«ã¯UpStreamç¨ã®HandlerãDownStreamç¨ã®Handlerãä¸ã¤ã®ã©ã¤ã³ä¸ã«é
ç½®ãããããããã®æ¹åã«å¯¾å¿ãã¦ããªãHandlerã¯åã«é£ã°ãããã ãã§ãã
æ´ã«è¨ãã¨ãHandlerã¯UpStreamã«ãDownStreamã«ã対å¿ãããã¨ãã§ããã®ã§ãããããå³ã«ãªãã¾ãã
encode/decodeå¦çãæå·å/解èªå¦çãªã©å¯¾ã®å¦çã¯ä¸ã¤ã®Handlerã§å¦çããã®ãããã¨æãããããããã¾ããããå®éã«ã¯UpStreamé¨åã ãã®æåãå¤ãããã¨ããæããããããæè»ã«å¤æ´ããããã«ã¯å¥ã
ã®Handlerã«ãã¦ãããã»ããããããããã¾ããã
UpStream ã«å¯¾å¿ããããã«ã¯ChannelUpstreamHandlerã¤ã³ã¿ã¼ãã§ã¼ã¹ãå®è£ ããå¿ è¦ãããã¾ãã
public interface ChannelUpstreamHandler extends ChannelHandler { void handleUpstream(ChannelHandlerContext ctx, ChannelEvent e) throws Exception; }
DownStream ã«å¯¾å¿ããããã«ã¯ChannelDownstreamHandlerã¤ã³ã¿ã¼ãã§ã¼ã¹ãå®è£ ãã¾ãã
public interface ChannelDownstreamHandler extends ChannelHandler { void handleDownstream(ChannelHandlerContext ctx, ChannelEvent e) throws Exception; }
ãããããã£ãä¸ã¤ã®ã¡ã½ããã§ããNettyã«ã¯ä¸è¨ã¤ã³ã¿ã¼ãã§ã¼ã¹ãå®è£ ãã便å©ãªHandlerãããããbuilt-inã§ç¨æããã¦ãã¦ããããç¶æ¿ãã¦Handlerãä½ãã¨ãã¯ä¸è¨ã®ã¡ã½ããã§ã¯ãªããä»ã®ã¡ã½ãããOverrideããã ãã®ãã¨ãå¤ãã§ããããããã㯠handleUpstream/handleDownstream ã¡ã½ããããä»ã®ã¡ã½ãããå¼ã³åºããã¦ããããã§ãããåºæ¬ã¯ãã®ã¡ã½ãããUpStreamãDownStreamã®ã¤ãã³ããå¦çããã¨ãããã¨ãå¿ããªãã§ãã ãããå¼æ°ã§æ¸¡ããã ChannelHandlerContext 㨠ChannelEvent ã¨ã¯ãªãã§ããããã
ChannelHandlerContext
ChannelHandlerContext ã使ã㨠ChannelPipeline ãä»ã® Handler ã«ä½ç¨ãããã¨ãã§ãã¾ãã
- ã¤ãã³ãã®éä¿¡
次ã®Handlerã«ã¤ãã³ãã渡ãããã«ã¯ãå¿ ããã®ChannelHandlerContextã使ç¨ãã¦æ示çã«ã¤ãã³ãã渡ããªããã°ããã¾ããã
ctx.sendUpstream(ChannelEvent) ctx.sendDownstream(ChannelEvent)
ä¸è¨ã¡ã½ãããå¼ã°ãªãã¨æ¬¡ã®Handlerã«ã¤ãã³ãã渡ããã¾ããã
ããã built-in ã® Handler ãç¶æ¿ããæã¯ãããªãã§ããã¨æãæãããã¾ãããããã¯ç¶æ¿å
ã®Handlerããã£ã¦ããã¦ããããã§ããNetty ãèªåçã«æ¬¡ã®Handlerã«å¦çã移ãã¦ãããã¨æãããããã¾ããããframing ãªã©ã®ããã«åä¿¡ãã¼ã¿ãä¸å®å¤ã¾ã§æºã¾ãã¾ã§æ¬¡ã®Handlerã«ã¤ãã³ããéããããªãæãªã©ãããã®ã§ãããªã£ã¦ãããã ã¨æãã¾ãã
- ChannelPipelineãåçã«å¤æ´ãã
ctx.getPipeline()
ãå¼ã¶ã¨ãã®Handlerã®å ¥ã£ã¦ããChannelPipelineãåå¾ã§ããHandlerã®åçãªè¿½å ãåé¤ãã§ãã¾ããä¾ãã°å¿ è¦ã«å¿ãã¦SSLã®Handlerã追å ããã¨ãããã¨ãã§ãã¾ãã<æ¸ãããã追è¨äºå®>
ãåèã
jboss netty 3.2 javadoc
javadoc ライクにソースが見れる
チュートリアルとExample
ãã®æ¹ã®ããã°(è±èª)ãã¨ã¦ãä¸å¯§ã«æ¸ããã¦ãã¦åèã«ãªãã¾ãããæ¯éèªãã§ã¿ã¦ãã ããã
Handler について
NIO FWの比較 パフォーマンス
NIO FWの比較 メモリ
NIO FWの比較 機能
NIO FWの比較 プロトコル