Taking Apache Camel For A Ride
Upcoming SlideShare
Loading in...5
×
 

Taking Apache Camel For A Ride

on

  • 6,182 views

Presentation from ApacheCon US 2008

Presentation from ApacheCon US 2008

Statistics

Views

Total Views
6,182
Views on SlideShare
6,028
Embed Views
154

Actions

Likes
6
Downloads
157
Comments
0

3 Embeds 154

http://blog.systemix.fr 131
http://www.slideshare.net 20
http://www.linkedin.com 3

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Taking Apache Camel For A Ride Taking Apache Camel For A Ride Presentation Transcript

  • Taking Apache Camel For A Ride Bruce Snyder bsnyder@apache.org 7 Nov 2008 New Orleans, Louisiana
  • Protocol Integration is Common
  • Data Format Integration is Common View slide
  • Integration is Messy! View slide
  • SOA = Spaghetti Oriented Architecture
  • Options For Integration 1 2 3
  • Too Many Choices!
  • The Easiest Solution - Apache Camel http://activemq.apache.org/camel/
  • What is Apache Camel?
  • Enterprise Integration Patterns http://enterpriseintegrationpatterns.com/
  • Message Routing
  • Patterns
  • History of Apache Camel
  • Camel Components http://activemq.apache.org/camel/components.html
  • Camel Components
  • Simple Routing
  • More Simple Routing
  • Pipeline Routing
  • Multicast Routing
  • Multicast-to-Many Pipeline Routes
  • Language Support For Message Processing • BeanShell • JSP EL • Javascript • OGNL • Groovy • SQL • Python • XPath • PHP • XQuery • Ruby
  • Getting Started - 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>
  • Pattern Examples
  • Patterns
  • 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;); } };
  • 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>
  • 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;); } }
  • 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>
  • Splitter public class MyRouteBuilder extends RouteBuilder { public void configure() { from(quot;file://ordersquot;). splitter(body().tokenize(quot;nquot;)). to(quot;activemq:Order.Itemsquot;); } }
  • 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;); } }
  • Aggregator public class MyRouteBuilder extends RouteBuilder { public void configure() { from(quot;activemq:Inventory.Itemsquot;). aggregator(header(quot;symbolquot;).isEqualTo(quot;IBMquot;). to(quot;activemq:Inventory.Orderquot;); } }
  • 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;); } }
  • Resequencer public class MyRouteBuilder extends RouteBuilder { public void configure() { from(quot;direct:a”). resequencer(header(quot;customerRankquot;)). to(quot;seda:bquot;); } }
  • Routing Slip public class MyRouteBuilder extends RouteBuilder { public void configure() { from(quot;direct:a”).routingSlip(); } }
  • Throttler public class MyRouteBuilder extends RouteBuilder { public void configure() { from(quot;seda:a”). throttler(3).timePeriodMillis(30000). to(quot;seda:bquot;); } }
  • Delayer public class MyRouteBuilder extends RouteBuilder { public void configure() { from(quot;seda:a”). delayer(header(quot;JMSTimestampquot;, 3000). to(quot;seda:bquot;); } }
  • Load Balancer public class MyRouteBuilder extends RouteBuilder { public void configure() { from(quot;file:/path/to/filequot;). loadBalance().roundRobin(). to(quot;seda:aquot;, quot;seda:bquot;, quot;seda:cquot;); } } Policy Description Round Robin Balance the exchange load across the available endpoints Random Randomly choose an endpoint to send the exchange Sticky Sticky load balancing of exchanges using an expression Topic Send exchange to all endpoints (like a JMS topic)
  • Multicast public class MyRouteBuilder extends RouteBuilder { public void configure() { from(quot;direct:aquot;). multicast(). to(quot;direct:x?x=y&amp;1=2quot;, quot;direct:yquot;, quot;direct:zquot;); } }
  • Demo
  • More Patterns
  • Wire Tap public class MyRouteBuilder extends RouteBuilder { public void configure() { from(quot;direct:aquot;). to(quot;log:com.mycompany.messages?level=infoquot;). to(quot;mock:fooquot;); } }
  • Content Enricher public class MyRouteBuilder extends RouteBuilder { public void configure() { from(quot;activemq:My.Queuequot;). to(quot;velocity:com/acme/MyResponse.vmquot;). to(quot;activemq:Another.Queuequot;); } }
  • More Content Enricher public class MyRouteBuilder extends RouteBuilder { public void configure() { from(quot;activemq:My.Queuequot;). beanRef(quot;myBeanNamequot;, quot;myMethodNamequot;). to(quot;activemq:Another.Queuequot;); } }
  • Content Filter public class MyRouteBuilder extends RouteBuilder { public void configure() { from(quot;direct:startquot;).process(new Processor() { public void process(Exchange exchange) { Message in = exchange.getIn(); in.setBody(in.getBody(String.class) + quot; World!quot;); } }).to(quot;mock:resultquot;); } }
  • Combine Patterns public class MyRouteBuilder extends RouteBuilder { public void configure() { from(quot;seda:a”). resequencer(header(quot;JMSGroupSeqquot;)). delayer(3000). to(quot;mock:xquot;); } }
  • Configure Error Handling Global Error Handler: RouteBuilder builder = new RouteBuilder() { public void configure() { errorHandler(deadLetterChannel(quot;file:errorsquot;)); from(quot;bean:fooquot;).to(quot;seda:bquot;); } };
  • Configure Error Handling Local Error Handler: RouteBuilder builder = new RouteBuilder() { public void configure() { from(quot;seda:aquot;). errorHandler(loggingErrorHandler(quot;FOO.BARquot;)). to(quot;seda:bquot;); from(quot;seda:bquot;).to(quot;seda:cquot;); } };
  • Configure Exception Policies RouteBuilder builder = new RouteBuilder() { public void configure() { exception(IOException.class) .initialRedeliveryDelay(5000L) .maximumRedeliveries(3) .maximumRedeliveryDelay(30000L) .backOffMultiplier(1.0) .useExponentialBackOff() .setHeader(MESSAGE_INFO, constant(quot;Damned IOException!quot;)) .to(quot;activemq:errorsquot;); from(quot;seda:aquot;).to(quot;seda:bquot;); } };
  • Beans
  • Make Context Discover Beans 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>
  • Bean as a Message Translator public class MyRouteBuilder extends RouteBuilder { public void configure() { from(quot;activemq:Incoming”). beanRef(quot;myBeanquot;). to(quot;activemq:Outgoingquot;); } }
  • 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;); } }
  • Binding Beans to Camel Endpoints public class Foo { @MessageDriven(uri=quot;activemq:cheesequot;) public void onCheese(String name) { ... } }
  • Binding Method Arguments public class Foo { public void onCheese( @XPath(quot;/foo/barquot;) String name, @Header(quot;JMSCorrelationIDquot;) String id) { ... } }
  • 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;); } } }
  • Type Conversion
  • Type Conversion @Converter public class IOConverter { @Converter public static InputStream toInputStream(File file) throws FileNotFoundException { return new BufferedInputStream( new FileInputStream(file)); } }
  • Type Convertors public class MyRouteBuilder extends RouteBuilder { public void configure() { from(quot;direct:startquot;).process(new Processor() { public void process(Exchange exchange) { Message in = exchange.getIn(); in.setBody(in.getBody(String.class) + quot; World!quot;); } }).to(quot;mock:resultquot;); } } Support for the following types: • File • String • byte[] and ByteBuffer • InputStream and OutputStream • Reader and Writer • Document and Source
  • Message Mapper
  • 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;); } }
  • Another Message Translator public class MyRouteBuilder extends RouteBuilder { public void configure() { from(quot;activemq:FOO.TEST”). transform(body().append(getDynamicText())). to(quot;http://outgoing.com/fooquot;); } }
  • Business Activity Monitoring (BAM)
  • 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;); } }
  • Complex Routing is Easier from(“http://localhost:8080/requests/”). tryBlock(). to(“activemq:queue:requests”). setOutBody(constant(“<ack/>”)). handle(Throwable.class). setFaultBody(constant(“<nack/>”)); from((“activemq:queue:requests?transacted=true”). process(requestTransformer). to(“http://host:8080/Request”). filter(xpath(“//nack”)). process(nackTransformer). to(“jdbc:store”); from(“http://localhost:8080/responses/”). tryBlock(). to(“activemq:queue:responses”). setOutBody(constant(“<ack/>”)). handle(Throwable.class). setFaultBody(constant(“<nack/>”)); from(“activemq:queue:responses?transacted=true”). process(responseTransformer). to(“jdbc:store”); from(“http://localhost:8080/pull/”). to(“jdbc:load”);
  • Finally, the Camel Truck!
  • Ride the Camel! http://activemq.apache.org/camel/