Apache Camel - Rotas para as suas mensagens

2,059 views

Published on

0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
2,059
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
1
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Apache Camel - Rotas para as suas mensagens

  1. 1. Apache Camel rotas para as suas mensagens Bruno Borges 2009
  2. 2. Apache Camel
  3. 3. Integração MSMQ JMS RSS SOAP FTP JT/400 JDBC SMTP HTTP FIX JBI JPA TCP Twitter LDAP SOAP
  4. 4. Integração
  5. 5. Camel
  6. 6. ?
  7. 7. Framework Open Source para Padrões de Integração
  8. 8. EIP ● Enterprise Integration Patterns
  9. 9. Padrões Roteamento de Mensagens C o n te n t- b a seC o m o t ra t a r u m a situ a çã o o n d e a im p le m e n ta çã o d e u m a f u n çã o d R o u te r ló g ica e st á e sp a lh a d a e m m ú lt ip lo s siste m a s? M e ssa g e F ilteCro m o e vit a r re ce b im e n to d e m e n sa g e n s in d e se ja d a s? C o m o p r o ce ssa r u m a m e n sa g e m , se e sta co n t é m e le m e n to s q u e S p litte r d e ve m se r p ro ce ssa d o s in d ivid u a lm e n t e d e fo rm a s d ife re n t e s? C o m o co m b in a r o re su lta d o d e m e n sa g e n s in d ivid u a is, p o ré m A g g re g a to r r e la cio n a d a s, n u m a ú n ica sa íd a ? C o m o re to m a r a o r d e m d e m e n sa g e n s re la cio n a d a s e n via d a s f o ra d e R e se q u e n ce r o rd e m ? R e cip ie n t L ist o m o ro te a r u m a m e n sa g e m a u m a list a d in â m ica d e d e st in a rá rio s? C
  10. 10. Roteamento ___________ ___________ ___________ ___________   ______ mensagem
  11. 11. Linguagens ● BeanShell ● SQL ● JavaScript ● XPath ● Groovy ● XQuery ● Python ● JSR 223 Scripting ● PHP ● OGNL ● Ruby ● EL (JSP/JSF) ● Scala
  12. 12. Componentes ● ActiveMQ ● JBI / JCR ● AMQP ● JDBC / JPA ● Atom / RSS ● JMS ● Beans ● JT/400 ● Comet (Jetty) ● LDAP ● CXF ● LOG ● DataSet (testes) ● Mail ● Direct ● Mina ● File ● MSMQ ● FIX ● Quartz ● FTP / SFTP ● RMI ● Hibernate ● Velocity ● HL7   MLLP ● XMPP ● HTTP ● Xquery ● iBatis ● ... Twitter ?
  13. 13. Definição de Rotas ● CamelContext public class CamelStartup { public static void main(String... args) throws Exception { CamelContext context = new DefaultCamelContext(); context.addRoutes(new MyRoute()); context.start(); } } ● RouteBuilder ● DSL – Domain Specific Language – Java – Spring XML – Scala
  14. 14. Definição de Rotas ● Java public class TwitterRoute extends RouteBuilder { public void configure() throws Exception { from("file:outbox") .transform(body().convertToString()) .to("twitter:tweetit"); from("twitter:tweets?follow=brunoborges") .transform(body().convertToString()) .to("file:twitters-log"); } }
  15. 15. Definição de Rotas ● Spring XML <camelContext xmlns="http://activemq.apache.org/camel/schema/spring"> <route> <from uri="activemq:topic:Quotes"/> <filter> <xpath>/quote/product = 'widget'</xpath> <to uri="mqseries:WidgetQuotes"/> </filter> </route> </camelContext>
  16. 16. Definição de Rotas ● Scala class MyRouteBuilder extends RouteBuilder { "direct:a" --> "mock:a" "direct:b" to "mock:b" }
  17. 17. Repetindo ... Roteamento de Mensagens C o n te n t- b a seC o m o t ra t a r u m a situ a çã o o n d e a im p le m e n ta çã o d e u m a f u n çã o d R o u te r ló g ica e st á e sp a lh a d a e m m ú lt ip lo s siste m a s? M e ssa g e F ilteCro m o e vit a r re ce b im e n to d e m e n sa g e n s in d e se ja d a s? C o m o p r o ce ssa r u m a m e n sa g e m , se e sta co n t é m e le m e n to s q u e S p litte r d e ve m se r p ro ce ssa d o s in d ivid u a lm e n t e d e fo rm a s d ife re n t e s? C o m o co m b in a r o re su lta d o d e m e n sa g e n s in d ivid u a is, p o ré m A g g re g a to r r e la cio n a d a s, n u m a ú n ica sa íd a ? C o m o re to m a r a o r d e m d e m e n sa g e n s re la cio n a d a s e n via d a s f o ra d e R e se q u e n ce r o rd e m ? R e cip ie n t L ist o m o ro te a r u m a m e n sa g e m a u m a list a d in â m ica d e d e st in a rá rio s? C
  18. 18. Content-based Router RouteBuilder route = new RouteBuilder() { public void configure() { from("seda:a") .choice() .when(header("foo").isEqualTo("bar")) .to("seda:b") .when(header("foo").isEqualTo("cheese")) .to("seda:c") .otherwise() .to("seda:d"); } };
  19. 19. Content-based Router <camelContext xmlns="http://activemq.apache.org/camel/schema/spring"> <route> <from uri="activemq:NewOrders" /> <choice> <when> <xpath>/order/product = 'widget'</xpath> <to uri="activemq:Orders.Widgets" /> </when> <when> <xpath>/order/product = 'gadget'</xpath> <to uri="activemq:Orders.Gadgets" /> </when> <otherwise> <to uri="activemq:Orders.Bad" /> </otherwise> </choice> </route> </camelContext>
  20. 20. Content-based Router "direct:a" ==> { to ("mock:polyglot") choice { when (_.in == "<hello/>") to ("mock:english") when (_.in == "<hallo/>") { to ("mock:dutch") to ("mock:german") } otherwise to ("mock:french") } }
  21. 21. Message Filter public class MyRouteBuilder extends RouteBuilder { public void configure() { from("activemq:topic:Quotes") .filter() .xpath("/quote/product = 'widget'") .to("mqseries:WidgetQuotes"); } }
  22. 22. Message Filter <camelContext xmlns="http://activemq.apache.org/camel/schema/spring"> <route> <from uri="activemq:topic:Quotes" /> <filter> <xpath>/quote/product = 'widget'</xpath> <to uri="mqseries:WidgetQuotes" /> </filter> </route> </camelContext>
  23. 23. Message Filter "direct:a" when(_.in == "<hello/>") to("mock:a") "direct:b" ==> { when(_.in == "<hallo/>") { --> ("mock:b") to ("mock:c") } otherwise { to ("mock:e") } to ("mock:d") }
  24. 24. Message Splitter public class MyRouteBuilder extends RouteBuilder { public void configure() { from("file://orders") .splitter(body().tokenize("n")) .to("activemq:Order.Items"); } }
  25. 25. Message Splitter public class MyRouteBuilder extends RouteBuilder { public void configure() { from("file://orders") // Splitter com XQuery .splitter(xquery("/order/items")) .to("activemq:Order.Items"); } }
  26. 26. Message Aggregator public class MyRouteBuilder extends RouteBuilder { public void configure() { from("activemq:Inventory.Items") .aggregator().xpath("/order/@id") .to("activemq:Inventory.Order"); } }
  27. 27. Resequencer public class MyRouteBuilder extends RouteBuilder { public void configure() { from("direct:a") .resequencer(header("JMSPriority")) .to("seda:b"); } }
  28. 28. Recipient List public class MyRouteBuilder extends RouteBuilder { public void configure() { from("direct:a") .recipientList(header("destinos")); } }
  29. 29. Camel Twitter CAMEL-1520
  30. 30. Padrões Tradução de Mensagens M e ssa g e C o m o sist e m a s u sa n d o d if e re n t e s f o r m a t o s d e m e n sa g e n s p o d e m T ra n sla to r co m u n ica r-se e n t re si? C o n te n t C o m o se co m u n ica r co m u m sist e m a se a m e n sa g e m d e o rig e m n ã o E n rich e r d isp õ e d e to d o s o s d a d o s n e ce ssá rio s? C o m o sim p lif ica r m e n sa g e n s m u it o g ra n d e s, q u a n d o so m e n te p o u co s C o n te n t F ilte r a d o s in t e re ssa m ? d C o m o p ro ce ssa r m e n sa g e n s se m a n t ica m e n t e sim ila re s, m a s e m N o rm a liz e r d if e re n t e s f o rm a t o s?
  31. 31. Message Translator
  32. 32. Content Enricher
  33. 33. Content Filter
  34. 34. Exemplos from("direct:inicio") .process(new Processor() { public void process(Exchange exchange) { Message in = exchange.getIn(); in.setBody(in.getBody(String.class) + " by Camel!"); } }) .to("mock:result"); from("direct:inicio") .transform(body().append(" Camel!")) .to("mock:result"); <camelContext xmlns="http://camel.apache.org/schema/spring"> <route> <from uri="direct:start" /> <transform> <simple>${in.body} extra data!</simple> </transform> <to uri="mock:end" /> </route> </camelContext>
  35. 35. Normalizer
  36. 36. Dependency Injection Google Guice
  37. 37. Beans
  38. 38. Beans Tradutores public class MinhaRota extends RouteBuilder { @Override public void configure() throws Exception { from("activemq:Inbox") .beanRef("meuBean") .beanRef("meuOutroBean", "metodoQualquer") .to("activemq:Outbox"); } }
  39. 39. Beans Conversores package org.apache.camel.component.twitter; import java.text.ParseException; import org.apache.camel.Converter; @Converter public class TwitterConverter { @Converter public static String toString(Status status) throws ParseException { return status.toString(); } }
  40. 40. Anotações em Beans public class Foo { public void onBar( @XPath("/foo/bar") String nome, @Header("JMSCorrelationID") String id) { // faz algo } }
  41. 41. Beans Consumidores public class Foo { @Consume(uri = "jms:queueFoo") public void onFoo( Exchange e, @Header("JMSGroupID") String grupo) { // faz algo } }
  42. 42. Camel, onde tem? ● Apache ServiceMix (ESB) ● OpenESB ● Apache ActiveMQ ● FUSE ● Java Embedded
  43. 43. Monte um Camelo! Perguntas ? Perguntas ?
  44. 44. Links http://camel.apache.org http://enterpriseintegrationpatterns.com http://www.jawsys.com.br http://blog.brunoborges.com.br

×