Apache Camel rotas para as suas mensagens Bruno Borges 2009
Apache Camel
Integração SOAP MSMQ JMS RSS FTP JT/400 JDBC SMTP HTTP FIX JBI JPA Twitter SOAP LDAP TCP
Integração
Camel
?
Framework Open Source para Padrões de Integração
EIP Enterprise Integration Patterns
Padrões Roteamento de Mensagens C o n te n t- b a se d R o u te r M e ssa g e F iltec ro 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? S p litte r A g g re g a to r R e se q u e n ce r C 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 ló g ica e st á e sp a lh a d a e m m ú lt ip lo s siste m 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 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 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 o rd e m? R e cip ie n t L istc 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?
Roteamento mensagem
Linguagens BeanShell SQL JavaScript XPath Groovy XQuery Python JSR 223 Scripting PHP OGNL Ruby EL (JSP/JSF) Scala
Componentes ActiveMQ AMQP Atom / RSS Beans Comet (Jetty) CXF DataSet (testes) Direct File FIX FTP / SFTP Hibernate HL7 MLLP HTTP ibatis JBI / JCR JDBC / JPA JMS JT/400 LDAP LOG Mail Mina MSMQ Quartz RMI Velocity XMPP Xquery... Twitter?
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
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");
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>
Definição de Rotas Scala class MyRouteBuilder extends RouteBuilder { "direct:a" --> "mock:a" "direct:b" to "mock:b"
Repetindo... Roteamento de Mensagens C o n te n t- b a se d R o u te r M e ssa g e F iltec ro 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? S p litte r A g g re g a to r R e se q u e n ce r C 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 ló g ica e st á e sp a lh a d a e m m ú lt ip lo s siste m 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 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 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 o rd e m? R e cip ie n t L istc 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?
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"); ;
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>
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")
Message Filter public class MyRouteBuilder extends RouteBuilder { public void configure() { from("activemq:topic:quotes").filter().xpath("/quote/product = 'widget'").to("mqseries:widgetquotes");
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>
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")
Message Splitter public class MyRouteBuilder extends RouteBuilder { public void configure() { from("file://orders").splitter(body().tokenize("\n")).to("activemq:order.items");
Message Splitter public class MyRouteBuilder extends RouteBuilder { public void configure() { from("file://orders") // Splitter com XQuery.splitter(xquery("/order/items")).to("activemq:Order.Items");
Message Aggregator public class MyRouteBuilder extends RouteBuilder { public void configure() { from("activemq:inventory.items").aggregator().xpath("/order/@id").to("activemq:inventory.order");
Resequencer public class MyRouteBuilder extends RouteBuilder { public void configure() { from("direct:a").resequencer(header("jmspriority")).to("seda:b");
Recipient List public class MyRouteBuilder extends RouteBuilder { public void configure() { from("direct:a").recipientlist(header("destinos"));
Camel Twitter CAMEL-1520
Padrões Tradução de Mensagens M e ssa g e T ra n sla to r C o n te n t E n rich e r C o n te n t F ilte r N o rm a liz e r 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 co m u n ica r-se e n t re si? 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 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 d a d o s in t e re ssa m? 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 d if e re n t e s f o rm a t o s?
Message Translator
Content Enricher
Content Filter
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>
Normalizer
Dependency Injection Google Guice
Beans
Beans Tradutores public class MinhaRota extends RouteBuilder { @Override public void configure() throws Exception { from("activemq:inbox").beanref("meubean").beanref("meuoutrobean", "metodoqualquer").to("activemq:outbox");
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();
Anotações em Beans public class Foo { public void onbar( @XPath("/foo/bar") String nome, @Header("JMSCorrelationID") String id) { // faz algo
Beans Consumidores public class Foo { @Consume(uri = "jms:queuefoo") public void onfoo( Exchange e, @Header("JMSGroupID") String grupo) { // faz algo
Camel, onde tem? Apache ServiceMix (ESB) OpenESB Apache ActiveMQ FUSE Java Embedded
Monte um Camelo! Perguntas?
Links http://camel.apache.org http://enterpriseintegrationpatterns.com http://www.jawsys.com.br http://blog.brunoborges.com.br