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

Taking Apache Camel For a Ride

Jan 25, 2015

Download

Technology

Bruce Snyder

 
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 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: Taking Apache Camel For a Ride

2

Integration is Messy!

Page 3: Taking Apache Camel For a Ride

System Integration

3

Page 4: Taking Apache Camel For a Ride

Data Formats

4

Page 5: Taking Apache Camel For a Ride

Apache Camel

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

Page 6: Taking Apache Camel For a Ride

What is Apache Camel?

6

Page 7: Taking Apache Camel For a Ride

Enterprise Integration Patterns

http://enterpriseintegrationpatterns.com/

7

Page 8: Taking Apache Camel For a Ride

Patterns

8

Page 9: Taking Apache Camel For a Ride

Message Routing

9

Page 10: Taking Apache Camel For a Ride

Language Support

10

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

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

Page 11: Taking Apache Camel For a Ride

Apache Camel Components

11

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

Page 12: Taking Apache Camel For a Ride

History of Apache Camel

12

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

Pattern Examples

14

Page 15: Taking Apache Camel For a Ride

Patterns Again

15

Page 16: 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: 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: 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: 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: 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: 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: 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: 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: 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: 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: 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: 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: Taking Apache Camel For a Ride

Beans

28

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

Type Conversion

32

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

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

Business Activity Monitoring (BAM)

40

Page 41: 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: Taking Apache Camel For a Ride

Ride the Camel!

42

Page 43: Taking Apache Camel For a Ride

Questions?

43