Professional Documents
Culture Documents
Tom Donohue
Table of Contents
Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Introduction to this book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
Getting ready . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Section I: The basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Introducing Apache Camel . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Getting on the Camel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
What does a Route actually look like? . . . . . . . . . . . . . . . . 12
Messages in Camel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
Your first Camel project . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
Preface
Hey, hello, hi! Thanks very much for downloading this book.
I really liked Camel for its simplicity, and that it’s pure Java
under the hood. Easy to work with and maintain.
So, I started to write some articles on Camel (the ones you now
see on cleverbuilder.com) to share common tasks that you
might want to do in Camel, or explain some core concepts.
I hope you enjoy the book, that you enjoy learning Camel —
and that you get truly hooked, like I did.
Tom
1
Introduction to this book
So you want to get started with Apache Camel the easy way?
Maybe you’ve tried other tutorials, but they all seem too
complicated. Or half-finished. Or unreadable. Or just a bunch
of code pasted onto a web page.
2
Getting ready
To get the most out of this book, it’s best to follow the tutorials,
step-by-step (well, the book is called Camel Step-by-Step…).
Once you’ve installed those, turn over to the next page and
let’s get started!
3
Section I: The basics
Introducing Apache Camel
You could think of Camel like a plumbing toolkit for Java. Just
like real plumbing pipes, Camel takes data from one point, and
pipes it to another. Along the way, the data can be changed,
transformed, or sent through other pipes.
4
Included in your plumbing toolkit is a range of adaptors
(components in Camel-speak) that fit all sorts of different apps
and software systems.
5
Camel facts
Here are some fun facts, and reasons to get to know the
Camel!
6
What is Apache Camel used for?
Almost any time you need to ship data from A to B, you can
probably use Apache Camel.
I’ve listed some typical data shipping scenarios below. You can
implement all of these with Camel:
So let’s look inside the Camel box and see how it actually
works.
7
Getting on the Camel
How do you think like a Camel? Let’s look at the main concepts.
Route
8
// This is a complete Camel route definition!
from("file:home/customers/new")
.to("file:home/customers/old");
Endpoints
Components
9
components, and how you might reference them in an
endpoint:
As you can see, each component can usually read and write:
10
When you want to perform some common activities on a
message, such as transformation, splitting and logging, you’ll
use an EIP. Here are some common EIPs in Camel:
Camel Context
11
What does a Route actually look
like?
So. Now you know that when you develop in Camel, you create
routes that move data between endpoints, using components.
A really simple route in the Java DSL could look something like
this:
from("file:home/customers/new")
.to("file:home/customers/old");
12
<route>
<from uri="file:home/customers/new"/>
<to uri="file:home/customers/old"/>
</route>
from("file:home/customers/new")
.log("Received a new customer!")
.to("file:home/customers/old");
<route>
<from uri="file:home/customers/new"/>
<log message="Received a new customer!"/>
<to uri="file:home/customers/old"/>
</route>
13
Messages in Camel
Camel works with data using a message model.
The real power of Camel becomes clear when you start using
different types of objects. Camel has a lot of built-in support for
14
converting between different object types.
In fact, for many common file types, you might barely even
have to write any code. (Less time writing boilerplate code?
Sounds good, doesn’t it?)
You’ve learned the basics - we’ll pick the rest up on the way.
Let’s now put things into practice and walk through creating
your first Camel project. Head to the next page to get
started!
15
Your first Camel project
In this part of the book, I’m going to show you how to create a
new Apache Camel project in Eclipse.
But it’s one of the most common IDEs in use today, used
by millions of developers just like you. Eclipse makes it
very easy to create a project and run it.
16
Note: you’ll need to be online to complete this tutorial.
17
4. The next step asks you to select a Maven archetype for your
project, from a list of available archetypes. (You might need
to wait a while for the full list to load.)
18
6. Now your Camel project is created in Eclipse! In a moment,
we’ll run the app and see what it does.
project/
└ src/
├ main/
│ ├ java/
│ │ └ com/
│ │ └ example/
│ │ ├ MainApp.java
│ │ └ MyRouteBuilder.java
│ └ resources/
│ └ log4j2.properties
└ test/
├ java/
└ resources/
19
package com.example;
import org.apache.camel.builder.RouteBuilder;
If you check out the code, you’ll see that the MyRouteBuilder
class in the Eclipse project comes with a route defined inside it
already.
20
Which DSL should you use?
The demo project defines a route using the code below. The
code is given in special Camel syntax, or DSL (Domain-specific
Language):
21
public void configure() {
from("file:src/data?noop=true")
.choice()
.when(xpath("/person/city = 'London'"))
.log("UK message")
.to("file:target/messages/uk")
.otherwise()
.log("Other message")
.to("file:target/messages/others");
}
This code looks complicated, but it really just moves files from
one folder to another.
• The from() EIP starts the route. The part in brackets defines
the URI of the endpoint. In this case, the endpoint is a File
component (which has a URI prefix of file:). The part after
that states which folder to consume from (src/data).
22
• The otherwise() keyword defines what should happen if
the previous test returns false. If it’s false, it writes a log
message, "Other message", and moves the file to the
target/messages/others folder.
You’ll see that, although it’s been formatted for easier reading,
the entire route is a single Java statement – it starts with the
method call from(), and ends with a to() method call and the
statement terminator, ;.
23
If you’re prompted by a Select Java
Application dialog box, tell Eclipse to run
the app as a Camel route by selecting Main
– org.apache.camel from the list.
3. Camel will launch, process the files, and you’ll see the
output from the route in the Console window.
24
Camel found your route definitions (in MyRouteBuilder) and
started a CamelContext, containing your single route.
25
This is a very basic demo of Camel, but you can start to see
what it’s capable of. Using components and EIPs you can build
almost any kind of data flow that you can think of.
Now let’s move on and see what other cool stuff we can build
with Camel!
26
End of Preview
If you’ve liked this sample, please tell your friends they can get
their own copy at https://cleverbuilder.com.
27