44
Riding Apache Camel Willem Jiang [email protected] 2008-12

Riding Apache Camel

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Riding Apache Camel

Riding Apache Camel

Willem Jiang [email protected]

2008-12

Page 2: Riding Apache Camel

About author

• Apache CXF commiter and PMC member

• Apache Camel commiter and PMC member

[email protected]

Page 3: Riding Apache Camel

Riding Apache Camel

•What's Camel

•EIP Examples

•Beans, Type Conversion , Data Format

•Some tips of Camel Riding

Page 4: Riding Apache Camel

What is Camel?

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

Page 5: Riding Apache Camel

What is Camel?

•A Camel can carry 4 times as much load as other beasts of burden!

•Apache Camel is a powerful Spring based Integration Framework.

•Camel implements the Enterprise Integration Patterns allowing you to configure routing and mediation rules in either a Java based Domain Specific Language (or Fluent API) or via Spring based Xml Configuration files. Either approaches mean you get smart completion of routing rules in your IDE whether in your Java or XML editor.

•Apache Camel uses URIs so that it can easily work directly with any kind of Transport or messaging model such as HTTP, ActiveMQ, JMS, JBI, MINA or CXF together with working with pluggable Data Format options. Apache Camel is a small library which has minimal dependencies for easy embedding in any Java application.

Page 6: Riding Apache Camel

Book by Gregor & Bobby!

Page 7: Riding Apache Camel

Message Routing

Page 8: Riding Apache Camel

Message Routing in EIP

Page 9: Riding Apache Camel

Camel Components

Page 10: Riding Apache Camel

Simple Routing

Page 11: Riding Apache Camel

More Simple Routing

Page 12: Riding Apache Camel

Pipeline

Page 13: Riding Apache Camel

Multicast Routing

Page 14: Riding Apache Camel

Some examples of the EIP implemation

Page 15: Riding Apache Camel

Message Filter

Page 16: Riding Apache Camel

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

Message Filter

from("activemq:topic:Quotes). filter().xpath("/quote/product = ‘widget’"). to("mqseries:WidgetQuotes");

Page 17: Riding Apache Camel

Language Support For Message Processing

• BeanShell

• Javascript

• Groovy

• Python

• PHP

• Ruby

• JSP EL

• OGNL

• SQL

• Xpath

• XQuery

Page 18: Riding Apache Camel

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://activemq.apache.org/camel/schema/spring http://activemq.apache.org/camel/schema/spring/camel-spring.xsd"> <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>

</beans>

Message Filter : Spring XML

Page 19: Riding Apache Camel

Message Filter : Java Complete

package com.acme.quotes;

import org.apache.camel.builder.RouteBuilder;

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

// forward widget quotes to MQSeries from("activemq:topic:Quotes). filter().xpath("/quote/product = ‘widget’"). to("mqseries:WidgetQuotes"); }}

Page 20: Riding Apache Camel

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

Starting the CamelContext

<camelContext id="camel" xmlns="http://activemq.apache.org/camel/schema/spring"> <route> <from uri="direct:start"/> <choice> <when> <xpath>$destination = 'firstChoice'</xpath> <to uri="mock:matched"/> </when> <otherwise> <to uri="mock:notMatched"/> </otherwise> </choice> </route> </camelContext>

Page 21: Riding Apache Camel

Content Base Router

from("activemq:NewOrders”). choice().when("/quote/product = ‘widget’"). to("activemq:Orders.Widgets"). choice().when("/quote/product = ‘gadget’"). to("activemq:Orders.Gadgets"). otherwise().to("activemq:Orders.Bad");

Page 22: Riding Apache Camel

<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 23: Riding Apache Camel

How camel do this routing work ?•Camel Components

•Camel Endpoints

•Camel Consumer

•Camel Producer

•Camel-Core

Page 24: Riding Apache Camel

How camel do this routing work ?

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

Page 25: Riding Apache Camel

Beans

Page 26: Riding Apache Camel

Bean as a Message Translator

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

Page 27: Riding Apache Camel

Bean

public class Foo {

public void someMethod(String name) { ... }}

Page 28: Riding Apache Camel

Bean as a Message Translator with method name

from("activemq:Incoming”). beanRef("myBeanName", "someMethod"). to("activemq:Outgoing");

Page 29: Riding Apache Camel

public class Foo {

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

Binding Beans to Camel Endpoints

Page 30: Riding Apache Camel

public class Foo {

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

Binding Method Arguments

for more annotations seehttp://activemq.apache.org/camel/bean-integration.html

Page 31: Riding Apache Camel

Type Conversion

Page 32: Riding Apache Camel

package com.acme.foo.converters;

import org.apache.camel.Converter;import java.io.*;

@Converterpublic class IOConverter {

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

Type Conversion

# META-INF/services/org/apache/camel/TypeConverter

com.acme.foo.converters

Page 33: Riding Apache Camel

protected RouteBuilder createRouteBuilder() { return new RouteBuilder() { public void configure() { from("direct:start").convertBodyTo(InputStream.class).to("mock:result"); } }; } protected RouteBuilder createRouteBuilder() { return new RouteBuilder() { public void configure() { from("direct:start").process(new Processor() { public void process(Exchange exchange) { Message in = exchange.getIn(); in.setBody(in.getBody(InputStream.class)); } }).to("mock:result"); }; }

Type Conversion

Page 34: Riding Apache Camel

Data Format

from("activemq:QueueWithJavaObjects). marshal().jaxb(). to("mqseries:QueueWithXmlMessages");

from("activemq:QueueTestMessage). marshal().zip(). to("mqseries:QueueWithCompressMessage");

from("activemq:QueueWithXmlMessages). unmarshal().jaxb(). to("mqseries:QueueWithJavaObjects");

Page 35: Riding Apache Camel

Riding tips of camel

Page 36: Riding Apache Camel

Camel Riding from Java

•/META-INF/spring/camelContext.xml

•set the CLASSPATH

•java org.apache.camel.spring.Main

Page 37: Riding Apache Camel

Maven Tooling

<project>... <build> <plugins> <plugin> <groupId>org.apache.camel</groupId> <artifactId>camel-maven-plugin</artifactId> </plugin> </plugins> </build> <reporting> <plugins> <plugin> <groupId>org.apache.camel</groupId> <artifactId>camel-maven-plugin</artifactId> </plugin> </plugins> </reporting></project>

mvn camel:run

Page 38: Riding Apache Camel

Maven Plugin Site Report

Page 39: Riding Apache Camel

Writing You Own Component

•Component

•Endpoint

•Consumer

•Provider

Page 40: Riding Apache Camel

Where would I use Camel?

•standalone or in any Spring application

•inside ActiveMQ’s JMS client or the broker

•inside your ESB such as ServiceMix via the servicemix-camel Service Unit

•inside CXF either as a transport or reusing CXF inside Camel

Page 41: Riding Apache Camel

How to write your routing rule in Camel

•What's the magic of from, to, choice ......

• /camel-core/src/main/java/org/apache/camel/model

•Implementing it in DSL way

• Defining the type class that you want

•Implementing it in a Spring configuration way

• Adding the annotation for JAXB consuming

Page 42: Riding Apache Camel

Questions?

Page 43: Riding Apache Camel

Let's take a look at Camel-CXF example

•Open eclipse and go to code please

•What does Camel-CXF example have ?

•Multi-binding and Multi-transport supporting

•LoadBalancing

•JAXWS WebSerivce Provider API

Page 44: Riding Apache Camel

Where do I get more info?

please do take Camel for a ride!

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

don’t get the hump! :-)