42
Taking Apache Camel For a Ride Bruce Snyder 11 April 2008 Amsterdam, The Netherlands

Taking Apache Camel For A Ride

Embed Size (px)

DESCRIPTION

Presentation from ApacheCon EU 2008

Citation preview

Page 1: Taking Apache Camel For A Ride

Taking Apache Camel For a Ride

Bruce Snyder11 April 2008

Amsterdam, The Netherlands

Page 2: Taking Apache Camel For A Ride

System Integration

2

Page 3: Taking Apache Camel For A Ride

3

Page 4: Taking Apache Camel For A Ride

Apache Camel

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

4

Page 5: Taking Apache Camel For A Ride

What is Apache Camel?

5

Page 6: Taking Apache Camel For A Ride

Enterprise Integration Patterns

Text

http://enterpriseintegrationpatterns.com/6

Page 7: Taking Apache Camel For A Ride

Patterns

7

Page 8: Taking Apache Camel For A Ride

Message Routing

8

Page 9: Taking Apache Camel For A Ride

Language Support

9

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

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

Page 10: Taking Apache Camel For A Ride

Apache Camel Components

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

Page 11: Taking Apache Camel For A Ride

History of Apache Camel

11

Page 12: Taking Apache Camel For A Ride

The Camel Context

12

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

Pattern Examples

13

Page 14: Taking Apache Camel For A Ride

Patterns Again

14

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

15

Page 16: 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

16

Page 17: Taking Apache Camel For A Ride

Message Filter

17

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

Page 18: Taking Apache Camel For A Ride

Message Filter

18

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

Splitter

19

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

Page 20: Taking Apache Camel For A Ride

Splitter Using XQuery

20

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

Page 21: Taking Apache Camel For A Ride

Aggregator

21

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

Page 22: Taking Apache Camel For A Ride

Message Translator

22

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

Page 23: Taking Apache Camel For A Ride

Resequencer

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

23

Page 24: Taking Apache Camel For A Ride

Throttlerpublic class MyRouteBuilder extends RouteBuilder { public void configure() { from("seda:a”). throttler(3).timePeriodMillis(30000). to("seda:b"); }}

24

Page 25: Taking Apache Camel For A Ride

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

25

Page 26: Taking Apache Camel For A Ride

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

26

Page 27: Taking Apache Camel For A Ride

Beans

27

Page 28: Taking Apache Camel For A Ride

Bean

28

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

Bean as a Message Translator

29

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

Page 30: Taking Apache Camel For A Ride

Bean as a Message Translator

With Method Name

30

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

Page 31: Taking Apache Camel For A Ride

Type Conversion

31

Page 32: Taking Apache Camel For A Ride

Type Conversion

32

@Converterpublic class IOConverter {

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

new FileInputStream(file)); }}

Page 33: Taking Apache Camel For A Ride

Binding Beans to Camel Endpoints

33

public class Foo {

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

Page 34: Taking Apache Camel For A Ride

Binding Method Arguments

34

public class Foo {

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

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

Page 35: Taking Apache Camel For A Ride

Injecting Endpoints Into Beans

35

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

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

Page 36: Taking Apache Camel For A Ride

Spring Remoting - Client Side

36

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

Page 37: 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: Taking Apache Camel For A Ride

Dependency Injection

38

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

Business Activity Monitoring (BAM)

39

Page 40: Taking Apache Camel For A Ride

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

Business Activity Monitoring (BAM)

40

Page 41: Taking Apache Camel For A Ride

Ride the Camel!

41

Page 42: Taking Apache Camel For A Ride

Questions?

42