Netty is an asynchronous event-driven network application framework for Java. It provides an asynchronous model with non-blocking I/O based on Java NIO. To create an HTTP server with Netty, one sets up thread pools for handling connections and requests, provides a ChannelFactory and PipelineFactory, and puts it all together with a Bootstrap. The server can then be started to listen for connections. Custom handlers can be added to the pipeline to process requests. Channels are gathered and closed to release resources when the server is shut down. TCP options like keepAlive and noDelay can be configured but their effects should be measured for each application.
9. blocking vs non-blocking
OS
select()/poll() - traditional POSIX polling O(n)
epoll() - event-based polling O(1), Linux 2.5.44+
kqueue - FreeBSD, Mac OS X
I/O Completion Ports - Windows NT 3.5+, Solaris 10+
10. blocking vs non-blocking
Java
SelectableChannel.register(selector) - blocking operation,
registers an "interest" for IO events
Selector.select - non-blocking operation, checks for
occurred events
19. Creating an HTTP server
Set up thread pools
boss pool for handling incoming connections
worker pool for processing IO
piaefnlEeuo bsPo =Eeuosnwahdhedol)
rvt ia xctr osol xctr.eCceTraPo(
;
piaefnlEeuo wrePo =Eeuosnwahdhedol
rvt ia xctr okrol xctr.eCceTraPo
(;
)
20. Creating an HTTP server
Provide ChannelFactory
piaefnlCanlatr canlatr =
rvt ia hneFcoy hneFcoy
nwNoevrokthneFcoybsPo,wrePo)
e iSreSceCanlatr(osol okrol;
21. Creating an HTTP server
Set up PipelineFactory
piaefnlCanlieieatr pplnFcoy=
rvt ia hnePplnFcoy ieieatr
nwCanlieieatr( {
e hnePplnFcoy)
@vrie
Oerd
pbi Canlieiegtieie)trw Ecpin{
ulc hnePpln ePpln( hos xeto
Canlieiep=Canl.ieie)
hnePpln hnesppln(;
padat"tpevroe" nwHtSreCdc);
.dLs(htSreCdc, e tpevroe()
padat"ade" nwCsoRqetade(lCanl);
.dLs(hnlr, e utmeusHnlralhnes)
rtr p
eun ;
}
};
22. Creating an HTTP server
Put all together with Bootstrap
piaefnlSreBosrpbosrp=nwSreBosrpca
rvt ia evrotta otta e evrotta(h
neFcoy;
nlatr)
...
bosrpstieieatr(ieieatr)
otta.ePplnFcoypplnFcoy;
/ Otosfraprn canl-i acpscnetos
/ pin o aet hne t cet oncin
bosrpstpin"oaAdes,nwIeSceAdespr);
otta.eOto(lcldrs" e ntoktdrs(ot)
bosrpstpin"esAdes,tu)
otta.eOto(ruedrs" re;
/ Otosfriscide -te poesI
/ pin o t hlrn hy rcs O
bosrpstpin"hl.cNDly,tu)
otta.eOto(cidtpoea" re;
bosrpstpin"hl.eplv" fle;
otta.eOto(cidkeAie, as)
23. Creating an HTTP server
Writing a custom handler
casCsoHnlretnsSmlCanlptemade {
ls utmade xed ipehneUsraHnlr
...
@vrie
Oerd
pbi vi msaeeevdCanladeCnetcx Msae
ulc od esgRcie(hneHnlrotx t, esgE
vn e trw Ecpin{
et ) hos xeto
HtRqetrq=(tpeus)egtesg(;
tpeus e HtRqet .eMsae)
/ pouersos adwiei t acanl
/ rdc epne n rt t o hne
HtRsos rs=nwDfuttpepneHT__,O)
tpepne e e ealHtRsos(TP11 K;
Canluueftr =Canl.rt(t.eCanl) rs;
hneFtr uue hneswiecxgthne(, e)
ftr.dLsee(hneFtrLsee.LS)
uueaditnrCanluueitnrCOE;
}
}
24. Creating an HTTP server
Gather channels for future closing
piaefnlCanlru alhnes=nwDfuthneGop
rvt ia hneGop lCanl e ealCanlru(
)
...
/ aanisd orcso hnlr
/ gi nie u utm ade
casCsoHnlretnsSmlCanlptemade {
ls utmade xed ipehneUsraHnlr
...
@vrie
Oerd
pbi vi canlpnCanladeCnetcx Canltt
ulc od hneOe(hneHnlrotx t, hneSae
Eete trw Ecpin{
vn ) hos xeto
Canlcanl=egthne(;
hne hne .eCanl)
alhnesadcanl;
lCanl.d(hne)
}
}
25. Creating an HTTP server
Release resources
close gathered channels
release external resources
alhnescoe)aatnnerpil(;
lCanl.ls(.wiUitrutby)
bosrprlaexenleore(;
otta.eesEtraRsucs)
26. TCPnodelay, TCPkeepalive
Remember that bootstrap code?
/ Otosfriscide -te poesI
/ pin o t hlrn hy rcs O
bosrpstpin"hl.cNDly,tu)
otta.eOto(cidtpoea" re;
bosrpstpin"hl.eplv" fle;
otta.eOto(cidkeAie, as)
What do these options mean?