Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Netty - Network - Application development the easy way

Netty - Network - Application development the easy way

This presentation was hold as part of JAX 2013 in Mainz and cover Netty 4.

Norman Maurer

April 24, 2013
Tweet

More Decks by Norman Maurer

Other Decks in Programming

Transcript

  1. NORMAN MAURER Red Hat (JBoss) - EAP Core Team Former

    contractor for Apple Inc Author - Netty in Action Apache Software Foundation Member Netty / Vert.x Core-Developer and all things NIO Java and Scala Twitter: @normanmaurer Github: https://github.com/normanmaurer
  2. WHAT IS NETTY ? “Netty is a NIO client server

    framework which enable quick and easy development of network applications such as protocol servers and clients...”
  3. FEATURES Asynchronous Simple and unified API High-Performing Listeners and Callbacks

    supported Zero-file-copy Buffer Pooling Gathering / Scattering ... and many more
  4. ASYNCHRONOUS... All I/O Operations don't block! Get notified once the

    operation completes Be able to share one Thread across many connections
  5. BLOCKING-IO MAY NOT SCALE! GOOD LUCK WITH SERVING 1 MILLION

    CONCURRENT CONNECTIONS “ Usually a Thread takes memory from 256kb to 1mb for the stack space! ”
  6. ASYNCHRONOUS IN ACTION C h a n n e l

    c h a n n e l = . . . C h a n n e l F u t u r e c f = c h a n n e l . w r i t e ( d a t a ) ; c f . a d d L i s t e n e r ( n e w C h a n n e l F u t u r e L i s t e n e r ( ) { @ O v e r r i d e p u b l i c v o i d o p e r a t i o n C o m p l e t e ( C h a n n e l F u t u r e f u t u r e ) t h r o w s E x c e p t i o n { i f ( ! f u t u r e . i s S u c c e s s ( ) { f u t u r e . c a u s e ( ) . p r i n t S t a c k t r a c e ( ) ; . . . } . . . } } ) ; . . . c f . s y n c ( ) ;
  7. SUPPORTED PROTOCOLS TCP UDP SCTP* UDT Serial * O n

    l y s u p p o r t e d o n l i n u x
  8. BYTEBUF Separate index for reader/writer CompositeByteBuf Direct and Heap implementations

    Resizable with max capacity Support for reference-counting Method-Chaining B y t e B u f b u f = . . . ; b u f . w r i t e I n t ( 1 ) . w r i t e B y t e s ( d a t a ) . w r i t e B o o l e a n ( t r u e ) . . .
  9. CHANNELHANDLER FACTS Inbound vs. Outbound State vs. Operation Byte or

    Message based Always called by the assigned EventExecutor Type-safe
  10. ADAPTER IN ACTION @ S h a r a b

    l e p u b l i c c l a s s E c h o 4 S e r v e r H a n d l e r e x t e n d s C h a n n e l I n b o u n d B y t e H a n d l e r A d a p t e r { @ O v e r r i d e p u b l i c v o i d i n b o u n d B u f f e r U p d a t e d ( C h a n n e l H a n d l e r C o n t e x t c t x , B y t e B u f i n ) { B y t e B u f o u t = c t x . n e x t O u t b o u n d B y t e B u f f e r ( ) ; o u t . d i s c a r d R e a d B y t e s ( ) . w r i t e B y t e s ( i n ) ; c t x . f l u s h ( ) ; } @ O v e r r i d e p u b l i c v o i d e x c e p t i o n C a u g h t ( C h a n n e l H a n d l e r C o n t e x t c t x , T h r o w a b l e c a u s e ) { c t x . c l o s e ( ) ; } }
  11. CHANNELPIPELINE Holds the ChannelHandlers for a Channel All events will

    get passed through it On the fly modification One-to-one relation between Channel and ChannelPipeline
  12. CHANNELPIPELINE Kind of a unix-pipe-like thing... $ e c h

    o " N e t t y i s s h i t . . . . " | s e d - e ' s / i s / i s t h e / ' N e t t y i s t h e s h i t . . . . “You see, everything is adjustable!”
  13. ADDING CHANNELHANDLERS C h a n n e l c

    h = . . . ; C h a n n e l P i p e l i n e p = c h . p i p e l i n e ( ) ; E v e n t E x e c u t o r e 1 = n e w D e f a u l t E v e n t E x e c u t o r ( 1 6 ) ; E v e n t E x e c u t o r e 2 = n e w D e f a u l t E v e n t E x e c u t o r ( 8 ) ; / / E x e c u t e d i n E v e n t L o o p T h r e a d p . a d d L a s t ( n e w M y P r o t o c o l C o d e c ( ) ) ; / / E x e c u t e d i n o n e o f t h e T h r e a d s o f e 1 p . a d d L a s t ( e 1 , n e w M y D a t a b a s e A c c e s s i n g H a n d l e r ( ) ) ; / / E x e c u t e d i n o n e o f t h e T h r e a d s o f e 1 p . a d d L a s t ( e 2 , n e w M y H a r d D i s k A c c e s s i n g H a n d l e r ( ) ) ;
  14. All the submitted Tasks will get executed in the IO-Thread!

    “ Yay, no need to worry about synchronization! ”
  15. RUN A TASK IN THE EVENTLOOP p u b l

    i c c l a s s M y H a n d l e r e x t e n d s C h a n n e l O u t b o u n d M e s s a g e H a n d l e r A d a p t e r { @ O v e r r i d e p u b l i c v o i d f l u s h ( C h a n n e l H a n d l e r C o n t e x t c t x , C h a n n e l P r o m i s e p r o m i s e ) { c t x . f l u s h ( p r o m i s e ) ; i f ( ! p r o m i s e . i s D o n e ( ) { / / s c h e d u l e t a s k f o r i n 3 0 s e c o n d s c t x . e x e c u t o r ( ) . s c h e d u l e ( n e w M y W r i t e T i m e o u t T a s k ( p r o m i s e ) , 3 0 , T i m e U n i t . S E C O N D S ) ; } / / R u n a n a r b i t r a r y t a s k f r o m a n I / O t h r e a d . c t x . e x e c u t o r ( ) . e x e c u t e ( n e w R u n n a b l e ( ) { . . . } ) ; } }
  16. BOOTSTRAP A SERVER S e r v e r B

    o o t s t r a p b = n e w S e r v e r B o o t s t r a p ( ) ; t r y { b . g r o u p ( n e w N i o E v e n t L o o p G r o u p ( ) , n e w N i o E v e n t L o o p G r o u p ( ) ) . c h a n n e l ( N i o S e r v e r S o c k e t C h a n n e l . c l a s s ) . l o c a l A d d r e s s ( n e w I n e t S o c k e t A d d r e s s ( p o r t ) ) . c h i l d H a n d l e r ( n e w C h a n n e l I n i t i a l i z e r < S o c k e t C h a n n e l > ( ) { @ O v e r r i d e p u b l i c v o i d i n i t C h a n n e l ( S o c k e t C h a n n e l c h ) t h r o w s E x c e p t i o n { c h . p i p e l i n e ( ) . a d d L a s t ( n e w Y o u r C h a n n e l H a n d l e r ( ) ) ; } } ) ; C h a n n e l F u t u r e f = b . b i n d ( ) . s y n c ( ) ; f . c h a n n e l ( ) . c l o s e F u t u r e ( ) . s y n c ( ) ; } f i n a l l y { b . s h u t d o w n ( ) ; }
  17. BOOTSTRAP A CLIENT B o o t s t r

    a p b = n e w B o o t s t r a p ( ) ; t r y { b . g r o u p ( n e w N i o E v e n t L o o p G r o u p ( ) ) . c h a n n e l ( N i o S o c k e t C h a n n e l . c l a s s ) . r e m o t e A d d r e s s ( n e w I n e t S o c k e t A d d r e s s ( " 1 0 . 0 . 0 . 1 " , 2 5 ) ) . h a n d l e r ( n e w C h a n n e l I n i t i a l i z e r < S o c k e t C h a n n e l > ( ) { @ O v e r r i d e p u b l i c v o i d i n i t C h a n n e l ( S o c k e t C h a n n e l c h ) { c h . p i p e l i n e ( ) . a d d L a s t ( n e w Y o u r C h a n n e l H a n d l e r ( ) ) ; } } ) ; C h a n n e l F u t u r e f = b . c o n n e c t ( ) . s y n c ( ) ; f . c h a n n e l ( ) . c l o s e F u t u r e ( ) . s y n c ( ) ; } f i n a l l y { b . s h u t d o w n ( ) ; }
  18. TOO LOW LEVEL ? “You may like Vert.x which is

    based on Netty and also polyglot!”