Post on 03-Jul-2015
Apache Camelrotas para as suas mensagens
Bruno Borges2009
Apache Camel
Integração
JMS
FTP
HTTP
TCP
SOAP
SOAP
SMTP
JPA
JDBC
RSS
JBI
JT/400
FIX
MSMQ
LDAP
Integração
Camel
?
Framework Open Source
para
Padrões de Integração
EIP● Enterprise Integration Patterns
Padrões
M e ssa g e F ilte rCo 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 da s?
S p lit te r
A g g re g a to r
R e se q u e n ce r
R e cip ie n t L istCo m o ro te a r u m a m e n sa g em a u m a list a d inâ m ica d e d e st in a rá r io s?
C o n te n t- b a se d R o u te r
Co m o t ra ta r u m a s itu a çã o o n d e a im p le m e n ta çã o d e u m a fu n çã o ló g ica e stá e sp a lh a d a e m m ú lt ip los s is te m a s?
Co m o p ro ce ssa r u m a m e n sa ge 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 iv id u a lm e n te d e fo rm a s d ife re n tes?
Co m o co m b ina r o re su lta d o d e m e n sa g e n s in d iv idu a is, p o ré m re la cio n a d as, n u m a ú n ica sa íd a ?
Co m o re to m a r a o rd e m d e m e n sa g e n s re la c io n a d a s e n via d as fo ra d e o rd e m ?
Roteamento de Mensagens
Roteamento
mensagem
_________________________________
___________ ______
Linguagens● BeanShell● JavaScript● Groovy● Python● PHP● Ruby● Scala
● SQL● XPath● XQuery● JSR 223 Scripting● OGNL● EL (JSP/JSF)
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
● RouteBuilder● DSL – Domain Specific Language
– Java
– Spring XML
– Scala
public class CamelStartup { public static void main(String... args) throws Exception { CamelContext context = new DefaultCamelContext(); context.addRoutes(new MyRoute()); context.start(); }}
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 ...
M e ssa g e F ilte rCo 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 da s?
S p lit te r
A g g re g a to r
R e se q u e n ce r
R e cip ie n t L istCo m o ro te a r u m a m e n sa g em a u m a list a d inâ m ica d e d e st in a rá r io s?
C o n te n t- b a se d R o u te r
Co m o t ra ta r u m a s itu a çã o o n d e a im p le m e n ta çã o d e u m a fu n çã o ló g ica e stá e sp a lh a d a e m m ú lt ip los s is te m a s?
Co m o p ro ce ssa r u m a m e n sa ge 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 iv id u a lm e n te d e fo rm a s d ife re n tes?
Co m o co m b ina r o re su lta d o d e m e n sa g e n s in d iv idu a is, p o ré m re la cio n a d as, n u m a ú n ica sa íd a ?
Co m o re to m a r a o rd e m d e m e n sa g e n s re la c io n a d a s e n via d as fo ra d e o rd e m ?
Roteamento de Mensagens
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
C o n te n t F ilte r
N o rm a liz e r
M e ssa g e T ra n sla to r
Co m o sistem a s u sa n d o d ife re n te s fo rm a to 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 s i?
C o n te n t E n rich e r
Co m o se co m u n ica r com u m sis te m a se a m e n sa g em d e o r ig e m n ã o d isp õ e d e to do s o s da do s n e ce ssá r io s?
Co m o sim p lif ica r m e n sa g e n s m u ito g ra n d e s, q u a n do so m e n te p o u co s d a d o s in te re ssa m ?
Co m o p ro ce ssa r m e n sa g e n s se m a n t ica m e n te sim ila re s, m a s e m d ife re n te s fo rm a to s?
Tradução de Mensagens
Message Translator
Content Enricher
Content Filter
Exemplosfrom("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 {
@Overridepublic 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;
@Converterpublic 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 ?Perguntas ?
Links
http://camel.apache.org
http://enterpriseintegrationpatterns.com
http://www.jawsys.com.br
http://blog.brunoborges.com.br