netty from the trenches
TRANSCRIPT
![Page 1: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/1.jpg)
Netty from the trenches
June 2015@jordi9
![Page 2: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/2.jpg)
Netty…?
![Page 3: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/3.jpg)
Netty is an asynchronous event-driven
network application framework
for rapid development of
maintainable high performance
protocol servers & clients.
![Page 4: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/4.jpg)
Netty is an asynchronous event-driven
network application framework
for rapid development of
maintainable high performance
protocol servers & clients.
![Page 5: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/5.jpg)
![Page 6: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/6.jpg)
Netty is a NIO client server
framework which enables quick and easy development
of network applications such
as protocol servers and clients. It
greatly simplifies and streamlines
network programming such as TCP and UDP
socket server.
![Page 7: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/7.jpg)
Netty is a NIO client server
framework which enables quick and easy development
of network applications such
as protocol servers and clients. It
greatly simplifies and streamlines
network programming such as TCP and UDP
socket server.
![Page 8: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/8.jpg)
Netty is a NIO client server
framework which enables quick and easy development
of network applications such
as protocol servers and clients. It
greatly simplifies and streamlines
network programming such as TCP and UDP
socket server.
![Page 9: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/9.jpg)
OH, MY
![Page 10: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/10.jpg)
The Freaking Basics
![Page 11: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/11.jpg)
You’ve heard of:
Async apps
![Page 12: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/12.jpg)
You’ve heard of:
Event driven frameworks
![Page 13: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/13.jpg)
You’ve heard of:
non-blockingoperations
![Page 14: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/14.jpg)
You’ve heard of:Node is cooler
because...
![Page 15: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/15.jpg)
I/O
![Page 16: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/16.jpg)
I/O is everywhere
![Page 17: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/17.jpg)
I/O, approx:
CPU Memory
Device
![Page 18: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/18.jpg)
read()
![Page 19: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/19.jpg)
read()
//wait...
![Page 20: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/20.jpg)
read()
//wait...
keepWorkingBro()
![Page 21: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/21.jpg)
Our programBLOCKS
![Page 22: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/22.jpg)
Resources WASTED
![Page 23: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/23.jpg)
You know what I’m talking
about
![Page 24: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/24.jpg)
WebserverRequest
Database
//wait...
![Page 25: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/25.jpg)
OH, Parallelism!
![Page 26: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/26.jpg)
new Thread();
![Page 27: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/27.jpg)
WebserverRequest
Database
//wait...
![Page 28: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/28.jpg)
WebserverRequest
Database
//wait...
Request
Database
//wait...
![Page 29: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/29.jpg)
WebserverRequest
Database
//wait...
Request
Database
//wait...
Request
Database
//wait...
![Page 30: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/30.jpg)
A mess
![Page 31: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/31.jpg)
We can do better
![Page 32: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/32.jpg)
read()
![Page 33: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/33.jpg)
read()
keepWorkingBro()
![Page 34: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/34.jpg)
read()
keepWorkingBro()
//I’m //done!
![Page 35: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/35.jpg)
You (kinda) did this:
Listeners
![Page 36: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/36.jpg)
You (kinda) did this:
Guava’s EventBus
![Page 37: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/37.jpg)
You (kinda) did this:
Javascript callbacks
![Page 38: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/38.jpg)
Hollywood principle
“Don’t call us,we’ll call you”
![Page 39: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/39.jpg)
Keep working, instead of
waiting
![Page 40: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/40.jpg)
Hi, Async I/O
![Page 41: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/41.jpg)
NIOwith Java
![Page 42: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/42.jpg)
java.nio
![Page 43: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/43.jpg)
since 1.4, 2002java.nio
![Page 44: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/44.jpg)
since 1.7: NIO2more goodies
![Page 45: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/45.jpg)
Hi, Complexity
![Page 46: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/46.jpg)
Netty to the Rescue
![Page 47: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/47.jpg)
Built on top of:
java.nio
![Page 48: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/48.jpg)
Built on top of:
java.net
![Page 49: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/49.jpg)
Dependecy-haters:
Just JDK 1.6+
![Page 50: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/50.jpg)
ONEbig difference
![Page 51: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/51.jpg)
All Netty APIsare async
![Page 52: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/52.jpg)
It’s can be one more tool!
![Page 53: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/53.jpg)
What the hell is Netty for?
![Page 54: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/54.jpg)
Think...
![Page 55: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/55.jpg)
HTTP everywhere
![Page 56: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/56.jpg)
Really BRO?
![Page 57: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/57.jpg)
Maybe, you’re trying to
implement a protocol
![Page 58: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/58.jpg)
Finally, hello Netty!
![Page 59: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/59.jpg)
Netty:
Tool to implement network
applications
![Page 60: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/60.jpg)
Netty:
Both from server and client side
![Page 61: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/61.jpg)
Netty:
NIO everywhere
![Page 62: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/62.jpg)
Why Netty?
![Page 63: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/63.jpg)
Netty has been designed carefully
with the experiences
earned from the implementation of a lot of protocols
such as FTP, SMTP, HTTP, and
various binary and text-based legacy
protocols. As a result, Netty has
succeeded to find a way to achieve
ease of development, performance, stability, and
flexibility without a compromise
![Page 64: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/64.jpg)
Netty has been designed carefully
with the experiences
earned from the implementation of a lot of protocols
such as FTP, SMTP, HTTP, and
various binary and text-based legacy
protocols. As a result, Netty has
succeeded to find a way to achieve
ease of development, performance, stability, and
flexibility without a compromise
![Page 65: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/65.jpg)
Netty has been designed carefully
with the experiences
earned from the implementation of a lot of protocols
such as FTP, SMTP, HTTP, and
various binary and text-based legacy
protocols. As a result, Netty has
succeeded to find a way to achieve...
![Page 66: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/66.jpg)
Ease of development
![Page 67: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/67.jpg)
Performance
![Page 68: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/68.jpg)
Stability
![Page 69: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/69.jpg)
Flexibility
![Page 70: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/70.jpg)
Without a compromise
![Page 71: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/71.jpg)
IT’S TRUE
![Page 72: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/72.jpg)
“Implementing a protocol”
NOT THAT HARD
![Page 73: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/73.jpg)
Most common protocols:
out-of-the-box
![Page 74: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/74.jpg)
Boring Protocols:
HTTP
![Page 75: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/75.jpg)
Boring Protocols:
SSL
![Page 76: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/76.jpg)
Boring Protocols:
UDP
![Page 77: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/77.jpg)
Cooler Protocols:
SPDY
![Page 78: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/78.jpg)
Cooler Protocols:
HTTP/2
![Page 79: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/79.jpg)
Cooler Protocols:
Protobuf
![Page 80: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/80.jpg)
Even Protocols:
Memcache
![Page 81: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/81.jpg)
Not only I/O
![Page 82: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/82.jpg)
Powerful Thread model
![Page 83: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/83.jpg)
But, where’s the downside?
![Page 84: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/84.jpg)
Downers:
Constant API changes*
![Page 85: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/85.jpg)
Downers:
Constant API changes*
*For the best
![Page 86: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/86.jpg)
Downers:
Learning curve...
![Page 87: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/87.jpg)
Downers:
Learning curve...
Buy Netty in Action :P
![Page 88: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/88.jpg)
Downers:
Join the mailing list
![Page 89: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/89.jpg)
Downers:
Join the mailing list
You’ll see how STUPID you are
![Page 90: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/90.jpg)
Downers:
Join the mailing list
But you’ll learn ALOT ;)
![Page 91: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/91.jpg)
Netty,The Code
![Page 92: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/92.jpg)
Daytime Protocol
![Page 93: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/93.jpg)
![Page 94: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/94.jpg)
TheServer
![Page 95: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/95.jpg)
class SimpleDaytimeHandler extends ChannelInboundHandlerAdapter {
}
The Hand e
![Page 96: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/96.jpg)
class SimpleDaytimeHandler extends ChannelInboundHandlerAdapter {
// Handler = Business Logic
}
The Hand e
![Page 97: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/97.jpg)
class SimpleDaytimeHandler extends ChannelInboundHandlerAdapter {
// ChannelHandler: handles operations // for that Channel, duh
}
The Hand e
![Page 98: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/98.jpg)
class SimpleDaytimeHandler extends ChannelInboundHandlerAdapter {
// Inbound handler: incoming traffic, // dispatch events to next handler// If we have inbound...
}
The Hand e
![Page 99: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/99.jpg)
class SimpleDaytimeHandler extends ChannelInboundHandlerAdapter {
// Outbound handler: same, but the // other direction. Yeah, there’s some// flow between Handlers (eg: Pipeline)
}
The Hand e
![Page 100: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/100.jpg)
class SimpleDaytimeHandler extends ChannelInboundHandlerAdapter {
// Inbound + Outbound...
}
The Hand e
![Page 101: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/101.jpg)
class SimpleDaytimeHandler extends ChannelInboundHandlerAdapter {
// Inbound + Outbound..
// 5.0: DEPRECATED!// [NOTE: Insert a grumpy cat here]
}
The Hand e
![Page 102: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/102.jpg)
Hand e me hod
![Page 103: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/103.jpg)
@Overridepublic void channelRead( ChannelHandlerContext ctx, Object msg) {
// Will trigger when we receive // some data
}
Hand e me hod
![Page 104: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/104.jpg)
@Overridepublic void channelActive( ChannelHandlerContext ctx, Object msg) {
// Will a Channel is opened, this // method will be called
}
Hand e me hod
![Page 105: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/105.jpg)
@Overridepublic void channelActive(ChannelHandlerContext ctx, Object msg) {
String date = DATE_TIME.print(new DateTime()); // Get the date
}
Hand e wo
![Page 106: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/106.jpg)
@Overridepublic void channelActive(ChannelHandlerContext ctx, Object msg) {
String date = DATE_TIME.print(new DateTime()); ctx.writeAndFlush(ByteBufUtil.encodeString( ctx.alloc(), CharBuffer.wrap(date), CharsetUtil.US_ASCII));
}
Hand e wo
![Page 107: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/107.jpg)
It’s clear, right? :D
![Page 108: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/108.jpg)
Network standard way of
working?byte[]
![Page 109: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/109.jpg)
You said this was fun?
![Page 110: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/110.jpg)
MeetByteBuf
ByteBufUtil
![Page 111: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/111.jpg)
@Overridepublic void channelActive(ChannelHandlerContext ctx, Object msg) {
String date = DATE_TIME.print(new DateTime()); ctx.writeAndFlush(ByteBufUtil.encodeString( ctx.alloc(), CharBuffer.wrap(date), CharsetUtil.US_ASCII)); // We need to encode the String
}
Hand e wo
![Page 112: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/112.jpg)
@Overridepublic void channelActive(ChannelHandlerContext ctx, Object msg) {
String date = DATE_TIME.print(new DateTime()); ctx.writeAndFlush(ByteBufUtil.encodeString( ctx.alloc(), CharBuffer.wrap(date), CharsetUtil.US_ASCII)); // We allocate some space // +Netty: Keeps internal pools
}
Hand e wo
![Page 113: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/113.jpg)
@Overridepublic void channelActive(ChannelHandlerContext ctx, Object msg) {
String date = DATE_TIME.print(new DateTime()); ctx.writeAndFlush(ByteBufUtil.encodeString( ctx.alloc(), CharBuffer.wrap(date), CharsetUtil.US_ASCII)); // Write the message // Request to actually flush the data // back to the Channel
}
Hand e wo
![Page 114: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/114.jpg)
We have our Handler in place
![Page 115: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/115.jpg)
Let’s Bootstrap the server
![Page 116: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/116.jpg)
Ma n asspublic class DaytimeServer { void run() throws Exception { // fun stuff }
public static void main(String[] args) throws Exception { DaytimeServer daytimeServer = new DaytimeServer(); daytimeServer.run(); }
}
![Page 117: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/117.jpg)
java -jar daytimeserver-fat.jar
![Page 118: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/118.jpg)
un()EventLoopGroup bossGroup = new NioEventLoopGroup();EventLoopGroup workerGroup = new NioEventLoopGroup();
![Page 119: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/119.jpg)
E en LoopG oup
Netty’s way to handle threads
![Page 120: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/120.jpg)
E en LoopG oupEventLoopGroup contains someEventLoops
![Page 121: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/121.jpg)
E en LoopEventLoop
handles manyChannels
![Page 122: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/122.jpg)
About to die BRO?
![Page 123: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/123.jpg)
E en LoopG oup
E en Loop
hannehanne
hannehanne
hannehanne
hannehanne
hannehanne
hannehanne
hannehanne
hannehanne
E en Loop
![Page 124: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/124.jpg)
E en LoopG oup
E en Loop
hannehanne
hannehanne
hannehanne
hannehanne
hannehanne
hannehanne
hannehanne
hannehanne
E en Loop
![Page 125: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/125.jpg)
This immutable assignment is
the key
![Page 126: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/126.jpg)
un()EventLoopGroup bossGroup = new NioEventLoopGroup();EventLoopGroup workerGroup = new NioEventLoopGroup();
![Page 127: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/127.jpg)
un()EventLoopGroup bossGroup = new NioEventLoopGroup();EventLoopGroup workerGroup = new NioEventLoopGroup();
// Boss group accepts connections// Work group handles the work
![Page 128: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/128.jpg)
Se e Boo s apServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup) .channel(NioServerSocketChannel.class) .localAddress(8080) .option(ChannelOption.SO_BACKLOG, 100)
![Page 129: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/129.jpg)
Se e Boo s apServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup) .channel(NioServerSocketChannel.class) .localAddress(8080) .option(ChannelOption.SO_BACKLOG, 100)
// We assign both event loops
![Page 130: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/130.jpg)
Se e Boo s apServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup) .channel(NioServerSocketChannel.class) .localAddress(8080) .option(ChannelOption.SO_BACKLOG, 100)
// We use a ServerSocketChannel// to accept TCP/IP connections// as the RFC says
![Page 131: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/131.jpg)
Se e Boo s apServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup) .channel(NioServerSocketChannel.class) .localAddress(8080) .option(ChannelOption.SO_BACKLOG, 100)
// Simply bind the local address
![Page 132: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/132.jpg)
Se e Boo s apServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup) .channel(NioServerSocketChannel.class) .localAddress(8080) .option(ChannelOption.SO_BACKLOG, 100)
// Set some Socket options... why not?// Just remember: This is not handled// by Netty or the JVM, it’s the OS
![Page 133: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/133.jpg)
We’re almost there!
![Page 134: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/134.jpg)
hanne P pe neb.childHandler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { ChannelPipeline p = ch.pipeline(); p.addLast(new LoggingHandler(LogLevel.INFO)); p.addLast(new SimpleDaytimeHandler()); }});
![Page 135: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/135.jpg)
hanne P pe neb.childHandler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { ChannelPipeline p = ch.pipeline(); p.addLast(new LoggingHandler(LogLevel.INFO)); p.addLast(new SimpleDaytimeHandler()); }});
// ChannelPipeline to define your// application workflow
![Page 136: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/136.jpg)
hanne P pe neb.childHandler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { ChannelPipeline p = ch.pipeline(); p.addLast(new LoggingHandler(LogLevel.INFO)); p.addLast(new SimpleDaytimeHandler()); }});
// Append our handlers// ProTip: use LoggingHandler to// understand Netty
![Page 137: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/137.jpg)
hanne P pe neb.childHandler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { ChannelPipeline p = ch.pipeline(); p.addLast(new LoggingHandler(LogLevel.INFO)); p.addLast(new SimpleDaytimeHandler()); }});
// Finally, we add our handler
![Page 138: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/138.jpg)
RUN!b.childHandler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { ChannelPipeline p = ch.pipeline(); p.addLast(new LoggingHandler(LogLevel.INFO)); p.addLast(new SimpleDaytimeHandler()); }});
ChannelFuture f = b.bind().sync();f.channel().closeFuture().sync();
// It works!
![Page 139: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/139.jpg)
.I.A.Futures
ByteBuf APICodecs
TransportsZero-file-copy
![Page 140: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/140.jpg)
We can also create
client code
![Page 141: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/141.jpg)
Real lifeInsights
![Page 142: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/142.jpg)
We’re using Netty for Real-Time Bidding
![Page 143: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/143.jpg)
Boo s ap
![Page 144: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/144.jpg)
Tons o op ons
![Page 145: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/145.jpg)
Don’t be afraid of “low-level”
![Page 146: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/146.jpg)
Ta HTTP
![Page 147: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/147.jpg)
Explore what’s inside Netty
![Page 148: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/148.jpg)
Integrate things you love. In my
case: GUICE
![Page 149: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/149.jpg)
Gu e@InjectDaytimeService(Provider<DaytimeServer> daytimeProvider) {}
b.childHandler(() → { ChannelPipeline p = ch.pipeline(); p.addLast(new LoggingHandler(LogLevel.INFO)); p.addLast(daytimeProvider.get());});
// Inject a Provider<T> and get // instances
![Page 150: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/150.jpg)
But… is Netty FAST?
![Page 151: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/151.jpg)
2ms*
![Page 152: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/152.jpg)
* Without network latency
2ms*
![Page 153: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/153.jpg)
Yah but… what volume are you talking about?
![Page 154: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/154.jpg)
+10k QPS
![Page 155: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/155.jpg)
1 node*+3k QPS
*Fou 2.79GHz In e Pen um Xeon P o esso s, 7.5GB RAM
![Page 156: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/156.jpg)
But, is this ALOT or not?
![Page 157: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/157.jpg)
Parse example
http://blog.parse.com/learn/how-we-moved-our-api-from-ruby-to-go-and-saved-our-sanity/
(From Ruby to Go) A year and a half in, at the end of 2012, we had 200 API servers running on m1.xlarge instance types with 24 unicorn workers per instance. This was to serve 3000 requests per second for 60,000 mobile apps
![Page 158: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/158.jpg)
We have 16 nodes! 4x Netty nodes10x Kafka, DNS, the usual suspects…
![Page 159: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/159.jpg)
I KNOW IT’S NOT FAIR,
but it’s good stuff for your ego BRO
![Page 160: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/160.jpg)
Lovely ecosystemlike Ratpack,
or users, with vert.x or akka
![Page 161: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/161.jpg)
It will change the way you program some of your apps
![Page 162: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/162.jpg)
Hello, Async problems
![Page 163: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/163.jpg)
Forget about max onn params
![Page 164: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/164.jpg)
One more thing...
![Page 165: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/165.jpg)
(It’s 5:00 in the morning and I had
to say it)
![Page 166: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/166.jpg)
(did you notice the “Apple
background”?)
![Page 167: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/167.jpg)
![Page 168: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/168.jpg)
![Page 169: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/169.jpg)
JUST KIDDINGIt doesn’ really
matter!
![Page 170: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/170.jpg)
Have fun!
![Page 171: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/171.jpg)
THANKS
![Page 172: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/172.jpg)
ProTip: We’re hiring
![Page 173: Netty from the trenches](https://reader033.vdocuments.site/reader033/viewer/2022052401/55cae62ebb61eb4c788b483a/html5/thumbnails/173.jpg)
Q & A
#HiddenTrovitTrack@jordi9