Riding Apache Camel

Post on 19-Jan-2015

7.959 views 2 download

Tags:

description

 

Transcript of Riding Apache Camel

Riding Apache Camel

Willem Jiang ningjiang@apache.org

2008-12

About author

• Apache CXF commiter and PMC member

• Apache Camel commiter and PMC member

• ningjiang@apache.org

Riding Apache Camel

•What's Camel

•EIP Examples

•Beans, Type Conversion , Data Format

•Some tips of Camel Riding

What is Camel?

http://activemq.apache.org/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.

Book by Gregor & Bobby!

Message Routing

Message Routing in EIP

Camel Components

Simple Routing

More Simple Routing

Pipeline

Multicast Routing

Some examples of the EIP implemation

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>

Message Filter

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

Language Support For Message Processing

• BeanShell

• Javascript

• Groovy

• Python

• PHP

• Ruby

• JSP EL

• OGNL

• SQL

• Xpath

• XQuery

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

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

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>

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

<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

How camel do this routing work ?•Camel Components

•Camel Endpoints

•Camel Consumer

•Camel Producer

•Camel-Core

How camel do this routing work ?

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

Beans

Bean as a Message Translator

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

Bean

public class Foo {

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

Bean as a Message Translator with method name

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

public class Foo {

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

Binding Beans to Camel Endpoints

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

Type Conversion

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

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

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

Riding tips of camel

Camel Riding from Java

•/META-INF/spring/camelContext.xml

•set the CLASSPATH

•java org.apache.camel.spring.Main

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

Maven Plugin Site Report

Writing You Own Component

•Component

•Endpoint

•Consumer

•Provider

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

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

Questions?

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

Where do I get more info?

please do take Camel for a ride!

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

don’t get the hump! :-)