Share
Embed
Scala & Camel
Presentation for Slovak Scala Group about Scala - camel - play and their inter-play (pun intended)
bs76
December 19, 2013
Transcript
30 slides
-
CAMEL ~ AKKA ~ SCALA MATCH MADE IN HEAVEN Created
by Boris Steiner / @becrash76 -
AGENDA Camel Akka Camel & Akka Camel & Akka &
play framework -
CAMEL Apache Camel ™ is a versatile open-source integration framework
based on known Enterprise Integration Patterns. -
ABOUT CAMEL http://camel.apache.org Licensed under Apache 2.0 license It is
a framework as well as runtime for integration Enterprise Integration Patterns supported -
BUILDING BLOCKS Route - message from somewhere, transformation, routing to
destination Component - lookup via URI polling/non polling, synchronous/asynchronous Endpoint - referenced by URI Producer - Provides a channel on which clients can create and invoke message exchanges on an Endpoint Consumer - A consumer of message exchanges from an Endpoint Exchange - in/out Message Message - body, headers, properties factory for endpoints (~80) -
ROUTES How to get message from an Endpoint to another
Endpoint Example: Watch filesystem, wait for file *.xml parse route based on <order id=ORDER_ID... process send to other systems -
ROUTE IN SPRING < i m p o r t
r e s o u r c e = " m y C o o l R o u t e s . x m l " > < c a m e l c o n t e x t x m l n s = " h t t p : / / c a m e l . a p a c h e . o r g / s c h e m a / s p r i n g " > < ! - - r e f e r t o a g i v e n r o u t e t o b e u s e d - - > < r o u t e c o n t e x t r e f r e f = " m y C o o l R o u t e s " > < ! - - w e c a n o f c o u r s e s t i l l u s e r o u t e s i n s i d e c a m e l C o n t e x t - - > < r o u t e i d = " i n s i d e " > < f r o m u r i = " d i r e c t : i n s i d e " > < t o u r i = " m o c k : i n s i d e " > < / t o > < / f r o m > < / r o u t e > < / r o u t e c o n t e x t r e f > < / c a m e l c o n t e x t > < / i m p o r t > -
JAVA ROUTE DSL p u b l i c c
l a s s M y R o u t e B u i l d e r e x t e n d s R o u t e B u i l d e r { / * * * L e t ' s c o n f i g u r e t h e C a m e l r o u t i n g r u l e s u s i n g J a v a c o d e . . . * / p u b l i c v o i d c o n f i g u r e ( ) { / / h e r e i s a s a m p l e w h i c h p r o c e s s e s t h e i n p u t f i l e s / / ( l e a v i n g t h e m i n p l a c e - s e e t h e ' n o o p ' f l a g ) / / t h e n p e r f o r m s c o n t e n t b a s e d r o u t i n g o n t h e m e s s a g e u s i n g X P a t h f r o m ( " f i l e : s r c / d a t a ? n o o p = t r u e " ) . c h o i c e ( ) . w h e n ( x p a t h ( " / p e r s o n / c i t y = ' L o n d o n ' " ) ) . t o ( " f i l e : t a r g e t / m e s s a g e s / u k " ) . o t h e r w i s e ( ) . t o ( " f i l e : t a r g e t / m e s s a g e s / o t h e r s " ) ; } } -
SCALA DSL Scala EIP DSL " d i r e
c t : b " = = > { w h e n ( _ . i n = = " < h a l l o > " ) { - - > ( " m o c k : b " ) t o ( " m o c k : c " ) } o t h e r w i s e { t o ( " m o c k : e " ) } t o ( " m o c k : d " ) } < / h a l l o > -
HOW TO RUN Create Context Build routes Add routes to
context Start context Profit -
AKKA Akka is a toolkit and runtime for building highly
concurrent, distributed, and fault tolerant event- driven applications on the JVM. -
ABOUT AKKA http://akka.io Apache 2 license Actor model for distributed,
scalable applications "Let it crash" model for fault-tolerance -
BUILDING BLOCKS Actors Lightweight Asynchronous Abstraction for paralelism and concurrency
Location transparent Unlike camel, integration is not the main point -
ACTORS (SCALA) i m p o r t a k
k a . a c t o r . A c t o r i m p o r t a k k a . a c t o r . P r o p s i m p o r t a k k a . e v e n t . L o g g i n g c l a s s M y A c t o r e x t e n d s A c t o r { v a l l o g = L o g g i n g ( c o n t e x t . s y s t e m , t h i s ) d e f r e c e i v e = { c a s e " t e s t " ⇒ l o g . i n f o ( " r e c e i v e d t e s t " ) c a s e _ ⇒ l o g . i n f o ( " r e c e i v e d u n k n o w n m e s s a g e " ) } } i m p o r t a k k a . a c t o r . A c t o r S y s t e m / / A c t o r S y s t e m i s a h e a v y o b j e c t : c r e a t e o n l y o n e p e r a p p l i c a t i o n v a l s y s t e m = A c t o r S y s t e m ( " m y S y s t e m " ) v a l m y A c t o r = s y s t e m . a c t o r O f ( P r o p s [ M y A c t o r ] , " m y a c t o r 2 " ) -
CLIENT Lookup actor / / w i l l l
o o k u p t h i s a b s o l u t e p a t h c o n t e x t . a c t o r S e l e c t i o n ( " / u s e r / s e r v i c e A / a g g r e g a t o r " ) / / w i l l l o o k u p s i b l i n g b e n e a t h s a m e s u p e r v i s o r c o n t e x t . a c t o r S e l e c t i o n ( " . . / j o e " ) / / w i l l l o o k a l l c h i l d r e n t o s e r v i c e B w i t h n a m e s s t a r t i n g w i t h w o r k e r c o n t e x t . a c t o r S e l e c t i o n ( " / u s e r / s e r v i c e B / w o r k e r * " ) / / w i l l l o o k u p a l l s i b l i n g s b e n e a t h s a m e s u p e r v i s o r c o n t e x t . a c t o r S e l e c t i o n ( " . . / * " ) a c t o r R e f ! " t e s t " / / a s y n c h c a l l - t e l l i m p o r t s c a l a . c o n c u r r e n t . d u r a t i o n . _ i m p o r t a k k a . p a t t e r n . a s k v a l f u t u r e = m y A c t o r . a s k ( " h e l l o " ) ( 5 s e c o n d s ) / / s y n c h c a l l - a s k -
CAMEL & AKKA Akka has for Camel all Camel's endpoints
available to actors ! Consumer example: plugin i m p o r t a k k a . c a m e l . { C a m e l M e s s a g e , C o n s u m e r } c l a s s M y E n d p o i n t e x t e n d s C o n s u m e r { d e f e n d p o i n t U r i = " m i n a 2 : t c p : / / l o c a l h o s t : 6 2 0 0 ? t e x t l i n e = t r u e " d e f r e c e i v e = { c a s e m s g : C a m e l M e s s a g e ⇒ { / * . . . * / } c a s e _ ⇒ { / * . . . * / } } } / / s t a r t a n d e x p o s e a c t o r v i a t c p i m p o r t a k k a . a c t o r . { A c t o r S y s t e m , P r o p s } v a l s y s t e m = A c t o r S y s t e m ( " s o m e - s y s t e m " ) v a l m i n a = s y s t e m . a c t o r O f ( P r o p s [ M y E n d p o i n t ] ) -
PRODUCER EXAMLE Mix in Producer OneWay - asynch don't expect
any reply i m p o r t a k k a . a c t o r . A c t o r i m p o r t a k k a . c a m e l . { P r o d u c e r , O n e w a y } i m p o r t a k k a . a c t o r . { A c t o r S y s t e m , P r o p s } c l a s s O r d e r s e x t e n d s A c t o r w i t h P r o d u c e r w i t h O n e w a y { d e f e n d p o i n t U r i = " j m s : q u e u e : O r d e r s " } v a l s y s = A c t o r S y s t e m ( " s o m e - s y s t e m " ) v a l o r d e r s = s y s . a c t o r O f ( P r o p s [ O r d e r s ] ) o r d e r s ! < o r d e r a m o u n t = " 1 0 0 " c u r r e n c y = " P L N " i t e m i d = " 1 2 3 4 5 " > < / o r d e r > -
INTEGRATION i m p o r t a k k
a . c a m e l . { C a m e l M e s s a g e , C o n s u m e r , P r o d u c e r , . . . } override endppint: d e f e n d p o i n t U r i = . . . Akka's CamelMessage immutable unlike Camel's Exchange transformation before send possible: Route building also possible o v e r r i d e d e f t r a n s f o r m O u t g o i n g M e s s a g e ( m s g : A n y ) = m s g m a t c h { c a s e m s g : C a m e l M e s s a g e ⇒ u p p e r C a s e ( m s g ) . . . -
PLAY FRAMEWORK Straight forward path: Camel <-> Akka <-> Play
Framework This means Camel in Play for free -
WHEN AND HOW TO USE? Main focus play May be
applicable elsewhere -
INTEGRATION SCENARIO Integration with other systems (obviously) When: integration specs
change often, but is not "core" of the business e.g. there are versions of integrations where structures/processes change many systems/protocols to intgrate with integrated systems are legacy or not under our control How: Separate integration into Camel routes -
SIMPLE WORKFLOW When: Need state transitions Transitions change often Reusable
steps May need support outside of Camel (e.g. saving states, state machine) How: Embed Camel into app Standalone router communication via e.g. JMS -
BATCH PROCESSING When: Legacy communication via file system Need retries/locking/.done
file support need to track what has been processed How: Embed Camel into app Standalone router communication via e.g. JMS, DB -
MANAGE RESOURCE WITH CONSTRAINTS (TRUE STORY, BRO) When: Need to
send emails, have constraints on #mails/min Need to throttle traffic out Manage retries, notify admin when #emails not sent How: Standalone router polling DB for mails to send Generation of mail body using velocity -
WHEN NOT TO USE ? "We are a pure X
shop we never mix technologies" Need to control all aspects of dependencies (Camel has a lot of them) ? -
DEMO Simple app: Hockey match score updates Router in Scala
DSL watches for files with score changes score update via ActiveMQ to play app actor play actor send asynch notification via websocket to clients Update from play app sent via ActiveMQ to router router saves in DB There's everything: Camel, Play, Akka, WebSockets Source code