DOSUG Taking Apache Camel For A Ride

5,013 views

Published on

Bruce Snyder's excellent talk on Apache Camel to the Denver Open Source Users Group (www.denveropensource.org) in June 2008

Published in: Economy & Finance, Technology
1 Comment
9 Likes
Statistics
Notes
  • i want about camels
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total views
5,013
On SlideShare
0
From Embeds
0
Number of Embeds
43
Actions
Shares
0
Downloads
153
Comments
1
Likes
9
Embeds 0
No embeds

No notes for slide

DOSUG 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=quot;http://activemq.apache.org/camel/schema/springquot;> <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(quot;seda:aquot;).choice().when(header(quot;fooquot;) .isEqualTo(quot;barquot;)).to(quot;seda:bquot;) .when(header(quot;fooquot;).isEqualTo(quot;cheesequot;)) .to(quot;seda:cquot;).otherwise().to(quot;seda:dquot;); } }; 16
  17. 17. Content Based Router <camelContext xmlns=quot;http://activemq.apache.org/camel/schema/springquot;> <route> <from uri=quot;activemq:NewOrdersquot;/> <choice> <when> <xpath>/order/product = 'widget'</xpath> <to uri=quot;activemq:Orders.Widgetsquot;/> </when> <when> <xpath>/order/product = 'gadget'</xpath> <to uri=quot;activemq:Orders.Gadgetsquot;/> </when> <otherwise> <to uri=quot;activemq:Orders.Badquot;/> </otherwise> </choice> </route> </camelContext> 17
  18. 18. Message Filter public class MyRouteBuilder extends RouteBuilder { public void configure() { from(quot;activemq:topic:Quotes). filter().xpath(quot;/quote/product = ‘widget’quot;). to(quot;mqseries:WidgetQuotesquot;); } } 18
  19. 19. Message Filter <camelContext xmlns=quot;http://activemq.apache.org/camel/schema/springquot;> <route> <from uri=quot;activemq:topic:Quotesquot;/> <filter> <xpath>/quote/product = ‘widget’</xpath> <to uri=quot;mqseries:WidgetQuotesquot;/> </filter> </route> </camelContext> 19
  20. 20. Splitter public class MyRouteBuilder extends RouteBuilder { public void configure() { from(quot;file://ordersquot;). splitter(body().tokenize(quot;nquot;)). to(quot;activemq:Order.Itemsquot;); } } 20
  21. 21. Splitter Using XQuery public class MyRouteBuilder extends RouteBuilder { public void configure() { from(quot;file://ordersquot;). splitter().xquery(quot;/order/itemsquot;). to(quot;activemq:Order.Itemsquot;); } } 21
  22. 22. Aggregator public class MyRouteBuilder extends RouteBuilder { public void configure() { from(quot;activemq:Inventory.Itemsquot;). aggregator().xpath(quot;/order/@idquot;). to(quot;activemq:Inventory.Orderquot;); } } 22
  23. 23. Message Translator public class MyRouteBuilder extends RouteBuilder { public void configure() { from(quot;file://incoming”). to(quot;xslt:com/acme/mytransform.xslquot;). to(quot;http://outgoing.com/fooquot;); } } 23
  24. 24. Resequencer public class MyRouteBuilder extends RouteBuilder { public void configure() { from(quot;direct:a”). resequencer(header(quot;JMSPriorityquot;)). to(quot;seda:bquot;); } } 24
  25. 25. Throttler public class MyRouteBuilder extends RouteBuilder { public void configure() { from(quot;seda:a”). throttler(3).timePeriodMillis(30000). to(quot;seda:bquot;); } } 25
  26. 26. Delayer public class MyRouteBuilder extends RouteBuilder { public void configure() { from(quot;seda:a”). delayer(header(quot;JMSTimestampquot;, 3000). to(quot;seda:bquot;); } } 26
  27. 27. Combine Patterns public class MyRouteBuilder extends RouteBuilder { public void configure() { from(quot;seda:a”). resequencer(header(quot;JMSPriorityquot;)). delayer(3000). to(quot;seda:bquot;); } } 27
  28. 28. Beans 28
  29. 29. Bean package com.mycompany.beans; public class MyBean { public void someMethod(String name) { ... } } <camelContext xmlns=quot;http://activemq.apache.org/camel/schema/springquot;> <package>com.mycompany.beans</package> </camelContext> 29
  30. 30. Bean as a Message Translator public class MyRouteBuilder extends RouteBuilder { public void configure() { from(quot;activemq:Incoming”). beanRef(quot;myBeanquot;). to(quot;activemq:Outgoingquot;); } } 30
  31. 31. Bean as a Message Translator *With Method Name public class MyRouteBuilder extends RouteBuilder { public void configure() { from(quot;activemq:Incoming”). beanRef(quot;myBeanquot;, quot;someMethodquot;). to(quot;activemq:Outgoingquot;); } } 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=quot;activemq:cheesequot;) public void onCheese(String name) { ... } } 34
  35. 35. Binding Method Arguments public class Foo { public void onCheese( @XPath(quot;/foo/barquot;) String name, @Header(quot;JMSCorrelationIDquot;) String id) { ... } } http://activemq.apache.org/camel/bean-integration.html 35
  36. 36. Injecting Endpoints Into Beans public class Foo { @EndpointInject(uri=quot;activemq:foo.barquot;) ProducerTemplate producer; public void doSomething() { if (whatever) { producer.sendBody(quot;<hello>world!</hello>quot;); } } } 36
  37. 37. Spring Remoting - Server Side <camelContext xmlns=quot;http://activemq.apache.org/camel/schema/springquot;> <export id=quot;sayServicequot; uri=quot;activemq:MyServicequot; serviceRef=quot;sayImplquot; serviceInterface=quot;com.acme.MyServiceInterfacequot;/> </camelContext> <bean id=quot;sayImplquot; class=quot;com.acme.MyServiceImplquot;/> 37
  38. 38. Spring Remoting - Client Side <camelContext xmlns=quot;http://activemq.apache.org/camel/schema/springquot;> <proxy id=quot;sayServicequot; serviceUrl=quot;activemq:MyServicequot; serviceInterface=quot;com.acme.MyServiceInterfacequot;/> </camelContext> 38
  39. 39. Dependency Injection <camelContext xmlns=quot;http://activemq.apache.org/camel/schema/springquot;> ... </camelContext> <bean id=quot;activemqquot; class=quot;org.apache.camel.component.jms.JmsComponentquot;> <property name=quot;connectionFactoryquot;> <bean class=quot;org.apache.activemq.ActiveMQConnectionFactoryquot;> <property name=quot;brokerURLquot; value=quot;vm://localhost?broker.persistent=falsequot;/> </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(quot;activemq:PurchaseOrdersquot;) .correlate(xpath(quot;/purchaseOrder/@idquot;).stringResult()); ActivityBuilder invoice = activity(quot;activemq:Invoicesquot;) .correlate(xpath(quot;/invoice/@purchaseOrderIdquot;).stringResult()); // now lets add some BAM rules invoice.starts().after(purchaseOrder.completes()) .expectWithin(seconds(1)) .errorIfOver(seconds(2)).to(quot;activemq:FailedProcessesquot;); } } 41
  42. 42. Ride the Camel! 42
  43. 43. Questions? 43

×