Camel in Action - Pdf 12

www.it-ebooks.info
Camel in Action

www.it-ebooks.info

www.it-ebooks.info
Camel in Action
CLAUS IBSEN
JONATHAN ANSTEY
MANNING
Greenwich
(74° w. long.)

www.it-ebooks.info
For online information and ordering of this and other Manning books, please visit
www.manning.com. The publisher offers discounts on this book when ordered in quantity.
For more information, please contact
Special Sales Department
Manning Publications Co.
180 Broad Street, Suite 1323
Stamford, CT 06901
Email: [email protected]
©2011 by Manning Publications Co. 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, without prior written
permission of the publisher.
Many of the designations used by manufacturers and sellers to distinguish their products are
claimed as trademarks. Where those designations appear in the book, and Manning
Publications was aware of a trademark claim, the designations have been printed in initial caps
or all caps.
Recognizing the importance of preserving what has been written, it is Manning’s policy to have

8 ■ Enterprise integration patterns 237
PART 3 OUT IN THE WILD 281
9 ■ Using transactions 283
10 ■ Concurrency and scalability 315
11 ■ Developing Camel projects 359
12 ■ Management and monitoring 385
13 ■ Running and deploying Camel 410
14 ■ Bean routing and remoting 443

www.it-ebooks.info

www.it-ebooks.info
ix
contents
foreword xvii
foreword xix
preface xxi
acknowledgments xxiii
about this book xxv
about the cover illustration xxix
about the authors xxxi
PART 1 FIRST STEPS. 1
1
Meeting Camel 3
1.1 Introducing Camel 4
What is Camel? 4

Why use Camel? 5

Getting

2.4 Creating routes with Spring 34
Bean injection and Spring 34

The Spring DSL 37

Using
Camel and Spring 40
2.5 Routing and EIPs 43
Using a content-based router 44

Using message filters 49
Using multicasting 50

Using recipient lists 52

Using the
wireTap method 55
2.6 Summary and best practices 57
PART 2 CORE CAMEL 59
3
Transforming data with Camel 61
3.1 Data transformation overview 62
Data transformation with Camel 62
3.2 Transforming data using EIPs and Java 63
Using the Message Translator EIP 63

Using the Content
Enricher EIP 70
3.3 Transforming XML 73
Transforming XML with XSLT 73

CONTENTS
xi
4
Using beans with Camel 93
4.1 Using beans the hard way and the easy way 94
Invoking a bean from pure Java 94

Invoking a bean defined in
Spring 95

Using beans the easy way 96
4.2 The Service Activator pattern 97
4.3 Camel’s bean registries 98
SimpleRegistry 100

JndiRegistry 101

ApplicationContext-
Registry 101

OsgiServiceRegistry 102
4.4 Selecting bean methods 103
How Camel selects bean methods 104

Camel’s method-selection
algorithm 105

Some method-selection examples 107
Potential method-selection problems 109
4.5 Bean parameter binding 111

Features of the
error handlers 128
5.3 Using error handlers with redelivery 129
An error-handling use case 129

Using redelivery 130

Error
handlers and scopes 135

Handling faults 137
5.4 Using exception policies 138
Understanding how onException catches exceptions 139

Understanding
how onException works with redelivery 142

Understanding how
onException can handle exceptions 143

Custom exception
handling 146

Ignoring exceptions 148

Implementing an error
handler solution 149
5.5 Other error-handling features 150
Using onWhen 150


Verifying that the correct message arrived 169
Using expressions with mocks 170

Testing the ordering of
messages 174

Using mocks to simulate real components 175
6.3 Simulating errors 178
Simulating errors using a processor 178

Simulating errors using
mocks 180

Simulating errors using interceptors 180
6.4 Testing without mocks 183
Integration testing 183

Using NotifyBuilder 185
6.5 Summary and best practices 187
7
Understanding components 188
7.1 Overview of Camel components 189
Manually adding components 190

Autodiscovering components 190
7.2 Working with files (File and FTP components) 192
Reading and writing files with the File component 193

Accessing remote
files with the FTP component 196

Using the Timer component 232

Enterprise scheduling with
Quartz 233
7.9 Summary and best practices 235
8
Enterprise integration patterns 237
8.1 Introducing enterprise integration patterns 238
The Aggregator and Splitter EIPs 238

The Routing Slip and
Dynamic Router EIPs 239

The Load Balancer EIP 239
8.2 The Aggregator EIP 239
Introducing the Aggregator EIP 240

Completion conditions for
the Aggregator 243

Using persistence with the Aggregator 248
Using recovery with the Aggregator 251
8.3 The Splitter EIP 255
Using the Splitter 256

Using beans for splitting 258

Splitting
big messages 260


PART 3 OUT IN THE WILD 281
9
Using transactions 283
9.1 Why use transactions? 284
The Rider Auto Parts partner integration application 284

Setting
up the JMS broker and the database 287

The story of the lost
message 288
9.2 Transaction basics 289
About Spring’s transaction support 290

Adding
transactions 291

Testing transactions 293

www.it-ebooks.info
CONTENTS
xiv
9.3 The Transactional Client EIP 296
Using local transactions 297

Using global transactions 298
9.4 Configuring and using transactions 301
Configuring transactions 301

Using transactions with multiple

Using concurrency with the Threads EIP 331

Using concurrency
with the Multicast EIP 332

Using concurrency with the Wire
Tap EIP 334
10.4 Synchronicity and threading 335
Asynchronous caller using one thread 336

Synchronous caller
using one thread 337

Asynchronous caller using multiple
threads 339

Synchronous caller using multiple threads 340
Returning an early reply to a caller 342
10.5 The concurrency client API 344
The concurrency client API in Java 344

The concurrency client
API in Camel 347
10.6 The asynchronous routing engine 350
Hitting the scalability limit 350

Scalability in Camel 352
Components supporting asynchronous processing 353
Asynchronous API 354



Adding Scala routes to the CamelContext 382
Mixing Java and Scala 382
11.5 Summary and best practices 384
12
Management and monitoring 385
12.1 Monitoring Camel 386
Checking health at the network level 386

Checking health at the
JVM level 388

Checking health at the application level 388
12.2 Using JMX with Camel 389
Using JConsole to manage Camel 390

Using JConsole to remotely
manage Camel 391
12.3 Tracking application activity 393
Using log files 393

Using core logs 394

Using custom
logging 394

Using Tracer 398

Using notifications 402
12.4 Managing Camel applications 405


Embedded in JBoss Application Server 436
13.5 Camel and OSGi 437
Setting up Maven to generate an OSGi bundle 438

Installing
and running Apache Karaf 439

Deploying the example 440
13.6 Summary and best practices 441
14
Bean routing and remoting 443
14.1 Using beans for routing 444
Inventory update at Rider Auto Parts 444

Receiving messages
with @Consume 445

Sending messages with @Produce 448
When to use beans for routing 450
14.2 Hiding middleware 451
Introducing the starter kit 453

Using Spring remoting and
Camel proxies 456
14.3 Summary and best practices 460
appendix A Simple, the expression language 461
appendix B Expressions and predicates 471
appendix C The producer and consumer templates 477
appendix D The Camel community 483

www.it-ebooks.info
FOREWORD
xviii
source messaging and enterprise service bus (ESB) products. These tools embrace the
EIP vocabulary by implementing many patterns directly in the platform. With Apache
Camel, a Splitter pattern translates directly into a “split” element in the Camel DSL.
We couldn’t have wished for a more direct translation of the pattern language into an
implementation platform.
Claus and Jon bring the saga to a grand finale by showing us how to use the Camel
pattern language to compose real-life messaging solutions. In doing so, they not only
cover fundamental concepts like routing and transformation, but also dig into often-
neglected parts of the development process, including testing, monitoring, and deploy-
ing. They find the right balance of the pattern language, Camel core concepts, and run-
ning code to help you build easy-to-understand and robust messaging solutions.
G
REGOR HOHPE
COAUTHOR OF ENTERPRISE INTEGRATION PATTERNS
WWW.EAIPATTERNS.COM

www.it-ebooks.info
xix
foreword
I was one of the original founders of both Apache ActiveMQ (an open source high-
performance message broker) and ServiceMix (an open source ESB based on JBI and
OSGi). I found that Enterprise Integration Patterns were becoming increasingly cen-
tral to what we were doing on these projects and how we were using them; the only dif-
ference was the context and technologies with which we were using the patterns.
There have been many libraries and frameworks over the years to help with inte-
gration. But frequently the concepts behind the Enterprise Integration Patterns get
transformed into some complex class hierarchies or objects that need to be wired

DSL flavor they wished
(whether Java, XML, Groovy, Ruby, Scala, or whatever) and yet, at runtime, still be able
to introspect the framework and understand all of the EIPs that were being used. They
would be able to visualize the core patterns to the team at any point in the project life-
cycle, auto-document the patterns, or even support things like graphical editing of the
Enterprise Integration Patterns at design time or runtime.
So Apache Camel was born, and since then we’ve seen the codebase, community,
and number of components, technologies, and data formats grow massively as more
and more developers have found Apache Camel an ideal way to design, implement,
and maintain the Enterprise Integration Patterns.
In this book Claus and Jon describe the Enterprise Integration Patterns and the
concepts which underlie Apache Camel. Then they walk you through how to take the
concepts and apply them to many real-life scenarios to provide scalable and efficient
solutions that are easy to understand and quick to adapt to your integration needs. I
hope you’ll enjoy reading this book as much as I did!
J
AMES STRACHAN
CO-FOUNDER OF APACHE ACTIVEMQ
CAMEL, AND SERVICEMIX
TECHNICAL DIRECTOR FUSESOURCE.COM
HTTP://MACSTRAC.BLOGSPOT.COM

www.it-ebooks.info
xxi
preface
Developers who have done integration work know what a difficult task it can be. IT sys-
tems may not have been designed to be accessible from other systems, and if they were
designed for interoperability, they may not speak the protocol you need. As a devel-
oper, you end up spending a considerable amount of time working with the plumbing
of the integration protocols to open up the IT systems to the outside world.

the professional company that offers enterprise services around various Apache proj-
ects. This book is written by the people who wrote the Camel code, which ensures you
have the most updated Camel book on the market.
Writing this book has been a very intense journey, proven by the fact that we were
able to complete the manuscript in a year. It took a long time to implement the exam-
ples and to ensure that the accompanying source code is of the highest standard. But
the result is a great source of examples that should inspire you to get the best out of
Camel, and it should be a good starting point for your Camel projects. While we were
writing this book, we were also implementing new features in Camel, which often
meant we had to go back and revise the material along the way. But we have kept up,
and this book uses the latest Camel release at the time of writing (Camel 2.5).
We hope this book brings great value to you and helps you prosper in the Camel
community.
CLAUS IBSEN

www.it-ebooks.info
xxiii
acknowledgments
We first want to thank Cynthia Kane, our development editor at Manning, who put up
with our many missed deadlines and gave great feedback during the writing process.
We’d also like to thank our awesome copy editor, Andy Carroll, for catching an amaz-
ing number of grammatical errors in the early revisions of the book. The greater Man-
ning team deserves kudos as well; they’ve made for a very pleasant writing experience
over the past year and a half.
Big thanks to our team of reviewers, who provided invaluable feedback during var-
ious stages of the book’s development: Bruce Snyder, Charles Moulliard, Christophe
Avare, Christopher Hunt, Domingo Suarez Torres, Doug Tillman, Fintan Bolton, Gor-
don Dickens, Gregor Hohpe, Jeroen Benckhuijsen, John S. Griffon, Kevin Jackson,
Marco Ughetti, Martin Gilday, Martin Krasser, Michael Nash, Mick Knutson, Roman
Kalukiewicz, Tijs Rademakers, and Willem Jiang.

A warm thank you goes to our dog, Bambi , wh o pa tie ntl y sl eep s in my office, and occa-
sionally wakes up and politely “asks” me for a break and a walk. I must admit many of
the ideas and thoughts behind this book came to me during my walks with Bambi.
JON
I would like to thank my amazing wife, Lisa, for the patience, support, and encourage-
ment I needed throughout the writing of this book. It simply would not have hap-
pened if it wasn’t for you. To Georgia, my beautiful daughter: thank you for cheering
me up when the writing got the better of me. I love you both!

www.it-ebooks.info


Nhờ tải bản gốc

Tài liệu, ebook tham khảo khác

Music ♫

Copyright: Tài liệu đại học © DMCA.com Protection Status