Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Taking Apache Camel For a Ride

5,246 views

Published on

Published in: Technology
  • Be the first to comment

Taking Apache Camel For a Ride

  1. 1. Taking Apache Camel For a Ride Bruce Snyder bsnyder@apache.org 3 June 2008 1
  2. 2. Integration is Messy! 2
  3. 3. System Integration 3
  4. 4. Data Formats 4
  5. 5. Apache Camel http://activemq.apache.org/camel/ 5
  6. 6. What is Apache Camel? 6
  7. 7. Enterprise Integration Patterns http://enterpriseintegrationpatterns.com/ 7
  8. 8. Patterns 8
  9. 9. Message Routing 9
  10. 10. Language Support • BeanShell • SQL • Javascript • XPath • Groovy • XQuery • Python • OGNL • PHP • JSR 223 scripting • Ruby 10
  11. 11. Apache Camel Components http://activemq.apache.org/camel/components.html 11
  12. 12. History of Apache Camel 12
  13. 13. The Camel Context CamelContext context = new DefaultCamelContext(); context.addRoutes(new MyRouteBuilder()); context.start(); <camelContext xmlns="http://activemq.apache.org/camel/schema/spring"> <package>com.acme.quotes</package> </camelContext> 13
  14. 14. Pattern Examples 14
  15. 15. Patterns Again 15
  16. 16. Content Based Router RouteBuilder builder = 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"); } }; 16
  17. 17. 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> 17
  18. 18. Message Filter public class MyRouteBuilder extends RouteBuilder { public void configure() { from("activemq:topic:Quotes). filter().xpath("/quote/product = ‘widget’"). to("mqseries:WidgetQuotes"); } } 18
  19. 19. 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> 19
  20. 20. Splitter public class MyRouteBuilder extends RouteBuilder { public void configure() { from("file://orders"). splitter(body().tokenize("n")). to("activemq:Order.Items"); } } 20
  21. 21. Splitter Using XQuery public class MyRouteBuilder extends RouteBuilder { public void configure() { from("file://orders"). splitter().xquery("/order/items"). to("activemq:Order.Items"); } } 21
  22. 22. Aggregator public class MyRouteBuilder extends RouteBuilder { public void configure() { from("activemq:Inventory.Items"). aggregator().xpath("/order/@id"). to("activemq:Inventory.Order"); } } 22
  23. 23. Message Translator public class MyRouteBuilder extends RouteBuilder { public void configure() { from("file://incoming”). to("xslt:com/acme/mytransform.xsl"). to("http://outgoing.com/foo"); } } 23
  24. 24. Resequencer public class MyRouteBuilder extends RouteBuilder { public void configure() { from("direct:a”). resequencer(header("JMSPriority")). to("seda:b"); } } 24
  25. 25. Throttler public class MyRouteBuilder extends RouteBuilder { public void configure() { from("seda:a”). throttler(3).timePeriodMillis(30000). to("seda:b"); } } 25
  26. 26. Delayer public class MyRouteBuilder extends RouteBuilder { public void configure() { from("seda:a”). delayer(header("JMSTimestamp", 3000). to("seda:b"); } } 26
  27. 27. Combine Patterns public class MyRouteBuilder extends RouteBuilder { public void configure() { from("seda:a”). resequencer(header("JMSPriority")). delayer(3000). to("seda:b"); } } 27
  28. 28. Beans 28
  29. 29. Bean package com.mycompany.beans; public class MyBean { public void someMethod(String name) { ... } } <camelContext xmlns="http://activemq.apache.org/camel/schema/spring"> <package>com.mycompany.beans</package> </camelContext> 29
  30. 30. Bean as a Message Translator public class MyRouteBuilder extends RouteBuilder { public void configure() { from("activemq:Incoming”). beanRef("myBean"). to("activemq:Outgoing"); } } 30
  31. 31. Bean as a Message Translator *With Method Name public class MyRouteBuilder extends RouteBuilder { public void configure() { from("activemq:Incoming”). beanRef("myBean", "someMethod"). to("activemq:Outgoing"); } } 31
  32. 32. Type Conversion 32
  33. 33. Type Conversion @Converter public class IOConverter { @Converter public static InputStream toInputStream(File file) throws FileNotFoundException { return new BufferedInputStream( new FileInputStream(file)); } } 33
  34. 34. Binding Beans to Camel Endpoints public class Foo { @MessageDriven(uri="activemq:cheese") public void onCheese(String name) { ... } } 34
  35. 35. Binding Method Arguments public class Foo { public void onCheese( @XPath("/foo/bar") String name, @Header("JMSCorrelationID") String id) { ... } } http://activemq.apache.org/camel/bean-integration.html 35
  36. 36. Injecting Endpoints Into Beans public class Foo { @EndpointInject(uri="activemq:foo.bar") ProducerTemplate producer; public void doSomething() { if (whatever) { producer.sendBody("<hello>world!</hello>"); } } } 36
  37. 37. Spring Remoting - Server Side <camelContext xmlns="http://activemq.apache.org/camel/schema/spring"> <export id="sayService" uri="activemq:MyService" serviceRef="sayImpl" serviceInterface="com.acme.MyServiceInterface"/> </camelContext> <bean id="sayImpl" class="com.acme.MyServiceImpl"/> 37
  38. 38. Spring Remoting - Client Side <camelContext xmlns="http://activemq.apache.org/camel/schema/spring"> <proxy id="sayService" serviceUrl="activemq:MyService" serviceInterface="com.acme.MyServiceInterface"/> </camelContext> 38
  39. 39. Dependency Injection <camelContext xmlns="http://activemq.apache.org/camel/schema/spring"> ... </camelContext> <bean id="activemq" class="org.apache.camel.component.jms.JmsComponent"> <property name="connectionFactory"> <bean class="org.apache.activemq.ActiveMQConnectionFactory"> <property name="brokerURL" value="vm://localhost?broker.persistent=false"/> </bean> </property> </bean> 39
  40. 40. Business Activity Monitoring (BAM) 40
  41. 41. Business Activity Monitoring (BAM) public class MyActivities extends ProcessBuilder { public void configure() throws Exception { // lets define some activities, correlating on an // XPath query of the message body ActivityBuilder purchaseOrder = activity("activemq:PurchaseOrders") .correlate(xpath("/purchaseOrder/@id").stringResult()); ActivityBuilder invoice = activity("activemq:Invoices") .correlate(xpath("/invoice/@purchaseOrderId").stringResult()); // now lets add some BAM rules invoice.starts().after(purchaseOrder.completes()) .expectWithin(seconds(1)) .errorIfOver(seconds(2)).to("activemq:FailedProcesses"); } } 41
  42. 42. Ride the Camel! 42
  43. 43. Questions? 43

×