+ All Categories
Home > Technology > Taking Apache Camel For A Ride

Taking Apache Camel For A Ride

Date post: 07-Nov-2014
Category:
Upload: bruce-snyder
View: 6,239 times
Download: 1 times
Share this document with a friend
Description:
Presentation from ApacheCon US 2008
Popular Tags:
68
Taking Apache Camel For A Ride Bruce Snyder [email protected] 7 Nov 2008 New Orleans, Louisiana
Transcript
Page 1: Taking Apache Camel For A Ride

Taking Apache Camel

For A RideBruce [email protected] Nov 2008 New Orleans, Louisiana

Page 2: Taking Apache Camel For A Ride

Protocol Integration is Common

Page 3: Taking Apache Camel For A Ride

Data Format Integration is Common

Page 4: Taking Apache Camel For A Ride

Integration is Messy!

Page 5: Taking Apache Camel For A Ride
Page 6: Taking Apache Camel For A Ride

SOA = Spaghetti Oriented Architecture

Page 7: Taking Apache Camel For A Ride
Page 8: Taking Apache Camel For A Ride

Options For Integration

1 2

3

Page 9: Taking Apache Camel For A Ride

Too Many Choices!

Page 10: Taking Apache Camel For A Ride

The Easiest Solution - Apache Camel

http://activemq.apache.org/camel/

Page 11: Taking Apache Camel For A Ride

What is Apache Camel?

Page 12: Taking Apache Camel For A Ride

Enterprise Integration Patterns

http://enterpriseintegrationpatterns.com/

Page 13: Taking Apache Camel For A Ride

Message Routing

Page 14: Taking Apache Camel For A Ride

Patterns

Page 15: Taking Apache Camel For A Ride

History of Apache Camel

Page 16: Taking Apache Camel For A Ride

Camel Components

http://activemq.apache.org/camel/components.html

Page 17: Taking Apache Camel For A Ride

Camel Components

Page 18: Taking Apache Camel For A Ride

Simple Routing

Page 19: Taking Apache Camel For A Ride

More Simple Routing

Page 20: Taking Apache Camel For A Ride

Pipeline Routing

Page 21: Taking Apache Camel For A Ride

Multicast Routing

Page 22: Taking Apache Camel For A Ride

Multicast-to-Many Pipeline Routes

Page 23: Taking Apache Camel For A Ride

Language Support For Message Processing

• BeanShell• Javascript• Groovy• Python• PHP• Ruby

• JSP EL• OGNL• SQL• XPath• XQuery

Page 24: Taking Apache Camel For A Ride

Getting Started - The Camel Context

<camelContext xmlns="http://activemq.apache.org/camel/schema/spring"> <package>com.acme.quotes</package></camelContext>

CamelContext context = new DefaultCamelContext();context.addRoutes(new MyRouteBuilder());context.start();

Page 25: Taking Apache Camel For A Ride

PatternExamples

Page 26: Taking Apache Camel For A Ride

Patterns

Page 27: Taking Apache Camel For A Ride

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"); } };

Page 28: Taking Apache Camel For A Ride

<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

Page 29: Taking Apache Camel For A Ride

Message Filter

public class MyRouteBuilder extends RouteBuilder { public void configure() { from("activemq:topic:Quotes). filter().xpath("/quote/product = ‘widget’"). to("mqseries:WidgetQuotes"); }}

Page 30: Taking Apache Camel For A Ride

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>

Page 31: Taking Apache Camel For A Ride

Splitter

public class MyRouteBuilder extends RouteBuilder { public void configure() { from("file://orders").

splitter(body().tokenize("\n")). to("activemq:Order.Items"); }}

Page 32: Taking Apache Camel For A Ride

Splitter Using XQuery

public class MyRouteBuilder extends RouteBuilder { public void configure() { from("file://orders").

splitter().xquery("/order/items"). to("activemq:Order.Items"); }}

Page 33: Taking Apache Camel For A Ride

Aggregator

public class MyRouteBuilder extends RouteBuilder { public void configure() { from("activemq:Inventory.Items"). aggregator(header("symbol").isEqualTo("IBM"). to("activemq:Inventory.Order"); }}

Page 34: Taking Apache Camel For A Ride

Message Translator

public class MyRouteBuilder extends RouteBuilder { public void configure() { from("file://incoming”).

to("xslt:com/acme/mytransform.xsl"). to("http://outgoing.com/foo"); }}

Page 35: Taking Apache Camel For A Ride

Resequencer

public class MyRouteBuilder extends RouteBuilder { public void configure() { from("direct:a”).

resequencer(header("customerRank")). to("seda:b"); }}

Page 36: Taking Apache Camel For A Ride

Routing Slip

public class MyRouteBuilder extends RouteBuilder { public void configure() { from("direct:a”).routingSlip();

}}

Page 37: Taking Apache Camel For A Ride

Throttler

public class MyRouteBuilder extends RouteBuilder { public void configure() { from("seda:a”).

throttler(3).timePeriodMillis(30000). to("seda:b"); }}

Page 38: Taking Apache Camel For A Ride

Delayer

public class MyRouteBuilder extends RouteBuilder { public void configure() { from("seda:a”).

delayer(header("JMSTimestamp", 3000). to("seda:b"); }}

Page 39: Taking Apache Camel For A Ride

Load Balancerpublic class MyRouteBuilder extends RouteBuilder { public void configure() { from("file:/path/to/file"). loadBalance().roundRobin(). to("seda:a", "seda:b", "seda:c"); }}

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)

Page 40: Taking Apache Camel For A Ride

Multicast

public class MyRouteBuilder extends RouteBuilder { public void configure() { from("direct:a"). multicast(). to("direct:x?x=y&amp;1=2", "direct:y", "direct:z");

}}

Page 41: Taking Apache Camel For A Ride

Demo

Page 42: Taking Apache Camel For A Ride

MorePatterns

Page 43: Taking Apache Camel For A Ride

Wire Tap

public class MyRouteBuilder extends RouteBuilder { public void configure() { from("direct:a"). to("log:com.mycompany.messages?level=info"). to("mock:foo");

}}

Page 44: Taking Apache Camel For A Ride

Content Enricher

public class MyRouteBuilder extends RouteBuilder { public void configure() { from("activemq:My.Queue"). to("velocity:com/acme/MyResponse.vm"). to("activemq:Another.Queue");

}}

Page 45: Taking Apache Camel For A Ride

More Content Enricher

public class MyRouteBuilder extends RouteBuilder { public void configure() { from("activemq:My.Queue"). beanRef("myBeanName", "myMethodName"). to("activemq:Another.Queue");

}}

Page 46: Taking Apache Camel For A Ride

Content Filter

public class MyRouteBuilder extends RouteBuilder { public void configure() { from("direct:start").process(new Processor() { public void process(Exchange exchange) { Message in = exchange.getIn(); in.setBody(in.getBody(String.class) + " World!"); } }).to("mock:result"); }}

Page 47: Taking Apache Camel For A Ride

Combine Patterns

public class MyRouteBuilder extends RouteBuilder { public void configure() { from("seda:a”). resequencer(header("JMSGroupSeq")).

delayer(3000). to("mock:x"); }}

Page 48: Taking Apache Camel For A Ride

Configure Error Handling

Global Error Handler:

RouteBuilder builder = new RouteBuilder() { public void configure() { errorHandler(deadLetterChannel("file:errors")); from("bean:foo").to("seda:b"); }};

Page 49: Taking Apache Camel For A Ride

Configure Error Handling

Local Error Handler: RouteBuilder builder = new RouteBuilder() { public void configure() { from("seda:a"). errorHandler(loggingErrorHandler("FOO.BAR")). to("seda:b");

from("seda:b").to("seda:c"); }};

Page 50: Taking Apache Camel For A Ride

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("Damned IOException!")) .to("activemq:errors");

from("seda:a").to("seda:b"); }};

Page 51: Taking Apache Camel For A Ride

Beans

Page 52: Taking Apache Camel For A Ride

Make Context Discover Beans

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>

Page 53: Taking Apache Camel For A Ride

Bean as a Message Translator

public class MyRouteBuilder extends RouteBuilder { public void configure() {

from("activemq:Incoming”). beanRef("myBean"). to("activemq:Outgoing"); }}

Page 54: Taking Apache Camel For A Ride

Bean as a Message Translator

public class MyRouteBuilder extends RouteBuilder { public void configure() { from("activemq:Incoming”). beanRef("myBean", "someMethod"). to("activemq:Outgoing"); }}

*With Method Name

Page 55: Taking Apache Camel For A Ride

Binding Beans to Camel Endpoints

public class Foo {

@MessageDriven(uri="activemq:cheese") public void onCheese(String name) { ... }}

Page 56: Taking Apache Camel For A Ride

Binding Method Arguments

public class Foo {

public void onCheese( @XPath("/foo/bar") String name, @Header("JMSCorrelationID") String id) { ... }}

Page 57: Taking Apache Camel For A Ride

Injecting Endpoints Into Beans

public class Foo { @EndpointInject(uri="activemq:foo.bar") ProducerTemplate producer;

public void doSomething() { if (whatever) { producer.sendBody("<hello>world!</hello>"); } }}

Page 58: Taking Apache Camel For A Ride

Type Conversion

Page 59: Taking Apache Camel For A Ride

Type Conversion

@Converterpublic class IOConverter {

@Converter public static InputStream toInputStream(File file) throws FileNotFoundException { return new BufferedInputStream(

new FileInputStream(file)); }}

Page 60: Taking Apache Camel For A Ride

Type Convertors

public class MyRouteBuilder extends RouteBuilder { public void configure() { from("direct:start").process(new Processor() { public void process(Exchange exchange) { Message in = exchange.getIn(); in.setBody(in.getBody(String.class) + " World!"); } }).to("mock:result"); }}

Support for the following types: • File• String• byte[] and ByteBuffer • InputStream and OutputStream • Reader and Writer • Document and Source

Page 61: Taking Apache Camel For A Ride

Message Mapper

Page 62: Taking Apache Camel For A Ride

Message Translator

public class MyRouteBuilder extends RouteBuilder { public void configure() { from("file://incoming”).

to("xslt:com/acme/mytransform.xsl"). to("http://outgoing.com/foo"); }}

Page 63: Taking Apache Camel For A Ride

Another Message Translator

public class MyRouteBuilder extends RouteBuilder { public void configure() { from("activemq:FOO.TEST”). transform(body().append(getDynamicText())). to("http://outgoing.com/foo"); }}

Page 64: Taking Apache Camel For A Ride

Business Activity Monitoring (BAM)

Page 65: Taking Apache Camel For A Ride

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"); }}

Page 66: Taking Apache Camel For A Ride

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”);

Page 67: Taking Apache Camel For A Ride

Finally, the Camel Truck!

Page 68: Taking Apache Camel For A Ride

Ride the Camel!

http://activemq.apache.org/camel/

Ride the Camel!


Recommended