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
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 ( ) ;
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 ) . . .
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 ( ) ; } }
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 ( ) ) ;
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 ( ) { . . . } ) ; } }
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 ( ) ; }
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 ( ) ; }