Professional Documents
Culture Documents
IDE for implementing enterprise integration patterns easily in ServiceMix using Camel
fusesource.com
Experts in professional open source integration & messaging
#47
Get More Refcardz! Visit refcardz.com
CONTENTS INCLUDE:
n
About Enterprise Integration Patterns The Top Twelve Integration Patterns
About Apache Camel
for Apache Camel
n
n
Essential Patterns
Conclusions and more...
By Claus Ibsen
n
r
Update fomel
c h e C a
Apa
Camel Camel supports Pipes and Filters using the pipeline node.
ABOUT ENTERPRISE INTEGRATION PATTERNS
Java DSL from(“jms:queue:order:in”).pipeline(“direct:transformOrd
er”, “direct:validateOrder”, “jms:queue:order:process”);
Integration is a complex problem. To help deal with the Where jms represents the JMS component used for consuming JMS
messages on the JMS broker. Direct is used for combining endpoints in a
complexity of integration problems, the Enterprise Integration synchronous fashion, allowing you to divide routes into sub routes and/or
Patterns (EIP) have become the standard way to describe, reuse common routes.
document and implement complex integration problems. Hohpe Tip: Pipeline is the default mode of operation when you specify multiple
outputs, so it can be omitted and replaced with the more common node:
& Woolf’s book the Enterprise Integration Patterns has become from(“jms:queue:order:in”).to(“direct:transformOrder”,
the bible in the integration space – essential reading for any “direct:validateOrder”, “jms:queue:order:process”);
integration professional. TIP: You can also separate each step as individual to nodes:
from(“jms:queue:order:in”)
Apache Camel is an open-source project for implementing .to(“direct:transformOrder”)
.to(“direct:validateOrder”)
the EIP easily in a few lines of Java code or Spring XML .to(“jms:queue:order:process”);
configuration. This Refcard guides you through the most XML DSL <route>
common Enterprise Integration Patterns and gives you examples <from uri=”jms:queue:order:in”/>
<pipeline>
of how to implement them either in Java code or using Spring <to uri=”direct:transformOrder”/>
XML. This Refcard is targeted at software developers and <to uri=”direct:validateOrder”/>
<to uri=”jms:queue:order:process”/>
enterprise architects, but anyone in the integration space can </pipeline>
</route>
benefit as well. <route>
<from uri=”jms:queue:order:in”/>
<to uri=”direct:transformOrder”/>
The Top Twelve Integration Patterns for Apache Camel
ESSENTIAL PATTERNS
Ready to start development with Camel?
This group consists of the most essential patterns that anyone Come to FuseSource for
working with integration must know.
• IDE, Training & Consulting
Pipes and Filters
How can we perform complex processing on a message while maintaining • Getting started resources
independence and flexibility?
• Enterprise subscriptions
Pipe Pipe Pipe Pipe
Decrypt Authenticate De-Dup
Solution
A single event often triggers a sequence of processing steps.
Use Pipes and Filters to divide a larger processing steps (filters) that are
FuseSource Free downloads-fusesource.com
connected by channels (pipes).
from(“direct:transformOrder”)
Gadget .process(new OrderTransformProcessor());
New Order Router Inventory
Bean
Instead of the processor, we can use Bean (POJO). An advantage of using
Problem How do we ensure a Message is sent to the correct recipient based on
a Bean over Processor is the fact that we do not have to implement or use
information from its content?
any Camel specific interfaces or types. This allows you to fully decouple your
Solution Use a Content-Based Router to route each message to the correct recipient beans from Camel.
based on the message content. public class OrderTransformerBean {
Camel Camel has extensive support for Content-Based Routing. Camel supports public StringtransformOrder(String body) {
content based routing based on choice, filter, or any other expression.
// do message translation here
}
Java DSL from(“jms:order.process”) }
.choice()
.when(header(“type”).isEqualTo(“widget”)) Object transformer = new OrderTransformerBean();
.to(“jms:order.widget”) from(“direct:transformOrder”).bean(transformer);
.when(header(“type”).isEqualTo(“gadget”))
.to(“jms:order.gadget”) TIP: Camel can create an instance of the bean automatically; you can just refer
.otherwise() to the class type.
.to(“jms:order.other”);
from(“direct:transformOrder”)
XML DSL <route> .bean(OrderTransformerBean.class);
<from uri=”jms:order.process”/>
<choice> TIP: Camel will try to figure out which method to invoke on the bean in case
<when> there are multiple methods. In case of ambiguity, you can specify which
<simple>${header.type} == ‘widget’</simple> methods to invoke by the method parameter:
<to uri=”jms:order.widget”/>
</when> from(“direct:transformOrder”)
<when> .bean(OrderTransformerBean.class, “transformOrder”);
<simple>${header.type} == ‘gadget’</simple>
<to uri=”jms:order.gadget”/> Transform
</when>
Transform is a particular processor allowing you to set a response to be
<otherwise>
returned to the original caller. We use transform to return a constant ACK
<to uri=”jms:order.other”/>
response to the TCP listener after we have copied the message to the JMS
</otherwise>
queue. Notice we use a constant to build an “ACK” string as response.
</choice>
</route>
from(“mina:tcp://localhost:8888?textline=true”)
.to(“jms:queue:order:in”)
TIP: In XML DSL you cannot invoke code, as opposed to the Java DSL. To .transform(constant(“ACK”));
express the predicates for the choices we need to use a language. We will use
simple language that uses a simple expression parser that supports a limited XML DSL Processor
set of operators. You can use any of the more powerful languages supported <route>
in Camel such as: JavaScript, Groovy, Unified EL and many others. <from uri=”direct:transformOrder”/>
<process ref=”transformer”/>
</route>
TIP: You can also use a method call to invoke a method on a bean to evaluate
the predicate. Lets try that: <bean id=”transformer” class=”com.mycompany.
<when> OrderTransformProcessor”/>
<method bean=”myBean” method=”isGadget”/>
... In XML DSL, Camel will look up the processor or POJO/Bean in the registry
</when> based on the id of the bean.
<bean id=”myBean” class=”com.mycomapany.MyBean”/>
Bean
<route>
public boolean isGadget(@Header(name = “type”) String
<from uri=”direct:transformOrder”/>
type) {
<bean ref=”transformer”/>
return type.equals(“Gadget”);
</route>
}
Notice how we use Bean Parameter Binding to instruct Camel to invoke this <bean id=”tramsformer”
method and pass in the type header as the String parameter. This allows your class=”com.mycompany.OrderTransformBean”/>
code to be fully decoupled from any Camel API so its easy to read, write and
unit test. Transform
<route>
<from uri=”mina:tcp://localhost:8888?textline=true”/>
Message Translator <to uri=”jms:queue:order:in”/>
<transform>
How can systems using different data formats communicate with each other <constant>ACK</constant>
using messaging? </transform>
</route>
Translator Annotation You can also use the @Consume annotation for transformations. For
DSL example, in the method below we consume from a JMS queue and do
the transformation in regular Java code. Notice that the input and output
parameters of the method is String. Camel will automatically coerce the
payload to the expected type defined by the method. Since this is a JMS
example, the response will be sent back to the JMS reply-to destination.
Incoming Message Translated Message
@Consume(uri=”jms:queue:order:transform”)
public String transformOrder(String body) {
// do message translation
Problem Each application uses its own data format, so we need to translate the
}
message into the data format the application supports.
Solution Use a special filter, a message translator, between filters or applications to TIP: You can use Bean Parameter Binding to help Camel coerce the Message
translate one data format into another. into the method parameters. For instance, you can use @Body, @Headers
parameter annotations to bind parameters to the body and headers.
Camel Camel supports the message translator using the processor, bean or
transform nodes.
Message Filter Annotation Pay attention to the Camel route as you must invoke the bean as if it were a
DSL, regular bean instead.
How can a component avoid receiving unwanted messages? continued from(“jms:queue:order”)
.bean(new MyRouterBean());
TIP: You can use @Body, @Header, and @Headers annotations to bind
parameters to the message body and headers in the method signature of the
route method on the bean.
Camel Camel has support for the Aggregator using the aggregate method. A
correlation expression is used to determine which messages are related.
An aggregation strategy is used to combine aggregated messages into the
outgoing message. Camel’s aggregator also supports a completion predicate
allowing you to signal when the aggregation is complete.
Resequencer
Problem How do we ensure ordering of messages? Camel, TIP: See Exception Clause for selective interception of thrown exception. This
continued allows you to route certain exceptions differently or even reset the failure by
Solution Use a stateful filter, a Resequencer, to collect and reorder messages so that they marking it as handled.
can be published in a specified order.
TIP: DeadLetterChannel supports processing the message before it gets
Camel Camel has support for the Resequencer using the resequence node. Camel redelivered using onRedelivery. This allows you to alter the message
uses a stateful batch processor that is capable of reordering related messages. beforehand (i.e. to set any custom headers).
Camel supports two resequencing algorithms:
Java DSL Global scope
batch: collects messages into a batch, sorts the messages and publishes the In global scope error handlers is defined before routes and applies to any routes
messages. which has not a route specific error handler.
stream: reorders, continuously, message streams based on detection of gaps
between messages. errorHandler(deadLetterChannel(“file:error”)
Batch is similar to the aggregator but with sorting. Stream is the traditional .maximumRedeliveries(3));
Resequencer pattern with gap detection. Stream requires usage of number
(longs) as sequencer numbers, enforced by the gap detection, as it must be from(...)
able to compute if gaps exist. A gap is detected if a number in a series is
missing, (e.g. 3, 4, 6 with number 5 missing). Camel will back off the messages Route scope
until number 5 arrives. In route scope the error handler is defined inside the route and applies only to
the given route.
Java DSL Batch:
We want to process received stock quotes, once a minute, ordered by their from(“jms:queue:event”)
stock symbol. We use XPath as the expression to select the stock symbol, as the .errorHandler(deadLetterChannel(“file:error/event”)
value used for sorting. .maximumRedeliveries(5).redeliveryDelay(5000))
from(“jms:topic:stock:quote”) // and here begins the route
.resequence().xpath(“/quote/@symbol”) .to(“log:event”)
.timeout(60 * 1000) .to(“bean:handleEvent”);
.to(“seda:quotes”);
XML DSL Global scope
Camel will default the order to ascending. You can provide your own To use global scoped error handler you refer to it using the errorHandlerRef
comparison for sorting if needed. attribute on the <camelContext> tag as shown:
Stream: <camelContext errorHandlerRef=”eh”>
Suppose we continuously poll a file directory for inventory updates, and it’s <errorHandler id=”eh” type=”DeadLetterChannel”
important they are processed in sequence by their inventory id. To do this we deadLetterUri=”file:error”>
enable streaming and use one hour as the timeout. <redeliveryPolicy maximumRedeliveries=”3”/>
</errorHandler>
from(“file://inventory”) <route>
.resequence().xpath(“/inventory/@id”) ...
.stream().timeout(60 * 60 * 1000) </route>
.to(“seda:inventoryUpdates”); </camelContext>
XML DSL Batch: Route scope
<route> Route scope is likewise configured by referring to an error handler using
<from uri=”jms:topic:stock:quote”/> errorHandlerRef attribute on the <route> tag as shown:
<resequence> <route errorHandlerRef=”other-eh”>
<xpath>/quote/@symbol</xpath> ...
<batch-config batchTimeout=”60000”/> </route>
</resequence>
<to uri=”seda:quotes”/>
</route> Wire Tap
Stream: How do you inspect messages that travel on a point-to-point channel?
<route>
<from uri=”file://inventory”/>
<resequence>
<xpath>/inventory/@id</xpath>
<stream-config timeout=”3600000”/> Source Destination
</resequence>
<to uri=”seda:quotes”/>
</route>
Notice that you can enable streaming by specifying <stream-config> instead
of <batch-config>.
Camel Camel has support for Wire Tap using the wireTap node that supports two
modes: traditional and new message. The traditional mode sends a copy of the
original message, as opposed to sending a new message. All messages are sent
Sender Message as Event Message and run in parallel with the original message.
Channel Intended
Receiver Java DSL Traditional
The route uses the traditional mode to send a copy of the original message to
Reroute Delivery the seda tapped queue, while the original message is routed to its destination,
the process order bean.
from(“jms:queue:order”)
Dead Dead Letter .wireTap(“seda:tappedOrder”)
Message Channel
.to(“bean:processOrder”);
New message
Problem The messaging system cannot deliver a message In this route, we tap the high-priority orders and send a new message
containing a body with the from part of the order.
Solution When a message cannot be delivered it should be moved to a Dead Letter Channel
Camel Camel has extensive support for Dead Letter Channel by its error handler and Tip: As Camel uses an Expression for evaluation you can use other functions
exception clauses. Error handler supports redelivery policies to decide how many than xpath; for instance to send a fixed String, you can use constant.
times to try redelivering a message, before moving it to a Dead Letter Channel.
from(“jms:queue:order”)
The default Dead Letter Channel will log the message at ERROR level and .choice()
perform up to 6 redeliveries using a one second delay before each retry.
.when(“/order/priority = ‘high’”)
Error handlers have two-level scope at either global or route. .wireTap(“seda:from”, xpath(“/order/from”))
TIP: The Camel in Action book, chapter 5 is devoted to cover error handling,
.to(“bean:processHighOrder”);
which is the best source for information
.otherwise()
.to(“bean:processOrder”);
About Author The personal blog of the author of this reference card. You can follow the
http://davsclaus.blogspot.com author on twitter @davsclaus.
#82
CONTENTS INCLUDE:
■
■
About Cloud Computing
Usage Scenarios Getting Started with
Aldon Cloud#64Computing
■
Underlying Concepts
Cost
by...
■
Upcoming Refcardz
youTechnologies ®
■
Data
t toTier
brough Comply.
borate.
Platform Management and more...
■
Chan
ge. Colla By Daniel Rubio
on:
dz. com
grati s
also minimizes the need to make design changes to support
CON
ABOUT CLOUD COMPUTING one time events. TEN TS
INC
s Inte -Patternvall
■
HTML LUD E:
Basics
Automated growthHTM
ref car
RichFaces
Usef
ContiPatterns an
■
However, the demands and technology used on such servers faced by web applications.Structure Tools
Core
Key ■
Structur Elem
E: has changed substantially in recent years, especially with al Elem ents
LUD
TS INC gration the entrance of service providers like Amazon, Google and Large scale growth scenarios involvingents
specialized
and mor equipment
rdz !
HTML
Microsoft. es e... away by
(e.g. load balancers and clusters) are all but abstracted
Continu at Every to isolat
e chang
ns
oftware i-patter relying on a cloud computing platform’s technology.
space
CSS3
n
Re fca
riptio e Work
Ant These companies Desc have a Privat
are in long deployed
itory
webmana applications
ge HTM
ns and lop softw n-con
trol repos to L BAS
trol that adapt and scale
Deve
les toto large user
a versio ing and making them
bases, In addition, several cloud computing ICSplatforms support data
n Con ment Patte
rn
it all fi minim
ize merg
HTM
e... le
Manage s and mor e Work knowledgeable in amany ine to
mainl aspects related tos multip
cloud computing. tier technologies that Lexceed the precedent set by Relational
space Comm and XHT
re
Repo
This Refcard active
will introduce are within
to you to cloud riente computing, with an
ION d units etc. Some platforms ram support large grapRDBMS deployments. The src
dy Ha
softw
EGRAT e ine loping task-o it and Java s written in hical on of
all attribute
softwar emphasis onDeve es by
Mainl
OUS
INT these ines providers, so youComm can better understand
also rece JavaScri user interfac web develop and the rris
Lucene
codel chang desc
ding e code Task Level as the
w ww.dzone.com
Spring Roo
reposit -patter particu s cies Pay only what you consume
tagge or Amazon’s cloud
the curr you cho platform
computing becisome
heavily based moron very little fine. b></ ed insid
lained ) and anti x” the are solution duce
nden For each (e.g. WAR es t
ent stan ose to writ more e imp a></ e
not lega each othe
b> is
be exp text to “fi
al Depeapplication deployment
Web ge until
nden a few years
t librari agonmen
t enviro was similar that will software
industry standard and virtualization apparen ortant, the
e HTM technology.
CI can ticular con used tterns to pro can
Minim
rity
packa
all depe that all
targe dard s will L t. HTM l, but r. Tags
es i-pa tend but to most phone services: plans with late alloted resources,
simp or Reg L VS XH
ts with an
y Integ alize file
s. Ant , they and XHT lify all help XHTML, ardless <a><
in a par hes som
etim ctices,
Binar Centr temp your you prov TML b></
proces in the end bad pra enting
nt nmen
incurred cost
geme whether e a single based on
Creatsuchareresources were consumed
t enviro orthenot. Virtualization
muchallows MLaare physical othe
piece ofr hardware to be understand of HTML
approac ed with the cial, but, rily implem nden
cy Mana rties nt targe es to of the actually web cod ide a solid ing has
essa d to Depe prope into differe chang
utilized by multiple operating
function systems.simplerThis allows ing.resourcesfoundati job adm been arou
efi nec itting
associat to be ben
er
pare te builds commo
are not Fortuna
late Verifi e comm than on
ality be allocated nd for
n com Cloud computing asRun it’sremo
known etoday has changed this. expecte irably, that
befor
They they
etc.
(e.g. bandwidth, elem CPU) tohas
nmemory, exclusively totely
Temp
job has some time
Build
lts whe
ually,
appear effects. rm a
Privat y, contin nt team Every mov
entsinstances. ed to CSS
used
to be, HTML d. Earl
ed resu gration
The various resourcesPerfo consumed by webperio applications
dicall (e.g.
opme page system
individual operating bec Brow y exp . Whi
adverse unintend d Builds sitory Build r to devel common (HTM . ause ser HTM anded le it
ous Inte web dev manufacture L had very far mor has done
Stage Repo
e bandwidth, memory, CPU) areIntegtallied
ration on a per-unit CI serve basis L or XHT
produc Continu Refcard
e Build rm an ack from extensio .) All are
e.com
the not
DZone, Inc.
s on
expand
ISBN-13: 978-1-936502-03-5
140 Preston Executive Dr. ISBN-10: 1-936502-03-8
Suite 100 50795
Cary, NC 27513
DZone communities deliver over 6 million pages each month to 888.678.0399
more than 3.3 million software developers, architects and decision 919.678.0300
makers. DZone offers something for everyone, including news,
Refcardz Feedback Welcome
$7.95
tutorials, cheat sheets, blogs, feature articles, source code and more.
refcardz@dzone.com 9 781936 502035
“DZone is a developer’s dream,” says PC Magazine.
Sponsorship Opportunities
Copyright © 2011 DZone, Inc. All rights reserved. No part of this publication may be reproduced, stored in a
retrieval system, or transmitted, in any form or by means electronic, mechanical, photocopying, or otherwise, sales@dzone.com Version 1.0
without prior written permission of the publisher.