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.

Riding Apache Camel


Published on

Published in: Technology
  • Be the first to comment

Riding Apache Camel

  1. 1. Riding Apache Camel Willem Jiang 2008-12
  2. 2. About author • Apache CXF commiter and PMC member • Apache Camel commiter and PMC member •
  3. 3. Riding Apache Camel •What's Camel •EIP Examples •Beans, Type Conversion , Data Format •Some tips of Camel Riding
  4. 4. What is Camel?
  5. 5. What is Camel? • A Camel can carry 4 times as much load as other beasts of burden! • Apache Camel is a powerful Spring based Integration Framework. • Camel implements the Enterprise Integration Patterns allowing you to configure routing and mediation rules in either a Java based Domain Specific Language (or Fluent API) or via Spring based Xml Configuration files. Either approaches mean you get smart completion of routing rules in your IDE whether in your Java or XML editor. • Apache Camel uses URIs so that it can easily work directly with any kind of Transport or messaging model such as HTTP, ActiveMQ, JMS, JBI, MINA or CXF together with working with pluggable Data Format options. Apache Camel is a small library which has minimal dependencies for easy embedding in any Java application.
  6. 6. Book by Gregor & Bobby!
  7. 7. Message Routing
  8. 8. Message Routing in EIP
  9. 9. Camel Components
  10. 10. Simple Routing
  11. 11. More Simple Routing
  12. 12. Pipeline
  13. 13. Multicast Routing
  14. 14. Some examples of the EIP implemation
  15. 15. Message Filter
  16. 16. Message Filter <camelContext xmlns=quot;;> <route> <from uri=quot;activemq:topic:Quotesquot;/> <filter> <xpath>/quote/product = ‘widget’</xpath> <to uri=quot;mqseries:WidgetQuotesquot;/> </filter> </route> </camelContext> from(quot;activemq:topic:Quotes). filter().xpath(quot;/quote/product = ‘widget’quot;). to(quot;mqseries:WidgetQuotesquot;);
  17. 17. Language Support For Message Processing • BeanShell • JSP EL • Javascript • OGNL • Groovy • SQL • Python • Xpath • PHP • XQuery • Ruby
  18. 18. Message Filter : Spring XML <?xml version=quot;1.0quot; encoding=quot;UTF-8quot;?> <beans xmlns=quot;; xmlns:xsi=quot;; xmlns:schemaLocation=quot;;> <camelContext xmlns=quot;;> <route> <from uri=quot;activemq:topic:Quotesquot;/> <filter> <xpath>/quote/product = ‘widget’</xpath> <to uri=quot;mqseries:WidgetQuotesquot;/> </filter> </route> </camelContext> </beans>
  19. 19. Message Filter : Java Complete package com.acme.quotes; import org.apache.camel.builder.RouteBuilder; public class MyRouteBuilder extends RouteBuilder { public void configure() { // forward widget quotes to MQSeries from(quot;activemq:topic:Quotes). filter().xpath(quot;/quote/product = ‘widget’quot;). to(quot;mqseries:WidgetQuotesquot;); } }
  20. 20. Starting the CamelContext CamelContext context = new DefaultCamelContext(); context.addRoutes(new MyRouteBuilder()); context.start(); <camelContext id=quot;camelquot; xmlns=quot;;> <route> <from uri=quot;direct:startquot;/> <choice> <when> <xpath>$destination = 'firstChoice'</xpath> <to uri=quot;mock:matchedquot;/> </when> <otherwise> <to uri=quot;mock:notMatchedquot;/> </otherwise> </choice> </route> </camelContext>
  21. 21. Content Base Router from(quot;activemq:NewOrders”). choice().when(quot;/quote/product = ‘widget’quot;). to(quot;activemq:Orders.Widgetsquot;). choice().when(quot;/quote/product = ‘gadget’quot;). to(quot;activemq:Orders.Gadgetsquot;). otherwise().to(quot;activemq:Orders.Badquot;);
  22. 22. Content Based Router <camelContext xmlns=quot;;> <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>
  23. 23. How camel do this routing work ? •Camel Components •Camel Endpoints •Camel Consumer •Camel Producer •Camel-Core
  24. 24. How camel do this routing work ?
  25. 25. Beans
  26. 26. Bean as a Message Translator from(quot;activemq:Incoming”). beanRef(quot;myBeanNamequot;). to(quot;activemq:Outgoingquot;);
  27. 27. Bean public class Foo { public void someMethod(String name) { ... } }
  28. 28. Bean as a Message Translator with method name from(quot;activemq:Incoming”). beanRef(quot;myBeanNamequot;, quot;someMethodquot;). to(quot;activemq:Outgoingquot;);
  29. 29. Binding Beans to Camel Endpoints public class Foo { @MessageDriven(uri=quot;activemq:cheesequot;) public void onCheese(String name) { ... } }
  30. 30. Binding Method Arguments public class Foo { public void onCheese( @XPath(quot;/foo/barquot;) String name, @Header(quot;JMSCorrelationIDquot;) String id) { ... } } for more annotations see
  31. 31. Type Conversion
  32. 32. Type Conversion package; import org.apache.camel.Converter; import*; @Converter public class IOConverter { @Converter public static InputStream toInputStream(File file) throws FileNotFoundException { return new BufferedInputStream(new FileInputStream(file)); } } # META-INF/services/org/apache/camel/TypeConverter
  33. 33. Type Conversion protected RouteBuilder createRouteBuilder() { return new RouteBuilder() { public void configure() { from(quot;direct:startquot;).convertBodyTo(InputStream.class).to(quot;mock:resultquot;); } }; } protected RouteBuilder createRouteBuilder() { return new RouteBuilder() { public void configure() { from(quot;direct:startquot;).process(new Processor() { public void process(Exchange exchange) { Message in = exchange.getIn(); in.setBody(in.getBody(InputStream.class)); } }).to(quot;mock:resultquot;); }; }
  34. 34. Data Format from(quot;activemq:QueueWithJavaObjects). marshal().jaxb(). to(quot;mqseries:QueueWithXmlMessagesquot;); from(quot;activemq:QueueWithXmlMessages). unmarshal().jaxb(). to(quot;mqseries:QueueWithJavaObjectsquot;); from(quot;activemq:QueueTestMessage). marshal().zip(). to(quot;mqseries:QueueWithCompressMessagequot;);
  35. 35. Riding tips of camel
  36. 36. Camel Riding from Java •/META-INF/spring/camelContext.xml •set the CLASSPATH •java org.apache.camel.spring.Main
  37. 37. Maven Tooling <project> ... <build> <plugins> <plugin> <groupId>org.apache.camel</groupId> <artifactId>camel-maven-plugin</artifactId> </plugin> </plugins> </build> <reporting> <plugins> <plugin> <groupId>org.apache.camel</groupId> <artifactId>camel-maven-plugin</artifactId> </plugin> </plugins> </reporting> </project> mvn camel:run
  38. 38. Maven Plugin Site Report
  39. 39. Writing You Own Component •Component •Endpoint •Consumer •Provider
  40. 40. Where would I use Camel? •standalone or in any Spring application •inside ActiveMQ’s JMS client or the broker •inside your ESB such as ServiceMix via the servicemix-camel Service Unit •inside CXF either as a transport or reusing CXF inside Camel
  41. 41. How to write your routing rule in Camel •What's the magic of from, to, choice ...... • /camel-core/src/main/java/org/apache/camel/model •Implementing it in DSL way • Defining the type class that you want •Implementing it in a Spring configuration way • Adding the annotation for JAXB consuming
  42. 42. Questions?
  43. 43. Let's take a look at Camel-CXF example •Open eclipse and go to code please •What does Camel-CXF example have ? •Multi-binding and Multi-transport supporting •LoadBalancing •JAXWS WebSerivce Provider API
  44. 44. Where do I get more info? please do take Camel for a ride! don’t get the hump! :-)