43
Taking Apache Camel For a Ride Bruce Snyder [email protected] 3 June 2008 Taking Apache Camel For a Ride 1

DOSUG Taking Apache Camel For A Ride

Embed Size (px)

DESCRIPTION

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

Citation preview

Page 1: DOSUG Taking Apache Camel For A Ride

Taking Apache Camel For a Ride

Bruce [email protected]

3 June 2008

Taking Apache Camel For a Ride

1

Page 2: DOSUG Taking Apache Camel For A Ride

2

Integration is Messy!

Page 3: DOSUG Taking Apache Camel For A Ride

System Integration

3

Page 4: DOSUG Taking Apache Camel For A Ride

Data Formats

4

Page 5: DOSUG Taking Apache Camel For A Ride

Apache Camel

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

Page 6: DOSUG Taking Apache Camel For A Ride

What is Apache Camel?

6

Page 7: DOSUG Taking Apache Camel For A Ride

Enterprise Integration Patterns

http://enterpriseintegrationpatterns.com/

7

Page 8: DOSUG Taking Apache Camel For A Ride

Patterns

8

Page 9: DOSUG Taking Apache Camel For A Ride

Message Routing

9

Page 10: DOSUG Taking Apache Camel For A Ride

Language Support

10

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

• SQL• XPath• XQuery• OGNL• JSR 223 scripting

Page 11: DOSUG Taking Apache Camel For A Ride

Apache Camel Components

11

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

Page 12: DOSUG Taking Apache Camel For A Ride

History of Apache Camel

12

Page 13: DOSUG Taking Apache Camel For A Ride

The Camel Context

13

<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 14: DOSUG Taking Apache Camel For A Ride

Pattern Examples

14

Page 15: DOSUG Taking Apache Camel For A Ride

Patterns Again

15

Page 16: DOSUG 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"); } };

16

Page 17: DOSUG 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

17

Page 18: DOSUG Taking Apache Camel For A Ride

Message Filter

18

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

Page 19: DOSUG Taking Apache Camel For A Ride

Message Filter

19

<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 20: DOSUG Taking Apache Camel For A Ride

Splitter

20

public class MyRouteBuilder extends RouteBuilder { public void configure() { from("file://orders"). splitter(body().tokenize("\n")). to("activemq:Order.Items"); }}

Page 21: DOSUG Taking Apache Camel For A Ride

Splitter Using XQuery

21

public class MyRouteBuilder extends RouteBuilder { public void configure() { from("file://orders"). splitter().xquery("/order/items"). to("activemq:Order.Items"); }}

Page 22: DOSUG Taking Apache Camel For A Ride

Aggregator

22

public class MyRouteBuilder extends RouteBuilder { public void configure() { from("activemq:Inventory.Items"). aggregator().xpath("/order/@id"). to("activemq:Inventory.Order"); }}

Page 23: DOSUG Taking Apache Camel For A Ride

Message Translator

23

public class MyRouteBuilder extends RouteBuilder { public void configure() { from("file://incoming”). to("xslt:com/acme/mytransform.xsl"). to("http://outgoing.com/foo"); }}

Page 24: DOSUG Taking Apache Camel For A Ride

Resequencer

public class MyRouteBuilder extends RouteBuilder { public void configure() { from("direct:a”). resequencer(header("JMSPriority")). to("seda:b"); }}

24

Page 25: DOSUG 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"); }}

25

Page 26: DOSUG 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"); }}

26

Page 27: DOSUG Taking Apache Camel For A Ride

Combine Patterns

public class MyRouteBuilder extends RouteBuilder { public void configure() { from("seda:a”). resequencer(header("JMSPriority")). delayer(3000). to("seda:b"); }}

27

Page 28: DOSUG Taking Apache Camel For A Ride

Beans

28

Page 29: DOSUG Taking Apache Camel For A Ride

Bean

29

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 30: DOSUG Taking Apache Camel For A Ride

Bean as a Message Translator

30

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

Page 31: DOSUG Taking Apache Camel For A Ride

Bean as a Message Translator

31

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

*With Method Name

Page 32: DOSUG Taking Apache Camel For A Ride

Type Conversion

32

Page 33: DOSUG Taking Apache Camel For A Ride

Type Conversion

33

@Converterpublic class IOConverter {

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

new FileInputStream(file)); }}

Page 34: DOSUG Taking Apache Camel For A Ride

Binding Beans to Camel Endpoints

34

public class Foo {

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

Page 35: DOSUG Taking Apache Camel For A Ride

Binding Method Arguments

35

public class Foo {

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

http://activemq.apache.org/camel/bean-integration.html

Page 36: DOSUG Taking Apache Camel For A Ride

Injecting Endpoints Into Beans

36

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

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

Page 37: DOSUG Taking Apache Camel For A Ride

Spring Remoting - Server Side

37

<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"/>

Page 38: DOSUG Taking Apache Camel For A Ride

Spring Remoting - Client Side

38

<camelContext xmlns="http://activemq.apache.org/camel/schema/spring"> <proxy id="sayService" serviceUrl="activemq:MyService" serviceInterface="com.acme.MyServiceInterface"/></camelContext>

Page 39: DOSUG Taking Apache Camel For A Ride

Dependency Injection

39

<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>

Page 40: DOSUG Taking Apache Camel For A Ride

Business Activity Monitoring (BAM)

40

Page 41: DOSUG Taking Apache Camel For A Ride

Business Activity Monitoring (BAM)

41

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 42: DOSUG Taking Apache Camel For A Ride

Ride the Camel!

42

Page 43: DOSUG Taking Apache Camel For A Ride

Questions?

43