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

RxJava and more at Vert.x

Julien Viet
October 20, 2016
200

RxJava and more at Vert.x

Julien Viet

October 20, 2016
Tweet

Transcript

  1. ABOUT ME Open source developer for 15 years @vertx_project lead

    So ware engineer at Red Hat https://github.com/vietj @julienviet
  2. < d e p e n d e n c

    y > < g r o u p i d > i o . v e r t x < / g r o u p i d > < a r t i f a c t i d > v e r t x - c o r e < / a r t i f a c t i d > < v e r s i o n > 3 . 3 . 3 < / v e r s i o n > < / d e p e n d e n c y >
  3. d e p e n d e n c i

    e s { c o m p i l e " i o . v e r t x : v e r t x - c o r e : 3 . 3 . 3 " }
  4. > b r e w i n s t a

    l l v e r t . x . . . > v e r t x r u n
  5. > s d k i n s t a l

    l v e r t x . . . > v e r t x r u n
  6. JAVA 8 p u b l i c c l

    a s s S e r v e r e x t e n d s A b s t r a c t V e r t i c l e { p u b l i c v o i d s t a r t ( ) { v e r t x . c r e a t e H t t p S e r v e r ( ) . r e q u e s t H a n d l e r ( r e q u e s t - > r e q u e s t . r e s p o n s e ( ) . p u t H e a d e r ( " c o n t e n t - t y p e " , " t e x t / p l a i n " ) . e n d ( " H e l l o f r o m V e r t . x " ) ) . l i s t e n ( 8 0 8 0 , " l o c a l h o s t " ) ; } }
  7. SCALA c l a s s S e r v

    e r e x t e n d s S c a l a V e r t i c l e { o v e r r i d e d e f s t a r t ( ) : U n i t = { v e r t x . c r e a t e H t t p S e r v e r ( ) . r e q u e s t H a n d l e r ( _ . r e s p o n s e ( ) . p u t H e a d e r ( " c o n t e n t - t y p e " , " t e x t / p l a i n " ) . e n d ( " H e l l o f r o m V e r t . x ! " ) ) . l i s t e n ( 8 0 8 0 ) } }
  8. CEYLON s h a r e d c l a

    s s S e r v e r ( ) e x t e n d s V e r t i c l e ( ) { s t a r t ( ) = > v e r t x . c r e a t e H t t p S e r v e r ( ) . r e q u e s t H a n d l e r ( ( r e q ) = > r e q . r e s p o n s e ( ) . p u t H e a d e r ( " c o n t e n t - t y p e " , " t e x t / p l a i n " ) . e n d ( " H e l l o f r o m V e r t . x ! " ) ) . l i s t e n ( 8 0 8 0 ) ; }
  9. JAVASCRIPT v e r t x . c r e

    a t e H t t p S e r v e r ( ) . r e q u e s t H a n d l e r ( f u n c t i o n ( r e q ) { r e q . r e s p o n s e ( ) . p u t H e a d e r ( " c o n t e n t - t y p e " , " t e x t / p l a i n " ) . e n d ( " H e l l o f r o m V e r t . x ! " ) ; } ) . l i s t e n ( 8 0 8 0 ) ;
  10. GROOVY v e r t x . c r e

    a t e H t t p S e r v e r ( ) . r e q u e s t H a n d l e r ( { r e q - > r e q . r e s p o n s e ( ) . p u t H e a d e r ( " c o n t e n t - t y p e " , " t e x t / p l a i n " ) . e n d ( " H e l l o f r o m V e r t . x ! " ) } ) . l i s t e n ( 8 0 8 0 )
  11. RUBY $ v e r t x . c r

    e a t e _ h t t p _ s e r v e r ( ) . r e q u e s t _ h a n d l e r ( ) { | r e q | r e q . r e s p o n s e ( ) . p u t _ h e a d e r ( " c o n t e n t - t y p e " , " t e x t / p l a i n " ) . e n d ( " H e l l o f r o m V e r t . x ! " ) } . l i s t e n ( 8 0 8 0 )
  12. RXJAVA Reactive Extensions for the JVM – a library for

    composing asynchronous and event-based programs using observable sequences for the Java VM. Composable Flexible
  13. ASYNC COORDINATION v e r t x . c r

    e a t e H t t p S e r v e r ( ) . l i s t e n ( 8 0 8 0 , r e s u l t 1 - > { i f ( r e s u l t 1 . s u c c e e d e d ( ) ) { H t t p S e r v e r s 2 = v e r t x . c r e a t e H t t p S e r v e r ( ) ; s 2 . l i s t e n ( 8 0 8 1 , r e s u l t 2 - > { i f ( r e s u l t 2 . s u c c e e d e d ( ) ) { S y s t e m . o u t . p r i n t l n ( " S u c c e e d e d " ) ; } e l s e { S y s t e m . o u t . p r i n t l n ( " F a i l e d " ) ; } } ) ; } e l s e { S y s t e m . o u t . p r i n t l n ( " F a i l e d " ) ; } } ) ;
  14. O b s e r v a b l e

    < H t t p S e r v e r > o b s 1 = v e r t x . c r e a t e H t t p S e r v e r ( ) . r e q u e s t H a n d l e r ( r e q - > h a n d l e R e q u e s t ( r e q ) ) . l i s t e n ( 8 0 8 0 ) ; O b s e r v a b l e < H t t p S e r v e r > o b s 2 = v e r t x . c r e a t e H t t p S e r v e r ( ) . r e q u e s t H a n d l e r ( r e q - > h a n d l e R e q u e s t ( r e q ) ) . l i s t e n ( 8 0 8 1 ) ; O b s e r v a b l e . c o n c a t ( o b s 1 , o b s 2 ) . s u b s c r i b e ( s e r v e r - > { S y s t e m . o u t . p r i n t l n ( " S e r v e r s t a r t e d " ) ; } , e r r - > { S y s t e m . o u t . p r i n t l n ( " S o m e t h i n g w e n t w r o n g " ) ; e r r . p r i n t S t a c k T r a c e ( ) ; } ) ;
  15. STREAM COMPOSITION E v e n t B u s

    e b = v e r t x . e v e n t B u s ( ) ; M e s s a g e C o n s u m e r < I n t e g e r > c o n s = e b . m e s s a g e C o n s u m e r ( " s e n s o r " ) ; L i s t < D o u b l e > s a m p l e s = n e w A r r a y L i s t < > ( ) ; c o n s . h a n d l e r ( m s g - > { d o u b l e h e a t = m s g . b o d y ( ) ; s a m p l e s . a d d ( h e a t ) ; } ) ; v e r t x . s e t P e r i o d i c ( 1 0 0 0 , i d - > { d o u b l e h e a t = s a m p l e s . s t r e a m ( ) . c o l l e c t ( C o l l e c t o r s . a v e r a g i n g D o u b l e ( d - > d ) ) ; S y s t e m . o u t . p r i n t l n ( " C u r r e n t h e a t : " + h e a t ) ; } ) ;
  16. E v e n t B u s e b

    = v e r t x . e v e n t B u s ( ) ; M e s s a g e C o n s u m e r < D o u b l e > c o n s = e b . m e s s a g e C o n s u m e r ( " s e n s o r " ) ; O b s e r v a b l e < M e s s a g e < D o u b l e > > o b s = c o n s . t o O b s e r v a b l e ( ) ; o b s . m a p ( m s g - > m s g . b o d y ( ) ) . . b u f f e r ( 1 , T i m e U n i t . S E C O N D S ) . . m a p ( b u f f e r - > b u f f e r . s t r e a m ( ) . c o l l e c t ( C o l l e c t o r s . a v e r a g i n g D o u b l e ( d - > d ) ) ) . . s u b s c r i b e ( h e a t - > S y s t e m . o u t . p r i n t l n ( " C u r r e n t h e a t : " + h e a t ) ) ;
  17. RXJAVA 2 Release 2.0 scheduled soon Evolution of RxJava 1

    API Full rewrite Based on Reactive Streams Better performances
  18. c l a s s M y T y p

    e { p u b l i c s t a t i c f i n a l M y T y p e N U L L _ S E N T I N E L = n e w M y T y p e ( ) ; . . . } O b s e r v a b l e < M y T y p e > o b s e r v a b l e = g e t O b j e c t s ( ) ; o b s e r v a b l e . s u s c r i b e ( o b j - > { i f ( o b j = = N U L L _ S E N T I N E L ) { / / N u l l i n s t a n c e } e l s e { / / N o t n u l l } } ) ;
  19. O b s e r v a b l e

    < O p t i o n a l < M y T y p e > > o b s e r v a b l e = g e t O b j e c t s ( ) ; o b s e r v a b l e . s u b s c r i b e ( o p t - > { o p t . i f P r e s e n t ( o b j - > { / / N u l l i n s t a n c e } } ) ;
  20. MIGRATING FROM RXJAVA 1 Does not come for free Full

    list of API change available RxJava2Interop project provides RxJava1 / RxJava2 adapters
  21. VERT.X AND RXJAVA 2 Changed to disallow null values in

    streams Pick the right type according to the context: Completable, Maybe or Single