Red Hat JBoss Fuse 6.1 Apache Camel Development Guide En US
User Manual: Pdf
Open the PDF directly: View PDF
.
Page Count: 554
| Download | |
| Open PDF In Browser | View PDF |
Red Hat JBoss Fuse 6.1
Apache Camel Development Guide
Develop applications with Apache Camel
Last Updated: 2017-10-12
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
Develop applications with Apache Camel
JBoss A-MQ Docs Team
Content Services
fuse-docs-support@redhat.com
Legal Notice
Copyright © 2013 Red Hat.
The text of and illustrations in this document are licensed by Red Hat under a Creative
Commons Attribution–Share Alike 3.0 Unported license ("CC-BY-SA"). An explanation of
CC-BY-SA is available at
http://creativecommons.org/licenses/by-sa/3.0/
. In accordance with CC-BY-SA, if you distribute this document or an adaptation of it,
you must provide the URL for the original version.
Red Hat, as the licensor of this document, waives the right to enforce, and agrees not to
assert, Section 4d of CC-BY-SA to the fullest extent permitted by applicable law.
Red Hat, Red Hat Enterprise Linux, the Shadowman logo, JBoss, OpenShift, Fedora, the
Infinity logo, and RHCE are trademarks of Red Hat, Inc., registered in the United States
and other countries.
Linux ® is the registered trademark of Linus Torvalds in the United States and other
countries.
Java ® is a registered trademark of Oracle and/or its affiliates.
XFS ® is a trademark of Silicon Graphics International Corp. or its subsidiaries in the
United States and/or other countries.
MySQL ® is a registered trademark of MySQL AB in the United States, the European
Union and other countries.
Node.js ® is an official trademark of Joyent. Red Hat Software Collections is not formally
related to or endorsed by the official Joyent Node.js open source or commercial project.
The OpenStack ® Word Mark and OpenStack logo are either registered
trademarks/service marks or trademarks/service marks of the OpenStack Foundation, in
the United States and other countries and are used with the OpenStack Foundation's
permission. We are not affiliated with, endorsed or sponsored by the OpenStack
Foundation, or the OpenStack community.
All other trademarks are the property of their respective owners.
Abstract
Guide to developing routes with Apache Camel.
Table of Contents
Table of Contents
. . . . .I.. .IMPLEMENTING
PART
. . . . . . . . . . . .ENTERPRISE
. . . . . . . . . .INTEGRATION
. . . . . . . . . . .PATTERNS
..............................9
.........
.CHAPTER
. . . . . . . 1.
. . BUILDING
. . . . . . . . BLOCKS
. . . . . . .FOR
. . . ROUTE
. . . . . .DEFINITIONS
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
..........
1.1. IMPLEMENTING A ROUTEBUILDER CLASS
10
1.2. BASIC JAVA DSL SYNTAX
11
1.3. ROUTER SCHEMA IN A SPRING XML FILE
14
1.4. ENDPOINTS
16
1.5. PROCESSORS
20
.CHAPTER
. . . . . . . 2.
. . BASIC
. . . . . PRINCIPLES
. . . . . . . . . .OF
. . ROUTE
. . . . . .BUILDING
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
..........
2.1. PIPELINE PROCESSING
30
2.2. MULTIPLE INPUTS
33
2.3. EXCEPTION HANDLING
36
2.4. BEAN INTEGRATION
52
2.5. CREATING EXCHANGE INSTANCES
62
2.6. TRANSFORMING MESSAGE CONTENT
63
2.7. PROPERTY PLACEHOLDERS
74
2.8. ASPECT ORIENTED PROGRAMMING
84
2.9. THREADING MODEL
2.10. CONTROLLING START-UP AND SHUTDOWN OF ROUTES
2.11. SCHEDULED ROUTE POLICY
85
93
98
2.12. JMX NAMING
2.13. PERFORMANCE AND OPTIMIZATION
106
108
. . . . . . . . 3.
CHAPTER
. . INTRODUCING
. . . . . . . . . . . ENTERPRISE
. . . . . . . . . . INTEGRATION
. . . . . . . . . . . .PATTERNS
. . . . . . . . . . . . . . . . . . . . . . . . . 110
...........
3.1. OVERVIEW OF THE PATTERNS
110
.CHAPTER
. . . . . . . 4.
. . MESSAGING
. . . . . . . . . .SYSTEMS
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
...........
4.1. MESSAGE
117
4.2. MESSAGE CHANNEL
4.3. MESSAGE ENDPOINT
118
120
4.4. PIPES AND FILTERS
4.5. MESSAGE ROUTER
121
123
4.6. MESSAGE TRANSLATOR
125
. . . . . . . . 5.
CHAPTER
. . MESSAGING
. . . . . . . . . .CHANNELS
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
...........
5.1.
5.2.
5.3.
5.4.
5.5.
POINT-TO-POINT CHANNEL
PUBLISH-SUBSCRIBE CHANNEL
DEAD LETTER CHANNEL
GUARANTEED DELIVERY
MESSAGE BUS
127
128
130
139
141
.CHAPTER
. . . . . . . 6.
. . MESSAGE
. . . . . . . .CONSTRUCTION
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143
...........
6.1. CORRELATION IDENTIFIER
143
6.2. EVENT MESSAGE
143
6.3. RETURN ADDRESS
145
.CHAPTER
. . . . . . . 7.
. . MESSAGE
. . . . . . . .ROUTING
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
...........
7.1. CONTENT-BASED ROUTER
147
7.2.
7.3.
7.4.
7.5.
MESSAGE FILTER
RECIPIENT LIST
SPLITTER
AGGREGATOR
148
150
159
168
1
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
7.6. RESEQUENCER
7.7. ROUTING SLIP
7.8. THROTTLER
187
191
193
7.9. DELAYER
7.10. LOAD BALANCER
7.11. MULTICAST
7.12. COMPOSED MESSAGE PROCESSOR
196
198
207
214
7.13.
7.14.
7.15.
7.16.
216
219
221
223
SCATTER-GATHER
LOOP
SAMPLING
DYNAMIC ROUTER
. . . . . . . . 8.
CHAPTER
. . MESSAGE
. . . . . . . .TRANSFORMATION
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227
...........
8.1.
8.2.
8.3.
8.4.
8.5.
CONTENT ENRICHER
CONTENT FILTER
NORMALIZER
CLAIM CHECK
SORT
8.6. VALIDATE
227
232
233
234
236
238
.CHAPTER
. . . . . . . 9.
. . MESSAGING
. . . . . . . . . .ENDPOINTS
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 240
...........
9.1. MESSAGING MAPPER
240
9.2. EVENT DRIVEN CONSUMER
241
9.3. POLLING CONSUMER
9.4. COMPETING CONSUMERS
242
242
9.5. MESSAGE DISPATCHER
244
9.6. SELECTIVE CONSUMER
9.7. DURABLE SUBSCRIBER
246
248
9.8. IDEMPOTENT CONSUMER
9.9. TRANSACTIONAL CLIENT
251
257
9.10. MESSAGING GATEWAY
257
9.11. SERVICE ACTIVATOR
258
.CHAPTER
. . . . . . . 10.
. . .SYSTEM
. . . . . . .MANAGEMENT
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261
...........
10.1. DETOUR
261
10.2. LOGEIP
10.3. WIRE TAP
262
263
. . . . . . . . .A.
APPENDIX
. .MIGRATING
. . . . . . . . . FROM
. . . . . SERVICEMIX
. . . . . . . . . . EIP
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269
...........
A.1. MIGRATING ENDPOINTS
269
A.2. COMMON ELEMENTS
A.3. SERVICEMIX EIP PATTERNS
271
272
A.4. CONTENT-BASED ROUTER
273
A.5. CONTENT ENRICHER
A.6. MESSAGE FILTER
275
277
A.7. PIPELINE
A.8. RESEQUENCER
278
279
A.9. STATIC RECIPIENT LIST
281
A.10. STATIC ROUTING SLIP
A.11. WIRE TAP
282
283
A.12. XPATH SPLITTER
285
. . . . .II.
PART
. . ROUTING
. . . . . . . .EXPRESSION
. . . . . . . . . .AND
. . . .PREDICATE
. . . . . . . . .LANGUAGES
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287
...........
. . . . . . . . 11.
CHAPTER
. . .INTRODUCTION
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 288
...........
2
Table of Contents
11.1. OVERVIEW OF THE LANGUAGES
11.2. HOW TO INVOKE AN EXPRESSION LANGUAGE
288
289
. . . . . . . . 12.
CHAPTER
. . .CONSTANT
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 294
...........
OVERVIEW
XML EXAMPLE
294
294
JAVA EXAMPLE
294
. . . . . . . . 13.
CHAPTER
. . .EL
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295
...........
OVERVIEW
ADDING JUEL PACKAGE
295
295
STATIC IMPORT
295
VARIABLES
EXAMPLE
295
296
. . . . . . . . 14.
CHAPTER
. . .THE
. . . .FILE
. . . LANGUAGE
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297
...........
14.1. WHEN TO USE THE FILE LANGUAGE
14.2. FILE VARIABLES
297
298
14.3. EXAMPLES
300
. . . . . . . . 15.
CHAPTER
. . .GROOVY
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 303
...........
OVERVIEW
ADDING THE SCRIPT MODULE
303
303
STATIC IMPORT
303
BUILT-IN ATTRIBUTES
EXAMPLE
303
304
USING THE PROPERTIES COMPONENT
304
.CHAPTER
. . . . . . . 16.
. . .HEADER
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 306
...........
OVERVIEW
306
XML EXAMPLE
306
JAVA EXAMPLE
306
.CHAPTER
. . . . . . . 17.
. . .JAVASCRIPT
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 307
...........
OVERVIEW
307
ADDING THE SCRIPT MODULE
STATIC IMPORT
307
307
BUILT-IN ATTRIBUTES
307
EXAMPLE
308
USING THE PROPERTIES COMPONENT
308
.CHAPTER
. . . . . . . 18.
. . .JOSQL
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 310
...........
OVERVIEW
310
ADDING THE JOSQL MODULE
310
STATIC IMPORT
310
VARIABLES
310
EXAMPLE
311
. . . . . . . . 19.
CHAPTER
. . .JXPATH
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 312
...........
OVERVIEW
ADDING JXPATH PACKAGE
312
312
VARIABLES
312
EXAMPLE
313
. . . . . . . . 20.
CHAPTER
. . .MVEL
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 314
...........
OVERVIEW
314
3
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
SYNTAX
ADDING THE MVEL MODULE
314
314
BUILT-IN VARIABLES
314
EXAMPLE
315
. . . . . . . . 21.
CHAPTER
. . .THE
. . . .OBJECT-GRAPH
. . . . . . . . . . . .NAVIGATION
. . . . . . . . . .LANGUAGE(OGNL)
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 316
...........
OVERVIEW
316
ADDING THE OGNL MODULE
STATIC IMPORT
316
316
BUILT-IN VARIABLES
316
EXAMPLE
317
. . . . . . . . 22.
CHAPTER
. . .PHP
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 318
...........
OVERVIEW
318
ADDING THE SCRIPT MODULE
318
STATIC IMPORT
BUILT-IN ATTRIBUTES
318
318
EXAMPLE
319
USING THE PROPERTIES COMPONENT
319
. . . . . . . . 23.
CHAPTER
. . .PROPERTY
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 320
...........
OVERVIEW
320
XML EXAMPLE
JAVA EXAMPLE
320
320
. . . . . . . . 24.
CHAPTER
. . .PYTHON
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 321
...........
OVERVIEW
321
ADDING THE SCRIPT MODULE
321
STATIC IMPORT
321
BUILT-IN ATTRIBUTES
EXAMPLE
321
322
USING THE PROPERTIES COMPONENT
322
. . . . . . . . 25.
CHAPTER
. . .REF
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 323
...........
OVERVIEW
323
STATIC IMPORT
323
XML EXAMPLE
JAVA EXAMPLE
323
323
. . . . . . . . 26.
CHAPTER
. . .RUBY
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 324
...........
OVERVIEW
324
ADDING THE SCRIPT MODULE
324
STATIC IMPORT
324
BUILT-IN ATTRIBUTES
EXAMPLE
324
325
USING THE PROPERTIES COMPONENT
325
. . . . . . . . 27.
CHAPTER
. . .THE
. . . .SIMPLE
. . . . . .LANGUAGE
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 326
...........
4
27.1. JAVA DSL
326
27.2. XML DSL
327
27.3. INVOKING AN EXTERNAL SCRIPT
328
27.4. EXPRESSIONS
27.5. PREDICATES
329
331
27.6. VARIABLE REFERENCE
333
27.7. OPERATOR REFERENCE
337
Table of Contents
. . . . . . . . 28.
CHAPTER
. . .SPEL
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 340
...........
OVERVIEW
340
SYNTAX
340
ADDING SPEL PACKAGE
340
VARIABLES
XML EXAMPLE
340
341
JAVA EXAMPLE
341
. . . . . . . . 29.
CHAPTER
. . .THE
. . . .XPATH
. . . . . LANGUAGE
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 343
...........
29.1. JAVA DSL
343
29.2. XML DSL
344
29.3. XPATH INJECTION
29.4. XPATH BUILDER
346
347
29.5. ENABLING SAXON
348
29.6. EXPRESSIONS
350
29.7. PREDICATES
353
29.8. USING VARIABLES AND FUNCTIONS
354
29.9. VARIABLE NAMESPACES
29.10. FUNCTION REFERENCE
355
356
. . . . . . . . 30.
CHAPTER
. . .XQUERY
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 358
...........
OVERVIEW
358
JAVA SYNTAX
358
ADDING THE SAXON MODULE
358
STATIC IMPORT
VARIABLES
358
358
EXAMPLE
359
. . . . .III.
PART
. . .WEB
. . . .SERVICES
. . . . . . . .AND
. . . ROUTING
. . . . . . . .WITH
. . . . .CAMEL
. . . . . CXF
. . . . . . . . . . . . . . . . . . . . . . . . . . . 360
...........
. . . . . . . . 31.
CHAPTER
. . .DEMONSTRATION
. . . . . . . . . . . . . . CODE
. . . . .FOR
. . . .CAMEL/CXF
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 361
...........
31.1. DOWNLOADING AND INSTALLING THE DEMONSTRATIONS
361
31.2. RUNNING THE DEMONSTRATIONS
361
. . . . . . . . 32.
CHAPTER
. . .JAVA-FIRST
. . . . . . . . . SERVICE
. . . . . . . IMPLEMENTATION
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 365
...........
32.1. JAVA-FIRST OVERVIEW
32.2. DEFINE SEI AND RELATED CLASSES
365
366
32.3. ANNOTATE SEI FOR JAX-WS
369
32.4. INSTANTIATE THE WS ENDPOINT
372
32.5. JAVA-TO-WSDL MAVEN PLUG-IN
374
. . . . . . . . 33.
CHAPTER
. . .WSDL-FIRST
. . . . . . . . . . SERVICE
. . . . . . .IMPLEMENTATION
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 377
...........
33.1. WSDL-FIRST OVERVIEW
33.2. CUSTOMERSERVICE WSDL CONTRACT
377
378
33.3. WSDL-TO-JAVA MAVEN PLUG-IN
381
33.4. INSTANTIATE THE WS ENDPOINT
383
33.5. DEPLOY TO AN OSGI CONTAINER
384
. . . . . . . . 34.
CHAPTER
. . .IMPLEMENTING
............A
. .WS
. . .CLIENT
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 388
...........
34.1. WS CLIENT OVERVIEW
388
34.2. WSDL-TO-JAVA MAVEN PLUG-IN
34.3. INSTANTIATE THE WS CLIENT PROXY
389
391
34.4. INVOKE WS OPERATIONS
393
34.5. DEPLOY TO AN OSGI CONTAINER
393
5
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
. . . . . . . . 35.
CHAPTER
. . .POJO-BASED
. . . . . . . . . . ROUTE
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 397
...........
35.1. PROCESSING MESSAGES IN POJO FORMAT
397
35.2. WSDL-TO-JAVA MAVEN PLUG-IN
398
35.3. INSTANTIATE THE WS ENDPOINT
35.4. SORT MESSAGES BY OPERATION NAME
400
403
35.5. PROCESS OPERATION PARAMETERS
404
35.6. DEPLOY TO OSGI
406
. . . . . . . . 36.
CHAPTER
. . .PAYLOAD-BASED
. . . . . . . . . . . . . .ROUTE
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 409
...........
36.1. PROCESSING MESSAGES IN PAYLOAD FORMAT
409
36.2. INSTANTIATE THE WS ENDPOINT
36.3. SORT MESSAGES BY OPERATION NAME
410
412
36.4. SOAP/HTTP-TO-JMS BRIDGE USE CASE
413
36.5. GENERATING RESPONSES USING TEMPLATES
416
36.6. TYPECONVERTER FOR CXFPAYLOAD
419
36.7. DEPLOY TO OSGI
420
. . . . . . . . 37.
CHAPTER
. . .PROVIDER-BASED
. . . . . . . . . . . . . . ROUTE
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 423
...........
37.1. PROVIDER-BASED JAX-WS ENDPOINT
37.2. CREATE A PROVIDER> IMPLEMENTATION CLASS
423
425
37.3. INSTANTIATE THE WS ENDPOINT
37.4. SORT MESSAGES BY OPERATION NAME
425
427
37.5. SOAP/HTTP-TO-JMS BRIDGE USE CASE
37.6. GENERATING RESPONSES USING TEMPLATES
427
430
37.7. TYPECONVERTER FOR SAXSOURCE
37.8. DEPLOY TO OSGI
433
433
.CHAPTER
. . . . . . . 38.
. . .PROXYING
. . . . . . . . .A. WEB
. . . . SERVICE
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 436
...........
38.1. PROXYING WITH HTTP
436
38.2. PROXYING WITH POJO FORMAT
38.3. PROXYING WITH PAYLOAD FORMAT
438
440
38.4. HANDLING HTTP HEADERS
441
.CHAPTER
. . . . . . . 39.
. . .FILTERING
. . . . . . . . .SOAP
. . . . MESSAGE
. . . . . . . . HEADERS
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 444
...........
39.1. BASIC CONFIGURATION
444
39.2. HEADER FILTERING
39.3. IMPLEMENTING A CUSTOM FILTER
39.4. INSTALLING FILTERS
446
447
450
. . . . .IV.
PART
. . .PROGRAMMING
. . . . . . . . . . . . EIP
. . .COMPONENTS
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 452
...........
. . . . . . . . 40.
CHAPTER
. . .UNDERSTANDING
. . . . . . . . . . . . . .MESSAGE
. . . . . . . .FORMATS
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 453
...........
40.1. EXCHANGES
40.2. MESSAGES
453
454
40.3. BUILT-IN TYPE CONVERTERS
40.4. BUILT-IN UUID GENERATORS
458
460
.CHAPTER
. . . . . . . 41.
. . .IMPLEMENTING
............A
. .PROCESSOR
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 463
...........
41.1. PROCESSING MODEL
463
41.2. IMPLEMENTING A SIMPLE PROCESSOR
41.3. ACCESSING MESSAGE CONTENT
463
464
41.4. THE EXCHANGEHELPER CLASS
466
.CHAPTER
. . . . . . . 42.
. . .TYPE
. . . . CONVERTERS
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 468
...........
42.1. TYPE CONVERTER ARCHITECTURE
468
6
Table of Contents
42.2. IMPLEMENTING TYPE CONVERTER USING ANNOTATIONS
42.3. IMPLEMENTING A TYPE CONVERTER DIRECTLY
470
473
. . . . . . . . 43.
CHAPTER
. . .PRODUCER
. . . . . . . . .AND
. . . .CONSUMER
. . . . . . . . . TEMPLATES
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 475
...........
43.1. USING THE PRODUCER TEMPLATE
43.2. USING THE CONSUMER TEMPLATE
475
489
. . . . . . . . 44.
CHAPTER
. . .IMPLEMENTING
............A
. .COMPONENT
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 492
...........
44.1. COMPONENT ARCHITECTURE
44.2. HOW TO IMPLEMENT A COMPONENT
44.3. AUTO-DISCOVERY AND CONFIGURATION
492
500
501
. . . . . . . . 45.
CHAPTER
. . .COMPONENT
. . . . . . . . . . INTERFACE
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 505
...........
45.1. THE COMPONENT INTERFACE
45.2. IMPLEMENTING THE COMPONENT INTERFACE
505
506
. . . . . . . . 46.
CHAPTER
. . .ENDPOINT
. . . . . . . . .INTERFACE
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 511
...........
46.1. THE ENDPOINT INTERFACE
46.2. IMPLEMENTING THE ENDPOINT INTERFACE
511
514
.CHAPTER
. . . . . . . 47.
. . .CONSUMER
. . . . . . . . . INTERFACE
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 521
...........
47.1. THE CONSUMER INTERFACE
521
47.2. IMPLEMENTING THE CONSUMER INTERFACE
525
.CHAPTER
. . . . . . . 48.
. . .PRODUCER
. . . . . . . . .INTERFACE
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 533
...........
48.1. THE PRODUCER INTERFACE
533
48.2. IMPLEMENTING THE PRODUCER INTERFACE
535
.CHAPTER
. . . . . . . 49.
. . .EXCHANGE
. . . . . . . . .INTERFACE
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 538
...........
49.1. THE EXCHANGE INTERFACE
538
. . . . . . . . 50.
CHAPTER
. . .MESSAGE
. . . . . . . .INTERFACE
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 542
...........
50.1. THE MESSAGE INTERFACE
50.2. IMPLEMENTING THE MESSAGE INTERFACE
542
544
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 545
INDEX
...........
7
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
8
PART I. IMPLEMENTING ENTERPRISE INTEGRATION PATTERNS
PART I. IMPLEMENTING ENTERPRISE INTEGRATION
PATTERNS
Abstract
This part describes how to build routes using Apache Camel. It covers the basic building
blocks and EIP components.
9
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
CHAPTER 1. BUILDING BLOCKS FOR ROUTE
DEFINITIONS
Abstract
Apache Camel supports two alternative Domain Specific Languages (DSL) for defining
routes: a Java DSL and a Spring XML DSL. The basic building blocks for defining routes are
endpoints and processors, where the behavior of a processor is typically modified by
expressions or logical predicates. Apache Camel enables you to define expressions and
predicates using a variety of different languages.
1.1. IMPLEMENTING A ROUTEBUILDER CLASS
Overview
To use the Domain Specific Language (DSL), you extend the RouteBuilder class and
override its configure() method (where you define your routing rules).
You can define as many RouteBuilder classes as necessary. Each class is instantiated once
and is registered with the CamelContext object. Normally, the lifecycle of each
RouteBuilder object is managed automatically by the container in which you deploy the
router.
RouteBuilder classes
As a router developer, your core task is to implement one or more RouteBuilder classes.
There are two alternative RouteBuilder classes that you can inherit from:
org.apache.camel.builder.RouteBuilder—this is the generic RouteBuilder base
class that is suitable for deploying into any container type. It is provided in the
camel-core artifact.
org.apache.camel.spring.SpringRouteBuilder—this base class is specially
adapted to the Spring container. In particular, it provides extra support for the
following Spring specific features: looking up beans in the Spring registry (using the
beanRef() Java DSL command) and transactions (see the Transactions Guide for
details). It is provided in the camel-spring artifact.
The RouteBuilder class defines methods used to initiate your routing rules (for example,
from(), intercept(), and exception()).
Implementing a RouteBuilder
Example 1.1, “Implementation of a RouteBuilder Class” shows a minimal RouteBuilder
implementation. The configure() method body contains a routing rule; each rule is a
single Java statement.
Example 1.1. Implementation of a RouteBuilder Class
import org.apache.camel.builder.RouteBuilder;
public class MyRouteBuilder extends RouteBuilder {
10
CHAPTER 1. BUILDING BLOCKS FOR ROUTE DEFINITIONS
public void configure() {
// Define routing rules here:
from("file:src/data?noop=true").to("file:target/messages");
// More rules can be included, in you like.
// ...
}
}
The form of the rule from(URL1).to(URL2) instructs the router to read files from the
directory src/data and send them to the directorytarget/messages. The option ?
noop=true instructs the router to retain (not delete) the source files in thesrc/data
directory.
1.2. BASIC JAVA DSL SYNTAX
What is a DSL?
A Domain Specific Language (DSL) is a mini-language designed for a special purpose. A DSL
does not have to be logically complete but needs enough expressive power to describe
problems adequately in the chosen domain. Typically, a DSL does not require a dedicated
parser, interpreter, or compiler. A DSL can piggyback on top of an existing object-oriented
host language, provided DSL constructs map cleanly to constructs in the host language API.
Consider the following sequence of commands in a hypothetical DSL:
command01;
command02;
command03;
You can map these commands to Java method invocations, as follows:
command01().command02().command03()
You can even map blocks to Java method invocations. For example:
command01().startBlock().command02().command03().endBlock()
The DSL syntax is implicitly defined by the data types of the host language API. For
example, the return type of a Java method determines which methods you can legally
invoke next (equivalent to the next command in the DSL).
Router rule syntax
Apache Camel defines a router DSL for defining routing rules. You can use this DSL to
define rules in the body of a RouteBuilder.configure() implementation. Figure 1.1,
“Local Routing Rules” shows an overview of the basic syntax for defining local routing rules.
11
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
Figure 1.1. Local Routing Rules
A local rule always starts with a from("EndpointURL") method, which specifies the source
of messages (consumer endpoint) for the routing rule. You can then add an arbitrarily long
chain of processors to the rule (for example, filter()). You typically finish off the rule with
a to("EndpointURL") method, which specifies the target (producer endpoint) for the
messages that pass through the rule. However, it is not always necessary to end a rule with
to(). There are alternative ways of specifying the message target in a rule.
NOTE
You can also define a global routing rule, by starting the rule with a special
processor type (such as intercept(), exception(), or errorHandler()).
Global rules are outside the scope of this guide.
Consumers and producers
A local rule always starts by defining a consumer endpoint, using from("EndpointURL"),
and typically (but not always) ends by defining a producer endpoint, using
to("EndpointURL"). The endpoint URLs, EndpointURL, can use any of the components
configured at deploy time. For example, you could use a file endpoint,
file:MyMessageDirectory, an Apache CXF endpoint, cxf:MyServiceName, or an Apache
ActiveMQ endpoint, activemq:queue:MyQName. For a complete list of component types, see
.
Exchanges
An exchange object consists of a message, augmented by metadata. Exchanges are of
central importance in Apache Camel, because the exchange is the standard form in which
messages are propagated through routing rules. The main constituents of an exchange are,
as follows:
In message—is the current message encapsulated by the exchange. As the
exchange progresses through a route, this message may be modified. So the In
message at the start of a route is typically not the same as the In message at the
end of the route. The org.apache.camel.Message type provides a generic model of
a message, with the following parts:
Body.
Headers.
Attachments.
12
CHAPTER 1. BUILDING BLOCKS FOR ROUTE DEFINITIONS
It is important to realize that this is a generic model of a message. Apache Camel
supports a large variety of protocols and endpoint types. Hence, it is not possible to
standardize the format of the message body or the message headers. For example,
the body of a JMS message would have a completely different format to the body of
a HTTP message or a Web services message. For this reason, the body and the
headers are declared to be of Object type. The original content of the body and the
headers is then determined by the endpoint that created the exchange instance
(that is, the endpoint appearing in the from() command).
Out message—is a temporary holding area for a reply message or for a transformed
message. Certain processing nodes (in particular, the to() command) can modify
the current message by treating the In message as a request, sending it to a
producer endpoint, and then receiving a reply from that endpoint. The reply
message is then inserted into the Out message slot in the exchange.
Normally, if an Out message has been set by the current node, Apache Camel
modifies the exchange as follows before passing it to the next node in the route:
the old In message is discarded and the Out message is moved to the In message
slot. Thus, the reply becomes the new current message. For a more detailed
discussion of how Apache Camel connects nodes together in a route, see
Section 2.1, “Pipeline Processing”.
There is one special case where an Out message is treated differently, however. If
the consumer endpoint at the start of a route is expecting a reply message, the Out
message at the very end of the route is taken to be the consumer endpoint's reply
message (and, what is more, in this case the final node must create an Out message
or the consumer endpoint would hang) .
Message exchange pattern (MEP)—affects the interaction between the exchange
and endpoints in the route, as follows:
Consumer endpoint—the consumer endpoint that creates the original exchange
sets the initial value of the MEP. The initial value indicates whether the
consumer endpoint expects to receive a reply (for example, the InOut MEP) or
not (for example, the InOnly MEP).
Producer endpoints—the MEP affects the producer endpoints that the exchange
encounters along the route (for example, when an exchange passes through a
to() node). For example, if the current MEP isInOnly, a to() node would not
expect to receive a reply from the endpoint. Sometimes you need to change the
current MEP in order to customize the exchange's interaction with a producer
endpoint. For more details, see Section 1.4, “Endpoints”.
Exchange properties—a list of named properties containing metadata for the
current message.
Message exchange patterns
Using an Exchange object makes it easy to generalize message processing to different
message exchange patterns. For example, an asynchronous protocol might define an MEP
that consists of a single message that flows from the consumer endpoint to the producer
endpoint (an InOnly MEP). An RPC protocol, on the other hand, might define an MEP that
consists of a request message and a reply message (an InOut MEP). Currently, Apache
Camel supports the following MEPs:
InOnly
13
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
RobustInOnly
InOut
InOptionalOut
OutOnly
RobustOutOnly
OutIn
OutOptionalIn
Where these message exchange patterns are represented by constants in the enumeration
type, org.apache.camel.ExchangePattern.
Grouped exchanges
Sometimes it is useful to have a single exchange that encapsulates multiple exchange
instances. For this purpose, you can use a grouped exchange. A grouped exchange is
essentially an exchange instance that contains a java.util.List of Exchange objects
stored in the Exchange.GROUPED_EXCHANGE exchange property. For an example of how to
use grouped exchanges, see Section 7.5, “Aggregator”.
Processors
A processor is a node in a route that can access and modify the stream of exchanges
passing through the route. Processors can take expression or predicate arguments, that
modify their behavior. For example, the rule shown in Figure 1.1, “Local Routing Rules”
includes a filter() processor that takes an xpath() predicate as its argument.
Expressions and predicates
Expressions (evaluating to strings or other data types) and predicates (evaluating to true or
false) occur frequently as arguments to the built-in processor types. For example, the
following filter rule propagates In messages, only if the foo header is equal to the value
bar:
from("seda:a").filter(header("foo").isEqualTo("bar")).to("seda:b");
Where the filter is qualified by the predicate, header("foo").isEqualTo("bar"). To
construct more sophisticated predicates and expressions, based on the message content,
you can use one of the expression and predicate languages (see Expression and Predicate
Languages).
1.3. ROUTER SCHEMA IN A SPRING XML FILE
Namespace
The router schema—which defines the XML DSL—belongs to the following XML schema
namespace:
http://camel.apache.org/schema/spring
14
CHAPTER 1. BUILDING BLOCKS FOR ROUTE DEFINITIONS
Specifying the schema location
The location of the router schema is normally specified to be
http://camel.apache.org/schema/spring/camel-spring.xsd, which references the
latest version of the schema on the Apache Web site. For example, the root beans element
of an Apache Camel Spring file is normally configured as shown in Example 1.2, “ Specifying
the Router Schema Location”.
Example 1.2. Specifying the Router Schema Location
Runtime schema location
At run time, Apache Camel does not download the router schema from schema location
specified in the Spring file. Instead, Apache Camel automatically picks up a copy of the
schema from the root directory of the camel-spring JAR file. This ensures that the version
of the schema used to parse the Spring file always matches the current runtime version.
This is important, because the latest version of the schema posted up on the Apache Web
site might not match the version of the runtime you are currently using.
Using an XML editor
Generally, it is recommended that you edit your Spring files using a full-feature XML editor.
An XML editor's auto-completion features make it much easier to author XML that complies
with the router schema and the editor can warn you instantly, if the XML is badly-formed.
XML editors generally do rely on downloading the schema from the location that you
specify in the xsi:schemaLocation attribute. In order to be sure you are using the correct
schema version whilst editing, it is usually a good idea to select a specific version of the
camel-spring.xsd file. For example, to edit a Spring file for the 2.3 version of Apache
Camel, you could modify the beans element as follows:
...
Change back to the default, camel-spring.xsd, when you are finished editing. To see
which schema versions are currently available for download, navigate to the Web page,
http://camel.apache.org/schema/spring.
1.4. ENDPOINTS
Overview
Apache Camel endpoints are the sources and sinks of messages in a route. An endpoint is a
very general sort of building block: the only requirement it must satisfy is that it acts either
as a source of messages (a consumer endpoint) or as a sink of messages (a producer
endpoint). Hence, there are a great variety of different endpoint types supported in Apache
Camel, ranging from protocol supporting endpoints, such as HTTP, to simple timer
endpoints, such as Quartz, that generate dummy messages at regular time intervals. One
of the major strengths of Apache Camel is that it is relatively easy to add a custom
component that implements a new endpoint type.
Endpoint URIs
Endpoints are identified by endpoint URIs, which have the following general form:
scheme:contextPath[?queryOptions]
The URI scheme identifies a protocol, such as http, and the contextPath provides URI details
that are interpreted by the protocol. In addition, most schemes allow you to define query
options, queryOptions, which are specified in the following format:
?option01=value01&option02=value02&...
For example, the following HTTP URI can be used to connect to the Google search engine
page:
http://www.google.com
The following File URI can be used to read all of the files appearing under the
C:\temp\src\data directory:
file://C:/temp/src/data
Not every scheme represents a protocol. Sometimes a scheme just provides access to a
useful utility, such as a timer. For example, the following Timer endpoint URI generates an
exchange every second (=1000 milliseconds). You could use this to schedule activity in a
route.
timer://tickTock?period=1000
Specifying time periods in a URI
16
CHAPTER 1. BUILDING BLOCKS FOR ROUTE DEFINITIONS
Many of the Apache Camel components have options whose value is a time period (for
example, for specifying timeout values and so on). By default, such time period options are
normally specified as a pure number, which is interpreted as a millisecond time period. But
Apache Camel also supports a more readable syntax for time periods, which enables you to
express the period in hours, minutes, and seconds. Formally, the human-readable time
period is a string that conforms to the following syntax:
[NHour(h|hour)][NMin(m|minute)][NSec(s|second)]
Where each term in square brackets, [], is optional and the notation, (A|B), indicates that
A and B are alternatives.
For example, you can configure timer endpoint with a 45 minute period as follows:
from("timer:foo?period=45m")
.to("log:foo");
You can also use arbitrary combinations of the hour, minute, and second units, as follows:
from("timer:foo?period=1h15m")
.to("log:foo");
from("timer:bar?period=2h30s")
.to("log:bar");
from("timer:bar?period=3h45m58s")
.to("log:bar");
Specifying raw values in URI options
By default, the option values that you specify in a URI are automatically URI-encoded. In
some cases this is undesirable beahavior. For example, when setting a password option, it
is preferable to transmit the raw character string without URI encoding.
It is possible to switch of URI encoding by specifying an option value with the syntax,
RAW(RawValue). For example,
from("SourceURI")
.to("ftp:joe@myftpserver.com?password=RAW(se+re?t&23)&binary=true")
In this example, the password value is transmitted as the literal value, se+re?t&23.
Apache Camel components
Each URI scheme maps to a Apache Camel component, where a Apache Camel component
is essentially an endpoint factory. In other words, to use a particular type of endpoint, you
must deploy the corresponding Apache Camel component in your runtime container. For
example, to use JMS endpoints, you would deploy the JMS component in your container.
Apache Camel provides a large variety of different components that enable you to
integrate your application with various transport protocols and third-party products. For
example, some of the more commonly used components are: File, JMS, CXF (Web services),
HTTP, Jetty, Direct, and Mock. For the full list of supported components, see the Apache
Camel component documentation.
Most of the Apache Camel components are packaged separately to the Camel core. If you
17
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
use Maven to build your application, you can easily add a component (and its third-party
dependencies) to your application simply by adding a dependency on the relevant
component artifact. For example, to include the HTTP component, you would add the
following Maven dependency to your project POM file:
2.12.0.redhat-610379
...
...
org.apache.camel
camel-http
${camel-version}
...
The following components are built-in to the Camel core (in the camel-core artifact), so
they are always available:
Bean
Browse
Dataset
Direct
File
Log
Mock
Properties
Ref
SEDA
Timer
VM
Consumer endpoints
A consumer endpoint is an endpoint that appears at thestart of a route (that is, in a from()
DSL command). In other words, the consumer endpoint is responsible for initiating
processing in a route: it creates a new exchange instance (typically, based on some
message that it has received or obtained), and provides a thread to process the exchange
in the rest of the route.
18
CHAPTER 1. BUILDING BLOCKS FOR ROUTE DEFINITIONS
For example, the following JMS consumer endpoint pulls messages off the payments queue
and processes them in the route:
from("jms:queue:payments")
.process(SomeProcessor)
.to("TargetURI");
Or equivalently, in Spring XML:
Some components are consumer only—that is, they can only be used to define consumer
endpoints. For example, the Quartz component is used exclusively to define consumer
endpoints. The following Quartz endpoint generates an event every second (1000
milliseconds):
from("quartz://secondTimer?trigger.repeatInterval=1000")
.process(SomeProcessor)
.to("TargetURI");
If you like, you can specify the endpoint URI as a formatted string, using the fromF() Java
DSL command. For example, to substitute the username and password into the URI for an
FTP endpoint, you could write the route in Java, as follows:
fromF("ftp:%s@fusesource.com?password=%s", username, password)
.process(SomeProcessor)
.to("TargetURI");
Where the first occurrence of %s is replaced by the value of theusername string and the
second occurrence of %s is replaced by the password string. This string formatting
mechanism is implemented by String.format() and is similar to the formatting provided
by the C printf() function. For details, see java.util.Formatter.
Producer endpoints
A producer endpoint is an endpoint that appears in themiddle or at the end of a route (for
example, in a to() DSL command). In other words, the producer endpoint receives an
existing exchange object and sends the contents of the exchange to the specified endpoint.
For example, the following JMS producer endpoint pushes the contents of the current
exchange onto the specified JMS queue:
from("SourceURI")
.process(SomeProcessor)
.to("jms:queue:orderForms");
Or equivalently in Spring XML:
19
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
Some components are producer only—that is, they can only be used to define producer
endpoints. For example, the HTTP endpoint is used exclusively to define producer
endpoints.
from("SourceURI")
.process(SomeProcessor)
.to("http://www.google.com/search?hl=en&q=camel+router");
If you like, you can specify the endpoint URI as a formatted string, using the toF() Java DSL
command. For example, to substitute a custom Google query into the HTTP URI, you could
write the route in Java, as follows:
from("SourceURI")
.process(SomeProcessor)
.toF("http://www.google.com/search?hl=en&q=%s", myGoogleQuery);
Where the occurrence of %s is replaced by your custom query string,myGoogleQuery. For
details, see java.util.Formatter.
1.5. PROCESSORS
Overview
To enable the router to do something more interesting than simply connecting a consumer
endpoint to a producer endpoint, you can add processors to your route. A processor is a
command you can insert into a routing rule to perform arbitrary processing of messages
that flow through the rule. Apache Camel provides a wide variety of different processors, as
shown in Table 1.1, “Apache Camel Processors”.
Table 1.1. Apache Camel Processors
20
Java DSL
XML DSL
Description
aggregate()
aggregate
Aggregator EIP: Creates an
aggregator, which combines
multiple incoming exchanges
into a single exchange.
aop()
aop
Use Aspect Oriented
Programming (AOP) to do
work before and after a
specified sub-route. See
Section 2.8, “Aspect Oriented
Programming”.
CHAPTER 1. BUILDING BLOCKS FOR ROUTE DEFINITIONS
Java DSL
XML DSL
Description
bean() , beanRef()
bean
Process the current exchange
by invoking a method on a
Java object (or bean). See
Section 2.4, “Bean
Integration”.
choice()
choice
Content Based Router EIP:
Selects a particular sub-route
based on the exchange
content, using when and
otherwise clauses.
convertBodyTo()
convertBodyTo
Converts the In message body
to the specified type.
delay()
delay
Delayer EIP: Delays the
propagation of the exchange
to the latter part of the route.
doTry()
doTry
Creates a try/catch block for
handling exceptions, using
doCatch, doFinally, and
end clauses.
end()
N/A
Ends the current command
block.
enrich(),enrichRef()
enrich
Content Enricher EIP:
Combines the current
exchange with data requested
from a specified producer
endpoint URI.
filter()
filter
Message Filter EIP: Uses a
predicate expression to filter
incoming exchanges.
idempotentConsumer()
idempotentConsumer
Idempotent Consumer EIP:
Implements a strategy to
suppress duplicate messages.
inheritErrorHandler()
@inheritErrorHandler
Boolean option that can be
used to disable the inherited
error handler on a particular
route node (defined as a subclause in the Java DSL and as
an attribute in the XML DSL).
21
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
22
Java DSL
XML DSL
Description
inOnly()
inOnly
Either sets the current
exchange's MEP to InOnly (if
no arguments) or sends the
exchange as an InOnly to the
specified endpoint(s).
inOut()
inOut
Either sets the current
exchange's MEP to InOut (if no
arguments) or sends the
exchange as an InOut to the
specified endpoint(s).
loadBalance()
loadBalance
Load Balancer EIP:
Implements load balancing
over a collection of endpoints.
log()
log
Logs a message to the
console.
loop()
loop
Loop EIP: Repeatedly resends
each exchange to the latter
part of the route.
markRollbackOnly()
@markRollbackOnly
(Transactions) Marks the
current transaction for
rollback only (no exception is
raised). In the XML DSL, this
option is set as a boolean
attribute on the rollback
element. See "Transaction
Guide".
markRollbackOnlyLast()
@markRollbackOnlyLast
(Transactions) If one or more
transactions have previously
been associated with this
thread and then suspended,
this command marks the
latest transaction for rollback
only (no exception is raised).
In the XML DSL, this option is
set as a boolean attribute on
the rollback element. See
"Transaction Guide".
marshal()
marshal
Transforms into a low-level or
binary format using the
specified data format, in
preparation for sending over a
particular transport protocol.
CHAPTER 1. BUILDING BLOCKS FOR ROUTE DEFINITIONS
Java DSL
XML DSL
Description
multicast()
multicast
Multicast EIP: Multicasts the
current exchange to multiple
destinations, where each
destination gets its own copy
of the exchange.
onCompletion()
onCompletion
Defines a sub-route
(terminated by end() in the
Java DSL) that gets executed
after the main route has
completed. For conditional
execution, use the onWhen
sub-clause. Can also be
defined on its own line (not in
a route).
onException()
onException
Defines a sub-route
(terminated by end() in the
Java DSL) that gets executed
whenever the specified
exception occurs. Usually
defined on its own line (not in
a route).
pipeline()
pipeline
Pipes and Filters EIP: Sends
the exchange to a series of
endpoints, where the output
of one endpoint becomes the
input of the next endpoint.
See also Section 2.1, “Pipeline
Processing”.
policy()
policy
Apply a policy to the current
route (currently only used for
transactional policies—see
"Transaction Guide").
pollEnrich(),pollEnrich
Ref()
pollEnrich
Content Enricher EIP:
Combines the current
exchange with data polled
from a specified consumer
endpoint URI.
process(),processRef
process
Execute a custom processor
on the current exchange. See
the section called “Custom
processor” and Part IV,
“Programming EIP
Components”.
recipientList()
recipientList
Recipient List EIP: Sends the
exchange to a list of
recipients that is calculated at
runtime (for example, based
on the contents of a header).
23
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
24
Java DSL
XML DSL
Description
removeHeader()
removeHeader
Removes the specified header
from the exchange's In
message.
removeHeaders()
removeHeaders
Removes the headers
matching the specified
pattern from the exchange's
In message. The pattern can
have the form, prefix*—in
which case it matches every
name starting with prefix—
otherwise, it is interpreted as
a regular expression.
removeProperty()
removeProperty
Removes the specified
exchange property from the
exchange.
resequence()
resequence
Resequencer EIP: Re-orders
incoming exchanges on the
basis of a specified
comparotor operation.
Supports a batch mode and a
stream mode.
rollback()
rollback
(Transactions) Marks the
current transaction for
rollback only (also raising an
exception, by default). See
"Transaction Guide".
routingSlip()
routingSlip
Routing Slip EIP: Routes the
exchange through a pipeline
that is constructed
dynamically, based on the list
of endpoint URIs extracted
from a slip header.
sample()
sample
Creates a sampling throttler,
allowing you to extract a
sample of exchanges from the
traffic on a route.
setBody()
setBody
Sets the message body of the
exchange's In message.
setExchangePattern()
setExchangePattern
Sets the current exchange's
MEP to the specified value.
See the section called
“Message exchange
patterns”.
CHAPTER 1. BUILDING BLOCKS FOR ROUTE DEFINITIONS
Java DSL
XML DSL
Description
setHeader()
setHeader
Sets the specified header in
the exchange's In message.
setOutHeader()
setOutHeader
Sets the specified header in
the exchange's Out message.
setProperty()
setProperty()
Sets the specified exchange
property.
sort()
sort
Sorts the contents of the In
message body (where a
custom comparator can
optionally be specified).
split()
split
Splitter EIP: Splits the current
exchange into a sequence of
exchanges, where each split
exchange contains a fragment
of the original message body.
stop()
stop
Stops routing the current
exchange and marks it as
completed.
threads()
threads
Creates a thread pool for
concurrent processing of the
latter part of the route.
throttle()
throttle
Throttler EIP: Limit the flow
rate to the specified level
(exchanges per second).
throwException()
throwException
Throw the specified Java
exception.
to()
to
Send the exchange to one or
more endpoints. See
Section 2.1, “Pipeline
Processing”.
toF()
N/A
Send the exchange to an
endpoint, using string
formatting. That is, the
endpoint URI string can
embed substitutions in the
style of the C printf()
function.
transacted()
transacted
Create a Spring transaction
scope that encloses the latter
part of the route. See
"Transaction Guide".
25
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
Java DSL
XML DSL
Description
transform()
transform
Message Translator EIP: Copy
the In message headers to the
Out message headers and set
the Out message body to the
specified value.
unmarshal()
unmarshal
Transforms the In message
body from a low-level or
binary format to a high-level
format, using the specified
data format.
validate()
validate
Takes a predicate expression
to test whether the current
message is valid. If the
predicate returns false,
throws a
PredicateValidationExce
ption exception.
wireTap()
wireTap
Wire Tap EIP: Sends a copy of
the current exchange to the
specified wire tap URI, using
the
ExchangePattern.InOnly
MEP.
Some sample processors
To get some idea of how to use processors in a route, see the following examples:
the section called “Choice”.
the section called “Filter”.
the section called “Throttler”.
the section called “Custom processor”.
Choice
The choice() processor is a conditional statement that is used to route incoming messages
to alternative producer endpoints. Each alternative producer endpoint is preceded by a
when() method, which takes a predicate argument. If the predicate is true, the following
target is selected, otherwise processing proceeds to the next when() method in the rule.
For example, the following choice() processor directs incoming messages to either
Target1, Target2, or Target3, depending on the values of Predicate1 and Predicate2:
from("SourceURL")
.choice()
.when(Predicate1).to("Target1")
.when(Predicate2).to("Target2")
.otherwise().to("Target3");
26
CHAPTER 1. BUILDING BLOCKS FOR ROUTE DEFINITIONS
Or equivalently in Spring XML:
header.foo = 'bar'
header.foo = 'manchu'
In the Java DSL, there is a special case where you might need to use the endChoice()
command. Some of the standard Apache Camel processors enable you to specify extra
parameters using special sub-clauses, effectively opening an extra level of nesting which is
usually terminated by the end() command. For example, you could specify a load balancer
clause as loadBalance().roundRobin().to("mock:foo").to("mock:bar").end(), which
load balances messages between the mock:foo and mock:bar endpoints. If the load
balancer clause is embedded in a choice condition, however, it is necessary to terminate
the clause using the endChoice() command, as follows:
from("direct:start")
.choice()
.when(bodyAs(String.class).contains("Camel"))
.loadBalance().roundRobin().to("mock:foo").to("mock:bar").endChoice()
.otherwise()
.to("mock:result");
Filter
The filter() processor can be used to prevent uninteresting messages from reaching the
producer endpoint. It takes a single predicate argument: if the predicate is true, the
message exchange is allowed through to the producer; if the predicate is false, the
message exchange is blocked. For example, the following filter blocks a message exchange,
unless the incoming message contains a header, foo, with value equal tobar:
from("SourceURL").filter(header("foo").isEqualTo("bar")).to("TargetURL");
Or equivalently in Spring XML:
header.foo = 'bar'
Throttler
The throttle() processor ensures that a producer endpoint does not get overloaded. The
throttler works by limiting the number of messages that can pass through per second. If
the incoming messages exceed the specified rate, the throttler accumulates excess
messages in a buffer and transmits them more slowly to the producer endpoint. For
example, to limit the rate of throughput to 100 messages per second, you can define the
following rule:
from("SourceURL").throttle(100).to("TargetURL");
Or equivalently in Spring XML:
Custom processor
If none of the standard processors described here provide the functionality you need, you
can always define your own custom processor. To create a custom processor, define a class
that implements the org.apache.camel.Processor interface and overrides the process()
method. The following custom processor, MyProcessor, removes the header named foo
from incoming messages:
Example 1.3. Implementing a Custom Processor Class
public class MyProcessor implements org.apache.camel.Processor {
public void process(org.apache.camel.Exchange exchange) {
inMessage = exchange.getIn();
if (inMessage != null) {
inMessage.removeHeader("foo");
}
}
};
28
CHAPTER 1. BUILDING BLOCKS FOR ROUTE DEFINITIONS
To insert the custom processor into a router rule, invoke the process() method, which
provides a generic mechanism for inserting processors into rules. For example, the
following rule invokes the processor defined in Example 1.3, “Implementing a Custom
Processor Class”:
org.apache.camel.Processor myProc = new MyProcessor();
from("SourceURL").process(myProc).to("TargetURL");
29
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
CHAPTER 2. BASIC PRINCIPLES OF ROUTE BUILDING
Abstract
Apache Camel provides several processors and components that you can link together in a
route. This chapter provides a basic orientation by explaining the principles of building a
route using the provided building blocks.
2.1. PIPELINE PROCESSING
Overview
In Apache Camel, pipelining is the dominant paradigm for connecting nodes in a route
definition. The pipeline concept is probably most familiar to users of the UNIX operating
system, where it is used to join operating system commands. For example, ls | more is an
example of a command that pipes a directory listing, ls, to the page-scrolling utility,more.
The basic idea of a pipeline is that the output of one command is fed into the input of the
next. The natural analogy in the case of a route is for the Out message from one processor
to be copied to the In message of the next processor.
Processor nodes
Every node in a route, except for the initial endpoint, is a processor, in the sense that they
inherit from the org.apache.camel.Processor interface. In other words, processors make
up the basic building blocks of a DSL route. For example, DSL commands such as filter(),
delayer(), setBody(), setHeader(), and to() all represent processors. When considering
how processors connect together to build up a route, it is important to distinguish two
different processing approaches.
The first approach is where the processor simply modifies the exchange's In message, as
shown in Figure 2.1, “Processor Modifying an In Message”. The exchange's Out message
remains null in this case.
Figure 2.1. Processor Modifying an In Message
The following route shows a setHeader() command that modifies the current In message
by adding (or modifying) the BillingSystem heading:
from("activemq:orderQueue")
.setHeader("BillingSystem", xpath("/order/billingSystem"))
.to("activemq:billingQueue");
The second approach is where the processor creates an Out message to represent the
result of the processing, as shown in Figure 2.2, “Processor Creating an Out Message”.
30
CHAPTER 2. BASIC PRINCIPLES OF ROUTE BUILDING
Figure 2.2. Processor Creating an Out Message
The following route shows a transform() command that creates an Out message with a
message body containing the string, DummyBody:
from("activemq:orderQueue")
.transform(constant("DummyBody"))
.to("activemq:billingQueue");
where constant("DummyBody") represents a constant expression. You cannot pass the
string, DummyBody, directly, because the argument to transform() must be an expression
type.
Pipeline for InOnly exchanges
Figure 2.3, “Sample Pipeline for InOnly Exchanges”shows an example of a processor
pipeline for InOnly exchanges. Processor A acts by modifying theIn message, while
processors B and C create an Out message. The route builder links the processors together
as shown. In particular, processors B and C are linked together in the form of a pipeline:
that is, processor B's Out message is moved to the In message before feeding the
exchange into processor C, and processor C's Out message is moved to the In message
before feeding the exchange into the producer endpoint. Thus the processors' outputs and
inputs are joined into a continuous pipeline, as shown in Figure 2.3, “Sample Pipeline for
InOnly Exchanges”.
Figure 2.3. Sample Pipeline for InOnly Exchanges
Apache Camel employs the pipeline pattern by default, so you do not need to use any
special syntax to create a pipeline in your routes. For example, the following route pulls
messages from a userdataQueue queue, pipes the message through a Velocity template (to
produce a customer address in text format), and then sends the resulting text address to
the queue, envelopeAddressQueue:
from("activemq:userdataQueue")
.to(ExchangePattern.InOut, "velocity:file:AdressTemplate.vm")
.to("activemq:envelopeAddresses");
Where the Velocity endpoint, velocity:file:AdressTemplate.vm, specifies the location of
a Velocity template file, file:AdressTemplate.vm, in the file system. Theto() command
changes the exchange pattern to InOut before sending the exchange to the Velocity
31
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
endpoint and then changes it back to InOnly afterwards. For more details of the Velocity
endpoint, see .
Pipeline for InOut exchanges
Figure 2.4, “Sample Pipeline for InOut Exchanges” shows an example of a processor
pipeline for InOut exchanges, which you typically use to support remote procedure call
(RPC) semantics. Processors A, B, and C are linked together in the form of a pipeline, with
the output of each processor being fed into the input of the next. The final Out message
produced by the producer endpoint is sent all the way back to the consumer endpoint,
where it provides the reply to the original request.
Figure 2.4. Sample Pipeline for InOut Exchanges
Note that in order to support the InOut exchange pattern, it is essential that the last node
in the route (whether it is a producer endpoint or some other kind of processor) creates an
Out message. Otherwise, any client that connects to the consumer endpoint would hang
and wait indefinitely for a reply message. You should be aware that not all producer
endpoints create Out messages.
Consider the following route that processes payment requests, by processing incoming
HTTP requests:
from("jetty:http://localhost:8080/foo")
.to("cxf:bean:addAccountDetails")
.to("cxf:bean:getCreditRating")
.to("cxf:bean:processTransaction");
Where the incoming payment request is processed by passing it through a pipeline of Web
services, cxf:bean:addAccountDetails, cxf:bean:getCreditRating, and
cxf:bean:processTransaction. The final Web service, processTransaction, generates a
response (Out message) that is sent back through the JETTY endpoint.
When the pipeline consists of just a sequence of endpoints, it is also possible to use the
following alternative syntax:
from("jetty:http://localhost:8080/foo")
.pipeline("cxf:bean:addAccountDetails", "cxf:bean:getCreditRating",
"cxf:bean:processTransaction");
Pipeline for InOptionalOut exchanges
The pipeline for InOptionalOut exchanges is essentially the same as the pipeline in
Figure 2.4, “Sample Pipeline for InOut Exchanges”. The difference between InOut and
InOptionalOut is that an exchange with theInOptionalOut exchange pattern is allowed to
have a null Out message as a reply. That is, in the case of anInOptionalOut exchange, a
32
CHAPTER 2. BASIC PRINCIPLES OF ROUTE BUILDING
null Out message is copied to the In message of the next node in the pipeline. By contrast,
in the case of an InOut exchange, a null Out message is discarded and the original In
message from the current node would be copied to the In message of the next node
instead.
2.2. MULTIPLE INPUTS
Overview
A standard route takes its input from just a single endpoint, using the from(EndpointURL)
syntax in the Java DSL. But what if you need to define multiple inputs for your route?
Apache Camel provides several alternatives for specifying multiple inputs to a route. The
approach to take depends on whether you want the exchanges to be processed
independently of each other or whether you want the exchanges from different inputes to
be combined in some way (in which case, you should use the the section called “Content
enricher pattern”).
Multiple independent inputs
The simplest way to specify multiple inputs is using the multi-argument form of the from()
DSL command, for example:
from("URI1", "URI2", "URI3").to("DestinationUri");
Or you can use the following equivalent syntax:
from("URI1").from("URI2").from("URI3").to("DestinationUri");
In both of these examples, exchanges from each of the input endpoints, URI1, URI2, and
URI3, are processed independently of each other and in separate threads. In fact, you can
think of the preceding route as being equivalent to the following three separate routes:
from("URI1").to("DestinationUri");
from("URI2").to("DestinationUri");
from("URI3").to("DestinationUri");
Segmented routes
For example, you might want to merge incoming messages from two different messaging
systems and process them using the same route. In most cases, you can deal with multiple
inputs by dividing your route into segments, as shown in Figure 2.5, “Processing Multiple
Inputs with Segmented Routes”.
Figure 2.5. Processing Multiple Inputs with Segmented Routes
The initial segments of the route take their inputs from some external queues—for example,
33
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
activemq:Nyse and activemq:Nasdaq—and send the incoming exchanges to an internal
endpoint, InternalUrl. The second route segment merges the incoming exchanges, taking
them from the internal endpoint and sending them to the destination queue,
activemq:USTxn. The InternalUrl is the URL for an endpoint that is intended only for use
within a router application. The following types of endpoints are suitable for internal use:
the section called “Direct endpoints”.
the section called “SEDA endpoints”.
the section called “VM endpoints”.
The main purpose of these endpoints is to enable you to glue together different segments
of a route. They all provide an effective way of merging multiple inputs into a single route.
Direct endpoints
The direct component provides the simplest mechanism for linking together routes. The
event model for the direct component is synchronous, so that subsequent segments of the
route run in the same thread as the first segment. The general format of a direct URL is
direct:EndpointID, where the endpoint ID, EndpointID, is simply a unique alphanumeric
string that identifies the endpoint instance.
For example, if you want to take the input from two message queues, activemq:Nyse and
activemq:Nasdaq, and merge them into a single message queue,activemq:USTxn, you can
do this by defining the following set of routes:
from("activemq:Nyse").to("direct:mergeTxns");
from("activemq:Nasdaq").to("direct:mergeTxns");
from("direct:mergeTxns").to("activemq:USTxn");
Where the first two routes take the input from the message queues, Nyse and Nasdaq, and
send them to the endpoint, direct:mergeTxns. The last queue combines the inputs from
the previous two queues and sends the combined message stream to the activemq:USTxn
queue.
The implementation of the direct endpoint behaves as follows: whenever an exchange
arrives at a producer endpoint (for example, to("direct:mergeTxns")), the direct
endpoint passes the exchange directly to all of the consumers endpoints that have the
same endpoint ID (for example, from("direct:mergeTxns")). Direct endpoints can only be
used to communicate between routes that belong to the same CamelContext in the same
Java virtual machine (JVM) instance.
SEDA endpoints
The SEDA component provides an alternative mechanism for linking together routes. You
can use it in a similar way to the direct component, but it has a different underlying event
and threading model, as follows:
Processing of a SEDA endpoint is not synchronous. That is, when you send an
exchange to a SEDA producer endpoint, control immediately returns to the
preceding processor in the route.
34
CHAPTER 2. BASIC PRINCIPLES OF ROUTE BUILDING
SEDA endpoints contain a queue buffer (of java.util.concurrent.BlockingQueue
type), which stores all of the incoming exchanges prior to processing by the next
route segment.
Each SEDA consumer endpoint creates a thread pool (the default size is 5) to
process exchange objects from the blocking queue.
The SEDA component supports the competing consumers pattern, which guarantees
that each incoming exchange is processed only once, even if there are multiple
consumers attached to a specific endpoint.
One of the main advantages of using a SEDA endpoint is that the routes can be more
responsive, owing to the built-in consumer thread pool. The stock transactions example can
be re-written to use SEDA endpoints instead of direct endpoints, as follows:
from("activemq:Nyse").to("seda:mergeTxns");
from("activemq:Nasdaq").to("seda:mergeTxns");
from("seda:mergeTxns").to("activemq:USTxn");
The main difference between this example and the direct example is that when using SEDA,
the second route segment (from seda:mergeTxns to activemq:USTxn) is processed by a
pool of five threads.
NOTE
There is more to SEDA than simply pasting together route segments. The
staged event-driven architecture (SEDA) encompasses a design philosophy for
building more manageable multi-threaded applications. The purpose of the
SEDA component in Apache Camel is simply to enable you to apply this design
philosophy to your applications. For more details about SEDA, see
http://www.eecs.harvard.edu/~mdw/proj/seda/.
VM endpoints
The VM component is very similar to the SEDA endpoint. The only difference is that,
whereas the SEDA component is limited to linking together route segments from within the
same CamelContext, the VM component enables you to link together routes from distinct
Apache Camel applications, as long as they are running within the same Java virtual
machine.
The stock transactions example can be re-written to use VM endpoints instead of SEDA
endpoints, as follows:
from("activemq:Nyse").to("vm:mergeTxns");
from("activemq:Nasdaq").to("vm:mergeTxns");
And in a separate router application (running in the same Java VM), you can define the
second segment of the route as follows:
from("vm:mergeTxns").to("activemq:USTxn");
Content enricher pattern
35
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
The content enricher pattern defines a fundamentally different way of dealing with multiple
inputs to a route. When an exchange enters the enricher processor, the enricher contacts
an external resource to retrieve information, which is then added to the original message.
In this pattern, the external resource effectively represents a second input to the message.
For example, suppose you are writing an application that processes credit requests. Before
processing a credit request, you need to augment it with the data that assigns a credit
rating to the customer, where the ratings data is stored in a file in the directory,
src/data/ratings. You can combine the incoming credit request with data from the
ratings file using the pollEnrich() pattern and a GroupedExchangeAggregationStrategy
aggregation strategy, as follows:
from("jms:queue:creditRequests")
.pollEnrich("file:src/data/ratings?noop=true", new
GroupedExchangeAggregationStrategy())
.bean(new MergeCreditRequestAndRatings(), "merge")
.to("jms:queue:reformattedRequests");
Where the GroupedExchangeAggregationStrategy class is a standard aggregation
strategy from the org.apache.camel.processor.aggregate package that adds each new
exchange to a java.util.List instance and stores the resulting list in the
Exchange.GROUPED_EXCHANGE exchange property. In this case, the list contains two
elements: the original exchange (from the creditRequests JMS queue); and the enricher
exchange (from the file endpoint).
To access the grouped exchange, you can use code like the following:
public class MergeCreditRequestAndRatings {
public void merge(Exchange ex) {
// Obtain the grouped exchange
List list = ex.getProperty(Exchange.GROUPED_EXCHANGE,
List.class);
// Get the exchanges from the grouped exchange
Exchange originalEx = list.get(0);
Exchange ratingsEx = list.get(1);
// Merge the exchanges
...
}
}
An alternative approach to this application would be to put the merge code directly into the
implementation of the custom aggregation strategy class.
For more details about the content enricher pattern, see Section 8.1, “Content Enricher”.
2.3. EXCEPTION HANDLING
Abstract
Apache Camel provides several different mechanisms, which let you handle exceptions at
different levels of granularity: you can handle exceptions within a route using doTry,
doCatch, and doFinally; or you can specify what action to take for each exception type
36
CHAPTER 2. BASIC PRINCIPLES OF ROUTE BUILDING
and apply this rule to all routes in a RouteBuilder using onException; or you can specify
what action to take for all exception types and apply this rule to all routes in a
RouteBuilder using errorHandler.
For more details about exception handling, see Section 5.3, “Dead Letter Channel”.
2.3.1. onException Clause
Overview
The onException clause is a powerful mechanism for trapping exceptions that occur in one
or more routes: it is type-specific, enabling you to define distinct actions to handle different
exception types; it allows you to define actions using essentially the same (actually, slightly
extended) syntax as a route, giving you considerable flexibility in the way you handle
exceptions; and it is based on a trapping model, which enables a single onException clause
to deal with exceptions occurring at any node in any route.
Trapping exceptions using onException
The onException clause is a mechanism for trapping, rather than catching exceptions. That
is, once you define an onException clause, it traps exceptions that occur at any point in a
route. This contrasts with the Java try/catch mechanism, where an exception is caught,
only if a particular code fragment is explicitly enclosed in a try block.
What really happens when you define an onException clause is that the Apache Camel
runtime implicitly encloses each route node in a try block. This is why the onException
clause is able to trap exceptions at any point in the route. But this wrapping is done for you
automatically; it is not visible in the route definitions.
Java DSL example
In the following Java DSL example, the onException clause applies to all of the routes
defined in the RouteBuilder class. If a ValidationException exception occurs while
processing either of the routes (from("seda:inputA") or from("seda:inputB")), the
onException clause traps the exception and redirects the current exchange to the
validationFailed JMS queue (which serves as a deadletter queue).
// Java
public class MyRouteBuilder extends RouteBuilder {
public void configure() {
onException(ValidationException.class)
.to("activemq:validationFailed");
from("seda:inputA")
.to("validation:foo/bar.xsd", "activemq:someQueue");
from("seda:inputB").to("direct:foo")
.to("rnc:mySchema.rnc", "activemq:anotherQueue");
}
}
XML DSL example
37
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
The preceding example can also be expressed in the XML DSL, using the onException
element to define the exception clause, as follows:
com.mycompany.ValidationException
Trapping multiple exceptions
You can define multiple onException clauses to trap exceptions in a RouteBuilder scope.
This enables you to take different actions in response to different exceptions. For example,
the following series of onException clauses defined in the Java DSL define different
deadletter destinations for ValidationException, ValidationException, and Exception:
onException(ValidationException.class).to("activemq:validationFailed");
onException(java.io.IOException.class).to("activemq:ioExceptions");
onException(Exception.class).to("activemq:exceptions");
You can define the same series of onException clauses in the XML DSL as follows:
com.mycompany.ValidationException
java.io.IOException
38
CHAPTER 2. BASIC PRINCIPLES OF ROUTE BUILDING
java.lang.Exception
You can also group multiple exceptions together to be trapped by the same onException
clause. In the Java DSL, you can group multiple exceptions as follows:
onException(ValidationException.class, BuesinessException.class)
.to("activemq:validationFailed");
In the XML DSL, you can group multiple exceptions together by defining more than one
exception element inside the onException element, as follows:
com.mycompany.ValidationException
com.mycompany.BuesinessException
When trapping multiple exceptions, the order of the onException clauses is significant.
Apache Camel initially attempts to match the thrown exception against the first clause. If
the first clause fails to match, the next onException clause is tried, and so on until a match
is found. Each matching attempt is governed by the following algorithm:
1. If the thrown exception is a chained exception (that is, where an exception has been
caught and rethrown as a different exception), the most nested exception type
serves initially as the basis for matching. This exception is tested as follows:
a. If the exception-to-test has exactly the type specified in the onException clause
(tested using instanceof), a match is triggered.
b. If the exception-to-test is a sub-type of the type specified in the onException
clause, a match is triggered.
2. If the most nested exception fails to yield a match, the next exception in the chain
(the wrapping exception) is tested instead. The testing continues up the chain until
either a match is triggered or the chain is exhausted.
Deadletter channel
The basic examples of onException usage have so far all exploited thedeadletter channel
pattern. That is, when an onException clause traps an exception, the current exchange is
routed to a special destination (the deadletter channel). The deadletter channel serves as a
holding area for failed messages that have not been processed. An administrator can
inspect the messages at a later time and decide what action needs to be taken.
For more details about the deadletter channel pattern, see Section 5.3, “Dead Letter
Channel”.
Use original message
By the time an exception is raised in the middle of a route, the message in the exchange
could have been modified considerably (and might not even by readable by a human).
Often, it is easier for an administrator to decide what corrective actions to take, if the
39
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
messages visible in the deadletter queue are the original messages, as received at the
start of the route.
In the Java DSL, you can replace the message in the exchange by the original message,
using the useOriginalMessage() DSL command, as follows:
onException(ValidationException.class)
.useOriginalMessage()
.to("activemq:validationFailed");
In the XML DSL, you can retrieve the original message by setting the useOriginalMessage
attribute on the onException element, as follows:
com.mycompany.ValidationException
Redelivery policy
Instead of interrupting the processing of a message and giving up as soon as an exception
is raised, Apache Camel gives you the option of attempting to redeliver the message at the
point where the exception occurred. In networked systems, where timeouts can occur and
temporary faults arise, it is often possible for failed messages to be processed successfully,
if they are redelivered shortly after the original exception was raised.
The Apache Camel redelivery supports various strategies for redelivering messages after an
exception occurs. Some of the most important options for configuring redelivery are as
follows:
maximumRedeliveries()
Specifies the maximum number of times redelivery can be attempted (default is 0). A
negative value means redelivery is always attempted (equivalent to an infinite value).
retryWhile()
Specifies a predicate (of Predicate type), which determines whether Apache Camel
ought to continue redelivering. If the predicate evaluates to true on the current
exchange, redelivery is attempted; otherwise, redelivery is stopped and no further
redelivery attempts are made.
This option takes precedence over the maximumRedeliveries() option.
In the Java DSL, redelivery policy options are specified using DSL commands in the
onException clause. For example, you can specify a maximum of six redeliveries, after
which the exchange is sent to the validationFailed deadletter queue, as follows:
onException(ValidationException.class)
.maximumRedeliveries(6)
.retryAttemptedLogLevel(org.apache.camel.LogginLevel.WARN)
.to("activemq:validationFailed");
40
CHAPTER 2. BASIC PRINCIPLES OF ROUTE BUILDING
In the XML DSL, redelivery policy options are specified by setting attributes on the
redeliveryPolicy element. For example, the preceding route can be expressed in XML
DSL as follows:
com.mycompany.ValidationException
The latter part of the route—after the redelivery options are set—is not processed until
after the last redelivery attempt has failed. For detailed descriptions of all the redelivery
options, see Section 5.3, “Dead Letter Channel”.
Alternatively, you can specify redelivery policy options in a redeliveryPolicyProfile
instance. You can then reference the redeliveryPolicyProfile instance using the
onException element's redeliverPolicyRef attribute. For example, the preceding route
can be expressed as follows:
com.mycompany.ValidationException
NOTE
The approach using redeliveryPolicyProfile is useful, if you want to re-use
the same redelivery policy in multiple onException clauses.
Conditional trapping
Exception trapping with onException can be made conditional by specifying theonWhen
option. If you specify the onWhen option in an onException clause, a match is triggered only
when the thrown exception matches the clause and the onWhen predicate evaluates to
true on the current exchange.
For example, in the following Java DSL fragment,the first onException clause triggers, only
if the thrown exception matches MyUserException and the user header is non-null in the
current exchange:
// Java
// Here we define onException() to catch MyUserException when
// there is a header[user] on the exchange that is not null
onException(MyUserException.class)
.onWhen(header("user").isNotNull())
.maximumRedeliveries(2)
.to(ERROR_USER_QUEUE);
// Here we define onException to catch MyUserException as a kind
41
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
// of fallback when the above did not match.
// Noitce: The order how we have defined these onException is
// important as Camel will resolve in the same order as they
// have been defined
onException(MyUserException.class)
.maximumRedeliveries(2)
.to(ERROR_QUEUE);
The preceding onException clauses can be expressed in the XML DSL as follows:
com.mycompany.MyUserException
${header.user} != null
com.mycompany.MyUserException
Handling exceptions
By default, when an exception is raised in the middle of a route, processing of the current
exchange is interrupted and the thrown exception is propagated back to the consumer
endpoint at the start of the route. When an onException clause is triggered, the behavior
is essentially the same, except that the onException clause performs some processing
before the thrown exception is propagated back.
But this default behavior is not the only way to handle an exception. TheonException
provides various options to modify the exception handling behavior, as follows:
the section called “Suppressing exception rethrow”—you have the option of
suppressing the rethrown exception after the onException clause has completed. In
other words, in this case the exception does not propagate back to the consumer
endpoint at the start of the route.
the section called “Continuing processing”—you have the option of resuming normal
processing of the exchange from the point where the exception originally occurred.
Implicitly, this approach also suppresses the rethrown exception.
the section called “Sending a response”—in the special case where the consumer
endpoint at the start of the route expects a reply (that is, having an InOut MEP), you
might prefer to construct a custom fault reply message, rather than propagating the
exception back to the consumer endpoint.
Suppressing exception rethrow
To prevent the current exception from being rethrown and propagated back to the
consumer endpoint, you can set the handled() option to true in the Java DSL, as follows:
42
CHAPTER 2. BASIC PRINCIPLES OF ROUTE BUILDING
onException(ValidationException.class)
.handled(true)
.to("activemq:validationFailed");
In the Java DSL, the argument to the handled() option can be of boolean type, of
Predicate type, or of Expression type (where any non-boolean expression is interpreted
as true, if it evaluates to a non-null value).
The same route can be configured to suppress the rethrown exception in the XML DSL,
using the handled element, as follows:
com.mycompany.ValidationException
true
Continuing processing
To continue processing the current message from the point in the route where the
exception was originally thrown, you can set the continued option to true in the Java DSL,
as follows:
onException(ValidationException.class)
.continued(true);
In the Java DSL, the argument to the continued() option can be of boolean type, of
Predicate type, or of Expression type (where any non-boolean expression is interpreted
as true, if it evaluates to a non-null value).
The same route can be configured in the XML DSL, using the continued element, as
follows:
com.mycompany.ValidationException
true
Sending a response
When the consumer endpoint that starts a route expects a reply, you might prefer to
construct a custom fault reply message, instead of simply letting the thrown exception
propagate back to the consumer. There are two essential steps you need to follow in this
case: suppress the rethrown exception using the handled option; and populate the
exchange's Out message slot with a custom fault message.
For example, the following Java DSL fragment shows how to send a reply message
containing the text string, Sorry, whenever the MyFunctionalException exception occurs:
43
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
// we catch MyFunctionalException and want to mark it as handled (= no
failure returned to client)
// but we want to return a fixed text response, so we transform OUT body
as Sorry.
onException(MyFunctionalException.class)
.handled(true)
.transform().constant("Sorry");
If you are sending a fault response to the client, you will often want to incorporate the text
of the exception message in the response. You can access the text of the current
exception message using the exceptionMessage() builder method. For example, you can
send a reply containing just the text of the exception message whenever the
MyFunctionalException exception occurs, as follows:
// we catch MyFunctionalException and want to mark it as handled (= no
failure returned to client)
// but we want to return a fixed text response, so we transform OUT body
and return the exception message
onException(MyFunctionalException.class)
.handled(true)
.transform(exceptionMessage());
The exception message text is also accessible from the Simple language, through the
exception.message variable. For example, you could embed the current exception text in
a reply message, as follows:
// we catch MyFunctionalException and want to mark it as handled (= no
failure returned to client)
// but we want to return a fixed text response, so we transform OUT body
and return a nice message
// using the simple language where we want insert the exception message
onException(MyFunctionalException.class)
.handled(true)
.transform().simple("Error reported: ${exception.message} - cannot
process this message.");
The preceding onException clause can be expressed in XML DSL as follows:
com.mycompany.MyFunctionalException
true
Error reported: ${exception.message} - cannot process this
message.
Exception thrown while handling an exception
An exception that gets thrown while handling an existing exception (in other words, one
that gets thrown in the middle of processing an onException clause) is handled in a special
way. Such an exception is handled by the special fallback exception handler, which handles
44
CHAPTER 2. BASIC PRINCIPLES OF ROUTE BUILDING
the exception as follows:
All existing exception handlers are ignored and processing fails immediately.
The new exception is logged.
The new exception is set on the exchange object.
The simple strategy avoids complex failure scenarios which could otherwise end up with an
onException clause getting locked into an infinite loop.
Scopes
The onException clauses can be effective in either of the following scopes:
RouteBuilder scope—onException clauses defined as standalone statements inside
a RouteBuilder.configure() method affect all of the routes defined in that
RouteBuilder instance. On the other hand, these onException clauses have no
effect whatsoever on routes defined inside any otherRouteBuilder instance. The
onException clauses must appear before the route definitions.
All of the examples up to this point are defined using the RouteBuilder scope.
Route scope—onException clauses can also be embedded directly within a route.
These onException clauses affect only the route in which they are defined.
Route scope
You can embed an onException clause anywhere inside a route definition, but you must
terminate the embedded onException clause using the end() DSL command.
For example, you can define an embedded onException clause in the Java DSL, as follows:
// Java
from("direct:start")
.onException(OrderFailedException.class)
.maximumRedeliveries(1)
.handled(true)
.beanRef("orderService", "orderFailed")
.to("mock:error")
.end()
.beanRef("orderService", "handleOrder")
.to("mock:result");
You can define an embedded onException clause in the XML DSL, as follows:
com.mycompany.OrderFailedException
true
45
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
2.3.2. Error Handler
Overview
The errorHandler() clause provides similar features to theonException clause, except
that this mechanism is not able to discriminate between different exception types. The
errorHandler() clause is the original exception handling mechanism provided by Apache
Camel and was available before the onException clause was implemented.
Java DSL example
The errorHandler() clause is defined in a RouteBuilder class and applies to all of the
routes in that RouteBuilder class. It is triggered whenever an exception of any kind occurs
in one of the applicable routes. For example, to define an error handler that routes all failed
exchanges to the ActiveMQ deadLetter queue, you can define a RouteBuilder as follows:
public class MyRouteBuilder extends RouteBuilder {
public void configure() {
errorHandler(deadLetterChannel("activemq:deadLetter"));
// The preceding error handler applies
// to all of the following routes:
from("activemq:orderQueue")
.to("pop3://fulfillment@acme.com");
from("file:src/data?noop=true")
.to("file:target/messages");
// ...
}
}
Redirection to the dead letter channel will not occur, however, until all attempts at
redelivery have been exhausted.
XML DSL example
In the XML DSL, you define an error handler within a camelContext scope using the
errorHandler element. For example, to define an error handler that routes all failed
exchanges to the ActiveMQ deadLetter queue, you can define an errorHandler element
as follows:
46
CHAPTER 2. BASIC PRINCIPLES OF ROUTE BUILDING
Types of error handler
Table 2.1, “Error Handler Types” provides an overview of the different types of error
handler you can define.
Table 2.1. Error Handler Types
Java DSL Builder
XML DSL Type Attribute
Description
defaultErrorHandler()
DefaultErrorHandler
Propagates exceptions back
to the caller and supports the
redelivery policy, but it does
not support a dead letter
queue.
deadLetterChannel()
DeadLetterChannel
Supports the same features
as the default error handler
and, in addition, supports a
dead letter queue.
loggingErrorChannel()
LoggingErrorChannel
Logs the exception text
whenever an exception
occurs.
noErrorHandler()
NoErrorHandler
Dummy handler
implementation that can be
used to disable the error
handler.
TransactionErrorHandler
An error handler for
transacted routes. A default
transaction error handler
instance is automatically used
for a route that is marked as
transacted.
47
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
2.3.3. doTry, doCatch, and doFinally
Overview
To handle exceptions within a route, you can use a combination of the doTry, doCatch, and
doFinally clauses, which handle exceptions in a similar way to Java'stry, catch, and
finally blocks.
Similarities between doCatch and Java catch
In general, the doCatch() clause in a route definition behaves in an analogous way to the
catch() statement in Java code. In particular, the following features are supported by the
doCatch() clause:
Multiple doCatch clauses—you can have multiple doCatch clauses within a single
doTry block. The doCatch clauses are tested in the order they appear, just like Java
catch() statements. Apache Camel executes the firstdoCatch clause that matches
the thrown exception.
NOTE
This algorithm is different from the exception matching algorithm used
by the onException clause—see Section 2.3.1, “onException Clause”
for details.
Rethrowing exceptions—you can rethrow the current exception from within a
doCatch clause using the handled sub-clause (see the section called “Rethrowing
exceptions in doCatch”).
Special features of doCatch
There are some special features of the doCatch() clause, however, that have no analogue
in the Java catch() statement. The following features are specific todoCatch():
Catching multiple exceptions—the doCatch clause allows you to specify a list of
exceptions to catch, in contrast to the Java catch() statement, which catches only
one exception (see the section called “Example”).
Conditional catching—you can catch an exception conditionally, by appending an
onWhen sub-clause to the doCatch clause (see the section called “Conditional
exception catching using onWhen”).
Example
The following example shows how to write a doTry block in the Java DSL, where the
doCatch() clause will be executed, if either theIOException exception or the
IllegalStateException exception are raised, and the doFinally() clause is always
executed, irrespective of whether an exception is raised or not.
from("direct:start")
.doTry()
.process(new ProcessorFail())
.to("mock:result")
.doCatch(IOException.class, IllegalStateException.class)
48
CHAPTER 2. BASIC PRINCIPLES OF ROUTE BUILDING
.to("mock:catch")
.doFinally()
.to("mock:finally")
.end();
Or equivalently, in Spring XML:
java.io.IOException
java.lang.IllegalStateException
Rethrowing exceptions in doCatch
It is possible to rethrow an exception in a doCatch() clause by calling the handled() subclause with its argument set to false, as follows:
from("direct:start")
.doTry()
.process(new ProcessorFail())
.to("mock:result")
.doCatch(IOException.class)
// mark this as NOT handled, eg the caller will also get the
exception
.handled(false)
.to("mock:io")
.doCatch(Exception.class)
// and catch all other exceptions
.to("mock:error")
.end();
In the preceding example, if the IOException is caught by doCatch(), the current
exchange is sent to the mock:io endpoint, and then the IOException is rethrown. This
gives the consumer endpoint at the start of the route (in the from() command) an
opportunity to handle the exception as well.
The following example shows how to define the same route in Spring XML:
49
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
java.io.IOException
false
java.lang.Exception
Conditional exception catching using onWhen
A special feature of the Apache Camel doCatch() clause is that you can conditionalize the
catching of exceptions based on an expression that is evaluated at run time. In other
words, if you catch an exception using a clause of the form,
doCatch(ExceptionList).doWhen(Expression), an exception will only be caught, if the
predicate expression, Expression, evaluates to true at run time.
For example, the following doTry block will catch the exceptions,IOException and
IllegalStateException, only if the exception message contains the word,Severe:
from("direct:start")
.doTry()
.process(new ProcessorFail())
.to("mock:result")
.doCatch(IOException.class, IllegalStateException.class)
.onWhen(exceptionMessage().contains("Severe"))
.to("mock:catch")
.doCatch(CamelExchangeException.class)
.to("mock:catchCamel")
.doFinally()
.to("mock:finally")
.end();
Or equivalently, in Spring XML:
java.io.IOException
java.lang.IllegalStateException
50
CHAPTER 2. BASIC PRINCIPLES OF ROUTE BUILDING
${exception.message} contains 'Severe'
org.apache.camel.CamelExchangeException
2.3.4. Propagating SOAP Exceptions
Overview
The Camel CXF component provides an integration with Apache CXF, enabling you to send
and receive SOAP messages from Apache Camel endpoints. You can easily define Apache
Camel endpoints in XML, which can then be referenced in a route using the endpoint's bean
ID. For more details, see CXF.
How to propagate stack trace information
It is possible to configure a CXF endpoint so that, when a Java exception is thrown on the
server side, the stack trace for the exception is marshalled into a fault message and
returned to the client. To enable this feaure, set the dataFormat to PAYLOAD and set the
faultStackTraceEnabled property to true in the cxfEndpoint element, as follows:
For security reasons, the stack trace does not include the causing exception (that is, the
part of a stack trace that follows Caused by). If you want to include the causing exception
in the stack trace, set the exceptionMessageCauseEnabled property to true in the
cxfEndpoint element, as follows:
51
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
WARNING
You should only enable the exceptionMessageCauseEnabled flag for
testing and diagnostic purposes. It is normal practice for servers to
conceal the original cause of an exception to make it harder for hostile
users to probe the server.
2.4. BEAN INTEGRATION
Overview
Bean integration provides a general purpose mechanism for processing messages using
arbitrary Java objects. By inserting a bean reference into a route, you can call an arbitrary
method on a Java object, which can then access and modify the incoming exchange. The
mechanism that maps an exchange's contents to the parameters and return values of a
bean method is known as parameter binding. Parameter binding can use any combination of
the following approaches in order to initialize a method's parameters:
Conventional method signatures — If the method signature conforms to certain
conventions, the parameter binding can use Java reflection to determine what
parameters to pass.
Annotations and dependency injection — For a more flexible binding mechanism,
employ Java annotations to specify what to inject into the method's arguments. This
dependency injection mechanism relies on Spring 2.5 component scanning.
Normally, if you are deploying your Apache Camel application into a Spring
container, the dependency injection mechanism will work automatically.
Explicitly specified parameters — You can specify parameters explicitly (either as
constants or using the Simple language), at the point where the bean is invoked.
Bean registry
Beans are made accessible through a bean registry, which is a service that enables you to
look up beans using either the class name or the bean ID as a key. The way that you create
an entry in the bean registry depends on the underlying framework—for example, plain
Java, Spring, Guice, or Blueprint. Registry entries are usually created implicitly (for
example, when you instantiate a Spring bean in a Spring XML file).
52
CHAPTER 2. BASIC PRINCIPLES OF ROUTE BUILDING
Registry plug-in strategy
Apache Camel implements a plug-in strategy for the bean registry, defining an integration
layer for accessing beans which makes the underlying registry implementation
transparent. Hence, it is possible to integrate Apache Camel applications with a variety of
different bean registries, as shown in Table 2.2, “Registry Plug-Ins”.
Table 2.2. Registry Plug-Ins
Registry Implementation
Camel Component with Registry Plug-In
Spring bean registry
camel-spring
Guice bean registry
camel-guice
Blueprint bean registry
camel-blueprint
OSGi service registry
deployed in OSGi container
Normally, you do not have to worry about configuring bean registries, because the relevant
bean registry is automatically installed for you. For example, if you are using the Spring
framework to define your routes, the Spring ApplicationContextRegistry plug-in is
automatically installed in the current CamelContext instance.
Deployment in an OSGi container is a special case. When an Apache Camel route is
deployed into the OSGi container, the CamelContext automatically sets up a registry chain
for resolving bean instances: the registry chain consists of the OSGi registry, followed by
the Blueprint (or Spring) registry.
Accessing a bean created in Java
To process exchange objects using a Java bean (which is a plain old Java object or POJO),
use the bean() processor, which binds the inbound exchange to a method on the Java
object. For example, to process inbound exchanges using the class, MyBeanProcessor,
define a route like the following:
from("file:data/inbound")
.bean(MyBeanProcessor.class, "processBody")
.to("file:data/outbound");
Where the bean() processor creates an instance of MyBeanProcessor type and invokes the
processBody() method to process inbound exchanges. This approach is adequate if you
only want to access the MyBeanProcessor instance from a single route. However, if you
want to access the same MyBeanProcessor instance from multiple routes, use the variant
of bean() that takes the Object type as its first argument. For example:
MyBeanProcessor myBean = new MyBeanProcessor();
from("file:data/inbound")
.bean(myBean, "processBody")
.to("file:data/outbound");
53
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
from("activemq:inboundData")
.bean(myBean, "processBody")
.to("activemq:outboundData");
Accessing overloaded bean methods
If a bean defines overloaded methods, you can choose which of the overloaded methods to
invoke by specifying the method name along with its parameter types. For example, if the
MyBeanBrocessor class has two overloaded methods, processBody(String) and
processBody(String,String), you can invoke the latter overloaded method as follows:
from("file:data/inbound")
.bean(MyBeanProcessor.class, "processBody(String,String)")
.to("file:data/outbound");
Alternatively, if you want to identify a method by the number of parameters it takes, rather
than specifying the type of each parameter explicitly, you can use the wildcard character,
*. For example, to invoke a method namedprocessBody that takes two parameters,
irrespective of the exact type of the parameters, invoke the bean() processor as follows:
from("file:data/inbound")
.bean(MyBeanProcessor.class, "processBody(*,*)")
.to("file:data/outbound");
When specifying the method, you can use either a simple unqualified type name—for
example, processBody(Exchange)—or a fully qualified type name—for example,
processBody(org.apache.camel.Exchange).
NOTE
In the current implementation, the specified type name must be an exact
match of the parameter type. Type inheritance is not taken into account.
Specify parameters explicitly
You can specify parameter values explicitly, when you call the bean method. The following
simple type values can be passed:
Boolean: true or false.
Numeric: 123, 7, and so on.
String: 'In single quotes' or "In double quotes".
Null object: null.
The following example shows how you can mix explicit parameter values with type
specifiers in the same method invocation:
from("file:data/inbound")
.bean(MyBeanProcessor.class, "processBody(String, 'Sample string value',
true, 7)")
.to("file:data/outbound");
54
CHAPTER 2. BASIC PRINCIPLES OF ROUTE BUILDING
In the preceding example, the value of the first parameter would presumably be
determined by a parameter binding annotation (see the section called “Basic annotations”).
In addition to the simple type values, you can also specify parameter values using the
Simple language (Chapter 27, The Simple Language). This means that the full power of the
Simple language is available when specifying parameter values. For example, to pass the
message body and the value of the title header to a bean method:
from("file:data/inbound")
.bean(MyBeanProcessor.class,
"processBodyAndHeader(${body},${header.title})")
.to("file:data/outbound");
You can also pass the entire header hash map as a parameter. For example, in the
following example, the second method parameter must be declared to be of type
java.util.Map:
from("file:data/inbound")
.bean(MyBeanProcessor.class,
"processBodyAndAllHeaders(${body},${header})")
.to("file:data/outbound");
Basic method signatures
To bind exchanges to a bean method, you can define a method signature that conforms to
certain conventions. In particular, there are two basic conventions for method signatures:
the section called “Method signature for processing message bodies”.
the section called “Method signature for processing exchanges”.
Method signature for processing message bodies
If you want to implement a bean method that accesses or modifies the incoming message
body, you must define a method signature that takes a single String argument and returns
a String value. For example:
// Java
package com.acme;
public class MyBeanProcessor {
public String processBody(String body) {
// Do whatever you like to 'body'...
return newBody;
}
}
Method signature for processing exchanges
For greater flexibility, you can implement a bean method that accesses the incoming
exchange. This enables you to access or modify all headers, bodies, and exchange
properties. For processing exchanges, the method signature takes a single
org.apache.camel.Exchange parameter and returns void. For example:
55
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
// Java
package com.acme;
public class MyBeanProcessor {
public void processExchange(Exchange exchange) {
// Do whatever you like to 'exchange'...
exchange.getIn().setBody("Here is a new message body!");
}
}
Accessing a bean created in Spring XML
Instead of creating a bean instance in Java, you can create an instance using Spring XML. In
fact, this is the only feasible approach if you are defining your routes in XML. To define a
bean in XML, use the standard Spring bean element. The following example shows how to
create an instance of MyBeanProcessor:
...
It is also possible to pass data to the bean's constructor arguments using Spring syntax. For
full details of how to use the Spring bean element, see The IoC Container from the Spring
reference guide.
When you create an object instance using the bean element, you can reference it later
using the bean's ID (the value of the bean element's id attribute). For example, given the
bean element with ID equal tomyBeanId, you can reference the bean in a Java DSL route
using the beanRef() processor, as follows:
from("file:data/inbound").beanRef("myBeanId",
"processBody").to("file:data/outbound");
Where the beanRef() processor invokes the MyBeanProcessor.processBody() method on
the specified bean instance. You can also invoke the bean from within a Spring XML route,
using the Camel schema's bean element. For example:
Parameter binding annotations
The basic parameter bindings described in the section called “Basic method signatures”
might not always be convenient to use. For example, if you have a legacy Java class that
performs some data manipulation, you might want to extract data from an inbound
exchange and map it to the arguments of an existing method signature. For this kind of
parameter binding, Apache Camel provides the following kinds of Java annotation:
56
CHAPTER 2. BASIC PRINCIPLES OF ROUTE BUILDING
the section called “Basic annotations”.
the section called “Expression language annotations”.
the section called “Inherited annotations”.
Basic annotations
Table 2.3, “Basic Bean Annotations” shows the annotations from the org.apache.camel
Java package that you can use to inject message data into the arguments of a bean
method.
Table 2.3. Basic Bean Annotations
Annotation
Meaning
@Attachments
Binds to a list of attachments.
@Body
Binds to an inbound message
body.
@Header
Binds to an inbound message
header.
@Headers
Binds to a java.util.Map of
the inbound message
headers.
@OutHeaders
Binds to a java.util.Map of
the outbound message
headers.
@Property
Binds to a named exchange
property.
@Properties
Binds to a java.util.Map of
the exchange properties.
Parameter?
String name of the header.
String name of the property.
For example, the following class shows you how to use basic annotations to inject message
data into the processExchange() method arguments.
// Java
import org.apache.camel.*;
public class MyBeanProcessor {
public void processExchange(
@Header(name="user") String user,
@Body String body,
Exchange exchange
) {
// Do whatever you like to 'exchange'...
57
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
exchange.getIn().setBody(body + "UserName = " + user);
}
}
Notice how you are able to mix the annotations with the default conventions. As well as
injecting the annotated arguments, the parameter binding also automatically injects the
exchange object into the org.apache.camel.Exchange argument.
Expression language annotations
The expression language annotations provide a powerful mechanism for injecting message
data into a bean method's arguments. Using these annotations, you can invoke an arbitrary
script, written in the scripting language of your choice, to extract data from an inbound
exchange and inject the data into a method argument. Table 2.4, “Expression Language
Annotations” shows the annotations from the org.apache.camel.language package (and
sub-packages, for the non-core annotations) that you can use to inject message data into
the arguments of a bean method.
Table 2.4. Expression Language Annotations
Annotation
Description
@Bean
Injects a Bean expression.
@Constant
Injects a Constant expression
@EL
Injects an EL expression.
@Groovy
Injects a Groovy expression.
@Header
Injects a Header expression.
@JavaScript
Injects a JavaScript expression.
@OGNL
Injects an OGNL expression.
@PHP
Injects a PHP expression.
@Python
Injects a Python expression.
@Ruby
Injects a Ruby expression.
@Simple
Injects a Simple expression.
@XPath
Injects an XPath expression.
@XQuery
Injects an XQuery expression.
For example, the following class shows you how to use the @XPath annotation to extract a
username and a password from the body of an incoming message in XML format:
58
CHAPTER 2. BASIC PRINCIPLES OF ROUTE BUILDING
// Java
import org.apache.camel.language.*;
public class MyBeanProcessor {
public void checkCredentials(
@XPath("/credentials/username/text()") String user,
@XPath("/credentials/password/text()") String pass
) {
// Check the user/pass credentials...
...
}
}
The @Bean annotation is a special case, because it enables you to inject the result of
invoking a registered bean. For example, to inject a correlation ID into a method argument,
you can use the @Bean annotation to invoke an ID generator class, as follows:
// Java
import org.apache.camel.language.*;
public class MyBeanProcessor {
public void processCorrelatedMsg(
@Bean("myCorrIdGenerator") String corrId,
@Body String body
) {
// Check the user/pass credentials...
...
}
}
Where the string, myCorrIdGenerator, is the bean ID of the ID generator instance. The ID
generator class can be instantiated using the spring bean element, as follows:
...
Where the MySimpleIdGenerator class could be defined as follows:
// Java
package com.acme;
public class MyIdGenerator {
private UserManager userManager;
public String generate(
@Header(name = "user") String user,
@Body String payload
) throws Exception {
User user = userManager.lookupUser(user);
String userId = user.getPrimaryId();
String id = userId + generateHashCodeForPayload(payload);
59
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
return id;
}
}
Notice that you can also use annotations in the referenced bean class, MyIdGenerator. The
only restriction on the generate() method signature is that it must return the correct type
to inject into the argument annotated by @Bean. Because the @Bean annotation does not let
you specify a method name, the injection mechanism simply invokes the first method in the
referenced bean that has the matching return type.
NOTE
Some of the language annotations are available in the core component
(@Bean, @Constant, @Simple, and @XPath). For non-core components, however,
you will have to make sure that you load the relevant component. For
example, to use the OGNL script, you must load the camel-ognl component.
Inherited annotations
Parameter binding annotations can be inherited from an interface or from a superclass. For
example, if you define a Java interface with a Header annotation and a Body annotation, as
follows:
// Java
import org.apache.camel.*;
public interface MyBeanProcessorIntf {
void processExchange(
@Header(name="user") String user,
@Body String body,
Exchange exchange
);
}
The overloaded methods defined in the implementation class, MyBeanProcessor, now
inherit the annotations defined in the base interface, as follows:
// Java
import org.apache.camel.*;
public class MyBeanProcessor implements MyBeanProcessorIntf {
public void processExchange(
String user, // Inherits Header annotation
String body, // Inherits Body annotation
Exchange exchange
) {
...
}
}
Interface implementations
The class that implements a Java interface is often protected, private or in package-only
scope. If you try to invoke a method on an implementation class that is restricted in this
60
CHAPTER 2. BASIC PRINCIPLES OF ROUTE BUILDING
way, the bean binding falls back to invoking the corresponding interface method, which is
publicly accessible.
For example, consider the following public BeanIntf interface:
// Java
public interface BeanIntf {
void processBodyAndHeader(String body, String title);
}
Where the BeanIntf interface is implemented by the following protectedBeanIntfImpl
class:
// Java
protected class BeanIntfImpl implements BeanIntf {
void processBodyAndHeader(String body, String title) {
...
}
}
The following bean invocation would fall back to invoking the public
BeanIntf.processBodyAndHeader method:
from("file:data/inbound")
.bean(BeanIntfImpl.class, "processBodyAndHeader(${body},
${header.title})")
.to("file:data/outbound");
Invoking static methods
Bean integration has the capability to invoke static methods without creating an instance of
the associated class. For example, consider the following Java class that defines the static
method, changeSomething():
// Java
...
public final class MyStaticClass {
private MyStaticClass() {
}
public static String changeSomething(String s) {
if ("Hello World".equals(s)) {
return "Bye World";
}
return null;
}
public void doSomething() {
// noop
}
}
You can use bean integration to invoke the static changeSomething method, as follows:
61
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
from("direct:a")
.bean(MyStaticClass.class, "changeSomething")
.to("mock:a");
Note that, although this syntax looks identical to the invocation of an ordinary function,
bean integration exploits Java reflection to identify the method as static and proceeds to
invoke the method without instantiating MyStaticClass.
Invoking an OSGi service
In the special case where a route is deployed into a Red Hat JBoss Fuse container, it is
possible to invoke an OSGi service directly using bean integration. For example, assuming
that one of the bundles in the OSGi container has exported the service,
org.fusesource.example.HelloWorldOsgiService, you could invoke thesayHello
method using the following bean integration code:
from("file:data/inbound")
.bean(org.fusesource.example.HelloWorldOsgiService.class, "sayHello")
.to("file:data/outbound");
You could also invoke the OSGi service from within a Spring or Blueprint XML file, using the
bean component, as follows:
The way this works is that Apache Camel sets up a chain of registries when it is deployed in
the OSGi container. First of all, it looks up the specified class name in the OSGi service
registry; if this lookup fails, it then falls back to the local Spring DM or Blueprint registry.
2.5. CREATING EXCHANGE INSTANCES
Overview
When processing messages with Java code (for example, in a bean class or in a processor
class), it is often necessary to create a fresh exchange instance. If you need to create an
Exchange object, the easiest approach is to invoke the methods of theExchangeBuilder
class, as described here.
ExchangeBuilder class
The fully qualified name of the ExchangeBuilder class is as follows:
org.apache.camel.builder.ExchangeBuilder
The ExchangeBuilder exposes the static method, anExchange, which you can use to start
building an exchange object.
Example
62
CHAPTER 2. BASIC PRINCIPLES OF ROUTE BUILDING
For example, the following code creates a new exchange object containing the message
body string, Hello World!, and with headers containing username and password
credentials:
// Java
import org.apache.camel.Exchange;
import org.apache.camel.builder.ExchangeBuilder;
...
Exchange exch = ExchangeBuilder.anExchange(camelCtx)
.withBody("Hello World!")
.withHeader("username", "jdoe")
.withHeader("password", "pass")
.build();
ExchangeBuilder methods
The ExchangeBuilder class supports the following methods:
ExchangeBuilder anExchange(CamelContext context)
(static method) Initiate building an exchange object.
Exchange build()
Build the exchange.
ExchangeBuilder withBody(Object body)
Set the message body on the exchange (that is, sets the exchange's In message body).
ExchangeBuilder withHeader(String key, Object value)
Set a header on the exchange (that is, sets a header on the exchange's In message).
ExchangeBuilder withPattern(ExchangePattern pattern)
Sets the exchange pattern on the exchange.
ExchangeBuilder withProperty(String key, Object value)
Sets a property on the exchange.
2.6. TRANSFORMING MESSAGE CONTENT
Abstract
Apache Camel supports a variety of approaches to transforming message content. In
addition to a simple native API for modifying message content, Apache Camel supports
integration with several different third-party libraries and transformation standards.
2.6.1. Simple Message Transformations
Overview
63
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
The Java DSL has a built-in API that enables you to perform simple transformations on
incoming and outgoing messages. For example, the rule shown in Example 2.1, “Simple
Transformation of Incoming Messages” appends the text, World!, to the end of the
incoming message body.
Example 2.1. Simple Transformation of Incoming Messages
from("SourceURL").setBody(body().append(" World!")).to("TargetURL");
Where the setBody() command replaces the content of the incoming message's body.
API for simple transformations
You can use the following API classes to perform simple transformations of the message
content in a router rule:
org.apache.camel.model.ProcessorDefinition
org.apache.camel.builder.Builder
org.apache.camel.builder.ValueBuilder
ProcessorDefinition class
The org.apache.camel.model.ProcessorDefinition class defines the DSL commands
you can insert directly into a router rule—for example, the setBody() command in
Example 2.1, “Simple Transformation of Incoming Messages”. Table 2.5, “Transformation
Methods from the ProcessorDefinition Class” shows the ProcessorDefinition methods
that are relevant to transforming message content:
Table 2.5. Transformation Methods from the ProcessorDefinition Class
64
Method
Description
Type convertBodyTo(Class type)
Converts the IN message body to the specified
type.
Type removeFaultHeader(String name)
Adds a processor which removes the header
on the FAULT message.
Type removeHeader(String name)
Adds a processor which removes the header
on the IN message.
Type removeProperty(String name)
Adds a processor which removes the exchange
property.
ExpressionClause> setBody()
Adds a processor which sets the body on the
IN message.
Type setFaultBody(Expression
expression)
Adds a processor which sets the body on the
FAULT message.
CHAPTER 2. BASIC PRINCIPLES OF ROUTE BUILDING
Method
Description
Type setFaultHeader(String name,
Expression expression)
Adds a processor which sets the header on the
FAULT message.
ExpressionClause> setHeader(String name)
Adds a processor which sets the header on the
IN message.
Type setHeader(String name,
Expression expression)
Adds a processor which sets the header on the
IN message.
ExpressionClause> setOutHeader(String name)
Adds a processor which sets the header on the
OUT message.
Type setOutHeader(String name,
Expression expression)
Adds a processor which sets the header on the
OUT message.
ExpressionClause> setProperty(String name)
Adds a processor which sets the exchange
property.
Type setProperty(String name,
Expression expression)
Adds a processor which sets the exchange
property.
ExpressionClause> transform()
Adds a processor which sets the body on the
OUT message.
Type transform(Expression
expression)
Adds a processor which sets the body on the
OUT message.
Builder class
The org.apache.camel.builder.Builder class provides access to message content in
contexts where expressions or predicates are expected. In other words, Builder methods
are typically invoked in the arguments of DSL commands—for example, the body()
command in Example 2.1, “Simple Transformation of Incoming Messages”. Table 2.6,
“Methods from the Builder Class” summarizes the static methods available in theBuilder
class.
Table 2.6. Methods from the Builder Class
Method
Description
static
ValueBuilder body()
Returns a predicate and value builder for the
inbound body on an exchange.
static
ValueBuilder bodyAs(Class
type)
Returns a predicate and value builder for the
inbound message body as a specific type.
65
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
Method
Description
static
ValueBuilder constant(Object
value)
Returns a constant expression.
static
ValueBuilder faultBody()
Returns a predicate and value builder for the
fault body on an exchange.
static
ValueBuilder
faultBodyAs(Class type)
Returns a predicate and value builder for the
fault message body as a specific type.
static
ValueBuilder header(String name)
Returns a predicate and value builder for
headers on an exchange.
static
ValueBuilder outBody()
Returns a predicate and value builder for the
outbound body on an exchange.
static
ValueBuilder outBodyAs(Class
type)
Returns a predicate and value builder for the
outbound message body as a specific type.
static ValueBuilder property(String
name)
Returns a predicate and value builder for
properties on an exchange.
static ValueBuilder
regexReplaceAll(Expression content,
String regex, Expression
replacement)
Returns an expression that replaces all
occurrences of the regular expression with the
given replacement.
static ValueBuilder
regexReplaceAll(Expression content,
String regex, String replacement)
Returns an expression that replaces all
occurrences of the regular expression with the
given replacement.
static ValueBuilder sendTo(String
uri)
Returns an expression processing the
exchange to the given endpoint uri.
static
ValueBuilder
systemProperty(String name)
Returns an expression for the given system
property.
static
ValueBuilder
systemProperty(String name, String
defaultValue)
Returns an expression for the given system
property.
ValueBuilder class
The org.apache.camel.builder.ValueBuilder class enables you to modify values
66
CHAPTER 2. BASIC PRINCIPLES OF ROUTE BUILDING
returned by the Builder methods. In other words, the methods inValueBuilder provide a
simple way of modifying message content. Table 2.7, “Modifier Methods from the
ValueBuilder Class” summarizes the methods available in the ValueBuilder class. That is,
the table shows only the methods that are used to modify the value they are invoked on
(for full details, see the API Reference documentation).
Table 2.7. Modifier Methods from the ValueBuilder Class
Method
Description
ValueBuilder append(Object
value)
Appends the string evaluation of this
expression with the given value.
Predicate contains(Object value)
Create a predicate that the left hand
expression contains the value of the right hand
expression.
ValueBuilder convertTo(Class
type)
Converts the current value to the given type
using the registered type converters.
ValueBuilder convertToString()
Converts the current value a String using the
registered type converters.
Predicate endsWith(Object value)
T evaluate(Exchange exchange,
Class type)
Predicate in(Object... values)
Predicate in(Predicate...
predicates)
Predicate isEqualTo(Object value)
Returns true, if the current value is equal to
the given value argument.
Predicate isGreaterThan(Object
value)
Returns true, if the current value is greater
than the given value argument.
Predicate
isGreaterThanOrEqualTo(Object
value)
Returns true, if the current value is greater
than or equal to the given value argument.
Predicate isInstanceOf(Class type)
Returns true, if the current value is an instance
of the given type.
Predicate isLessThan(Object value)
Returns true, if the current value is less than
the given value argument.
67
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
Method
Description
Predicate isLessThanOrEqualTo(Object
value)
Returns true, if the current value is less than or
equal to the given value argument.
Predicate isNotEqualTo(Object value)
Returns true, if the current value is not equal
to the given value argument.
Predicate isNotNull()
Returns true, if the current value is not null.
Predicate isNull()
Returns true, if the current value is null.
Predicate matches(Expression
expression)
Predicate not(Predicate predicate)
Negates the predicate argument.
ValueBuilder prepend(Object value)
Prepends the string evaluation of this
expression to the given value.
Predicate regex(String regex)
ValueBuilder
regexReplaceAll(String regex,
Expression replacement)
Replaces all occurrencies of the regular
expression with the given replacement.
ValueBuilder
regexReplaceAll(String regex, String
replacement)
Replaces all occurrencies of the regular
expression with the given replacement.
ValueBuilder
regexTokenize(String regex)
Tokenizes the string conversion of this
expression using the given regular expression.
ValueBuilder sort(Comparator
comparator)
Sorts the current value using the given
comparator.
Predicate startsWith(Object value)
Returns true, if the current value matches the
string value of the value argument.
ValueBuilder tokenize()
Tokenizes the string conversion of this
expression using the comma token separator.
ValueBuilder tokenize(String
token)
Tokenizes the string conversion of this
expression using the given token separator.
2.6.2. Marshalling and Unmarshalling
Java DSL commands
68
CHAPTER 2. BASIC PRINCIPLES OF ROUTE BUILDING
You can convert between low-level and high-level message formats using the following
commands:
marshal()— Converts a high-level data format to a low-level data format.
unmarshal() — Converts a low-level data format to a high-level data format.
Data formats
Apache Camel supports marshalling and unmarshalling of the following data formats:
Java serialization
JAXB
XMLBeans
XStream
Java serialization
Enables you to convert a Java object to a blob of binary data. For this data format,
unmarshalling converts a binary blob to a Java object, and marshalling converts a Java
object to a binary blob. For example, to read a serialized Java object from an endpoint,
SourceURL, and convert it to a Java object, you use a rule like the following:
from("SourceURL").unmarshal().serialization()
..to("TargetURL");
Or alternatively, in Spring XML:
JAXB
Provides a mapping between XML schema types and Java types (see
https://jaxb.dev.java.net/). For JAXB, unmarshalling converts an XML data type to a Java
object, and marshalling converts a Java object to an XML data type. Before you can use
JAXB data formats, you must compile your XML schema using a JAXB compiler to generate
the Java classes that represent the XML data types in the schema. This is called binding the
schema. After the schema is bound, you define a rule to unmarshal XML data to a Java
object, using code like the following:
org.apache.camel.spi.DataFormat jaxb = new
org.apache.camel.model.dataformat.JaxbDataFormat("GeneratedPackageName");
69
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
from("SourceURL").unmarshal(jaxb)
..to("TargetURL");
where GeneratedPackagename is the name of the Java package generated by the JAXB
compiler, which contains the Java classes representing your XML schema.
Or alternatively, in Spring XML:
XMLBeans
Provides an alternative mapping between XML schema types and Java types (see
http://xmlbeans.apache.org/). For XMLBeans, unmarshalling converts an XML data type to a
Java object and marshalling converts a Java object to an XML data type. For example, to
unmarshal XML data to a Java object using XMLBeans, you use code like the following:
from("SourceURL").unmarshal().xmlBeans()
..to("TargetURL");
Or alternatively, in Spring XML:
XStream
Provides another mapping between XML types and Java types (see
http://xstream.codehaus.org/). XStream is a serialization library (like Java serialization),
enabling you to convert any Java object to XML. For XStream, unmarshalling converts an
XML data type to a Java object, and marshalling converts a Java object to an XML data type.
from("SourceURL").unmarshal().xstream()
..to("TargetURL");
NOTE
The XStream data format is currently not supported in Spring XML.
70
CHAPTER 2. BASIC PRINCIPLES OF ROUTE BUILDING
2.6.3. Endpoint Bindings
What is a binding?
In Apache Camel, a binding is a way of wrapping an endpoint in a contract—for example, by
applying a Data Format, a Content Enricher or a validation step. A condition or
transformation is applied to the messages coming in, and a complementary condition or
transformation is applied to the messages going out.
DataFormatBinding
The DataFormatBinding class is useful for the specific case where you want to define a
binding that marshals and unmarshals a particular data format (see Section 2.6.2,
“Marshalling and Unmarshalling”). In this case, all that you need to do to create a binding is
to create a DataFormatBinding instance, passing a reference to the relevant data format in
the constructor.
For example, the XML DSL snippet in Example 2.2, “JAXB Binding” shows a binding (with ID,
jaxb) that is capable of marshalling and unmarshalling the JAXB data format when it is
associated with an Apache Camel endpoint:
Example 2.2. JAXB Binding
...
Associating a binding with an endpoint
The following alternatives are available for associating a binding with an endpoint:
the section called “Binding URI”
the section called “BindingComponent”
Binding URI
To associate a binding with an endpoint, you can prefix the endpoint URI with
binding:NameOfBinding, where NameOfBinding is the bean ID of the binding (for example,
the ID of a binding bean created in Spring XML).
71
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
For example, the following example shows how to associate ActiveMQ endpoints with the
JAXB binding defined in Example 2.2, “JAXB Binding”.
...
...
BindingComponent
Instead of using a prefix to associate a binding with an endpoint, you can make the
association implicit, so that the binding does not need to appear in the URI. For existing
endpoints that do not have an implicit binding, the easiest way to achieve this is to wrap
the endpoint using the BindingComponent class.
For example, to associate the jaxb binding with activemq endpoints, you could define a
new BindingComponent instance as follows:
...
Where the (optional) second constructor argument to jaxbmq defines a URI prefix. You can
now use the jaxbmq ID as the scheme for an endpoint URI. For example, you can define the
following route using this binding component:
...
72
CHAPTER 2. BASIC PRINCIPLES OF ROUTE BUILDING
...
The preceding route is equivalent to the following route, which uses the binding URI
approach:
...
...
NOTE
For developers that implement a custom Apache Camel component, it is
possible to achieve this by implementing an endpoint class that inherits from
the org.apache.camel.spi.HasBinding interface.
BindingComponent constructors
The BindingComponent class supports the following constructors:
public BindingComponent()
No arguments form. Use property injection to configure the binding component instance.
public BindingComponent(Binding binding)
Associate this binding component with the specified Binding object, binding.
public BindingComponent(Binding binding, String uriPrefix)
Associate this binding component with the specified Binding object, binding, and URI
prefix, uriPrefix. This is the most commonly used constructor.
public BindingComponent(Binding binding, String uriPrefix, String uriPostfix)
This constructor supports the additional URI post-fix, uriPostfix, argument, which is
automatically appended to any URIs defined using this binding component.
Implementing a custom binding
In addition to the DataFormatBinding, which is used for marshalling and unmarshalling
data formats, you can implement your own custom bindings. Define a custom binding as
follows:
1. Implement an org.apache.camel.Processor class to perform a transformation on
messages incoming to a consumer endpoint (appearing in a from element).
73
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
2. Implement a complementary org.apache.camel.Processor class to perform the
reverse transformation on messages outgoing from a producer endpoint (appearing
in a to element).
3. Implement the org.apache.camel.spi.Binding interface, which acts as a factory
for the processor instances.
Binding interface
Example 2.3, “The org.apache.camel.spi.Binding Interface” shows the definition of the
org.apache.camel.spi.Binding interface, which you must implement to define a custom
binding.
Example 2.3. The org.apache.camel.spi.Binding Interface
// Java
package org.apache.camel.spi;
import org.apache.camel.Processor;
/**
* Represents a Binding or contract
* which can be applied to an Endpoint; such as ensuring that a
particular
* Data Format is
used on messages in and out of an endpoint.
*/
public interface Binding {
/**
* Returns a new {@link Processor} which is used by a producer on an
endpoint to implement
* the producer side binding before the message is sent to the
underlying endpoint.
*/
Processor createProduceProcessor();
/**
* Returns a new {@link Processor} which is used by a consumer on an
endpoint to process the
* message with the binding before its passed to the endpoint
consumer producer.
*/
Processor createConsumeProcessor();
}
When to use bindings
Bindings are useful when you need to apply the same kind of transformation to many
different kinds of endpoint.
2.7. PROPERTY PLACEHOLDERS
74
CHAPTER 2. BASIC PRINCIPLES OF ROUTE BUILDING
Overview
The property placeholders feature can be used to substitute strings into various contexts
(such as endpoint URIs and attributes in XML DSL elements), where the placeholder settings
are stored in Java properties files. This feature can be useful, if you want to share settings
between different Apache Camel applications or if you want to centralize certain
configuration settings.
For example, the following route sends requests to a Web server, whose host and port are
substituted by the placeholders, {{remote.host}} and {{remote.port}}:
from("direct:start").to("http://{{remote.host}}:{{remote.port}}");
The placeholder values are defined in a Java properties file, as follows:
# Java properties file
remote.host=myserver.com
remote.port=8080
Property files
Property settings are stored in one or more Java properties files and must conform to the
standard Java properties file format. Each property setting appears on its own line, in the
format Key=Value. Lines with # or ! as the first non-blank character are treated as
comments.
For example, a property file could have content as shown in Example 2.4, “Sample Property
File”.
Example 2.4. Sample Property File
# Property placeholder settings
# (in Java properties file format)
cool.end=mock:result
cool.result=result
cool.concat=mock:{{cool.result}}
cool.start=direct:cool
cool.showid=true
cheese.end=mock:cheese
cheese.quote=Camel rocks
cheese.type=Gouda
bean.foo=foo
bean.bar=bar
Resolving properties
The properties component must be configured with the locations of one or more property
files before you can start using it in route definitions. You must provide the property values
using one of the following resolvers:
classpath:PathName,PathName,...
75
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
(Default) Specifies locations on the classpath, wherePathName is a file pathname
delimited using forward slashes.
file:PathName,PathName,...
Specifies locations on the file system, where PathName is a file pathname delimited
using forward slashes.
ref:BeanID
Specifies the ID of a java.util.Properties object in the registry.
blueprint:BeanID
Specifies the ID of a cm:property-placeholder bean, which is used in the context of an
OSGi Blueprint file to access properties defined in the OSGi Configuration Admin service.
For details, see the section called “Integration with OSGi Blueprint property
placeholders”.
For example, to specify the com/fusesource/cheese.properties property file and the
com/fusesource/bar.properties property file, both located on the classpath, you would
use the following location string:
com/fusesource/cheese.properties,com/fusesource/bar.properties
NOTE
You can omit the classpath: prefix in this example, because the classpath
resolver is used by default.
Specifying locations using system properties and environment
variables
You can embed Java system properties and O/S environment variables in a location
PathName.
Java system properties can be embedded in a location resolver using the syntax,
${PropertyName}. For example, if the root directory of Red Hat JBoss Fuse is stored in the
Java system property, karaf.home, you could embed that directory value in a file location,
as follows:
file:${karaf.home}/etc/foo.properties
O/S environment variables can be embedded in a location resolver using the syntax,
${env:VarName}. For example, if the root directory of JBoss Fuse is stored in the
environment variable, SMX_HOME, you could embed that directory value in a file location, as
follows:
file:${env:SMX_HOME}/etc/foo.properties
Configuring the properties component
Before you can start using property placeholders, you must configure the properties
component, specifying the locations of one or more property files.
76
CHAPTER 2. BASIC PRINCIPLES OF ROUTE BUILDING
In the Java DSL, you can configure the properties component with the property file
locations, as follows:
// Java
import org.apache.camel.component.properties.PropertiesComponent;
...
PropertiesComponent pc = new PropertiesComponent();
pc.setLocation("com/fusesource/cheese.properties,com/fusesource/bar.proper
ties");
context.addComponent("properties", pc);
As shown in the addComponent() call, the name of the properties componentmust be set
to properties.
In the XML DSL, you can configure the properties component using the dedicated
propertyPlacholder element, as follows:
If you want the properties component to ignore any missing .properties files when it is
being initialized, you can set the ignoreMissingLocation option to true (normally, a
missing .properties file would result in an error being raised).
Placeholder syntax
After it is configured, the property component automatically substitutes placeholders (in
the appropriate contexts). The syntax of a placeholder depends on the context, as follows:
In endpoint URIs and in Spring XML files—the placeholder is specified as {{Key}}.
When setting XML DSL attributes—xs:string attributes are set using the following
syntax:
AttributeName="{{Key}}"
Other attribute types (for example, xs:int or xs:boolean) must be set using the
following syntax:
prop:AttributeName="Key"
Where prop is associated with the http://camel.apache.org/schema/placeholder
namespace.
When setting Java DSL EIP options—to set an option on an Enterprise Integration
Pattern (EIP) command in the Java DSL, add a placeholder() clause like the
following to the fluent DSL:
.placeholder("OptionName", "Key")
77
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
In Simple language expressions—the placeholder is specified as
${properties:Key}.
Substitution in endpoint URIs
Wherever an endpoint URI string appears in a route, the first step in parsing the endpoint
URI is to apply the property placeholder parser. The placeholder parser automatically
substitutes any property names appearing between double braces, {{Key}}. For example,
given the property settings shown in Example 2.4, “Sample Property File”, you could define
a route as follows:
from("{{cool.start}}")
.to("log:{{cool.start}}?showBodyType=false&showExchangeId=
{{cool.showid}}")
.to("mock:{{cool.result}}");
By default, the placeholder parser looks up the properties bean ID in the registry to find
the property component. If you prefer, you can explicitly specify the scheme in the
endpoint URIs. For example, by prefixing properties: to each of the endpoint URIs, you
can define the following equivalent route:
from("properties:{{cool.start}}")
.to("properties:log:{{cool.start}}?showBodyType=false&showExchangeId=
{{cool.showid}}")
.to("properties:mock:{{cool.result}}");
When specifying the scheme explicitly, you also have the option of specifying options to the
properties component. For example, to override the property file location, you could set the
location option as follows:
from("direct:start").to("properties:{{bar.end}}?
location=com/mycompany/bar.properties");
Substitution in Spring XML files
You can also use property placeholders in the XML DSL, for setting various attributes of the
DSL elements. In this context, the placholder syntax also uses double braces, {{Key}}. For
example, you could define a jmxAgent element using property placeholders, as follows:
78
CHAPTER 2. BASIC PRINCIPLES OF ROUTE BUILDING
Substitution of XML DSL attribute values
You can use the regular placeholder syntax for specifying attribute values of xs:string
type—for example, . But for attributes
of any other type (for example, xs:int or xs:boolean), you must use the special syntax,
prop:AttributeName="Key".
For example, given that a property file defines the stop.flag property to have the value,
true, you can use this property to set thestopOnException boolean attribute, as follows:
IMPORTANT
The prop prefix must be explicitly assigned to the
http://camel.apache.org/schema/placeholder namespace in your Spring
file, as shown in the beans element of the preceding example.
Substitution of Java DSL EIP options
79
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
When invoking an EIP command in the Java DSL, you can set any EIP option using the value
of a property placeholder, by adding a sub-clause of the form,
placeholder("OptionName", "Key").
For example, given that a property file defines the stop.flag property to have the value,
true, you can use this property to set thestopOnException option of the multicast EIP, as
follows:
from("direct:start")
.multicast().placeholder("stopOnException", "stop.flag")
.to("mock:a").throwException(new
IllegalAccessException("Damn")).to("mock:b");
Substitution in Simple language expressions
You can also substitute property placeholders in Simple language expressions, but in this
case the syntax of the placeholder is ${properties:Key}. For example, you can substitute
the cheese.quote placehoder inside a Simple expression, as follows:
from("direct:start")
.transform().simple("Hi ${body} do you think
${properties:cheese.quote}?");
It is also possible to override the location of the property file using the syntax,
${properties:Location:Key}. For example, to substitute the bar.quote placeholder
using the settings from the com/mycompany/bar.properties property file, you can define a
Simple expression as follows:
from("direct:start")
.transform().simple("Hi ${body}.
${properties:com/mycompany/bar.properties:bar.quote}.");
Integration with OSGi Blueprint property placeholders
If you deploy your route into the Red Hat JBoss Fuse OSGi container, you can integrate the
Apache Camel property placeholder mechanism with JBoss Fuse's Blueprint property
placeholder mechanism (in fact, the integration is enabled by default). There are two basic
approaches to setting up the integration, as follows:
the section called “Implicit Blueprint integration”.
the section called “Explicit Blueprint integration”.
Implicit Blueprint integration
If you define a camelContext element inside an OSGi Blueprint file, the Apache Camel
property placeholder mechanism automatically integrates with the Blueprint property
placeholder mechanism. That is, placeholders obeying the Apache Camel syntax (for
example, {{cool.end}}) that appear within the scope ofcamelContext are implicitly
resolved by looking up the Blueprint property placeholder mechanism.
For example, consider the following route defined in an OSGi Blueprint file, where the last
endpoint in the route is defined by the property placeholder, {{result}}:
80
CHAPTER 2. BASIC PRINCIPLES OF ROUTE BUILDING
The Blueprint property placeholder mechanism is initialized by creating a cm:propertyplaceholder bean. In the preceding example, thecm:property-placeholder bean is
associated with the camel.blueprint persistent ID, where a persistent ID is the standard
way of referencing a group of related properties from the OSGi Configuration Adminn
service. In other words, the cm:property-placeholder bean provides access to all of the
properties defined under the camel.blueprint persistent ID. It is also possible to specify
default values for some of the properties (using the nested cm:property elements).
In the context of Blueprint, the Apache Camel placeholder mechanism searches for an
instance of cm:property-placeholder in the bean registry. If it finds such an instance, it
automatically integrates the Apache Camel placeholder mechanism, so that placeholders
like, {{result}}, are resolved by looking up the key in the Blueprint property placeholder
mechanism (in this example, through the myblueprint.placeholder bean).
NOTE
The default Blueprint placeholder syntax (accessing the Blueprint properties
directly) is ${Key}. Hence, outside the scope of a camelContext element, the
placeholder syntax you must use is ${Key}. Whereas, inside the scope of a
camelContext element, the placeholder syntax you must use is{{Key}}.
Explicit Blueprint integration
81
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
If you want to have more control over where the Apache Camel property placeholder
mechanism finds its properties, you can define a propertyPlaceholder element and
specify the resolver locations explicitly.
For example, consider the following Blueprint configuration, which differs from the previous
example in that it creates an explicit propertyPlaceholder instance:
In the preceding example, the propertyPlaceholder element specifies explicitly which
cm:property-placeholder bean to use by setting the location to
blueprint:myblueprint.placeholder. That is, the blueprint: resolver explicitly
references the ID, myblueprint.placeholder, of the cm:property-placeholder bean.
This style of configuration is useful, if there is more than one cm:property-placeholder
bean defined in the Blueprint file and you need to specify which one to use. It also makes it
possible to source properties from multiple locations, by specifying a comma-separated list
of locations. For example, if you wanted to look up properties both from the cm:propertyplaceholder bean and from the properties file,myproperties.properties, on the
classpath, you could define the propertyPlaceholder element as follows:
Integration with Spring property placeholders
If you define your Apache Camel application using XML DSL in a Spring XML file, you can
integrate the Apache Camel property placeholder mechanism with Spring property
placeholder mechanism by declaring a Spring bean of type,
org.apache.camel.spring.spi.BridgePropertyPlaceholderConfigurer.
Define a BridgePropertyPlaceholderConfigurer, which replaces both Apache Camel's
propertyPlaceholder element and Spring's ctx:property-placeholder element in the
Spring XML file. You can then refer to the configured properties using either the Spring
${PropName} syntax or the Apache Camel {{PropName}} syntax.
For example, defining a bridge property placeholder that reads its property settings from
the cheese.properties file:
83
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
NOTE
Alternatively, you can set the location attribute of the
BridgePropertyPlaceholderConfigurer to point at a Spring properties file.
The Spring properties file syntax is fully supported.
2.8. ASPECT ORIENTED PROGRAMMING
Overview
The aspect oriented programming (AOP) feature in Apache Camel enables you to apply
before and after processing to a specified portion of a route. As a matter of fact, AOP does
not provide anything that you could not do with the regular route syntax. The advantage of
the AOP syntax, however, is that it enables you to specify before and after processing at a
single point in the route. In some cases, this gives a more readable syntax. The typical use
case for AOP is the application of a symmetrical pair of operations before and after a route
fragment is processed. For example, typical pairs of operations that you might want to
apply using AOP are: encrypt and decrypt; begin transaction and commit transaction;
allocate resources and deallocate resources; and so on.
Java DSL example
In Java DSL, the route fragment to which you apply before and after processing is
bracketed between aop() and end(). For example, the following route performs AOP
processing around the route fragment that calls the bean methods:
from("jms:queue:inbox")
.aop().around("log:before", "log:after")
.to("bean:order?method=validate")
.to("bean:order?method=handle")
.end()
.to("jms:queue:order");
Where the around() subclause specifies an endpoint, log:before, where the exchange is
routed before processing the route fragment and an endpoint,log:after, where the
exchange is routed after processing the route fragment.
AOP options in the Java DSL
Starting an AOP block with aop().around() is probably the most common use case, but
the AOP block supports other subclauses, as follows:
around()—specifies before and after endpoints.
begin()—specifies before endpoint only.
after()—specifies after endpoint only.
aroundFinally()—specifies a before endpoint, and an after endpoint that is always
called, even when an exception occurs in the enclosed route fragment.
84
CHAPTER 2. BASIC PRINCIPLES OF ROUTE BUILDING
afterFinally()—specifies an after endpoint that is always called, even when an
exception occurs in the enclosed route fragment.
Spring XML example
In the XML DSL, the route fragment to which you apply before and after processing is
enclosed in the aop element. For example, the following Spring XML route performs AOP
processing around the route fragment that calls the bean methods:
Where the beforeUri attribute specifies the endpoint where the exchange is routedbefore
processing the route fragment, and the afterUri attribute specifies the endpoint where the
exchange is routed after processing the route fragment.
AOP options in the Spring XML
The aop element supports the following optional attributes:
beforeUri
afterUri
afterFinallyUri
The various use cases described for the Java DSL can be obtained in Spring XML using the
appropriate combinations of these attributes. For example, the aroundFinally() Java DSL
subclause is equivalent to the combination of beforeUri and afterFinallyUri in Spring
XML.
2.9. THREADING MODEL
Java thread pool API
The Apache Camel threading model is based on the powerful Java concurrency API,
java.util.concurrent, that first became available in Sun's JDK 1.5. The key interface in this
API is the ExecutorService interface, which represents a thread pool. Using the
concurrency API, you can create many different kinds of thread pool, covering a wide range
of scenarios.
Apache Camel thread pool API
The Apache Camel thread pool API builds on the Java concurrency API by providing a central
factory (of org.apache.camel.spi.ExecutorServiceManager type) for all of the thread
pools in your Apache Camel application. Centralising the creation of thread pools in this
way provides several advantages, including:
85
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
Simplified creation of thread pools, using utility classes.
Integrating thread pools with graceful shutdown.
Threads automatically given informative names, which is beneficial for logging and
management.
Component threading model
Some Apache Camel components—such as SEDA, JMS, and Jetty—are inherently multithreaded. These components have all been implemented using the Apache Camel threading
model and thread pool API.
If you are planning to implement your own Apache Camel component, it is recommended
that you integrate your threading code with the Apache Camel threading model. For
example, if your component needs a thread pool, it is recommended that you create it
using the CamelContext's ExecutorServiceManager object.
Processor threading model
Some of the standard processors in Apache Camel create their own thread pool by default.
These threading-aware processors are also integrated with the Apache Camel threading
model and they provide various options that enable you to customize customize the thread
pools that they use.
Table 2.8, “Processor Threading Options” shows the various options for controlling and
setting thread pools on the threading-aware processors built-in to Apache Camel.
Table 2.8. Processor Threading Options
Processor
Java DSL
XML DSL
aggregate
parallelProcessing()
executorService()
executorServiceRef()
@parallelProcessing
@executorServiceRef
parallelProcessing()
executorService()
executorServiceRef()
@parallelProcessing
@executorServiceRef
parallelProcessing()
executorService()
executorServiceRef()
@parallelProcessing
@executorServiceRef
parallelProcessing()
executorService()
executorServiceRef()
@parallelProcessing
@executorServiceRef
multicast
recipientList
split
86
CHAPTER 2. BASIC PRINCIPLES OF ROUTE BUILDING
Processor
Java DSL
XML DSL
threads
executorService()
executorServiceRef()
poolSize()
maxPoolSize()
keepAliveTime()
timeUnit()
maxQueueSize()
rejectedPolicy()
@executorServiceRef
@poolSize
@maxPoolSize
@keepAliveTime
@timeUnit
@maxQueueSize
@rejectedPolicy
wireTap(String uri,
ExecutorService
executorService)
wireTap(String uri,
String
executorServiceRef)
@executorServiceRef
wireTap
Creating a default thread pool
To create a default thread pool for one of the threading-aware processors, enable the
parallelProcessing option, using the parallelProcessing() sub-clause, in the Java DSL,
or the parallelProcessing attribute, in the XML DSL.
For example, in the Java DSL, you can invoke the multicast processor with a default thread
pool (where the thread pool is used to process the multicast destinations concurrently) as
follows:
from("direct:start")
.multicast().parallelProcessing()
.to("mock:first")
.to("mock:second")
.to("mock:third");
You can define the same route in XML DSL as follows
Default thread pool profile settings
87
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
The default thread pools are automatically created by a thread factory that takes its
settings from the default thread pool profile. The default thread pool profile has the settings
shown in Table 2.9, “Default Thread Pool Profile Settings” (assuming that these settings
have not been modified by the application code).
Table 2.9. Default Thread Pool Profile Settings
Thread Option
Default Value
maxQueueSize
1000
poolSize
10
maxPoolSize
20
keepAliveTime
60 (seconds)
rejectedPolicy
CallerRuns
Changing the default thread pool profile
It is possible to change the default thread pool profile settings, so that all subsequent
default thread pools will be created with the custom settings. You can change the profile
either in Java or in Spring XML.
For example, in the Java DSL, you can customize the poolSize option and the
maxQueueSize option in the default thread pool profile, as follows:
// Java
import org.apache.camel.spi.ExecutorServiceManager;
import org.apache.camel.spi.ThreadPoolProfile;
...
ExecutorServiceManager manager = context.getExecutorServiceManager();
ThreadPoolProfile defaultProfile = manager.getDefaultThreadPoolProfile();
// Now, customize the profile settings.
defaultProfile.setPoolSize(3);
defaultProfile.setMaxQueueSize(100);
...
In the XML DSL, you can customize the default thread pool profile, as follows:
...
Note that it is essential to set the defaultProfile attribute to true in the preceding XML
DSL example, otherwise the thread pool profile would be treated like a custom thread pool
88
CHAPTER 2. BASIC PRINCIPLES OF ROUTE BUILDING
profile (see the section called “Creating a custom thread pool profile”), instead of replacing
the default thread pool profile.
Customizing a processor's thread pool
It is also possible to specify the thread pool for a threading-aware processor more directly,
using either the executorService or executorServiceRef options (where these options
are used instead of the parallelProcessing option). There are two approaches you can
use to customize a processor's thread pool, as follows:
Specify a custom thread pool—explicitly create an ExecutorService (thread pool)
instance and pass it to the executorService option.
Specify a custom thread pool profile—create and register a custom thread pool
factory. When you reference this factory using the executorServiceRef option, the
processor automatically uses the factory to create a custom thread pool instance.
When you pass a bean ID to the executorServiceRef option, the threading-aware
processor first tries to find a custom thread pool with that ID in the registry. If no thread
pool is registered with that ID, the processor then attempts to look up a custom thread pool
profile in the registry and uses the custom thread pool profile to instantiate a custom
thread pool.
Creating a custom thread pool
A custom thread pool can be any thread pool of java.util.concurrent.ExecutorService type.
The following approaches to creating a thread pool instance are recommended in Apache
Camel:
Use the org.apache.camel.builder.ThreadPoolBuilder utility to build the thread
pool class.
Use the org.apache.camel.spi.ExecutorServiceManager instance from the
current CamelContext to create the thread pool class.
Ultimately, there is not much difference between the two approaches, because the
ThreadPoolBuilder is actually defined using the ExecutorServiceManager instance.
Normally, the ThreadPoolBuilder is preferred, because it offers a simpler approach. But
there is at least one kind of thread (the ScheduledExecutorService) that can only be
created by accessing the ExecutorServiceManager instance directory.
Table 2.10, “Thread Pool Builder Options” shows the options supported by the
ThreadPoolBuilder class, which you can set when defining a new custom thread pool.
Table 2.10. Thread Pool Builder Options
Builder Option
Description
maxQueueSize()
Sets the maximum number of pending tasks
that this thread pool can store in its incoming
task queue. A value of -1 specifies an
unbounded queue. Default value is taken from
default thread pool profile.
89
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
Builder Option
Description
poolSize()
Sets the minimum number of threads in the
pool (this is also the initial pool size). Default
value is taken from default thread pool profile.
maxPoolSize()
Sets the maximum number of threads that can
be in the pool. Default value is taken from
default thread pool profile.
keepAliveTime()
If any threads are idle for longer than this
period of time (specified in seconds), they are
terminated. This allows the thread pool to
shrink when the load is light. Default value is
taken from default thread pool profile.
rejectedPolicy()
Specifies what course of action to take, if the
incoming task queue is full. You can specify
four possible values:
CallerRuns
(Default value) Gets the caller thread to run
the latest incoming task. As a side effect,
this option prevents the caller thread from
receiving any more tasks until it has
finished processing the latest incoming
task.
Abort
Aborts the latest incoming task by throwing
an exception.
Discard
Quietly discards the latest incoming task.
DiscardOldest
Discards the oldest unhandled task and
then attempts to enqueue the latest
incoming task in the task queue.
build()
Finishes building the custom thread pool and
registers the new thread pool under the ID
specified as the argument to build().
In Java DSL, you can define a custom thread pool using the ThreadPoolBuilder, as follows:
// Java
import org.apache.camel.builder.ThreadPoolBuilder;
import java.util.concurrent.ExecutorService;
...
ThreadPoolBuilder poolBuilder = new ThreadPoolBuilder(context);
ExecutorService customPool =
90
CHAPTER 2. BASIC PRINCIPLES OF ROUTE BUILDING
poolBuilder.poolSize(5).maxPoolSize(5).maxQueueSize(100).build("customPool
");
...
from("direct:start")
.multicast().executorService(customPool)
.to("mock:first")
.to("mock:second")
.to("mock:third");
Instead of passing the object reference, customPool, directly to the executorService()
option, you can look up the thread pool in the registry, by passing its bean ID to the
executorServiceRef() option, as follows:
// Java
from("direct:start")
.multicast().executorServiceRef("customPool")
.to("mock:first")
.to("mock:second")
.to("mock:third");
In XML DSL, you access the ThreadPoolBuilder using the threadPool element. You can
then reference the custom thread pool using the executorServiceRef attribute to look up
the thread pool by ID in the Spring registry, as follows:
Creating a custom thread pool profile
If you have many custom thread pool instances to create, you might find it more
convenient to define a custom thread pool profile, which acts as a factory for thread pools.
Whenever you reference a thread pool profile from a threading-aware processor, the
processor automatically uses the profile to create a new thread pool instance. You can
define a custom thread pool profile either in Java DSL or in XML DSL.
For example, in Java DSL you can create a custom thread pool profile with the bean ID,
customProfile, and reference it from within a route, as follows:
// Java
import org.apache.camel.spi.ThreadPoolProfile;
91
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
import org.apache.camel.impl.ThreadPoolProfileSupport;
...
// Create the custom thread pool profile
ThreadPoolProfile customProfile = new
ThreadPoolProfileSupport("customProfile");
customProfile.setPoolSize(5);
customProfile.setMaxPoolSize(5);
customProfile.setMaxQueueSize(100);
context.getExecutorServiceManager().registerThreadPoolProfile(customProfil
e);
...
// Reference the custom thread pool profile in a route
from("direct:start")
.multicast().executorServiceRef("customProfile")
.to("mock:first")
.to("mock:second")
.to("mock:third");
In XML DSL, use the threadPoolProfile element to create a custom pool profile (where
you let the defaultProfile option default to false, because this is not a default thread
pool profile). You can create a custom thread pool profile with the bean ID, customProfile,
and reference it from within a route, as follows:
Sharing a thread pool between components
Some of the standard poll-based components—such as File and FTP—allow you to specify
the thread pool to use. This makes it possible for different components to share the same
thread pool, reducing the overall number of threads in the JVM.
For example, the File component and the FTP component both expose the
scheduledExecutorService property, which you can use to specify the component's
ExecutorService object.
Customizing thread names
To make the application logs more readable, it is often a good idea to customize the thread
names (which are used to identify threads in the log). To customize thread names, you can
configure the thread name pattern by calling the setThreadNamePattern method on the
92
CHAPTER 2. BASIC PRINCIPLES OF ROUTE BUILDING
ExecutorServiceStrategy class or the ExecutorServiceManager class. Alternatively, an
easier way to set the thread name pattern is to set the threadNamePattern property on
the CamelContext object.
The following placeholders can be used in a thread name pattern:
#camelId#
The name of the current CamelContext.
#counter#
A unique thread identifier, implemented as an incrementing counter.
#name#
The regular Camel thread name.
#longName#
The long thread name—which can include endpoint parameters and so on.
The following is a typical example of a thread name pattern:
Camel (#camelId#) thread #counter# - #name#
The following example shows how to set the threadNamePattern attribute on a Camel
context using XML DSL:
2.10. CONTROLLING START-UP AND SHUTDOWN OF ROUTES
Overview
By default, routes are automatically started when your Apache Camel application (as
represented by the CamelContext instance) starts up and routes are automatically shut
down when your Apache Camel application shuts down. For non-critical deployments, the
details of the shutdown sequence are usually not very important. But in a production
environment, it is often crucial that existing tasks should run to completion during
shutdown, in order to avoid data loss. You typically also want to control the order in which
routes shut down, so that dependencies are not violated (which would prevent existing
tasks from running to completion).
For this reason, Apache Camel provides a set of features to support graceful shutdown of
applications. Graceful shutdown gives you full control over the stopping and starting of
routes, enabling you to control the shutdown order of routes and enabling current tasks to
run to completion.
93
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
Setting the route ID
It is good practice to assign a route ID to each of your routes. As well as making logging
messages and management features more informative, the use of route IDs enables you to
apply greater control over the stopping and starting of routes.
For example, in the Java DSL, you can assign the route ID, myCustomerRouteId, to a route
by invoking the routeId() command as follows:
from("SourceURI").routeId("myCustomRouteId").process(...).to(TargetURI);
In the XML DSL, set the route element's id attribute, as follows:
Disabling automatic start-up of routes
By default, all of the routes that the CamelContext knows about at start time will be started
automatically. If you want to control the start-up of a particular route manually, however,
you might prefer to disable automatic start-up for that route.
To control whether a Java DSL route starts up automatically, invoke the autoStartup
command, either with a boolean argument (true or false) or a String argument (true or
false). For example, you can disable automatic start-up of a route in the Java DSL, as
follows:
from("SourceURI")
.routeId("nonAuto")
.autoStartup(false)
.to(TargetURI);
You can disable automatic start-up of a route in the XML DSL by setting the autoStartup
attribute to false on the route element, as follows:
Manually starting and stopping routes
You can manually start or stop a route at any time in Java by invoking the startRoute()
and stopRoute() methods on the CamelContext instance. For example, to start the route
having the route ID, nonAuto, invoke the startRoute() method on the CamelContext
94
CHAPTER 2. BASIC PRINCIPLES OF ROUTE BUILDING
instance, context, as follows:
// Java
context.startRoute("nonAuto");
To stop the route having the route ID, nonAuto, invoke the stopRoute() method on the
CamelContext instance, context, as follows:
// Java
context.stopRoute("nonAuto");
Startup order of routes
By default, Apache Camel starts up routes in a non-deterministic order. In some
applications, however, it can be important to control the startup order. To control the
startup order in the Java DSL, use the startupOrder() command, which takes a positive
integer value as its argument. The route with the lowest integer value starts first, followed
by the routes with successively higher startup order values.
For example, the first two routes in the following example are linked together through the
seda:buffer endpoint. You can ensure that the first route segment startsafter the second
route segment by assigning startup orders (2 and 1 respectively), as follows:
Example 2.5. Startup Order in Java DSL
from("jetty:http://fooserver:8080")
.routeId("first")
.startupOrder(2)
.to("seda:buffer");
from("seda:buffer")
.routeId("second")
.startupOrder(1)
.to("mock:result");
// This route's startup order is unspecified
from("jms:queue:foo").to("jms:queue:bar");
Or in Spring XML, you can achieve the same effect by setting the route element's
startupOrder attribute, as follows:
Example 2.6. Startup Order in XML DSL
95
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
Each route must be assigned a unique startup order value. You can choose any positive
integer value that is less than 1000. Values of 1000 and over are reserved for Apache
Camel, which automatically assigns these values to routes without an explicit startup value.
For example, the last route in the preceding example would automatically be assigned the
startup value, 1000 (so it starts up after the first two routes).
Shutdown sequence
When a CamelContext instance is shutting down, Apache Camel controls the shutdown
sequence using a pluggable shutdown strategy. The default shutdown strategy implements
the following shutdown sequence:
1. Routes are shut down in the reverse of the start-up order.
2. Normally, the shutdown strategy waits until the currently active exchanges have
finshed processing. The treatment of running tasks is configurable, however.
3. Overall, the shutdown sequence is bound by a timeout (default, 300 seconds). If the
shutdown sequence exceeds this timeout, the shutdown strategy will force
shutdown to occur, even if some tasks are still running.
Shutdown order of routes
Routes are shut down in the reverse of the start-up order. That is, when a start-up order is
defined using the startupOrder() command (in Java DSL) orstartupOrder attribute (in
XML DSL), the first route to shut down is the route with the highest integer value assigned
by the start-up order and the last route to shut down is the route with the lowest integer
value assigned by the start-up order.
For example, in Example 2.5, “Startup Order in Java DSL”, the first route segment to be
shut down is the route with the ID, first, and the second route segment to be shut down is
the route with the ID, second. This example illustrates a general rule, which you should
observe when shutting down routes: the routes that expose externally-accessible consumer
endpoints should be shut down first, because this helps to throttle the flow of messages
through the rest of the route graph.
NOTE
Apache Camel also provides the option shutdownRoute(Defer), which enables
you to specify that a route must be amongst the last routes to shut down
(overriding the start-up order value). But you should rarely ever need this
option. This option was mainly needed as a workaround for earlier versions of
Apache Camel (prior to 2.3), for which routes would shut down in the same
order as the start-up order.
Shutting down running tasks in a route
If a route is still processing messages when the shutdown starts, the shutdown strategy
96
CHAPTER 2. BASIC PRINCIPLES OF ROUTE BUILDING
normally waits until the currently active exchange has finished processing before shutting
down the route. This behavior can be configured on each route using the
shutdownRunningTask option, which can take either of the following values:
ShutdownRunningTask.CompleteCurrentTaskOnly
(Default) Usually, a route operates on just a single message at a time, so you can safely
shut down the route after the current task has completed.
ShutdownRunningTask.CompleteAllTasks
Specify this option in order to shut down batch consumers gracefully. Some consumer
endpoints (for example, File, FTP, Mail, iBATIS, and JPA) operate on a batch of messages
at a time. For these endpoints, it is more appropriate to wait until all of the messages in
the current batch have completed.
For example, to shut down a File consumer endpoint gracefully, you should specify the
CompleteAllTasks option, as shown in the following Java DSL fragment:
// Java
public void configure() throws Exception {
from("file:target/pending")
.routeId("first").startupOrder(2)
.shutdownRunningTask(ShutdownRunningTask.CompleteAllTasks)
.delay(1000).to("seda:foo");
from("seda:foo")
.routeId("second").startupOrder(1)
.to("mock:bar");
}
The same route can be defined in the XML DSL as follows:
1000
Shutdown timeout
The shutdown timeout has a default value of 300 seconds. You can change the value of the
timeout by invoking the setTimeout() method on the shutdown strategy. For example, you
can change the timeout value to 600 seconds, as follows:
97
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
// Java
// context = CamelContext instance
context.getShutdownStrategy().setTimeout(600);
Integration with custom components
If you are implementing a custom Apache Camel component (which also inherits from the
org.apache.camel.Service interface), you can ensure that your custom code receives a
shutdown notification by implementing the org.apache.camel.spi.ShutdownPrepared
interface. This gives the component an opportunity execute custom code in preparation for
shutdown.
2.11. SCHEDULED ROUTE POLICY
2.11.1. Overview of Scheduled Route Policies
Overview
A scheduled route policy can be used to trigger events that affect a route at runtime. In
particular, the implementations that are currently available enable you to start, stop,
suspend, or resume a route at any time (or times) specified by the policy.
Scheduling tasks
The scheduled route policies are capable of triggering the following kinds of event:
Start a route—start the route at the time (or times) specified. This event only has an
effect, if the route is currently in a stopped state, awaiting activation.
Stop a route—stop the route at the time (or times) specified. This event only has an
effect, if the route is currently active.
Suspend a route—temporarily de-activate the consumer endpoint at the start of the
route (as specified in from()). The rest of the route is still active, but clients will not
be able to send new messages into the route.
Resume a route—re-activate the consumer endpoint at the start of the route,
returning the route to a fully active state.
Quartz component
The Quartz component is a timer component based on Terracotta's Quartz, which is an
open source implementation of a job scheduler. The Quartz component provides the
underlying implementation for both the simple scheduled route policy and the cron
scheduled route policy.
2.11.2. Simple Scheduled Route Policy
Overview
The simple scheduled route policy is a route policy that enables you to start, stop, suspend,
and resume routes, where the timing of these events is defined by providing the time and
98
CHAPTER 2. BASIC PRINCIPLES OF ROUTE BUILDING
date of an initial event and (optionally) by specifying a certain number of subsequent
repititions. To define a simple scheduled route policy, create an instance of the following
class:
org.apache.camel.routepolicy.quartz.SimpleScheduledRoutePolicy
Dependency
The simple scheduled route policy depends on the Quartz component, camel-quartz. For
example, if you are using Maven as your build system, you would need to add a
dependency on the camel-quartz artifact.
Java DSL example
Example 2.7, “Java DSL Example of Simple Scheduled Route” shows how to schedule a
route to start up using the Java DSL. The initial start time, startTime, is defined to be 3
seconds after the current time. The policy is also configured to start the route a second
time, 3 seconds after the initial start time, which is configured by setting
routeStartRepeatCount to 1 and routeStartRepeatInterval to 3000 milliseconds.
In Java DSL, you attach the route policy to the route by calling the routePolicy() DSL
command in the route.
Example 2.7. Java DSL Example of Simple Scheduled Route
// Java
SimpleScheduledRoutePolicy policy = new SimpleScheduledRoutePolicy();
long startTime = System.currentTimeMillis() + 3000L;
policy.setRouteStartDate(new Date(startTime));
policy.setRouteStartRepeatCount(1);
policy.setRouteStartRepeatInterval(3000);
from("direct:start")
.routeId("test")
.routePolicy(policy)
.to("mock:success");
NOTE
You can specify multiple policies on the route by calling routePolicy() with
multiple arguments.
XML DSL example
Example 2.8, “XML DSL Example of Simple Scheduled Route” shows how to schedule a
route to start up using the XML DSL.
In XML DSL, you attach the route policy to the route by setting the routePolicyRef
attribute on the route element.
Example 2.8. XML DSL Example of Simple Scheduled Route
99
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
NOTE
You can specify multiple policies on the route by setting the value of
routePolicyRef as a comma-separated list of bean IDs.
Defining dates and times
The initial times of the triggers used in the simple scheduled route policy are specified
using the java.util.Date type.The most flexible way to define aDate instance is through
the java.util.GregorianCalendar class. Use the convenient constructors and methods of the
GregorianCalendar class to define a date and then obtain aDate instance by calling
GregorianCalendar.getTime().
For example, to define the time and date for January 1, 2011 at noon, call a
GregorianCalendar constructor as follows:
// Java
import java.util.GregorianCalendar;
import java.util.Calendar;
...
GregorianCalendar gc = new GregorianCalendar(
2011,
Calendar.JANUARY,
1,
12, // hourOfDay
0,
// minutes
0
// seconds
);
java.util.Date triggerDate = gc.getTime();
The GregorianCalendar class also supports the definition of times in different time zones.
By default, it uses the local time zone on your computer.
Graceful shutdown
When you configure a simple scheduled route policy to stop a route, the route stopping
100
CHAPTER 2. BASIC PRINCIPLES OF ROUTE BUILDING
algorithm is automatically integrated with the graceful shutdown procedure (see
Section 2.10, “Controlling Start-Up and Shutdown of Routes”). This means that the task
waits until the current exchange has finished processing before shutting down the route.
You can set a timeout, however, that forces the route to stop after the specified time,
irrespective of whether or not the route has finished processing the exchange.
Scheduling tasks
You can use a simple scheduled route policy to define one or more of the following
scheduling tasks:
the section called “Starting a route”.
the section called “Stopping a route”.
the section called “Suspending a route”.
the section called “Resuming a route”.
Starting a route
The following table lists the parameters for scheduling one or more route starts.
Parameter
Type
Default
Description
routeStartDate
java.util.Date
None
Specifies the date
and time when the
route is started for
the first time.
routeStartRepeat
Count
int
0
When set to a nonzero value, specifies
how many times the
route should be
started.
routeStartRepeat
Interval
long
0
Specifies the time
interval between
starts, in units of
milliseconds.
Stopping a route
The following table lists the parameters for scheduling one or more route stops.
Parameter
Type
Default
Description
routeStopDate
java.util.Date
None
Specifies the date
and time when the
route is stopped for
the first time.
101
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
Parameter
Type
Default
Description
routeStopRepeatC
ount
int
0
When set to a nonzero value, specifies
how many times the
route should be
stopped.
routeStopRepeatI
nterval
long
0
Specifies the time
interval between
stops, in units of
milliseconds.
routeStopGracePe
riod
int
10000
Specifies how long to
wait for the current
exchange to finish
processing (grace
period) before
forcibly stopping the
route. Set to 0 for an
infinite grace period.
routeStopTimeUni
t
long
TimeUnit.MILLISE
CONDS
Specifies the time
unit of the grace
period.
Suspending a route
The following table lists the parameters for scheduling the suspension of a route one or
more times.
Parameter
Type
Default
Description
routeSuspendDate
java.util.Date
None
Specifies the date
and time when the
route is suspended
for the first time.
routeSuspendRepe
atCount
int
0
When set to a nonzero value, specifies
how many times the
route should be
suspended.
routeSuspendRepe
atInterval
long
0
Specifies the time
interval between
suspends, in units of
milliseconds.
Resuming a route
102
CHAPTER 2. BASIC PRINCIPLES OF ROUTE BUILDING
The following table lists the parameters for scheduling the resumption of a route one or
more times.
Parameter
Type
Default
Description
routeResumeDate
java.util.Date
None
Specifies the date
and time when the
route is resumed for
the first time.
routeResumeRepea
tCount
int
0
When set to a nonzero value, specifies
how many times the
route should be
resumed.
routeResumeRepea
tInterval
long
0
Specifies the time
interval between
resumes, in units of
milliseconds.
2.11.3. Cron Scheduled Route Policy
Overview
The cron scheduled route policy is a route policy that enables you to start, stop, suspend,
and resume routes, where the timing of these events is specified using cron expressions.
To define a cron scheduled route policy, create an instance of the following class:
org.apache.camel.routepolicy.quartz.CronScheduledRoutePolicy
Dependency
The simple scheduled route policy depends on the Quartz component, camel-quartz. For
example, if you are using Maven as your build system, you would need to add a
dependency on the camel-quartz artifact.
Java DSL example
Example 2.9, “Java DSL Example of a Cron Scheduled Route” shows how to schedule a
route to start up using the Java DSL. The policy is configured with the cron expression, */3
* * * * ?, which triggers a start event every 3 seconds.
In Java DSL, you attach the route policy to the route by calling the routePolicy() DSL
command in the route.
Example 2.9. Java DSL Example of a Cron Scheduled Route
// Java
CronScheduledRoutePolicy policy = new CronScheduledRoutePolicy();
policy.setRouteStartTime("*/3 * * * * ?");
103
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
from("direct:start")
.routeId("test")
.routePolicy(policy)
.to("mock:success");;
NOTE
You can specify multiple policies on the route by calling routePolicy() with
multiple arguments.
XML DSL example
Example 2.10, “XML DSL Example of a Cron Scheduled Route”shows how to schedule a
route to start up using the XML DSL.
In XML DSL, you attach the route policy to the route by setting the routePolicyRef
attribute on the route element.
Example 2.10. XML DSL Example of a Cron Scheduled Route
NOTE
You can specify multiple policies on the route by setting the value of
routePolicyRef as a comma-separated list of bean IDs.
Defining cron expressions
The cron expression syntax has its origins in the UNIXcron utility, which schedules jobs to
run in the background on a UNIX system. A cron expression is effectively a syntax for
wildcarding dates and times that enables you to specify either a single event or multiple
events that recur periodically.
A cron expression consists of 6 or 7 fields in the following order:
Seconds Minutes Hours DayOfMonth Month DayOfWeek [Year]
104
CHAPTER 2. BASIC PRINCIPLES OF ROUTE BUILDING
The Year field is optional and usually omitted, unless you want to define an event that
occurs once and once only. Each field consists of a mixture of literals and special
characters. For example, the following cron expression specifies an event that fires once
every day at midnight:
0 0 24 * * ?
The * character is a wildcard that matches every value of a field. Hence, the preceding
expression matches every day of every month. The ? character is a dummy placeholder
that means ignore this field. It always appears either in theDayOfMonth field or in the
DayOfWeek field, because it is not logically consistent to specify both of these fields at the
same time. For example, if you want to schedule an event that fires once a day, but only
from Monday to Friday, use the following cron expression:
0 0 24 ? * MON-FRI
Where the hyphen character specifies a range, MON-FRI. You can also use the forward slash
character, /, to specify increments. For example, to specify that an event fires every 5
minutes, use the following cron expression:
0 0/5 * * * ?
For a full explanation of the cron expression syntax, see the Wikipedia article on CRON
expressions.
Scheduling tasks
You can use a cron scheduled route policy to define one or more of the following scheduling
tasks:
the section called “Starting a route”.
the section called “Stopping a route”.
the section called “Suspending a route”.
the section called “Resuming a route”.
Starting a route
The following table lists the parameters for scheduling one or more route starts.
Parameter
Type
Default
Description
routeStartString
String
None
Specifies a cron
expression that
triggers one or more
route start events.
Stopping a route
The following table lists the parameters for scheduling one or more route stops.
105
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
Parameter
Type
Default
Description
routeStopTime
String
None
Specifies a cron
expression that
triggers one or more
route stop events.
routeStopGracePe
riod
int
10000
Specifies how long to
wait for the current
exchange to finish
processing (grace
period) before
forcibly stopping the
route. Set to 0 for an
infinite grace period.
routeStopTimeUni
t
long
TimeUnit.MILLISE
CONDS
Specifies the time
unit of the grace
period.
Suspending a route
The following table lists the parameters for scheduling the suspension of a route one or
more times.
Parameter
Type
Default
Description
routeSuspendTime
String
None
Specifies a cron
expression that
triggers one or more
route suspend
events.
Resuming a route
The following table lists the parameters for scheduling the resumption of a route one or
more times.
Parameter
Type
Default
Description
routeResumeTime
String
None
Specifies a cron
expression that
triggers one or more
route resume events.
2.12. JMX NAMING
Overview
106
CHAPTER 2. BASIC PRINCIPLES OF ROUTE BUILDING
Apache Camel allows you to customise the name of a CamelContext bean as it appears in
JMX, by defining a management name pattern for it. For example, you can customise the
name pattern of an XML CamelContext instance, as follows:
...
If you do not explicitly set a name pattern for the CamelContext bean, Apache Camel
reverts to a default naming strategy.
Default naming strategy
By default, the JMX name of a CamelContext bean is equal to the value of the bean'sid
attribute, prefixed by the current bundle ID. For example, if the id attribute on a
camelContext element is myCamel and the current bundle ID is 250, the JMX name would be
250-myCamel. In cases where there is more than oneCamelContext instance with the same
id in the bundle, the JMX name is disambiguated by adding a counter value as a suffix. For
example, if there are multiple instances of myCamel in the bundle, the corresponding JMX
MBeans are named as follows:
250-myCamel-1
250-myCamel-2
250-myCamel-3
...
Customising the JMX naming strategy
One drawback of the default naming strategy is that you cannot guarantee that a given
CamelContext bean will have the same JMX name between runs. If you want to have
greater consistency between runs, you can control the JMX name more precisely by
defining a JMX name pattern for the CamelContext instances.
Specifying a name pattern in Java
To specify a name pattern on a CamelContext in Java, call the setNamePattern method, as
follows:
// Java
context.getManagementNameStrategy().setNamePattern("#name#");
Specifying a name pattern in XML
To specify a name pattern on a CamelContext in XML, set the managementNamePattern
attribute on the camelContext element, as follows:
Name pattern tokens
You can construct a JMX name pattern by mixing literal text with any of the following
tokens:
107
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
Table 2.11. JMX Name Pattern Tokens
Token
Description
#camelId#
Value of the id attribute on the CamelContext bean.
#name#
Same as #camelId#.
#counter#
An incrementing counter (starting at 1 ).
#bundleId#
The OSGi bundle ID of the deployed bundle (OSGi only).
#symbolicName#
The OSGi symbolic name (OSGi only).
#version#
The OSGi bundle version (OSGi only).
Examples
Here are some examples of JMX name patterns you could define using the supported
tokens:
...
...
Ambiguous names
Because the customised naming pattern overrides the default naming strategy, it is
possible to define ambiguous JMX MBean names using this approach. For example:
...
...
...
In this case, Apache Camel would fail on start-up and report an MBean already exists
exception. You should, therefore, take extra care to ensure that you do not define
ambiguous name patterns.
2.13. PERFORMANCE AND OPTIMIZATION
Avoid unnecessary message copying
108
CHAPTER 2. BASIC PRINCIPLES OF ROUTE BUILDING
You can avoid making an unnecessary copy of the original message, by setting the
allowUseOriginalMessage option to false on the CamelContext object. For example, in
Blueprint XML you can set this option as follows:
...
You can safely set allowUseOriginalMessage to false, if the following conditions are
satisfied:
You do not set useOriginalMessage=true on any of the error handlers or on the
onException element.
You do not use the getOriginalMessage method anywhere in your Java application
code.
109
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
CHAPTER 3. INTRODUCING ENTERPRISE
INTEGRATION PATTERNS
Abstract
The Apache Camel's Enterprise Integration Patterns are inspired by a book of the same
name written by Gregor Hohpe and Bobby Woolf. The patterns described by these authors
provide an excellent toolbox for developing enterprise integration projects. In addition to
providing a common language for discussing integration architectures, many of the
patterns can be implemented directly using Apache Camel's programming interfaces and
XML configuration.
3.1. OVERVIEW OF THE PATTERNS
Enterprise Integration Patterns book
Apache Camel supports most of the patterns from the book, Enterprise Integration Patterns
by Gregor Hohpe and Bobby Woolf.
Messaging systems
The messaging systems patterns, shown in Table 3.1, “Messaging Systems”, introduce the
fundamental concepts and components that make up a messaging system.
Table 3.1. Messaging Systems
Icon
110
Name
Use Case
Message
How can two applications
connected by a message
channel exchange a piece of
information?
Message Channel
How does one application
communicate with another
application using messaging?
Message Endpoint
How does an application
connect to a messaging
channel to send and receive
messages?
Pipes and Filters
How can we perform complex
processing on a message
while still maintaining
independence and flexibility?
CHAPTER 3. INTRODUCING ENTERPRISE INTEGRATION PATTERNS
Icon
Name
Use Case
Message Router
How can you decouple
individual processing steps so
that messages can be passed
to different filters depending
on a set of defined conditions?
Message Translator
How do systems using
different data formats
communicate with each other
using messaging?
Messaging channels
A messaging channel is the basic component used for connecting the participants in a
messaging system. The patterns in Table 3.2, “Messaging Channels” describe the different
kinds of messaging channels available.
Table 3.2. Messaging Channels
Icon
Name
Use Case
Point to Point Channel
How can the caller be sure
that exactly one receiver will
receive the document or will
perform the call?
Publish Subscribe Channel
How can the sender broadcast
an event to all interested
receivers?
Dead Letter Channel
What will the messaging
system do with a message it
cannot deliver?
Guaranteed Delivery
How does the sender make
sure that a message will be
delivered, even if the
messaging system fails?
Message Bus
What is an architecture that
enables separate, decoupled
applications to work together,
such that one or more of the
applications can be added or
removed without affecting the
others?
111
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
Message construction
The message construction patterns, shown in Table 3.3, “Message Construction”, describe
the various forms and functions of the messages that pass through the system.
Table 3.3. Message Construction
Icon
Name
Use Case
Correlation Identifier
How does a requestor identify
the request that generated
the received reply?
Return Address
How does a replier know
where to send the reply?
Message routing
The message routing patterns, shown in Table 3.4, “Message Routing”, describe various
ways of linking message channels together, including various algorithms that can be
applied to the message stream (without modifying the body of the message).
Table 3.4. Message Routing
Icon
112
Name
Use Case
Content Based Router
How do we handle a situation
where the implementation of
a single logical function (e.g.,
inventory check) is spread
across multiple physical
systems?
Message Filter
How does a component avoid
receiving uninteresting
messages?
Recipient List
How do we route a message
to a list of dynamically
specified recipients?
Splitter
How can we process a
message if it contains multiple
elements, each of which
might have to be processed in
a different way?
CHAPTER 3. INTRODUCING ENTERPRISE INTEGRATION PATTERNS
Icon
Name
Use Case
Aggregator
How do we combine the
results of individual, but
related messages so that they
can be processed as a whole?
Resequencer
How can we get a stream of
related, but out-of-sequence,
messages back into the
correct order?
Composed Message Processor
How can you maintain the
overall message flow when
processing a message
consisting of multiple
elements, each of which may
require different processing?
Scatter-Gather
How do you maintain the
overall message flow when a
message needs to be sent to
multiple recipients, each of
which may send a reply?
Routing Slip
How do we route a message
consecutively through a series
of processing steps when the
sequence of steps is not
known at design-time, and
might vary for each message?
Throttler
How can I throttle messages
to ensure that a specific
endpoint does not get
overloaded, or that we don't
exceed an agreed SLA with
some external service?
Delayer
How can I delay the sending
of a message?
Load Balancer
How can I balance load across
a number of endpoints?
Multicast
How can I route a message to
a number of endpoints at the
same time?
Loop
How can I repeat processing a
message in a loop?
113
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
Icon
Name
Use Case
Sampling
How can I sample one
message out of many in a
given period to avoid
downstream route does not
get overloaded?
Message transformation
The message transformation patterns, shown in Table 3.5, “Message Transformation”,
describe how to modify the contents of messages for various purposes.
Table 3.5. Message Transformation
Icon
Name
Use Case
Content Enricher
How do we communicate with
another system if the
message originator does not
have all the required data
items available?
Content Filter
How do you simplify dealing
with a large message, when
you are interested in only a
few data items?
Claim Check
How can we reduce the data
volume of message sent
across the system without
sacrificing information
content?
Normalizer
How do you process
messages that are
semantically equivalent, but
arrive in a different format?
Sort
How can I sort the body of a
message?
Messaging endpoints
A messaging endpoint denotes the point of contact between a messaging channel and an
application. The messaging endpoint patterns, shown in Table 3.6, “Messaging Endpoints”,
describe various features and qualities of service that can be configured on an endpoint.
Table 3.6. Messaging Endpoints
114
CHAPTER 3. INTRODUCING ENTERPRISE INTEGRATION PATTERNS
Icon
Name
Use Case
Messaging Mapper
How do you move data
between domain objects and
the messaging infrastructure
while keeping the two
independent of each other?
Event Driven Consumer
How can an application
automatically consume
messages as they become
available?
Polling Consumer
How can an application
consume a message when the
application is ready?
Competing Consumers
How can a messaging client
process multiple messages
concurrently?
Message Dispatcher
How can multiple consumers
on a single channel
coordinate their message
processing?
Selective Consumer
How can a message consumer
select which messages it
wants to receive?
Durable Subscriber
How can a subscriber avoid
missing messages when it's
not listening for them?
Idempotent Consumer
How can a message receiver
deal with duplicate messages?
Transactional Client
How can a client control its
transactions with the
messaging system?
Messaging Gateway
How do you encapsulate
access to the messaging
system from the rest of the
application?
115
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
Icon
Name
Use Case
Service Activator
How can an application design
a service to be invoked both
via various messaging
technologies and via nonmessaging techniques?
System management
The system management patterns, shown in Table 3.7, “System Management”, describe
how to monitor, test, and administer a messaging system.
Table 3.7. System Management
Icon
116
Name
Use Case
Wire Tap
How do you inspect messages
that travel on a point-to-point
channel?
CHAPTER 4. MESSAGING SYSTEMS
CHAPTER 4. MESSAGING SYSTEMS
Abstract
This chapter introduces the fundamental building blocks of a messaging system, such as
endpoints, messaging channels, and message routers.
4.1. MESSAGE
Overview
A message is the smallest unit for transmitting data in a messaging system (represented by
the grey dot in the figure below). The message itself might have some internal structure—
for example, a message containing multiple parts—which is represented by geometrical
figures attached to the grey dot in Figure 4.1, “Message Pattern”.
Figure 4.1. Message Pattern
Types of message
Apache Camel defines the following distinct message types:
In message — A message that travels through a route from a consumer endpoint to
a producer endpoint (typically, initiating a message exchange).
Out message — A message that travels through a route from a producer endpoint
back to a consumer endpoint (usually, in response to an In message).
All of these message types are represented internally by the org.apache.camel.Message
interface.
Message structure
By default, Apache Camel applies the following structure to all message types:
Headers — Contains metadata or header data extracted from the message.
Body — Usually contains the entire message in its original form.
Attachments — Message attachments (required for integrating with certain
messaging systems, such as JBI).
It is important to remember that this division into headers, body, and attachments is an
abstract model of the message. Apache Camel supports many different components, that
generate a wide variety of message formats. Ultimately, it is the underlying component
implementation that decides what gets placed into the headers and body of a message.
117
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
Correlating messages
Internally, Apache Camel remembers the message IDs, which are used to correlate
individual messages. In practice, however, the most important way that Apache Camel
correlates messages is through exchange objects.
Exchange objects
An exchange object is an entity that encapsulates related messages, where the collection of
related messages is referred to as a message exchange and the rules governing the
sequence of messages are referred to as an exchange pattern. For example, two common
exchange patterns are: one-way event messages (consisting of an In message), and
request-reply exchanges (consisting of an In message, followed by an Out message).
Accessing messages
When defining a routing rule in the Java DSL, you can access the headers and body of a
message using the following DSL builder methods:
header(String name), body() — Returns the named header and the body of the
current In message.
outBody() — Returns the body of the currentOut message.
For example, to populate the In message's username header, you can use the following
Java DSL route:
from(SourceURL).setHeader("username", "John.Doe").to(TargetURL);
4.2. MESSAGE CHANNEL
Overview
A message channel is a logical channel in a messaging system. That is, sending messages
to different message channels provides an elementary way of sorting messages into
different message types. Message queues and message topics are examples of message
channels. You should remember that a logical channel is not the same as a physical
channel. There can be several different ways of physically realizing a logical channel.
In Apache Camel, a message channel is represented by an endpoint URI of a messageoriented component as shown in Figure 4.2, “Message Channel Pattern”.
118
CHAPTER 4. MESSAGING SYSTEMS
Figure 4.2. Message Channel Pattern
Message-oriented components
The following message-oriented components in Apache Camel support the notion of a
message channel:
ActiveMQ
JMS
AMQP
ActiveMQ
In ActiveMQ, message channels are represented by queues or topics. The endpoint URI for a
specific queue, QueueName, has the following format:
activemq:QueueName
The endpoint URI for a specific topic, TopicName, has the following format:
activemq:topic:TopicName
For example, to send messages to the queue, Foo.Bar, use the following endpoint URI:
activemq:Foo.Bar
See for more details and instructions on setting up the ActiveMQ component.
JMS
The Java Messaging Service (JMS) is a generic wrapper layer that is used to access many
different kinds of message systems (for example, you can use it to wrap ActiveMQ,
MQSeries, Tibco, BEA, Sonic, and others). In JMS, message channels are represented by
queues, or topics. The endpoint URI for a specific queue, QueueName, has the following
format:
jms:QueueName
119
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
The endpoint URI for a specific topic, TopicName, has the following format:
jms:topic:TopicName
See for more details and instructions on setting up the JMS component.
AMQP
In AMQP, message channels are represented by queues, or topics. The endpoint URI for a
specific queue, QueueName, has the following format:
amqp:QueueName
The endpoint URI for a specific topic, TopicName, has the following format:
amqp:topic:TopicName
See for more details and instructions on setting up the AMQP component.
4.3. MESSAGE ENDPOINT
Overview
A message endpoint is the interface between an application and a messaging system. As
shown in Figure 4.3, “Message Endpoint Pattern”, you can have a sender endpoint,
sometimes called a proxy or a service consumer, which is responsible for sending In
messages, and a receiver endpoint, sometimes called an endpoint or a service, which is
responsible for receiving In messages.
Figure 4.3. Message Endpoint Pattern
Types of endpoint
Apache Camel defines two basic types of endpoint:
Consumer endpoint — Appears at the start of a Apache Camel route and readsIn
messages from an incoming channel (equivalent to a receiver endpoint).
Producer endpoint — Appears at the end of a Apache Camel route and writesIn
messages to an outgoing channel (equivalent to a sender endpoint). It is possible to
define a route with multiple producer endpoints.
Endpoint URIs
120
CHAPTER 4. MESSAGING SYSTEMS
In Apache Camel, an endpoint is represented by an endpoint URI, which typically
encapsulates the following kinds of data:
Endpoint URI for a consumer endpoint — Advertises a specific location (for example,
to expose a service to which senders can connect). Alternatively, the URI can specify
a message source, such as a message queue. The endpoint URI can include settings
to configure the endpoint.
Endpoint URI for a producer endpoint — Contains details of where to send messages
and includes the settings to configure the endpoint. In some cases, the URI specifies
the location of a remote receiver endpoint; in other cases, the destination can have
an abstract form, such as a queue name.
An endpoint URI in Apache Camel has the following general form:
ComponentPrefix:ComponentSpecificURI
Where ComponentPrefix is a URI prefix that identifies a particular Apache Camel component
(see for details of all the supported components). The remaining part of the URI,
ComponentSpecificURI, has a syntax defined by the particular component. For example, to
connect to the JMS queue, Foo.Bar, you can define an endpoint URI like the following:
jms:Foo.Bar
To define a route that connects the consumer endpoint,
file://local/router/messages/foo, directly to the producer endpoint,jms:Foo.Bar, you
can use the following Java DSL fragment:
from("file://local/router/messages/foo").to("jms:Foo.Bar");
Alternatively, you can define the same route in XML, as follows:
4.4. PIPES AND FILTERS
Overview
The pipes and filters pattern, shown in Figure 4.4, “Pipes and Filters Pattern”, describes a
way of constructing a route by creating a chain of filters, where the output of one filter is
fed into the input of the next filter in the pipeline (analogous to the UNIX pipe command).
The advantage of the pipeline approach is that it enables you to compose services (some of
which can be external to the Apache Camel application) to create more complex forms of
message processing.
121
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
Figure 4.4. Pipes and Filters Pattern
Pipeline for the InOut exchange pattern
Normally, all of the endpoints in a pipeline have an input (In message) and an output (Out
message), which implies that they are compatible with the InOut message exchange
pattern. A typical message flow through an InOut pipeline is shown in Figure 4.5, “Pipeline
for InOut Exchanges”.
Figure 4.5. Pipeline for InOut Exchanges
Where the pipeline connects the output of each endpoint to the input of the next one. The
Out message from the final endpoint gets sent back to the original caller. You can define a
route for this pipeline, as follows:
from("jms:RawOrders").pipeline("cxf:bean:decrypt",
"cxf:bean:authenticate", "cxf:bean:dedup", "jms:CleanOrders");
The same route can be configured in XML, as follows:
There is no dedicated pipeline element in XML. The preceding combination of from and to
elements is semantically equivalent to a pipeline. See the section called “Comparison of
pipeline() and to() DSL commands”.
Pipeline for the InOnly and RobustInOnly exchange patterns
When there are no Out messages available from the endpoints in the pipeline (as is the
case for the InOnly and RobustInOnly exchange patterns), a pipeline cannot be connected
in the normal way. In this special case, the pipeline is constructed by passing a copy of the
122
CHAPTER 4. MESSAGING SYSTEMS
original In message to each of the endpoints in the pipeline, as shown inFigure 4.6,
“Pipeline for InOnly Exchanges”. This type of pipeline is equivalent to a recipient list with
fixed destinations(see Section 7.3, “Recipient List”).
Figure 4.6. Pipeline for InOnly Exchanges
The route for this pipeline is defined using the same syntax as an InOut pipeline (either in
Java DSL or in XML).
Comparison of pipeline() and to() DSL commands
In the Java DSL, you can define a pipeline route using either of the following syntaxes:
Using the pipeline() processor command — Use the pipeline processor to construct a
pipeline route as follows:
from(SourceURI).pipeline(FilterA, FilterB, TargetURI);
Using the to() command — Use the to() command to construct a pipeline route as
follows:
from(SourceURI).to(FilterA, FilterB, TargetURI);
Alternatively, you can use the equivalent syntax:
from(SourceURI).to(FilterA).to(FilterB).to(TargetURI);
Exercise caution when using the to() command syntax, because it is not always equivalent
to a pipeline processor. In Java DSL, the meaning of to() can be modified by the preceding
command in the route. For example, when the multicast() command precedes the to()
command, it binds the listed endpoints into a multicast pattern, instead of a pipeline
pattern(see Section 7.11, “Multicast”).
4.5. MESSAGE ROUTER
Overview
A message router, shown in Figure 4.7, “Message Router Pattern”, is a type of filter that
consumes messages from a single consumer endpoint and redirects them to the
appropriate target endpoint, based on a particular decision criterion. A message router is
concerned only with redirecting messages; it does not modify the message content.
123
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
Figure 4.7. Message Router Pattern
A message router can easily be implemented in Apache Camel using the choice()
processor, where each of the alternative target endpoints can be selected using a when()
subclause (for details of the choice processor, see Section 1.5, “Processors”).
Java DSL example
The following Java DSL example shows how to route messages to three alternative
destinations (either seda:a, seda:b, or seda:c) depending on the contents of thefoo
header:
from("seda:a").choice()
.when(header("foo").isEqualTo("bar")).to("seda:b")
.when(header("foo").isEqualTo("cheese")).to("seda:c")
.otherwise().to("seda:d");
XML configuration example
The following example shows how to configure the same route in XML:
$foo = 'bar'
$foo = 'cheese'
Choice without otherwise
124
CHAPTER 4. MESSAGING SYSTEMS
If you use choice() without an otherwise() clause, any unmatched exchanges are
dropped by default.
4.6. MESSAGE TRANSLATOR
Overview
The message translator pattern, shown in Figure 4.8, “Message Translator Pattern”
describes a component that modifies the contents of a message, translating it to a different
format. You can use Apache Camel's bean integration feature to perform the message
translation.
Figure 4.8. Message Translator Pattern
Bean integration
You can transform a message using bean integration, which enables you to call a method
on any registered bean. For example, to call the method, myMethodName(), on the bean
with ID, myTransformerBean:
from("activemq:SomeQueue")
.beanRef("myTransformerBean", "myMethodName")
.to("mqseries:AnotherQueue");
Where the myTransformerBean bean is defined in either a Spring XML file or in JNDI. If, you
omit the method name parameter from beanRef(), the bean integration will try to deduce
the method name to invoke by examining the message exchange.
You can also add your own explicit Processor instance to perform the transformation, as
follows:
from("direct:start").process(new Processor() {
public void process(Exchange exchange) {
Message in = exchange.getIn();
in.setBody(in.getBody(String.class) + " World!");
}
}).to("mock:result");
Or, you can use the DSL to explicitly configure the transformation, as follows:
from("direct:start").setBody(body().append(" World!")).to("mock:result");
125
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
You can also use templating to consume a message from one destination, transform it with
something like Velocity or XQuery and then send it on to another destination. For example,
using the InOnly exchange pattern (one-way messaging) :
from("activemq:My.Queue").
to("velocity:com/acme/MyResponse.vm").
to("activemq:Another.Queue");
If you want to use InOut (request-reply) semantics to process requests on theMy.Queue
queue on ActiveMQ with a template generated response, then you could use a route like
the following to send responses back to the JMSReplyTo destination:
from("activemq:My.Queue").
to("velocity:com/acme/MyResponse.vm");
126
CHAPTER 5. MESSAGING CHANNELS
CHAPTER 5. MESSAGING CHANNELS
Abstract
Messaging channels provide the plumbing for a messaging application. This chapter
describes the different kinds of messaging channels available in a messaging system, and
the roles that they play.
5.1. POINT-TO-POINT CHANNEL
Overview
A point-to-point channel, shown in Figure 5.1, “Point to Point Channel Pattern” is a message
channel that guarantees that only one receiver consumes any given message. This is in
contrast with a publish-subscribe channel, which allows multiple receivers to consume the
same message. In particular, with a point-to-point channel, it is possible for multiple
receivers to subscribe to the same channel. If more than one receiver competes to
consume a message, it is up to the message channel to ensure that only one receiver
actually consumes the message.
Figure 5.1. Point to Point Channel Pattern
Components that support point-to-point channel
The following Apache Camel components support the point-to-point channel pattern:
JMS
ActiveMQ
SEDA
JPA
XMPP
JMS
In JMS, a point-to-point channel is represented by a queue. For example, you can specify the
endpoint URI for a JMS queue called Foo.Bar as follows:
jms:queue:Foo.Bar
The qualifier, queue:, is optional, because the JMS component creates a queue endpoint by
default. Therefore, you can also specify the following equivalent endpoint URI:
127
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
jms:Foo.Bar
See for more details.
ActiveMQ
In ActiveMQ, a point-to-point channel is represented by a queue. For example, you can
specify the endpoint URI for an ActiveMQ queue called Foo.Bar as follows:
activemq:queue:Foo.Bar
See for more details.
SEDA
The Apache Camel Staged Event-Driven Architecture (SEDA) component is implemented
using a blocking queue. Use the SEDA component if you want to create a lightweight pointto-point channel that is internal to the Apache Camel application. For example, you can
specify an endpoint URI for a SEDA queue called SedaQueue as follows:
seda:SedaQueue
JPA
The Java Persistence API (JPA) component is an EJB 3 persistence standard that is used to
write entity beans out to a database. See for more details.
XMPP
The XMPP (Jabber) component supports the point-to-point channel pattern when it is used in
the person-to-person mode of communication. See for more details.
5.2. PUBLISH-SUBSCRIBE CHANNEL
Overview
A publish-subscribe channel, shown in Figure 5.2, “Publish Subscribe Channel Pattern”, is a
message channel that enables multiple subscribers to consume any given message. This is
in contrast with a point-to-point channel. Publish-subscribe channels are frequently used as
a means of broadcasting events or notifications to multiple subscribers.
128
CHAPTER 5. MESSAGING CHANNELS
Figure 5.2. Publish Subscribe Channel Pattern
Components that support publish-subscribe channel
The following Apache Camel components support the publish-subscribe channel pattern:
JMS
ActiveMQ
XMPP
SEDA for working with SEDA in the sameCamelContext which can work in pub-sub,
but allowing multiple consumers.
VM as SEDA, but for use within the same JVM.
JMS
In JMS, a publish-subscribe channel is represented by a topic. For example, you can specify
the endpoint URI for a JMS topic called StockQuotes as follows:
jms:topic:StockQuotes
See for more details.
ActiveMQ
In ActiveMQ, a publish-subscribe channel is represented by a topic. For example, you can
specify the endpoint URI for an ActiveMQ topic called StockQuotes, as follows:
129
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
activemq:topic:StockQuotes
See for more details.
XMPP
The XMPP (Jabber) component supports the publish-subscribe channel pattern when it is
used in the group communication mode. See for more details.
Static subscription lists
If you prefer, you can also implement publish-subscribe logic within the Apache Camel
application itself. A simple approach is to define a static subscription list, where the target
endpoints are all explicitly listed at the end of the route. However, this approach is not as
flexible as a JMS or ActiveMQ topic.
Java DSL example
The following Java DSL example shows how to simulate a publish-subscribe channel with a
single publisher, seda:a, and three subscribers, seda:b, seda:c, and seda:d:
from("seda:a").to("seda:b", "seda:c", "seda:d");
NOTE
This only works for the InOnly message exchange pattern.
XML configuration example
The following example shows how to configure the same route in XML:
5.3. DEAD LETTER CHANNEL
Overview
The dead letter channel pattern, shown in Figure 5.3, “Dead Letter Channel Pattern”,
describes the actions to take when the messaging system fails to deliver a message to the
intended recipient. This includes such features as retrying delivery and, if delivery
ultimately fails, sending the message to a dead letter channel, which archives the
undelivered messages.
130
CHAPTER 5. MESSAGING CHANNELS
Figure 5.3. Dead Letter Channel Pattern
Creating a dead letter channel in Java DSL
The following example shows how to create a dead letter channel using Java DSL:
errorHandler(deadLetterChannel("seda:errors"));
from("seda:a").to("seda:b");
Where the errorHandler() method is a Java DSL interceptor, which implies thatall of the
routes defined in the current route builder are affected by this setting. The
deadLetterChannel() method is a Java DSL command that creates a new dead letter
channel with the specified destination endpoint, seda:errors.
The errorHandler() interceptor provides a catch-all mechanism for handling all error
types. If you want to apply a more fine-grained approach to exception handling, you can
use the onException clauses instead(see the section called “onException clause”).
XML DSL example
You can define a dead letter channel in the XML DSL, as follows:
...
131
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
Redelivery policy
Normally, you do not send a message straight to the dead letter channel, if a delivery
attempt fails. Instead, you re-attempt delivery up to some maximum limit, and after all
redelivery attempts fail you would send the message to the dead letter channel. To
customize message redelivery, you can configure the dead letter channel to have a
redelivery policy. For example, to specify a maximum of two redelivery attempts, and to
apply an exponential backoff algorithm to the time delay between delivery attempts, you
can configure the dead letter channel as follows:
errorHandler(deadLetterChannel("seda:errors").maximumRedeliveries(2).useEx
ponentialBackOff());
from("seda:a").to("seda:b");
Where you set the redelivery options on the dead letter channel by invoking the relevant
methods in a chain (each method in the chain returns a reference to the current
RedeliveryPolicy object). Table 5.1, “Redelivery Policy Settings” summarizes the
methods that you can use to set redelivery policies.
Table 5.1. Redelivery Policy Settings
Method Signature
Default
Description
backOffMultiplier(doubl
e multiplier)
2
If exponential backoff is
enabled, let m be the backoff
multiplier and let d be the
initial delay. The sequence of
redelivery attempts are then
timed as follows:
d, m*d, m*m*d,
m*m*m*d, ...
collisionAvoidancePerce
nt(double
collisionAvoidancePerce
nt)
15
If collision avoidance is
enabled, let p be the collision
avoidance percent. The
collision avoidance policy
then tweaks the next delay by
a random amount, up to
plus/minus p% of its current
value.
delayPattern(String
delayPattern)
None
Apache Camel 2.0:
132
CHAPTER 5. MESSAGING CHANNELS
Method Signature
Default
Description
disableRedelivery()
true
Apache Camel 2.0: Disables
the redelivery feature. To
enable redelivery, set
maximumRedeliveries() to
a positive integer value.
handled(boolean
handled)
true
Apache Camel 2.0: If true,
the current exception is
cleared when the message is
moved to the dead letter
channel; if false, the
exception is propagated back
to the client.
initialRedeliveryDelay(
long
initialRedeliveryDelay)
1000
Specifies the delay (in
milliseconds) before
attempting the first
redelivery.
logStackTrace(boolean
logStackTrace)
false
Apache Camel 2.0: If true,
the JVM stack trace is
included in the error logs.
maximumRedeliveries(int
maximumRedeliveries)
0
Apache Camel 2.0: Maximum
number of delivery attempts.
maximumRedeliveryDelay(
long maxDelay)
60000
Apache Camel 2.0: When
using an exponential backoff
strategy (see
useExponentialBackOff()
), it is theoretically possible
for the redelivery delay to
increase without limit. This
property imposes an upper
limit on the redelivery delay
(in milliseconds)
onRedelivery(Processor
processor)
None
Apache Camel 2.0: Configures
a processor that gets called
before every redelivery
attempt.
redeliveryDelay(long
int)
0
Apache Camel 2.0: Specifies
the delay (in milliseconds)
between redelivery attempts.
133
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
Method Signature
Default
Description
retriesExhaustedLogLeve
l(LoggingLevel
logLevel)
LoggingLevel.ERROR
Apache Camel 2.0: Specifies
the logging level at which to
log delivery failure (specified
as an
org.apache.camel.Loggin
gLevel constant).
retryAttemptedLogLevel(
LoggingLevel logLevel)
LoggingLevel.DEBUG
Apache Camel 2.0: Specifies
the logging level at which to
redelivery attempts (specified
as an
org.apache.camel.Loggin
gLevel constant).
useCollisionAvoidance()
false
Enables collision avoidence,
which adds some
randomization to the backoff
timings to reduce contention
probability.
useOriginalMessage()
false
Apache Camel 2.0: If this
feature is enabled, the
message sent to the dead
letter channel is a copy of the
original message exchange,
as it existed at the beginning
of the route (in the from()
node).
useExponentialBackOff()
false
Enables exponential backoff.
allowRedeliveryWhileSto
pping()
true
Controls whether redelivery is
attempted during graceful
shutdown or while a route is
stopping. A delivery that is
already in progress when
stopping is initiated will not be
interrupted.
Redelivery headers
If Apache Camel attempts to redeliver a message, it automatically sets the headers
described in Table 5.2, “Dead Letter Redelivery Headers” on the In message.
Table 5.2. Dead Letter Redelivery Headers
Header Name
134
Type
Description
CHAPTER 5. MESSAGING CHANNELS
Header Name
Type
Description
CamelRedeliveryCounter
Integer
Apache Camel 2.0: Counts the
number of unsuccessful
delivery attempts. This value
is also set in
Exchange.REDELIVERY_COU
NTER.
CamelRedelivered
Boolean
Apache Camel 2.0: True, if
one or more redelivery
attempts have been made.
This value is also set in
Exchange.REDELIVERED.
CamelRedeliveryMaxCount
er
Integer
Apache Camel 2.6: Holds the
maximum redelivery setting
(also set in the
Exchange.REDELIVERY_MAX
_COUNTER exchange
property). This header is
absent if you use
retryWhile or have
unlimited maximum
redelivery configured.
Redelivery exchange properties
If Apache Camel attempts to redeliver a message, it automatically sets the exchange
properties described in Table 5.3, “Redelivery Exchange Properties”.
Table 5.3. Redelivery Exchange Properties
Exchange Property Name
Type
Description
Exchange.FAILURE_ROUTE_
ID
String
Provides the route ID of the
route that failed. The literal
name of this property is
CamelFailureRouteId .
Using the original message
Available as of Apache Camel 2.0 Because an exchange object is subject to modification
as it passes through the route, the exchange that is current when an exception is raised is
not necessarily the copy that you would want to store in the dead letter channel. In many
cases, it is preferable to log the message that arrived at the start of the route, before it was
subject to any kind of transformation by the route. For example, consider the following
route:
from("jms:queue:order:input")
.to("bean:validateOrder");
.to("bean:transformOrder")
135
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
.to("bean:handleOrder");
The preceding route listen for incoming JMS messages and then processes the messages
using the sequence of beans: validateOrder, transformOrder, and handleOrder. But
when an error occurs, we do not know in which state the message is in. Did the error
happen before the transformOrder bean or after? We can ensure that the original
message from jms:queue:order:input is logged to the dead letter channel by enabling
the useOriginalMessage option as follows:
// will use original body
errorHandler(deadLetterChannel("jms:queue:dead")
.useOriginalMessage().maximumRedeliveries(5).redeliveryDelay(5000);
Redeliver delay pattern
Available as of Apache Camel 2.0 The delayPattern option is used to specify delays for
particular ranges of the redelivery count. The delay pattern has the following syntax:
limit1:delay1;limit2:delay2;limit3:delay3;..., where each delayN is applied to
redeliveries in the range limitN <= redeliveryCount < limitN+1
For example, consider the pattern, 5:1000;10:5000;20:20000, which defines three groups
and results in the following redelivery delays:
Attempt number 1..4 = 0 milliseconds (as the first group starts with 5).
Attempt number 5..9 = 1000 milliseconds (the first group).
Attempt number 10..19 = 5000 milliseconds (the second group).
Attempt number 20.. = 20000 milliseconds (the last group).
You can start a group with limit 1 to define a starting delay. For example, 1:1000;5:5000
results in the following redelivery delays:
Attempt number 1..4 = 1000 millis (the first group)
Attempt number 5.. = 5000 millis (the last group)
There is no requirement that the next delay should be higher than the previous and you
can use any delay value you like. For example, the delay pattern, 1:5000;3:1000, starts
with a 5 second delay and then reduces the delay to 1 second.
Which endpoint failed?
When Apache Camel routes messages, it updates an Exchange property that contains the
last endpoint the Exchange was sent to. Hence, you can obtain the URI for the current
exchange's most recent destination using the following code:
// Java
String lastEndpointUri = exchange.getProperty(Exchange.TO_ENDPOINT,
String.class);
Where Exchange.TO_ENDPOINT is a string constant equal to CamelToEndpoint. This
property is updated whenever Camel sends a message to any endpoint.
136
CHAPTER 5. MESSAGING CHANNELS
If an error occurs during routing and the exchange is moved into the dead letter queue,
Apache Camel will additionally set a property named CamelFailureEndpoint, which
identifies the last destination the exchange was sent to before the error occcured. Hence,
you can access the failure endpoint from within a dead letter queue using the following
code:
// Java
String failedEndpointUri = exchange.getProperty(Exchange.FAILURE_ENDPOINT,
String.class);
Where Exchange.FAILURE_ENDPOINT is a string constant equal to CamelFailureEndpoint.
NOTE
These properties remain set in the current exchange, even if the failure occurs
after the given destination endpoint has finished processing. For example,
consider the following route:
from("activemq:queue:foo")
.to("http://someserver/somepath")
.beanRef("foo");
Now suppose that a failure happens in the foo bean. In this case the
Exchange.TO_ENDPOINT property and the Exchange.FAILURE_ENDPOINT
property still contain the value, http://someserver/somepath.
onRedelivery processor
When a dead letter channel is performing redeliveries, it is possible to configure a
Processor that is executed just before every redelivery attempt. This can be used for
situations where you need to alter the message before it is redelivered.
For example, the following dead letter channel is configured to call the
MyRedeliverProcessor before redelivering exchanges:
// we configure our Dead Letter Channel to invoke
// MyRedeliveryProcessor before a redelivery is
// attempted. This allows us to alter the message before
errorHandler(deadLetterChannel("mock:error").maximumRedeliveries(5)
.onRedelivery(new MyRedeliverProcessor())
// setting delay to zero is just to make unit teting faster
.redeliveryDelay(0L));
Where the MyRedeliveryProcessor process is implemented as follows:
// This is our processor that is executed before every redelivery attempt
// here we can do what we want in the java code, such as altering the
message
public class MyRedeliverProcessor implements Processor {
public void process(Exchange exchange) throws Exception {
// the message is being redelivered so we can alter it
// we just append the redelivery counter to the body
137
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
// you can of course do all kind of stuff instead
String body = exchange.getIn().getBody(String.class);
int count =
exchange.getIn().getHeader(Exchange.REDELIVERY_COUNTER, Integer.class);
exchange.getIn().setBody(body + count);
// the maximum redelivery was set to 5
int max =
exchange.getIn().getHeader(Exchange.REDELIVERY_MAX_COUNTER,
Integer.class);
assertEquals(5, max);
}
}
Control redelivery during shutdown or stopping
If you stop a route or initiate graceful shutdown, the default behavior of the error handler is
to continue attempting redelivery. Because this is typically not the desired behavior, you
have the option of disabling redelivery during shutdown or stopping, by setting the
allowRedeliveryWhileStopping option to false, as shown in the following example:
errorHandler(deadLetterChannel("jms:queue:dead")
.allowRedeliveryWhileStopping(false)
.maximumRedeliveries(20)
.redeliveryDelay(1000)
.retryAttemptedLogLevel(LoggingLevel.INFO));
NOTE
The allowRedeliveryWhileStopping option is true by default, for backwards
compatibility reasons. During aggressive shutdown, however, redelivery is
always suppressed, irrespective of this option setting (for example, after
graceful shutdown has timed out).
onException clause
Instead of using the errorHandler() interceptor in your route builder, you can define a
series of onException() clauses that define different redelivery policies and different dead
letter channels for various exception types. For example, to define distinct behavior for
each of the NullPointerException, IOException, and Exception types, you can define
the following rules in your route builder using Java DSL:
onException(NullPointerException.class)
.maximumRedeliveries(1)
.setHeader("messageInfo", "Oh dear! An NPE.")
.to("mock:npe_error");
onException(IOException.class)
.initialRedeliveryDelay(5000L)
.maximumRedeliveries(3)
.backOffMultiplier(1.0)
.useExponentialBackOff()
.setHeader("messageInfo", "Oh dear! Some kind of I/O exception.")
138
CHAPTER 5. MESSAGING CHANNELS
.to("mock:io_error");
onException(Exception.class)
.initialRedeliveryDelay(1000L)
.maximumRedeliveries(2)
.setHeader("messageInfo", "Oh dear! An exception.")
.to("mock:error");
from("seda:a").to("seda:b");
Where the redelivery options are specified by chaining the redelivery policy methods (as
listed in Table 5.1, “Redelivery Policy Settings”), and you specify the dead letter channel's
endpoint using the to() DSL command. You can also call other Java DSL commands in the
onException() clauses. For example, the preceding example callssetHeader() to record
some error details in a message header named, messageInfo.
In this example, the NullPointerException and the IOException exception types are
configured specially. All other exception types are handled by the generic Exception
exception interceptor. By default, Apache Camel applies the exception interceptor that
most closely matches the thrown exception. If it fails to find an exact match, it tries to
match the closest base type, and so on. Finally, if no other interceptor matches, the
interceptor for the Exception type matches all remaining exceptions.
5.4. GUARANTEED DELIVERY
Overview
Guaranteed delivery means that once a message is placed into a message channel, the
messaging system guarantees that the message will reach its destination, even if parts of
the application should fail. In general, messaging systems implement the guaranteed
delivery pattern, shown in Figure 5.4, “Guaranteed Delivery Pattern”, by writing messages
to persistent storage before attempting to deliver them to their destination.
Figure 5.4. Guaranteed Delivery Pattern
Components that support guaranteed delivery
The following Apache Camel components support the guaranteed delivery pattern:
JMS
ActiveMQ
139
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
ActiveMQ Journal
File Component
JMS
In JMS, the deliveryPersistent query option indicates whether or not persistent storage
of messages is enabled. Usually it is unnecessary to set this option, because the default
behavior is to enable persistent delivery. To configure all the details of guaranteed delivery,
it is necessary to set configuration options on the JMS provider. These details vary,
depending on what JMS provider you are using. For example, MQSeries, TibCo, BEA, Sonic,
and others, all provide various qualities of service to support guaranteed delivery.
See for more details.
ActiveMQ
In ActiveMQ, message persistence is enabled by default. From version 5 onwards, ActiveMQ
uses the AMQ message store as the default persistence mechanism. There are several
different approaches you can use to enabe message persistence in ActiveMQ.
The simplest option (different from Figure 5.4, “Guaranteed Delivery Pattern”) is to enable
persistence in a central broker and then connect to that broker using a reliable protocol.
After a message is been sent to the central broker, delivery to consumers is guaranteed.
For example, in the Apache Camel configuration file, META-INF/spring/camelcontext.xml, you can configure the ActiveMQ component to connect to the central broker
using the OpenWire/TCP protocol as follows:
...
...
If you prefer to implement an architecture where messages are stored locally before being
sent to a remote endpoint (similar to Figure 5.4, “Guaranteed Delivery Pattern”), you do
this by instantiating an embedded broker in your Apache Camel application. A simple way
to achieve this is to use the ActiveMQ Peer-to-Peer protocol, which implicitly creates an
embedded broker to communicate with other peer endpoints. For example, in the camelcontext.xml configuration file, you can configure the ActiveMQ component to connect to
all of the peers in group, GroupA, as follows:
...
...
Where broker1 is the broker name of the embedded broker (other peers in the group
140
CHAPTER 5. MESSAGING CHANNELS
should use different broker names). One limiting feature of the Peer-to-Peer protocol is that
it relies on IP multicast to locate the other peers in its group. This makes it unsuitable for
use in wide area networks (and in some local area networks that do not have IP multicast
enabled).
A more flexible way to create an embedded broker in the ActiveMQ component is to exploit
ActiveMQ's VM protocol, which connects to an embedded broker instance. If a broker of the
required name does not already exist, the VM protocol automatically creates one. You can
use this mechanism to create an embedded broker with custom configuration. For example:
...
...
Where activemq.xml is an ActiveMQ file which configures the embedded broker instance.
Within the ActiveMQ configuration file, you can choose to enable one of the following
persistence mechanisms:
AMQ persistence(the default) — A fast and reliable message store that is native to
ActiveMQ. For details, see amqPersistenceAdapter and AMQ Message Store.
JDBC persistence — Uses JDBC to store messages in any JDBC-compatible database.
For details, see jdbcPersistenceAdapter and ActiveMQ Persistence.
Journal persistence — A fast persistence mechanism that stores messages in a
rolling log file. For details, see journalPersistenceAdapter and ActiveMQ Persistence.
Kaha persistence — A persistence mechanism developed specifically for ActiveMQ.
For details, see kahaPersistenceAdapter and ActiveMQ Persistence.
See for more details.
ActiveMQ Journal
The ActiveMQ Journal component is optimized for a special use case where multiple,
concurrent producers write messages to queues, but there is only one active consumer.
Messages are stored in rolling log files and concurrent writes are aggregated to boost
efficiency.
See for more details.
5.5. MESSAGE BUS
Overview
Message bus refers to a messaging architecture, shown inFigure 5.5, “Message Bus
Pattern”, that enables you to connect diverse applications running on diverse computing
platforms. In effect, the Apache Camel and its components constitute a message bus.
141
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
Figure 5.5. Message Bus Pattern
The following features of the message bus pattern are reflected in Apache Camel:
Common communication infrastructure — The router itself provides the core of the
common communication infrastructure in Apache Camel. However, in contrast to
some message bus architectures, Apache Camel provides a heterogeneous
infrastructure: messages can be sent into the bus using a wide variety of different
transports and using a wide variety of different message formats.
Adapters — Where necessary, Apache Camel can translate message formats and
propagate messages using different transports. In effect, Apache Camel is capable
of behaving like an adapter, so that external applications can hook into the message
bus without refactoring their messaging protocols.
In some cases, it is also possible to integrate an adapter directly into an external
application. For example, if you develop an application using Apache CXF, where the
service is implemented using JAX-WS and JAXB mappings, it is possible to bind a
variety of different transports to the service. These transport bindings function as
adapters.
142
CHAPTER 6. MESSAGE CONSTRUCTION
CHAPTER 6. MESSAGE CONSTRUCTION
Abstract
The message construction patterns describe the various forms and functions of the
messages that pass through the system.
6.1. CORRELATION IDENTIFIER
Overview
The correlation identifier pattern, shown in Figure 6.1, “Correlation Identifier Pattern”,
describes how to match reply messages with request messages, given that an
asynchronous messaging system is used to implement a request-reply protocol. The
essence of this idea is that request messages should be generated with a unique token, the
request ID, that identifies the request message and reply messages should include a token,
the correlation ID, that contains the matching request ID.
Apache Camel supports the Correlation Identifier from the EIP patterns by getting or setting
a header on a Message.
When working with the ActiveMQ or JMS components, the correlation identifier header is
called JMSCorrelationID. You can add your own correlation identifier to any message
exchange to help correlate messages together in a single conversation (or business
process). A correlation identifier is usually stored in a Apache Camel message header.
Some EIP patterns spin off a sub message and, in those cases, Apache Camel adds a
correlation ID to the Exchange as a property with they key,Exchange.CORRELATION_ID,
which links back to the source Exchange. For example, the Splitter, Multicast, Recipient List,
and Wire Tap EIPs do this.
Figure 6.1. Correlation Identifier Pattern
6.2. EVENT MESSAGE
143
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
Event Message
Camel supports the Event Message from the Introducing Enterprise Integration Patterns by
supporting the Exchange Pattern on a Message which can be set to InOnly to indicate a
oneway event message. Camel Components then implement this pattern using the
underlying transport or protocols.
The default behaviour of many Components is InOnly such as forJMS, File or SEDA
Explicitly specifying InOnly
If you are using a component which defaults to InOut you can override the Exchange
Pattern for an endpoint using the pattern property.
foo:bar?exchangePattern=InOnly
From 2.0 onwards on Camel you can specify the Exchange Pattern using the dsl.
Using the Fluent Builders
from("mq:someQueue").
inOnly().
bean(Foo.class);
or you can invoke an endpoint with an explicit pattern
from("mq:someQueue").
inOnly("mq:anotherQueue");
Using the Spring XML Extensions
144
CHAPTER 6. MESSAGE CONSTRUCTION
6.3. RETURN ADDRESS
Return Address
Apache Camel supports the Return Address from the Introducing Enterprise Integration
Patterns using the JMSReplyTo header.
For example when using JMS with InOut, the component will by default be returned to the
address given in JMSReplyTo.
Example
Requestor Code
getMockEndpoint("mock:bar").expectedBodiesReceived("Bye World");
template.sendBodyAndHeader("direct:start", "World", "JMSReplyTo",
"queue:bar");
Route Using the Fluent Builders
from("direct:start").to("activemq:queue:foo?preserveMessageQos=true");
from("activemq:queue:foo").transform(body().prepend("Bye "));
from("activemq:queue:bar?disableReplyTo=true").to("mock:bar");
145
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
Route Using the Spring XML Extensions
Bye ${in.body}
For a complete example of this pattern, see this junit test case
146
CHAPTER 7. MESSAGE ROUTING
CHAPTER 7. MESSAGE ROUTING
Abstract
The message routing patterns describe various ways of linking message channels together.
This includes various algorithms that can be applied to the message stream (without
modifying the body of the message).
7.1. CONTENT-BASED ROUTER
Overview
A content-based router, shown in Figure 7.1, “Content-Based Router Pattern”, enables you
to route messages to the appropriate destination based on the message contents.
Figure 7.1. Content-Based Router Pattern
Java DSL example
The following example shows how to route a request from an input, seda:a, endpoint to
either seda:b, queue:c, or seda:d depending on the evaluation of various predicate
expressions:
RouteBuilder builder = new RouteBuilder() {
public void configure() {
from("seda:a").choice()
.when(header("foo").isEqualTo("bar")).to("seda:b")
.when(header("foo").isEqualTo("cheese")).to("seda:c")
.otherwise().to("seda:d");
}
};
XML configuration example
The following example shows how to configure the same route in XML:
$foo = 'bar'
147
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
$foo = 'cheese'
7.2. MESSAGE FILTER
Overview
A message filter is a processor that eliminates undesired messages based on specific
criteria. In Apache Camel, the message filter pattern, shown in Figure 7.2, “Message Filter
Pattern”, is implemented by the filter() Java DSL command. The filter() command
takes a single predicate argument, which controls the filter. When the predicate is true,
the incoming message is allowed to proceed, and when the predicate is false, the
incoming message is blocked.
Figure 7.2. Message Filter Pattern
Java DSL example
The following example shows how to create a route from endpoint, seda:a, to endpoint,
seda:b, that blocks all messages except for those messages whosefoo header have the
value, bar:
RouteBuilder builder = new RouteBuilder() {
public void configure() {
from("seda:a").filter(header("foo").isEqualTo("bar")).to("seda:b");
}
};
To evaluate more complex filter predicates, you can invoke one of the supported scripting
languages, such as XPath, XQuery, or SQL (see Expression and Predicate Languages). The
following example defines a route that blocks all messages except for those containing a
person element whose name attribute is equal to James:
148
CHAPTER 7. MESSAGE ROUTING
from("direct:start").
filter().xpath("/person[@name='James']").
to("mock:result");
XML configuration example
The following example shows how to configure the route with an XPath predicate in XML
(see Expression and Predicate Languages):
$foo = 'bar'
FILTERED ENDPOINT REQUIRED INSIDE TAG
Make sure you put the endpoint you want to filter (for example, ) before the closing tag or the filter will not be
applied (in 2.8+, omitting this will result in an error).
Filtering with beans
Here is an example of using a bean to define the filter behavior:
from("direct:start")
.filter().method(MyBean.class,
"isGoldCustomer").to("mock:result").end()
.to("mock:end");
public static class MyBean {
public boolean isGoldCustomer(@Header("level") String level) {
return level.equals("gold");
}
}
Using stop()
Available as of Camel 2.0
Stop is a special type of filter that filters out all messages. Stop is convenient to use in a
Content-Based Routerwhen you need to stop further processing in one of the predicates.
In the following example, we do not want messages with the word Bye in the message body
to propagate any further in the route. We prevent this in the when() predicate using
.stop().
from("direct:start")
149
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
.choice()
.when(bodyAs(String.class).contains("Hello")).to("mock:hello")
.when(bodyAs(String.class).contains("Bye")).to("mock:bye").stop()
.otherwise().to("mock:other")
.end()
.to("mock:result");
Knowing if Exchange was filtered or not
Available as of Camel 2.5
The Message Filter EIP will add a property on the Exchange which states if it was filtered or
not.
The property has the key Exchannge.FILTER_MATCHED which has the String value of
CamelFilterMatched. Its value is a boolean indicatingtrue or false. If the value is true
then the Exchange was routed in the filter block.
7.3. RECIPIENT LIST
Overview
A recipient list, shown in Figure 7.3, “Recipient List Pattern”, is a type of router that sends
each incoming message to multiple different destinations. In addition, a recipient list
typically requires that the list of recipients be calculated at run time.
Figure 7.3. Recipient List Pattern
Recipient list with fixed destinations
The simplest kind of recipient list is where the list of destinations is fixed and known in
advance, and the exchange pattern is InOnly. In this case, you can hardwire the list of
destinations into the to() Java DSL command.
150
CHAPTER 7. MESSAGE ROUTING
NOTE
The examples given here, for the recipient list with fixed destinations, work
only with the InOnly exchange pattern (similar to a pipeline). If you want to
create a recipient list for exchange patterns with Out messages, use the
multicast pattern instead.
Java DSL example
The following example shows how to route an InOnly exchange from a consumer endpoint,
queue:a, to a fixed list of destinations:
from("seda:a").to("seda:b", "seda:c", "seda:d");
XML configuration example
The following example shows how to configure the same route in XML:
Recipient list calculated at run time
In most cases, when you use the recipient list pattern, the list of recipients should be
calculated at runtime. To do this use the recipientList() processor, which takes a list of
destinations as its sole argument. Because Apache Camel applies a type converter to the
list argument, it should be possible to use most standard Java list types (for example, a
collection, a list, or an array). For more details about type converters, see Section 40.3,
“Built-In Type Converters”.
The recipients receive a copy of the same exchange instance and Apache Camel executes
them sequentially.
Java DSL example
The following example shows how to extract the list of destinations from a message header
called recipientListHeader, where the header value is a comma-separated list of
endpoint URIs:
from("direct:a").recipientList(header("recipientListHeader").tokenize(",")
);
In some cases, if the header value is a list type, you might be able to use it directly as the
argument to recipientList(). For example:
from("seda:a").recipientList(header("recipientListHeader"));
151
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
However, this example is entirely dependent on how the underlying component parses this
particular header. If the component parses the header as a simple string, this example will
not work. The header must be parsed into some type of Java list.
XML configuration example
The following example shows how to configure the preceding route in XML, where the
header value is a comma-separated list of endpoint URIs:
recipientListHeader
Sending to multiple recipients in parallel
Available as of Camel 2.2
The Recipient List supports parallelProcessing, which is similar to the corresponding
feature in Splitter. Use the parallel processing feature to send the exchange to multiple
recipients concurrently—for example:
from("direct:a").recipientList(header("myHeader")).parallelProcessing();
In Spring XML, the parallel processing feature is implemented as an attribute on the
recipientList tag—for example:
myHeader
Stop on exception
Available as of Camel 2.2
The Recipient List supports the stopOnException feature, which you can use to stop
sending to any further recipients, if any recipient fails.
from("direct:a").recipientList(header("myHeader")).stopOnException();
And in Spring XML its an attribute on the recipient list tag.
In Spring XML, the stop on exception feature is implemented as an attribute on the
recipientList tag—for example:
152
CHAPTER 7. MESSAGE ROUTING
myHeader
NOTE
You can combine parallelProcessing and stopOnException in the same
route.
Ignore invalid endpoints
Available as of Camel 2.3
The Recipient List supports the ignoreInvalidEndpoints option, which enables the
recipient list to skip invalid endpoints (Routing Slip also supports this option). For example:
from("direct:a").recipientList(header("myHeader")).ignoreInvalidEndpoints(
);
And in Spring XML, you can enable this option by setting the ignoreInvalidEndpoints
attribute on the recipientList tag, as follows
myHeader
Consider the case where myHeader contains the two endpoints, direct:foo,xxx:bar. The
first endpoint is valid and works. The second is invalid and, therefore, ignored. Apache
Camel logs at INFO level whenever an invalid endpoint is encountered.
Using custom AggregationStrategy
Available as of Camel 2.2
You can use a custom AggregationStrategy with the Recipient List, which is useful for
aggregating replies from the recipients in the list. By default, Apache Camel uses the
UseLatestAggregationStrategy aggregation strategy, which keeps just the last received
reply. For a more sophisticated aggregation strategy, you can define your own
implementation of the AggregationStrategy interface—see Aggregator EIP for details. For
example, to apply the custom aggregation strategy, MyOwnAggregationStrategy, to the
reply messages, you can define a Java DSL route as follows:
from("direct:a")
.recipientList(header("myHeader")).aggregationStrategy(new
MyOwnAggregationStrategy())
.to("direct:b");
153
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
In Spring XML, you can specify the custom aggregation strategy as an attribute on the
recipientList tag, as follows:
myHeader
Using custom thread pool
Available as of Camel 2.2
This is only needed when you use parallelProcessing. By default Camel uses a thread
pool with 10 threads. Notice this is subject to change when we overhaul thread pool
management and configuration later (hopefully in Camel 2.2).
You configure this just as you would with the custom aggregation strategy.
Using method call as recipient list
You can use a Bean to provide the recipients, for example:
from("activemq:queue:test").recipientList().method(MessageRouter.class,
"routeTo");
Where the MessageRouter bean is defined as follows:
public class MessageRouter {
public String routeTo() {
String queueName = "activemq:queue:test2";
return queueName;
}
}
Bean as recipient list
You can make a bean behave as a recipient list by adding the @RecipientList annotation
to a methods that returns a list of recipients. For example:
public class MessageRouter {
@RecipientList
public String routeTo() {
String queueList = "activemq:queue:test1,activemq:queue:test2";
return queueList;
}
}
154
CHAPTER 7. MESSAGE ROUTING
In this case, do not include the recipientList DSL command in the route. Define the route
as follows:
from("activemq:queue:test").bean(MessageRouter.class, "routeTo");
Using timeout
Available as of Camel 2.5
If you use parallelProcessing, you can configure a total timeout value in milliseconds.
Camel will then process the messages in parallel until the timeout is hit. This allows you to
continue processing if one message is slow.
In the example below, the recipientlist header has the value,
direct:a,direct:b,direct:c, so that the message is sent to three recipients. We have a
timeout of 250 milliseconds, which means only the last two messages can be completed
within the timeframe. The aggregation therefore yields the string result, BC.
from("direct:start")
.recipientList(header("recipients"), ",")
.aggregationStrategy(new AggregationStrategy() {
public Exchange aggregate(Exchange oldExchange, Exchange
newExchange) {
if (oldExchange == null) {
return newExchange;
}
String body = oldExchange.getIn().getBody(String.class);
oldExchange.getIn().setBody(body +
newExchange.getIn().getBody(String.class));
return oldExchange;
}
})
.parallelProcessing().timeout(250)
// use end to indicate end of recipientList clause
.end()
.to("mock:result");
from("direct:a").delay(500).to("mock:A").setBody(constant("A"));
from("direct:b").to("mock:B").setBody(constant("B"));
from("direct:c").to("mock:C").setBody(constant("C"));
NOTE
This timeout feature is also supported by splitter and both multicast and
recipientList.
By default if a timeout occurs the AggregationStrategy is not invoked. However you can
implement a specialized version
// Java
public interface TimeoutAwareAggregationStrategy extends
155
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
AggregationStrategy {
/**
* A timeout occurred
*
* @param oldExchange the oldest exchange (is <tt>null</tt> on
first aggregation as we only have the new exchange)
* @param index
the index
* @param total
the total
* @param timeout
the timeout value in millis
*/
void timeout(Exchange oldExchange, int index, int total, long
timeout);
This allows you to deal with the timeout in the AggregationStrategy if you really need to.
TIMEOUT IS TOTAL
The timeout is total, which means that after X time, Camel will aggregate the
messages which has completed within the timeframe. The remainders will be
cancelled. Camel will also only invoke the timeout method in the
TimeoutAwareAggregationStrategy once, for the first index which caused
the timeout.
Apply custom processing to the outgoing messages
Before recipientList sends a message to one of the recipient endpoints, it creates a
message replica, which is a shallow copy of the original message. If you want to perform
some custom processing on each message replica before the replica is sent to its endpoint,
you can invoke the onPrepare DSL command in the recipientList clause. The onPrepare
command inserts a custom processor just after the message has been shallow-copied and
just before the message is dispatched to its endpoint. For example, in the following route,
the CustomProc processor is invoked on the message replica foreach recipient endpoint:
from("direct:start")
.recipientList().onPrepare(new CustomProc());
A common use case for the onPrepare DSL command is to perform a deep copy of some or
all elements of a message. This allows each message replica to be modified independently
of the others. For example, the following CustomProc processor class performs a deep copy
of the message body, where the message body is presumed to be of type, BodyType, and
the deep copy is performed by the method, BodyType.deepCopy().
// Java
import org.apache.camel.*;
...
public class CustomProc implements Processor {
public void process(Exchange exchange) throws Exception {
BodyType body = exchange.getIn().getBody(BodyType.class);
// Make a _deep_ copy of of the body object
BodyType clone = BodyType.deepCopy();
exchange.getIn().setBody(clone);
156
CHAPTER 7. MESSAGE ROUTING
// Headers and attachments have already been
// shallow-copied. If you need deep copies,
// add some more code here.
}
}
Options
The recipientList DSL command supports the following options:
Name
Default Value
Description
delimiter
,
Delimiter used if the
Expression returned multiple
endpoints.
strategyRef
parallelProcessing
executorServiceRef
Refers to an
AggregationStrategy to be
used to assemble the replies
from the recipients, into a
single outgoing message from
the Recipient List. By default
Camel will use the last reply
as the outgoing message.
false
Camel 2.2: If enables then
sending messages to the
recipients occurs
concurrently. Note the caller
thread will still wait until all
messages has been fully
processed, before it
continues. Its only the
sending and processing the
replies from the recipients
which happens concurrently.
Camel 2.2: Refers to a
custom Thread Pool to be
used for parallel processing.
Notice if you set this option,
then parallel processing is
automatic implied, and you do
not have to enable that option
as well.
157
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
stopOnException
false
Camel 2.2: Whether or not to
stop continue processing
immediately when an
exception occurred. If disable,
then Camel will send the
message to all recipients
regardless if one of them
failed. You can deal with
exceptions in the
AggregationStrategy class
where you have full control
how to handle that.
ignoreInvalidEndpoints
false
Camel 2.3: If an endpoint uri
could not be resolved, should
it be ignored. Otherwise
Camel will thrown an
exception stating the
endpoint uri is not valid.
streaming
false
Camel 2.5: If enabled then
Camel will process replies
out-of-order, eg in the order
they come back. If disabled,
Camel will process replies in
the same order as the
Expression specified.
timeout
Camel 2.5: Sets a total
timeout specified in millis. If
the Recipient List hasn't been
able to send and process all
replies within the given
timeframe, then the timeout
triggers and the Recipient List
breaks out and continues.
Notice if you provide a
TimeoutAwareAggregationStra
tegy then the timeout
method is invoked before
breaking out.
onPrepareRef
Camel 2.8: Refers to a
custom Processor to prepare
the copy of the Exchange
each recipient will receive.
This allows you to do any
custom logic, such as deepcloning the message payload
if that's needed etc.
158
CHAPTER 7. MESSAGE ROUTING
shareUnitOfWork
false
Camel 2.8: Whether the unit
of work should be shared. See
the same option on Splitter for
more details.
7.4. SPLITTER
Overview
A splitter is a type of router that splits an incoming message into a series of outgoing
messages. Each of the outgoing messages contains a piece of the original message. In
Apache Camel, the splitter pattern, shown in Figure 7.4, “Splitter Pattern”, is implemented
by the split() Java DSL command.
Figure 7.4. Splitter Pattern
The Apache Camel splitter actually supports two patterns, as follows:
Simple splitter—implements the splitter pattern on its own.
Splitter/aggregator—combines the splitter pattern with the aggregator pattern, such
that the pieces of the message are recombined after they have been processed.
Java DSL example
The following example defines a route from seda:a to seda:b that splits messages by
converting each line of an incoming message into a separate outgoing message:
RouteBuilder builder = new RouteBuilder() {
public void configure() {
from("seda:a")
.split(bodyAs(String.class).tokenize("\n"))
.to("seda:b");
}
};
The splitter can use any expression language, so you can split messages using any of the
supported scripting languages, such as XPath, XQuery, or SQL (see Part II, “Routing
Expression and Predicate Languages”). The following example extractsbar elements from
an incoming message and insert them into separate outgoing messages:
159
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
from("activemq:my.queue")
.split(xpath("//foo/bar"))
.to("file://some/directory")
XML configuration example
The following example shows how to configure a splitter route in XML, using the XPath
scripting language:
//foo/bar
You can use the tokenize expression in the XML DSL to split bodies or headers using a
token, where the tokenize expression is defined using the tokenize element. In the
following example, the message body is tokenized using the \n separator character. To use
a regular expression pattern, set regex=true in the tokenize element.
Splitting into groups of lines
To split a big file into chunks of 1000 lines, you can define a splitter route as follows in the
Java DSL:
from("file:inbox")
.split().tokenize("\n", 1000).streaming()
.to("activemq:queue:order");
The second argument to tokenize specifies the number of lines that should be grouped
into a single chunk. The streaming() clause directs the splitter not to read the whole file at
once (resulting in much better performance if the file is large).
The same route can be defined in XML DSL as follows:
160
CHAPTER 7. MESSAGE ROUTING
The output when using the group option is always of java.lang.String type.
Splitter reply
If the exchange that enters the splitter has the InOut message-exchange pattern (that is, a
reply is expected), the splitter returns a copy of the original input message as the reply
message in the Out message slot. You can override this default behavior by implementing
your own aggregation strategy.
Parallel execution
If you want to execute the resulting pieces of the message in parallel, you can enable the
parallel processing option, which instantiates a thread pool to process the message pieces.
For example:
XPathBuilder xPathBuilder = new XPathBuilder("//foo/bar");
from("activemq:my.queue").split(xPathBuilder).parallelProcessing().to("act
ivemq:my.parts");
You can customize the underlying ThreadPoolExecutor used in the parallel splitter. For
example, you can specify a custom executor in the Java DSL as follows:
XPathBuilder xPathBuilder = new XPathBuilder("//foo/bar");
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(8, 16, 0L,
TimeUnit.MILLISECONDS, new LinkedBlockingQueue());
from("activemq:my.queue")
.split(xPathBuilder)
.parallelProcessing()
.executorService(threadPoolExecutor)
.to("activemq:my.parts");
You can specify a custom executor in the XML DSL as follows:
/invoice/lineItems
161
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
Using a bean to perform splitting
As the splitter can use any expression to do the splitting, we can use a bean to perform
splitting, by invoking the method() expression. The bean should return an iterable value
such as: java.util.Collection, java.util.Iterator, or an array.
The following route defines a method() expression that calls a method on the
mySplitterBean bean instance:
from("direct:body")
// here we use a POJO bean mySplitterBean to do the split of the
payload
.split()
.method("mySplitterBean", "splitBody")
.to("mock:result");
from("direct:message")
// here we use a POJO bean mySplitterBean to do the split of the
message
// with a certain header value
.split()
.method("mySplitterBean", "splitMessage")
.to("mock:result");
Where mySplitterBean is an instance of the MySplitterBean class, which is defined as
follows:
public class MySplitterBean {
/**
* The split body method returns something that is iteratable such as
a java.util.List.
*
* @param body the payload of the incoming message
* @return a list containing each part split
*/
public List splitBody(String body) {
// since this is based on an unit test you can of couse
// use different logic for splitting as Apache Camel have out
// of the box support for splitting a String based on comma
// but this is for show and tell, since this is java code
// you have the full power how you like to split your messages
List answer = new ArrayList();
String[] parts = body.split(",");
for (String part : parts) {
answer.add(part);
}
return answer;
}
/**
* The split message method returns something that is iteratable such
162
CHAPTER 7. MESSAGE ROUTING
as a java.util.List.
*
* @param header the header of the incoming message with the name user
* @param body the payload of the incoming message
* @return a list containing each part split
*/
public List splitMessage(@Header(value = "user") String
header, @Body String body) {
// we can leverage the Parameter Binding Annotations
// http://camel.apache.org/parameter-binding-annotations.html
// to access the message header and body at same time,
// then create the message that we want, splitter will
// take care rest of them.
// *NOTE* this feature requires Apache Camel version >= 1.6.1
List answer = new ArrayList();
String[] parts = header.split(",");
for (String part : parts) {
DefaultMessage message = new DefaultMessage();
message.setHeader("user", part);
message.setBody(body);
answer.add(message);
}
return answer;
}
}
Exchange properties
The following properties are set on each split exchange:
header
type
description
CamelSplitIndex
int
Apache Camel 2.0: A split
counter that increases for
each Exchange being split.
The counter starts from 0.
CamelSplitSize
int
Apache Camel 2.0: The total
number of Exchanges that
was split. This header is not
applied for stream based
splitting.
CamelSplitComplete
boolean
Apache Camel 2.4: Whether or
not this Exchange is the last.
Splitter/aggregator pattern
It is a common pattern for the message pieces to be aggregated back into a single
exchange, after processing of the individual pieces has completed. To support this pattern,
the split() DSL command lets you provide an AggregationStrategy object as the second
argument.
163
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
Java DSL example
The following example shows how to use a custom aggregation strategy to recombine a
split message after all of the message pieces have been processed:
from("direct:start")
.split(body().tokenize("@"), new MyOrderStrategy())
// each split message is then send to this bean where we can
process it
.to("bean:MyOrderService?method=handleOrder")
// this is important to end the splitter route as we do not want
to do more routing
// on each split message
.end()
// after we have split and handled each message we want to send a
single combined
// response back to the original caller, so we let this bean build it
for us
// this bean will receive the result of the aggregate strategy:
MyOrderStrategy
.to("bean:MyOrderService?method=buildCombinedResponse")
AggregationStrategy implementation
The custom aggregation strategy, MyOrderStrategy, used in the preceding route is
implemented as follows:
/**
* This is our own order aggregation strategy where we can control
* how each split message should be combined. As we do not want to
* lose any message, we copy from the new to the old to preserve the
* order lines as long we process them
*/
public static class MyOrderStrategy implements AggregationStrategy {
public Exchange aggregate(Exchange oldExchange, Exchange newExchange)
{
// put order together in old exchange by adding the order from new
exchange
if (oldExchange == null) {
// the first time we aggregate we only have the new exchange,
// so we just return it
return newExchange;
}
String orders = oldExchange.getIn().getBody(String.class);
String newLine = newExchange.getIn().getBody(String.class);
LOG.debug("Aggregate old orders: " + orders);
LOG.debug("Aggregate new order: " + newLine);
// put orders together separating by semi colon
orders = orders + ";" + newLine;
// put combined order back on old to preserve it
164
CHAPTER 7. MESSAGE ROUTING
oldExchange.getIn().setBody(orders);
// return old as this is the one that has all the orders gathered
until now
return oldExchange;
}
}
Stream based processing
When parallel processing is enabled, it is theoretically possible for a later message piece to
be ready for aggregation before an earlier piece. In other words, the message pieces might
arrive at the aggregator out of order. By default, this does not happen, because the splitter
implementation rearranges the message pieces back into their original order before
passing them into the aggregator.
If you would prefer to aggregate the message pieces as soon as they are ready (and
possibly out of order), you can enable the streaming option, as follows:
from("direct:streaming")
.split(body().tokenize(","), new MyOrderStrategy())
.parallelProcessing()
.streaming()
.to("activemq:my.parts")
.end()
.to("activemq:all.parts");
You can also supply a custom iterator to use with streaming, as follows:
// Java
import static org.apache.camel.builder.ExpressionBuilder.beanExpression;
...
from("direct:streaming")
.split(beanExpression(new MyCustomIteratorFactory(), "iterator"))
.streaming().to("activemq:my.parts")
STREAMING AND XPATH
You cannot use streaming mode in conjunction with XPath. XPath requires the
complete DOM XML document in memory.
Stream based processing with XML
If an incoming messages is a very large XML file, you can process the message most
efficiently using the tokenizeXML sub-command in streaming mode.
For example, given a large XML file that contains a sequence of order elements, you can
split the file into order elements using a route like the following:
from("file:inbox")
.split().tokenizeXML("order").streaming()
.to("activemq:queue:order");
You can do the same thing in XML, by defining a route like the following:
165
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
It is often the case that you need access to namespaces that are defined in one of the
enclosing (ancestor) elements of the token elements. You can copy namespace definitions
from one of the ancestor elements into the token element, by specifing which element you
want to inherit namespace definitions from.
In the Java DSL, you specify the ancestor element as the second argument of tokenizeXML.
For example, to inherit namespace definitions from the enclosing orders element:
from("file:inbox")
.split().tokenizeXML("order", "orders").streaming()
.to("activemq:queue:order");
In the XML DSL, you specify the ancestor element using the inheritNamespaceTagName
attribute. For example:
Options
The split DSL command supports the following options:
Name
strategyRef
166
Default Value
Description
Refers to an
AggregationStrategy to be
used to assemble the replies
from the sub-messages, into a
single outgoing message from
the Splitter. See the section
titled What does the splitter
return below for whats used
by default.
CHAPTER 7. MESSAGE ROUTING
parallelProcessing
false
executorServiceRef
If enables then processing the
sub-messages occurs
concurrently. Note the caller
thread will still wait until all
sub-messages has been fully
processed, before it
continues.
Refers to a custom Thread
Pool to be used for parallel
processing. Notice if you set
this option, then parallel
processing is automatic
implied, and you do not have
to enable that option as well.
stopOnException
false
Camel 2.2: Whether or not to
stop continue processing
immediately when an
exception occurred. If disable,
then Camel continue splitting
and process the submessages regardless if one of
them failed. You can deal with
exceptions in the
AggregationStrategy class
where you have full control
how to handle that.
streaming
false
If enabled then Camel will
split in a streaming fashion,
which means it will split the
input message in chunks. This
reduces the memory
overhead. For example if you
split big messages its
recommended to enable
streaming. If streaming is
enabled then the submessage replies will be
aggregated out-of-order, eg in
the order they come back. If
disabled, Camel will process
sub-message replies in the
same order as they where
splitted.
167
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
timeout
Camel 2.5: Sets a total
timeout specified in millis. If
the Recipient List hasn't been
able to split and process all
replies within the given
timeframe, then the timeout
triggers and the Splitter
breaks out and continues.
Notice if you provide a
TimeoutAwareAggregationStra
tegy then the timeout
method is invoked before
breaking out.
onPrepareRef
Camel 2.8: Refers to a
custom Processor to prepare
the sub-message of the
Exchange, before its
processed. This allows you to
do any custom logic, such as
deep-cloning the message
payload if that's needed etc.
shareUnitOfWork
false
Camel 2.8: Whether the unit
of work should be shared. See
further below for more details.
7.5. AGGREGATOR
Overview
The aggregator pattern, shown in Figure 7.5, “Aggregator Pattern”, enables you to combine
a batch of related messages into a single message.
Figure 7.5. Aggregator Pattern
To control the aggregator's behavior, Apache Camel allows you to specify the properties
described in Enterprise Integration Patterns, as follows:
Correlation expression — Determines which messages should be aggregated
together. The correlation expression is evaluated on each incoming message to
produce a correlation key. Incoming messages with the same correlation key are
168
CHAPTER 7. MESSAGE ROUTING
then grouped into the same batch. For example, if you want to aggregate all
incoming messages into a single message, you can use a constant expression.
Completeness condition — Determines when a batch of messages is complete. You
can specify this either as a simple size limit or, more generally, you can specify a
predicate condition that flags when the batch is complete.
Aggregation algorithm — Combines the message exchanges for a single correlation
key into a single message exchange.
For example, consider a stock market data system that receives 30,000 messages per
second. You might want to throttle down the message flow if your GUI tool cannot cope with
such a massive update rate. The incoming stock quotes can be aggregated together simply
by choosing the latest quote and discarding the older prices. (You can apply a delta
processing algorithm, if you prefer to capture some of the history.)
How the aggregator works
Figure 7.6, “Aggregator Implementation” shows an overview of how the aggregator works,
assuming it is fed with a stream of exchanges that have correlation keys such as A, B, C, or
D.
Figure 7.6. Aggregator Implementation
The incoming stream of exchanges shown in Figure 7.6, “Aggregator Implementation” is
processed as follows:
1. The correlator is responsible for sorting exchanges based on the correlation key. For
each incoming exchange, the correlation expression is evaluated, yielding the
correlation key. For example, for the exchange shown in Figure 7.6, “Aggregator
Implementation”, the correlation key evaluates to A.
2. The aggregation strategy is responsible for merging exchanges with the same
correlation key. When a new exchange, A, comes in, the aggregator looks up the
corresponding aggregate exchange, A', in the aggregation repository and combines
it with the new exchange.
Until a particular aggregation cycle is completed, incoming exchanges are
continuously aggregated with the corresponding aggregate exchange. An
aggregation cycle lasts until terminated by one of the completion mechanisms.
169
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
3. If a completion predicate is specified on the aggregator, the aggregate exchange is
tested to determine whether it is ready to be sent to the next processor in the
route. Processing continues as follows:
If complete, the aggregate exchange is processed by the latter part of the route.
There are two alternative models for this: synchronous (the default), which
causes the calling thread to block, or asynchronous (if parallel processing is
enabled), where the aggregate exchange is submitted to an executor thread
pool (as shown in Figure 7.6, “Aggregator Implementation”).
If not complete, the aggregate exchange is saved back to the aggregation
repository.
4. In parallel with the synchronous completion tests, it is possible to enable an
asynchronous completion test by enabling either the completionTimeout option or
the completionInterval option. These completion tests run in a separate thread
and, whenever the completion test is satisfied, the corresponding exchange is
marked as complete and starts to be processed by the latter part of the route
(either synchronously or asynchronously, depending on whether parallel processing
is enabled or not).
5. If parallel processing is enabled, a thread pool is responsible for processing
exchanges in the latter part of the route. By default, this thread pool contains ten
threads, but you have the option of customizing the pool (the section called
“Threading options”).
Java DSL example
The following example aggregates exchanges with the same StockSymbol header value,
using the UseLatestAggregationStrategy aggregation strategy. For a given StockSymbol
value, if more than three seconds elapse since the last exchange with that correlation key
was received, the aggregated exchange is deemed to be complete and is sent to the mock
endpoint.
from("direct:start")
.aggregate(header("id"), new UseLatestAggregationStrategy())
.completionTimeout(3000)
.to("mock:aggregated");
XML DSL example
The following example shows how to configure the same route in XML:
header.StockSymbol
170
CHAPTER 7. MESSAGE ROUTING
Specifying the correlation expression
In the Java DSL, the correlation expression is always passed as the first argument to the
aggregate() DSL command. You are not limited to using the Simple expression language
here. You can specify a correlation expression using any of the expression languages or
scripting languages, such as XPath, XQuery, SQL, and so on.
For exampe, to correlate exchanges using an XPath expression, you could use the following
Java DSL route:
from("direct:start")
.aggregate(xpath("/stockQuote/@symbol"), new
UseLatestAggregationStrategy())
.completionTimeout(3000)
.to("mock:aggregated");
If the correlation expression cannot be evaluated on a particular incoming exchange, the
aggregator throws a CamelExchangeException by default. You can suppress this exception
by setting the ignoreInvalidCorrelationKeys option. For example, in the Java DSL:
from(...).aggregate(...).ignoreInvalidCorrelationKeys()
In the XML DSL, you can set the ignoreInvalidCorrelationKeys option is set as an
attribute, as follows:
...
Specifying the aggregation strategy
In Java DSL, you can either pass the aggregation strategy as the second argument to the
aggregate() DSL command or specify it using theaggregationStrategy() clause. For
example, you can use the aggregationStrategy() clause as follows:
from("direct:start")
.aggregate(header("id"))
.aggregationStrategy(new UseLatestAggregationStrategy())
.completionTimeout(3000)
.to("mock:aggregated");
Apache Camel provides the following basic aggregation strategies (where the classes
belong to the org.apache.camel.processor.aggregate Java package):
UseLatestAggregationStrategy
171
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
Return the last exchange for a given correlation key, discarding all earlier exchanges
with this key. For example, this strategy could be useful for throttling the feed from a
stock exchange, where you just want to know the latest price of a particular stock
symbol.
UseOriginalAggregationStrategy
Return the first exchange for a given correlation key, discarding all later exchanges with
this key. You must set the first exchange by calling
UseOriginalAggregationStrategy.setOriginal() before you can use this strategy.
GroupedExchangeAggregationStrategy
Concatenates all of the exchanges for a given correlation key into a list, which is stored
in the Exchange.GROUPED_EXCHANGE exchange property. See the section called “Grouped
exchanges”.
Implementing a custom aggregation strategy
If you want to apply a different aggregation strategy, you can implement one of the
following aggregation strategy base interfaces:
org.apache.camel.processor.aggregate.AggregationStrategy
The basic aggregation strategy interface.
org.apache.camel.processor.aggregate.TimeoutAwareAggregationStrategy
Implement this interface, if you want your implementation to receive a notification when
an aggregation cycle times out. The timeout notification method has the following
signature:
void timeout(Exchange oldExchange, int index, int total, long timeout)
org.apache.camel.processor.aggregate.CompletionAwareAggregationStrategy
Implement this interface, if you want your implementation to receive a notification when
an aggregation cycle completes normally. The notification method has the following
signature:
void onCompletion(Exchange exchange)
For example, the following code shows two different custom aggregation strategies,
StringAggregationStrategy and ArrayListAggregationStrategy::
//simply combines Exchange String body values using '+' as a delimiter
class StringAggregationStrategy implements AggregationStrategy {
public Exchange aggregate(Exchange oldExchange, Exchange newExchange)
{
if (oldExchange == null) {
return newExchange;
}
String oldBody = oldExchange.getIn().getBody(String.class);
172
CHAPTER 7. MESSAGE ROUTING
String newBody = newExchange.getIn().getBody(String.class);
oldExchange.getIn().setBody(oldBody + "+" + newBody);
return oldExchange;
}
}
//simply combines Exchange body values into an ArrayList
Route processing steps
Given the preceding route definition, any message whose operation name matches
getCustomerStatus would be processed as follows:
1. To facilitate processing the payload body, the first step uses convertBodyTo to
convert the body type from org.apache.camel.component.cxf.CxfPayload (the
default payload type) to org.w3c.dom.Node.
2. The route then applies an XPath expression to the message in order to extract the
customer ID value and then stashes it in the customerId header.
3. The next step sends the message to the getCustomerStatus bean, which does
whatever processing is required to get the customer status for the specified
customer ID. The results from this step are stashed in message headers.
4. Finally, a response is generated using a velocity template.
NOTE
A common pattern when implementing Apache Camel routes is to use
message headers as a temporary stash to hold intermediate results (you could
also use exchange properties in the same way).
Converting XPath result to a string
Because the default return type of XPath is a node list, you must explicitly convert the
result to a string, if you want to obtain the string contents of an element. There are two
alternative ways of obtaining the string value of an element:
Specify the result type explicitly using the resultType attribute, as follows:
417
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
/cus:getCustomerStatus/customerId
Modify the expression so that it returns a text() node, which automatically
converts to string:
/cus:getCustomerStatus/customerId/text()
getCustomerStatus processor bean
The getCustomerStatus processor bean is an instance of the GetCustomerStatus
processor class, which is defined as follows:
// Java
package com.fusesource.customerwscamelcxfpayload;
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
public class GetCustomerStatus implements Processor
{
public void process(Exchange exchng) throws Exception {
String id = exchng.getIn().getHeader("customerId", String.class);
// Maybe do some kind of lookup here!
//
exchng.getIn().setHeader("status", "Away");
exchng.getIn().setHeader("statusMessage", "Going to sleep.");
}
}
The implementation shown here is just a placeholder. In a realistic application you would
perform some sort of checks or database lookup to obtain the customer status. In the
demonstration code, however, the status and statusMessage are simply set to constant
values and stashed in message headers.
In the preceding code, we make the modifications directly to the In message. When the
exchange's Out message is null, the next processor in the route gets a copy of the current
In message instead
NOTE
An exceptional case occurs when the message exchange pattern is inOnly, in
which case the Out message value is always copied into the In message, even
if it is null.
getCustomerStatusResponse.vm Velocity template
You can generate a response message very simply using a Velocity template. The Velocity
template consists of a message in plain text, where specific pieces of data can be inserted
using expressions—for example, the expression ${header.HeaderName} substitutes the
value of a named header.
418
CHAPTER 36. PAYLOAD-BASED ROUTE
The Velocity template for generating the getCustomerStatus reponse is located in the
customer-ws-camel-cxf-payload/src/main/resources directory and it contains the
following template script:
${headers.status}
${headers.statusMessage}
36.6. TYPECONVERTER FOR CXFPAYLOAD
Overview
Apache Camel supports a type converter mechanism, which is used to perform implicit and
explicit type conversions of message bodies and message headers. The payload
demonstration requires a customer type converter that can convert String objects to
CXFPayload objects. This type converter automatically gets invoked at the end of the
Camel route, when the generated response message (which is a String type) gets
converted into a CXFPayload object.
String to CXFPayload type converter
The String to CXFPayload type converter is implemented in the
AdditionalCxfPayloadConverters class, as follows:
// Java
package com.fusesource.customerwscamelcxfpayload;
import
import
import
import
import
import
import
import
import
import
import
import
import
import
import
import
import
java.io.ByteArrayInputStream;
java.io.StringWriter;
java.util.ArrayList;
java.util.List;
javax.xml.transform.OutputKeys;
javax.xml.transform.Transformer;
javax.xml.transform.TransformerFactory;
javax.xml.transform.dom.DOMSource;
javax.xml.transform.stream.StreamResult;
org.apache.camel.Converter;
org.apache.camel.component.cxf.CxfPayload;
org.apache.cxf.binding.soap.SoapHeader;
org.slf4j.Logger;
org.slf4j.LoggerFactory;
org.w3c.dom.Document;
org.w3c.dom.Element;
org.w3c.dom.Node;
@Converter
public class AdditionalCxfPayloadConverters {
...
@Converter
public static CxfPayload toCxfPayload(String xml) {
// System.out.println("To CxfPayload " + xml);
List elements = new ArrayList();
419
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
try {
Document doc = b.newDocumentBuilder().parse(new
ByteArrayInputStream(xml.getBytes()));
elements.add(doc.getDocumentElement());
} catch (Exception ex) {
log.warn("Exception while converting String payload to
CxfPayload; resulting payload will be empty.");
}
// The CxfPayload is changed to use Source object under layer, the
elements API only work if we already setup the list before creating the
CxfPayload
CxfPayload ret = new CxfPayload(null,
elements);
return ret;
}
...
}
Reference
For full details of the type converter mechanism in Apache Camel, see Section 40.3, “BuiltIn Type Converters” and Chapter 42, Type Converters.
36.7. DEPLOY TO OSGI
Overview
One of the options for deploying the payload-based route is to package it as an OSGi
bundle and deploy it into an OSGi container such as Red Hat JBoss Fuse. Some of the
advantages of an OSGi deployment include:
Bundles are a relatively lightweight deployment option (because dependencies can
be shared between deployed bundles).
OSGi provides sophisticated dependency management, ensuring that only versionconsistent dependencies are added to the bundle's classpath.
Using the Maven bundle plug-in
The Maven bundle plug-in is used to package your project as an OSGi bundle, in
preparation for deployment into the OSGi container. There are two essential modifications
to make to your project's pom.xml file:
1. Change the packaging type to bundle (by editing the value of the
project/packaging element in the POM).
2. Add the Maven bundle plug-in to your POM file and configure it as appropriate.
Configuring the Maven bundle plug-in is quite a technical task (although the default settings
are often adequate). For full details of how to customize the plug-in configuration, consult
Deploying into the OSGi Container and Managing OSGi Dependencies.
Sample bundle plug-in configuration
420
CHAPTER 36. PAYLOAD-BASED ROUTE
The following POM fragment shows a sample configuration of the Maven bundle plug-in,
which is appropriate for the current example.
...
com.fusesource.byexample.cxf-webinars
customer-ws-camel-cxf-payload
customer-ws-camel-cxf-payload
bundle
...
...
org.apache.felix
maven-bundle-plugin
true
org.apache.camel.component.velocity,
META-INF.cxf,
META-INF.cxf.osgi,
javax.jws,
javax.wsdl,
javax.xml.bind,
javax.xml.bind.annotation,
javax.xml.namespace,
javax.xml.ws,
org.w3c.dom,
org.apache.xpath.jaxp,
*
org.apache.cxf.*,
org.springframework.beans.*
...
Dynamic imports
The Java packages from Apache CXF and the Spring API are imported using dynamic
imports (specified using the DynamicImport-Package element). This is a pragmatic way of
dealing with the fact that Spring XML files are not terribly well integrated with the Maven
bundle plug-in. At build time, the Maven bundle plug-in is not able to figure out which Java
421
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
classes are required by the Spring XML code. By listing wildcarded package names in the
DynamicImport-Package element, however, you allow the OSGi container to figure out
which Java classes are needed by the Spring XML code at run time.
NOTE
In general, using DynamicImport-Package headers is not recommended in
OSGi, because it short-circuits OSGi version checking. Normally, what should
happen is that the Maven bundle plug-in lists the Java packages used at build
time, along with their versions, in the Import-Package header. At deploy time,
the OSGi container then checks that the available Java packages are
compatible with the build time versions listed in the Import-Package header.
With dynamic imports, this version checking cannot be performed.
Build and deploy the client bundle
After you have configured the POM file, you can build the Maven project and install it in
your local repository by entering the following command:
mvn install
Install the camel-velocity feature, which is needed for this example:
karaf@root> features:install camel-velocity
To deploy the route bundle, enter the following command at the console:
karaf@root> install -s mvn:com.fusesource.byexample.cxf-webinars/customerws-camel-cxf-payload
NOTE
If your local Maven repository is stored in a non-standard location, you might
need to customize the value of the
org.ops4j.pax.url.mvn.localRepository property in the
InstallDir/etc/org.ops4j.pax.url.mvn.cfg file, before you can use the
mvn: scheme to access Maven artifacts.
422
CHAPTER 37. PROVIDER-BASED ROUTE
CHAPTER 37. PROVIDER-BASED ROUTE
37.1. PROVIDER-BASED JAX-WS ENDPOINT
Overview
Use the provider-based approach, if you need to process very large Web services
messages. The provider-based approach is a variant of the PAYLOAD data format that
enables you to encode the message body as an XML streaming type, such as SAXSource.
Since the XMLstreaming types are more efficient than DOM objects, the provider-based
approach is ideal for large XML messages.
Demonstration location
The code presented in this chapter is taken from the following demonstration:
cxf-webinars-jboss-fuse-6.1/customer-ws-camel-cxf-provider
For details of how to download and install the demonstration code, see Chapter 31,
Demonstration Code for Camel/CXF
Camel CXF component
The Camel CXF component is an Apache CXF component that integrates Web services with
routes. You can use it either to instantiate consumer endpoints (at the start of a route),
which behave like Web service instances, or to instantiate producer endpoints (at any other
points in the route), which behave like WS clients.
NOTE
Came CXF endpoints—which are instantiated using the cxf:cxfEndpoint XML
element and are implemented by the Apache Camel project—are not to be
confused with the Apache CXF JAX-WS endpoints—which are instantiated using
the jaxws:endpoint XML element and are implemented by the Apache CXF
project.
Provider-based approach and the PAYLOAD data format
The provider-based approach is a variant of the PAYLOAD data format, which is enabled as
follows:
Define a custom javax.xml.ws.Provider class, where the
StreamType type is an XML streaming type, such asSAXSource.
The PAYLOAD data format is selected by an annotation on the custom Provider>
class (see the section called “The SAXSourceService provider class”).
The custom Provider> class is referenced by setting theserviceClass attribute
of the cxf:cxfEndpoint element in XML configuration.
The provider-based approach has the following characteristics:
423
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
Enables you to access the message body as a streamed XML type—for example,
javax.xml.transform.sax.SAXSource.
No JAX-WS or JAX-B stub code required.
The SOAP body is marshalled into a stream-based SAXSource type.
The SOAP headers are converted into headers in the exchange's In message, of
org.apache.cxf.binding.soap.SoapHeader type.
Implementing and building a provider-based route
To implement and build the demonstration provider-based route, starting from scratch, you
would perform the following steps:
1. Define a custom javax.xml.ws.Provider class (the current
demonstration uses SAXSource as the StreamType type).
2. Instantiate the Camel CXF endpoint in Spring, using the cxf:cxfEndpoint element
and reference the custom provider class (using the serviceClass attribute).
3. Implement the route in XML, where you can use the content-based router to sort
requests by operation name.
4. For each operation, define a processor bean to process the request.
5. Define velocity templates for generating the reponse messages.
6. Define a custom type converter, to support converting a String message body to a
SAXSource message body.
Sample provider-based route
Figure 37.1, “Sample Provider-Based Route” shows an outline of the route that is used to
process the operations of the CustomerService Web service using the provider-based
approach. After sorting the request messages by operation name, an operation-specific
processor bean reads the incoming request parameters. Finally, the response messages
are generated using Velocity templates.
Figure 37.1. Sample Provider-Based Route
424
CHAPTER 37. PROVIDER-BASED ROUTE
37.2. CREATE A PROVIDER> IMPLEMENTATION CLASS
Overview
The fundamental prerequisite for using provider mode is to define a custom Provider<>
class that implements the invoke() method. In fact, the sole purpose of this class is to
provide runtime type information for Apache CXF: the invoke() method never gets called!
By implementing the provider class in the way shown here, you are merely indicating to
the Apache CXF runtime that the WS endpoint should operate in in PAYLOAD mode and the
type of the message PAYLOAD should be SAXSource.
The SAXSourceService provider class
The definition of the provider class is relatively short and the complete definition of the
customer provider class, SAXSourceService, is as follows:
// Java
package com.fusesource.customerwscamelcxfprovider;
import
import
import
import
import
javax.xml.transform.sax.SAXSource;
javax.xml.ws.Provider;
javax.xml.ws.Service.Mode;
javax.xml.ws.ServiceMode;
javax.xml.ws.WebServiceProvider;
@WebServiceProvider()
@ServiceMode(Mode.PAYLOAD)
public class SAXSourceService implements Provider
{
public SAXSource invoke(SAXSource t) {
throw new UnsupportedOperationException("Not supported yet.");
}
}
The customer provider class, SAXSourceService, must be annotated by the
@WebServiceProvider annotation to mark it as a provider class and can be optionally
annotated by the @ServiceMode annotation to select PAYLOAD mode.
37.3. INSTANTIATE THE WS ENDPOINT
Overview
In Apache Camel, the CXF component is the key to integrating routes with Web services.
You can use the CXF component to create two different kinds of endpoint:
Consumer endpoint—(at the start of a route) represents a Web service instance,
which integrates with the route. The type of payload injected into the route depends
on the value of the endpoint's dataFormat option.
Producer endpoint—represents a special kind of WS client proxy, which converts the
current exchange object into an operation invocation on a remote Web service. The
format of the current exchange must match the endpoint's dataFormat setting.
425
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
The cxf:bean: URI syntax
The cxf:bean: URI is used to bind an Apache CXF endpoint to a route and has the following
general syntax:
cxf:bean:CxfEndpointID[?Options]
Where CxfEndpointID is the ID of a bean created using thecxf:cxfEndpoint element,
which configures the details of the WS endpoint. You can append options to this URI (where
the options are described in detail in ). Provider mode is essentially a variant of PAYLOAD
mode: you could specify this mode on the URI (by setting dataFormat=PAYLOAD), but this is
not necessary, because PAYLOAD mode is already selected by the @ServiceMode
annotation on the custom Provider class.
For example, to start a route with an endpoint in provider mode, where the endpoint is
configured by the customer-ws bean, define the route as follows:
...
The cxf:cxfEndpoint element
The cxf:cxfEndpoint element is used to define a WS endpoint that binds either to the
start (consumer endpoint) or the end (producer endpoint) of a route. For example, to define
the customer-ws WS endpoint in provider mode, you define acxf:cxfEndpoint element as
follows:
...
...
Specifying the WSDL location
The wsdlURL attribute of the cxf:cxfEndpoint element is used to specify the location of
the WSDL contract for this endpoint. The WSDL contract is used as the source of metadata
for this endpoint.
Specifying the service class
A key difference between provider mode and ordinary PAYLOAD mode is that the
serviceClass attribute must be set to the provider class,SAXSourceService.
426
CHAPTER 37. PROVIDER-BASED ROUTE
37.4. SORT MESSAGES BY OPERATION NAME
The operationName header
When the WS endpoint parses an incoming operation invocation in PROVIDER mode, it
automatically sets the operationName header to the name of the invoked operation. You
can then use this header to sort messages by operation name.
Sorting by operation name
For example, the customer-ws-camel-cxf-provider demonstration defines the following
route, which uses the content-based router pattern to sort incoming messages, based on
the operation name. The when predicates check the value of the operationName header
using simple language expressions, sorting messages into invocations on the
updateCustomer operation, the lookupCustomer operation, or the getCustomerStatus
operation.
...
${in.header.operationName} ==
'updateCustomer'
...
${in.header.operationName} ==
'lookupCustomer'
...
${in.header.operationName} ==
'getCustomerStatus'
...
...
37.5. SOAP/HTTP-TO-JMS BRIDGE USE CASE
Overview
In this section, we consider a SOAP/HTTP-to-JMS bridge use case: that is, you want to create
a route that transforms a synchronous operation invocation (over SOAP/HTTP) into an
asynchronous message delivery (by pushing the message onto a JMS queue). In this way, it
427
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
becomes possible to process the incoming operation invocations at a later time, by pulling
messages off the JMS queue.
Figure 37.2, “SOAP/HTTP-to-JMS Bridge” shows the general outline of a bridge that can
transform synchronous SOAP/HTTP invocations into asynchronous JMS message deliveries.
Figure 37.2. SOAP/HTTP-to-JMS Bridge
Transforming RPC operations to One Way
As shown in Figure 37.2, “SOAP/HTTP-to-JMS Bridge”, the route for transforming
synchronous SOAP/HTTP to asynchronous JMS works as follows:
1. The WS client invokes a synchronous operation on the Camel CXF endpoint at the
start of the route. The Camel CXF endpoint then creates an initial InOut exchange at
the start of the route, where the body of the exchange message contains a payload
in XML format.
2. The inOnly DSL command pushes a copy of the XML payload onto a JMS queue, so
that it can be processed offline at some later time.
3. The transform DSL command constructs an immediate response to send back to
the client, where the response has the form of an XML string.
4. The route explicitly converts the XML string to the
javax.xml.transform.sax.SAXSource type.
5. The response is sent back to the WS client, thus completing the synchronous
operation invocation.
Evidently, this transformation can only work, if the original operation invocation has no
return value. Otherwise, it would be impossible to generate a response message before the
request has been processed.
Creating a broker instance
You can use Apache ActiveMQ as the JMS implementation. A convenient approach to use in
this demonstration is to embed the Apache ActiveMQ broker in the bridge bundle. Simply
define an amq:broker element in the Spring XML file, as follows:
428
CHAPTER 37. PROVIDER-BASED ROUTE
...
NOTE
This broker instance is created with the persistent attribute set to false, so
that the messages are stored only in memory.
Configuring the JMS component
Because the broker is co-located with the bridge route (in the same JVM), the most efficient
way to connect to the broker is to use the VM (Virtual Machine) transport. Configure the
Apache ActiveMQ component as follows, to connect to the co-located broker using the VM
protocol:
...
...
NOTE
By defining the bean with an id value of activemq, you are implicitly
overriding the component associated with the endpoint URI prefix, activemq:.
In other words, your custom ActiveMQComponent instance is used instead of
the default ActiveMQComponent instance from the camel-activemq JAR file.
Sample SOAP/HTTP-to-JMS route
For example, you could define a route that implements the SOAP/HTTP-to-JMS bridge
specifically for the updateCustomer operation from the CustomerService SEI, as follows:
${in.header.operationName} == 'updateCustomer'
429
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
]]>
Sending to the JMS endpoint in inOnly mode
Note how the message payload is sent to the JMS queue using the inOnly DSL command
instead of the to DSL command. When you send a message using theto DSL command,
the default behavior is to use the same invocation mode as the current exchange. But the
current exchange has an InOut MEP, which means that the to DSL command would wait
forever for a response message from JMS.
The invocation mode we want to use when sending the payload to the JMS queue is InOnly
(asynchronous), and we can force this mode by inserting the inOnly DSL command into the
route.
NOTE
By specifying the option, jmsMessageType=Text, Camel CXF implicitly converts
the message payload to an XML string before pushing it onto the JMS queue.
Returning a literal response value
The transform DSL command uses an expression to set the body of the exchange'sOut
message and this message is then used as the response to the client. Your first impulse
when defining a response in XML format might be to use a DOM API, but in this example,
the response is specified as a string literal. This approach has the advantage of being both
efficient and very easy to program.
Type conversion of the response message
In this example, the reply message (like the request message) is required to be of type,
javax.xml.transform.sax.SAXSource. In the last step of the route, therefore, you must
convert the message body from String type to javax.xml.transform.sax.SAXSource
type, by invoking the convertBodyTo DSL command.
The implementation of the String to SAXSource conversion is provided by a custom type
converter, as described in Section 37.7, “TypeConverter for SAXSource”.
37.6. GENERATING RESPONSES USING TEMPLATES
Overview
One of the simples and quickest approaches to generating a response message is to use a
velocity template. Figure 37.3, “Response Generated by Velocity” shows the outline of a
general template-based route. At the start of the route is a Camel CXF endpoint in provider
mode, which is the appropriate mode to use for processing the message as an XML
document. After doing the work required to process the message and stashing some
430
CHAPTER 37. PROVIDER-BASED ROUTE
intermediate results in message headers, the route generates the response message using
a Velocity template.
Figure 37.3. Response Generated by Velocity
Sample template-based route
For example, you could define a template-based route specifically for the
getCustoemrStatus operation, as follows:
...
${in.header.operationName} ==
'getCustomerStatus'
/cus:getCustomerStatus/customerId
Route processing steps
Given the preceding route definition, any message whose operation name matches
getCustomerStatus would be processed as follows:
1. The route applies an XPath expression to the message in order to extract the
customer ID value and then stashes it in the customerId header.
2. The next step sends the message to the getCustomerStatus bean, which does
whatever processing is required to get the customer status for the specified
customer ID. The results from this step are stashed in message headers.
3. A response is generated using a Velocity template.
4. Finally, the XML string generated by the Velocity template must be explicitly
converted to the javax.xml.transform.sax.SAXSource type using convertBodyTo
(which implicitly relies on a type converter).
431
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
NOTE
A common pattern when implementing Apache Camel routes is to use
message headers as a temporary stash to hold intermediate results (you could
also use exchange properties in the same way).
XPath expressions and SAXSource
XPath expressions can be applied directly to SAXSource objects. The XPath implementation
has a pluggable architecture that supports a variety of XML parsers and when XPath
encounters a SAXSource object, it automatically loads the plug-in required to support
SAXSource parsing.
getCustomerStatus processor bean
The getCustomerStatus processor bean is an instance of the GetCustomerStatus
processor class, which is defined as follows:
// Java
package com.fusesource.customerwscamelcxfpayload;
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
public class GetCustomerStatus implements Processor
{
public void process(Exchange exchng) throws Exception {
String id = exchng.getIn().getHeader("customerId", String.class);
// Maybe do some kind of lookup here!
//
exchng.getIn().setHeader("status", "Away");
exchng.getIn().setHeader("statusMessage", "Going to sleep.");
}
}
The implementation shown here is just a placeholder. In a realistic application you would
perform some sort of checks or database lookup to obtain the customer status. In the
demonstration code, however, the status and statusMessage are simply set to constant
values and stashed in message headers.
getCustomerStatusResponse.vm Velocity template
You can generate a response message very simply using a Velocity template. The Velocity
template consists of a message in plain text, where specific pieces of data can be inserted
using expressions—for example, the expression ${header.HeaderName} substitutes the
value of a named header.
The Velocity template for generating the getCustomerStatus reponse is located in the
customer-ws-camel-cxf-provider/src/main/resources directory and it contains the
following template script:
432
CHAPTER 37. PROVIDER-BASED ROUTE
${headers.status}
${headers.statusMessage}
37.7. TYPECONVERTER FOR SAXSOURCE
Overview
Apache Camel supports a type converter mechanism, which is used to perform implicit and
explicit type conversions of message bodies and message headers. The type converter
mechanism is extensible and it so happens that the provider demonstration requires a
custom type converter that can convert String objects to SAXSource objects.
String to SAXSource type converter
The String to SAXSource type converter is implemented in the AdditionalConverters
class, as follows:
// Java
package com.fusesource.customerwscamelcxfprovider;
import
import
import
import
java.io.ByteArrayInputStream;
javax.xml.transform.sax.SAXSource;
org.apache.camel.Converter;
org.xml.sax.InputSource;
@Converter
public class AdditionalConverters {
@Converter
public static SAXSource toSAXSource(String xml) {
return new SAXSource(new InputSource(new
ByteArrayInputStream(xml.getBytes())));
}
}
Reference
For full details of the type converter mechanism in Apache Camel, see Section 40.3, “BuiltIn Type Converters” and Chapter 42, Type Converters.
37.8. DEPLOY TO OSGI
Overview
One of the options for deploying the provider-based route is to package it as an OSGi
bundle and deploy it into an OSGi container such as Red Hat JBoss Fuse. Some of the
advantages of an OSGi deployment include:
Bundles are a relatively lightweight deployment option (because dependencies can
be shared between deployed bundles).
433
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
OSGi provides sophisticated dependency management, ensuring that only versionconsistent dependencies are added to the bundle's classpath.
Using the Maven bundle plug-in
The Maven bundle plug-in is used to package your project as an OSGi bundle, in
preparation for deployment into the OSGi container. There are two essential modifications
to make to your project's pom.xml file:
1. Change the packaging type to bundle (by editing the value of the
project/packaging element in the POM).
2. Add the Maven bundle plug-in to your POM file and configure it as appropriate.
Configuring the Maven bundle plug-in is quite a technical task (although the default settings
are often adequate). For full details of how to customize the plug-in configuration, consult
Deploying into the OSGi Container and Managing OSGi Dependencies.
Sample bundle plug-in configuration
The following POM fragment shows a sample configuration of the Maven bundle plug-in,
which is appropriate for the current example.
...
com.fusesource.byexample.cxf-webinars
customer-ws-camel-cxf-provider
customer-ws-camel-cxf-provider
bundle
...
...
org.apache.felix
maven-bundle-plugin
true
org.apache.camel.component.velocity,
META-INF.cxf,
META-INF.cxf.osgi,
javax.jws,
javax.wsdl,
javax.xml.bind,
javax.xml.bind.annotation,
javax.xml.namespace,
javax.xml.ws,
org.w3c.dom,
org.apache.xpath.jaxp,
*
434
CHAPTER 37. PROVIDER-BASED ROUTE
org.apache.cxf.*,
org.springframework.beans.*
...
Dynamic imports
The Java packages from Apache CXF and the Spring API are imported using dynamic
imports (specified using the DynamicImport-Package element). This is a pragmatic way of
dealing with the fact that Spring XML files are not terribly well integrated with the Maven
bundle plug-in. At build time, the Maven bundle plug-in is not able to figure out which Java
classes are required by the Spring XML code. By listing wildcarded package names in the
DynamicImport-Package element, however, you allow the OSGi container to figure out
which Java classes are needed by the Spring XML code at run time.
NOTE
In general, using DynamicImport-Package headers is not recommended in
OSGi, because it short-circuits OSGi version checking. Normally, what should
happen is that the Maven bundle plug-in lists the Java packages used at build
time, along with their versions, in the Import-Package header. At deploy time,
the OSGi container then checks that the available Java packages are
compatible with the build time versions listed in the Import-Package header.
With dynamic imports, this version checking cannot be performed.
Build and deploy the client bundle
After you have configured the POM file, you can build the Maven project and install it in
your local repository by entering the following command:
mvn install
To deploy the route bundle, enter the following command at the container console:
karaf@root> install -s mvn:com.fusesource.byexample.cxf-webinars/customerws-camel-cxf-provider
NOTE
If your local Maven repository is stored in a non-standard location, you might
need to customize the value of the
org.ops4j.pax.url.mvn.localRepository property in the
EsbInstallDir/etc/org.ops4j.pax.url.mvn.cfg file, before you can use
the mvn: scheme to access Maven artifacts.
435
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
CHAPTER 38. PROXYING A WEB SERVICE
Abstract
A common use case for the Camel CXF component is to use a route as a proxy for a Web
service. That is, in order to perform additional processing of WS request and response
messages, you interpose a route between the WS client and the original Web service.
38.1. PROXYING WITH HTTP
Overview
The simplest way to proxy a SOAP/HTTP Web service is to treat the request and reply
messages as HTTP packets. This type of proxying can be used where there is no
requirement to read or modify the messages passing through the route. For example, you
could use this kind of proxying to apply various patterns of flow control on the WS
messges.
Figure 38.1, “Proxy Route with Message in HTTP Format”shows an overview of how to
proxy a Web service using an Apache Camel route, where the route treats the messages as
HTTP packets. The key feature of this route is that both the consumer endpoint (at the start
of the route) and the producer endpoint (at the end of the route) must be compatible with
the HTTP packet format.
Figure 38.1. Proxy Route with Message in HTTP Format
Alternatives for the consumer endpoint
The following Apache Camel endpoints can be used as consumer endpoints for HTTP format
messages:
Jetty endpoint—is a lightweight Web server. You can use Jetty to handle messages
for any HTTP-based protocol, including the commonly-used Web service SOAP/HTTP
protocol.
Camel CXF endpoint in MESSAGE mode—when a Camel CXF endpoint is used in
MESSAGE mode, the body of the exchange message is the raw message received
from the transport layer (which is HTTP). In other words, the Camel CXF endpoint in
MESSAGE mode is equivalent to a Jetty endpoint in the case of HTTP-based
protocols.
Consumer endpoint for HTTP
A Jetty endpoint has the general form, jetty:HttpAddress. To configure the Jetty endpoint
to be a proxy for a Web service, use a HttpAddress value that is almost identical to the
HTTP address the client connects to, except that Jetty's version of HttpAddress uses the
436
CHAPTER 38. PROXYING A WEB SERVICE
special hostname, 0.0.0.0 (which matches all of the network interfaces on the current
machine).
...
matchOnUriPrefix option
Normally, a Jetty consumer endpoint accepts only an exact match on the context path. For
example, a request that is sent to the address http://localhost:9093/Customers would
be accepted, but a request sent to http://localhost:9093/Customers/Foo would be
rejected. By setting matchOnUriPrefix to true, however, you enable a kind of wildcarding
on the context path, so that any context path prefixed by /Customers is accepted.
Alternatives for the producer endpoint
The following Apache Camel endpoints can be used as producer endpoints for HTTP format
messages:
Jetty HTTP client endpoint—(recommended) the Jetty library implements a HTTP
client. In particular, the Jetty HTTP client features support for HttpClient thread
pools, which means that the Jetty implementation scales particularly well.
HTTP endpoint—the HTTP endpoint implements a HTTP client based on the
HttpClient 3.x API.
HTTP4 endpoint—the HTTP endpoint implements a HTTP client based on the
HttpClient 4.x API.
Producer endpoint for HTTP
To configure a Jetty HTTP endpoint to send HTTP requests to a remote SOAP/HTTP Web
service, set the uri attribute of the to element at the end of the route to be the address of
the remote Web service, as follows:
...
bridgeEndpoint option
The HTTP component supports a bridgeEndpoint option, which you can enable on a HTTP
producer endpoint to configure the endpoint appropriately for operating in a HTTP-to-HTTP
bridge (as is the case in this demonstration). In particular, when bridgeEndpoint=true, the
HTTP endpoint ignores the value of the Exchange.HTTP_URI header, using the HTTP
address from the endpoint URI instead.
throwExceptionOnFailure option
437
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
Setting throwExceptionOnFailure to false ensures that any HTTP exceptions are relayed
back to the original WS client, instead of being thrown within the route.
Handling message headers
When defining a HTTP bridge application, the CamelHttp* headers set by the consumer
endpoint at the start of the route can affect the behavior of the producer endpoint. For this
reason, in a bridge application it is advisable to remove the CamelHttp* headers before the
message reaches the producer endpoint, as follows:
...
Outgoing HTTP headers
By default, any headers in the exchange that are not prefixed by Camel will be converted
into HTTP headers and sent out over the wire by the HTTP producer endpoint. This could
have adverse consequences on the behavior of your application, so it is important to be
aware of any headers that are set in the exchange object and to remove them, if
necessary.
For more details about dealing with headers, see Section 38.4, “Handling HTTP Headers”.
38.2. PROXYING WITH POJO FORMAT
Overview
If you want to access the content of the Web services messages that pass throught the
route, you might prefer to process the messages in POJO format: that is, where the body of
the exchange consists of a list of Java objects representing the WS operation parameters.
The key advantate of using POJO format is that you can easily process the contents of a
message, by accessing the operation parameters as Java objects.
Figure 38.2, “Proxy Route with Message in POJO Format”shows an overview of how to
proxy a Web service using an Apache Camel route, where the route processes the
messages in POJO format. The key feature of this route is that both the consumer endpoint
(at the start of the route) and the producer endpoint (at the end of the route) must be
compatible with the POJO data format.
Figure 38.2. Proxy Route with Message in POJO Format
Consumer endpoint for CXF/POJO
To parse incoming messages into POJO data format, the consumer endpoint at the start of
438
CHAPTER 38. PROXYING A WEB SERVICE
the route must be a Camel CXF endpoint that is configured to use POJO mode. Use the
cxf:bean:BeanID URI format to reference the Camel CXF endpoint as follows (where the
dataFormat option defaults to POJO):
...
The bean with the ID, customerServiceProxy, is a Camel CXF/POJO endpoint, which is
defined as follows:
...
...
Producer endpoint for CXF/POJO
To convert the exchange body from POJO data format to a SOAP/HTTP message, the
producer endpoint at the end of the route must be a Camel CXF endpoint configured to use
POJO mode. Use the cxf:bean:BeanID URI format to reference the Camel CXF endpoint as
follows (where the dataFormat option defaults to POJO):
...
The bean with the ID, customerServiceReal, is a Camel CXF/POJO endpoint, which is
defined as follows:
...
...
38.3. PROXYING WITH PAYLOAD FORMAT
Overview
If you want to access the content of the Web services messages that pass throught the
route, you might prefer to process the messages in the normal PAYLOAD format: that is,
where the body of the exchange is accessible as an XML document (essentially, an
org.w3c.dom.Node object). The key advantate of using PAYLOAD format is that you can
easily process the contents of a message, by accessing the message body as an XML
document.
Figure 38.3, “Proxy Route with Message in PAYLOAD Format”shows an overview of how to
proxy a Web service using an Apache Camel route, where the route processes the
messages in PAYLOAD format. The key feature of this route is that both the consumer
endpoint (at the start of the route) and the producer endpoint (at the end of the route)
must be compatible with the PAYLOAD data format.
Figure 38.3. Proxy Route with Message in PAYLOAD Format
Consumer endpoint for CXF/PAYLOAD
To parse incoming messages into PAYLOAD data format, the consumer endpoint at the
start of the route must be a Camel CXF endpoint that is configured to use PAYLOAD mode.
Use the cxf:bean:BeanID URI format to reference the Camel CXF endpoint as follows,
where you must set the dataFormat option to PAYLOAD:
...
The bean with the ID, customerServiceProxy, is a Camel CXF/PAYLOAD endpoint, which is
defined as follows:
...
...
Producer endpoint for CXF/PAYLOAD
To convert the exchange body from PAYLOAD data format to a SOAP/HTTP message, the
producer endpoint at the end of the route must be a Camel CXF endpoint configured to use
PAYLOAD mode. Use the cxf:bean:BeanID URI format to reference the Camel CXF endpoint
as follows, where you must set the dataFormat option to PAYLOAD:
...
The bean with the ID, customerServiceReal, is a Camel CXF/PAYLOAD endpoint, which is
defined as follows:
...
...
Outgoing HTTP headers
By default, any headers in the exchange that are not prefixed by Camel will be converted
into HTTP headers and sent out over the wire by the Camel CXF producer endpoint. This
could have adverse consequences on the behavior of your application, so it is important to
be aware of any headers that are set in the exchange object and to remove them, if
necessary.
For more details about dealing with headers, see Section 38.4, “Handling HTTP Headers”.
38.4. HANDLING HTTP HEADERS
Overview
When building bridge applications using HTTP or HTTP-based components, it is important to
be aware of how the HTTP-based endpoints process headers. In many cases, internal
headers (prefixed by Camel) or other headers can cause unwanted side-effects on your
application. It is often necessary to remove or filter out certain headings or classes of
headings in your route, in order to ensure that your application behaves as expected.
441
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
HTTP-based components
The behavior described in this section affects not just the Camel HTTP component (camelhttp), but also a number of other HTTP-based components, including:
camel-http
camel-http4
camel-jetty
camel-restlet
camel-cxf
HTTP consumer endpoint
When a HTTP consumer endpoint receives an incoming message, it creates an In message
with the following headers:
CamelHttp* headers
Several headers with the CamelHttp prefix are created, which record the status of the
incoming message. For details of these internal headers, see HTTP.
HTTP headers
All of the HTTP headers from the original incoming message are mapped to headers on
the exchange's In message.
URL options (Jetty only)
The URL options from the original HTTP request URL are mapped to headers on the
exchange's In message. For example, given the client request with the URL,
http://myserver/myserver?orderid=123, a Jetty consumer endpoint creates the
orderid header with value 123.
HTTP producer endpoint
When a HTTP producer endoint receives an exchange and converts it to the target message
format, it handles the In message headers as follows:
CamelHttp*
Headers prefixed by CamelHttp are used to control the behavior of the HTTP producer
entpoint. Any headers of this kind are consumed by the HTTP producer endpoint and the
endpoint behaves as directed.
Camel*
All other headers prefixed by Camel are presumed to be meant for internal use and are
not mapped to HTTP headers in the target message (in other words, these headers are
ignored).
*
All other headers are converted to HTTP headers in the target message, with the
exception of the following headers, which are blocked (based on a case-insensitive
match):
content-length
442
CHAPTER 38. PROXYING A WEB SERVICE
content-type
cache-control
connection
date
pragma
trailer
transfer-encoding
upgrade
via
warning
Implications for HTTP bridge applications
When defining a HTTP bridge application (that is, a route starting with a HTTP consumer
endpoint and ending with a HTTP producer endpoint), the CamelHttp* headers set by the
consumer endpoint at the start of the route can affect the behavior of the producer
endpoint. For this reason, in a bridge application it is advisable to remove the CamelHttp*
headers, as follows:
from("http://0.0.0.0/context/path")
.removeHeaders("CamelHttp*)
...
.to("http://remoteHost/context/path");
Setting a custom header filter
If you want to customize the way that a HTTP producer endpoint processes headers, you
can define your own customer header filter by defining the headerFilterStrategy option
on the endpoint URI. For example, to configure a producer endpoint with the
myHeaderFilterStrategy filter, you could use a URI like the following:
http://remoteHost/context/path?
headerFilterStrategy=#myHeaderFilterStrategy
Where myHeaderFilterStrategy is the bean ID of your custom filter instance.
443
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
CHAPTER 39. FILTERING SOAP MESSAGE HEADERS
Abstract
The Camel CXF component supports a flexible header filtering mechanism, which enables
you to process SOAP headers, applying different filters according to the header's XML
namespace.
39.1. BASIC CONFIGURATION
Overview
When more than one CXF endpoint appears in a route, you need to decide whether or not
to allow headers to propagate between the endpoints. By default, the headers are relayed
back and forth between the endpoints, but in many cases it might be necessary to filter the
headers or to block them altogether. You can control header propagation by applying filters
to producer endpoints.
CxfHeaderFilterStrategy
Header filtering is controlled by the CxfHeaderFilterStrategy class. Basic configuration of
the CxfHeaderFilterStrategy class involves setting one or more of the following options:
the section called “relayHeaders option”.
the section called “relayAllMessageHeaders option”.
relayHeaders option
The semantics of the relayHeaders option can be summarized as follows:
In-band headers
Out-of-band headers
relayHeaders=true,
dataFormat=PAYLOAD
Filter
Filter
relayHeaders=true,
dataFormat=POJO
Relay all
Filter
relayHeaders=false
Block
Block
In-band headers
An in-band header is a header that is explicitly defined as part of the WSDL binding contract
for an endpoint.
Out-of-band headers
An out-of-band header is a header that is serialized over the wire, but is not explicitly part
of the WSDL binding contract. In particular, the SOAP binding permits out-of-band headers,
444
CHAPTER 39. FILTERING SOAP MESSAGE HEADERS
because the SOAP specification does not require headers to be defined in the WSDL
contract.
Payload format
The CXF endpoint's payload format affects the filter behavior as follows:
POJO
(Default) Only out-of-band headers are available for filtering, because the in-band
headers have already been processed and removed from the list by CXF. The in-band
headers are incorporated into the MessageContentList in POJO mode. If you require
access to headers in POJO mode, you have the option of implementing a custom CXF
interceptor or JAX-WS handler.
PAYLOAD
In this mode, both in-band and out-of-band headers are available for filtering.
MESSAGE
Not applicable. (In this mode, the message remains in a raw format and the headers are
not processed at all.)
Default filter
The default filter is of type, SoapMessageHeaderFilter, which removes only the SOAP
headers that the SOAP specification expects an intermediate Web service to consume. For
more details, see the section called “SoapMessageHeaderFilter”.
Overriding the default filter
You can override the default CxfHeaderFilterStrategy instance by defining a new
CxfHeaderFilterStrategy bean and associating it with a CXF endpoint.
Sample relayHeaders configuration
The following example shows how you can use the relayHeaders option to create a
CxfHeaderFilterStrategy bean that blocks all message headers. The CXF endpoints in
the route use the headerFilterStrategy option to install the filter strategy in the
endpoint, where the headerFilterStrategy setting has the syntax,
headerFilterStrategy=#BeanID.
...
...
relayAllMessageHeaders option
The relayAllMessageHeaders option is used to propagate all SOAP headers, without
applying any filtering (any installed filters would be bypassed). In order to enable this
feature, you must set both relayHeaders and relayAllMessageHeaders to true.
Sample relayAllMessageHeaders configuration
The following example shows how to configure CXF endpoints to propagate all SOAP
message headers. The propagateAllMessages filter strategy sets both relayHeaders and
relayAllMessageHeaders to true.
...
SoapMessageHeaderFilter
The first header filter in the preceding example is the SoapMessageHeaderFilter filter,
which is the default header filter. This filter is designed to filter standard SOAP headers and
is bound to the following XML namespaces:
http://schemas.xmlsoap.org/soap/
http://schemas.xmlsoap.org/wsdl/soap/
http://schemas.xmlsoap.org/wsdl/soap12/
This filter peeks at the header element, in order to decide whether or not to block a
particular header. If the soap:actor attribute (SOAP 1.1) or the soap:role attribute (SOAP
450
CHAPTER 39. FILTERING SOAP MESSAGE HEADERS
1.2) is present and has the value next, the header is removed from the message.
Otherwise, the header is propagated.
Namespace clashes
Normally, each namespace should be bound to just a single header filter. If a namespace is
bound to more than one header filter, this normally causes an error. It is possible, however,
to override this policy by setting the allowFilterNamespaceClash property to true in the
CxfHeaderFilterStrategy instance. When this policy is set totrue, the nearest to last
filter is selected, in the event of a namespace clash.
451
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
PART IV. PROGRAMMING EIP COMPONENTS
Abstract
This guide describes how to use the Apache Camel API.
452
CHAPTER 40. UNDERSTANDING MESSAGE FORMATS
CHAPTER 40. UNDERSTANDING MESSAGE FORMATS
Abstract
Before you can begin programming with Apache Camel, you should have a clear
understanding of how messages and message exchanges are modelled. Because Apache
Camel can process many message formats, the basic message type is designed to have an
abstract format. Apache Camel provides the APIs needed to access and transform the data
formats that underly message bodies and message headers.
40.1. EXCHANGES
Overview
An exchange object is a wrapper that encapsulates a received message and stores its
associated metadata (including the exchange properties). In addition, if the current
message is dispatched to a producer endpoint, the exchange provides a temporary slot to
hold the reply (the Out message).
An important feature of exchanges in Apache Camel is that they support lazy creation of
messages. This can provide a significant optimization in the case of routes that do not
require explicit access to messages.
Figure 40.1. Exchange Object Passing through a Route
Figure 40.1, “Exchange Object Passing through a Route” shows an exchange object passing
through a route. In the context of a route, an exchange object gets passed as the argument
of the Processor.process() method. This means that the exchange object is directly
accessible to the source endpoint, the target endpoint, and all of the processors in
between.
The Exchange interface
The org.apache.camel.Exchange interface defines methods to accessIn and Out
messages, as shown in Example 40.1, “Exchange Methods”.
Example 40.1. Exchange Methods
// Access the In message
Message getIn();
void
setIn(Message in);
453
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
// Access the Out message (if any)
Message getOut();
void
setOut(Message out);
boolean hasOut();
// Access the exchange ID
String getExchangeId();
void
setExchangeId(String id);
For a complete description of the methods in the Exchange interface, see Section 49.1,
“The Exchange Interface”.
Lazy creation of messages
Apache Camel supports lazy creation of In, Out, and Fault messages. This means that
message instances are not created until you try to access them (for example, by calling
getIn() or getOut()). The lazy message creation semantics are implemented by the
org.apache.camel.impl.DefaultExchange class.
If you call one of the no-argument accessors (getIn() or getOut()), or if you call an
accessor with the boolean argument equal to true (that is, getIn(true) or getOut(true)),
the default method implementation creates a new message instance, if one does not
already exist.
If you call an accessor with the boolean argument equal to false (that is, getIn(false) or
getOut(false)), the default method implementation returns the current message value.[2]
Lazy creation of exchange IDs
Apache Camel supports lazy creation of exchange IDs. You can call getExchangeId() on
any exchange to obtain a unique ID for that exchange instance, but the ID is generated
only when you actually call the method. The DefaultExchange.getExchangeId()
implementation of this method delegates ID generation to the UUID generator that is
registered with the CamelContext.
For details of how to register UUID generators with the CamelContext, see Section 40.4,
“Built-In UUID Generators”.
40.2. MESSAGES
Overview
Message objects represent messages using the following abstract model:
Message body
Message headers
Message attachments
The message body and the message headers can be of arbitrary type (they are declared as
type Object) and the message attachments are declared to be of type
javax.activation.DataHandler , which can contain arbitrary MIME types. If you need to
454
CHAPTER 40. UNDERSTANDING MESSAGE FORMATS
obtain a concrete representation of the message contents, you can convert the body and
headers to another type using the type converter mechanism and, possibly, using the
marshalling and unmarshalling mechanism.
One important feature of Apache Camel messages is that they support lazy creation of
message bodies and headers. In some cases, this means that a message can pass through
a route without needing to be parsed at all.
The Message interface
The org.apache.camel.Message interface defines methods to access the message body,
message headers and message attachments, as shown in Example 40.2, “Message
Interface”.
Example 40.2. Message Interface
// Access the message body
Object getBody();
T getBody(Class type);
void
setBody(Object body);
void setBody(Object body, Class type);
// Access message headers
Object getHeader(String name);
T getHeader(String name, Class type);
void
setHeader(String name, Object value);
Object removeHeader(String name);
Map getHeaders();
void setHeaders(Map headers);
// Access message attachments
javax.activation.DataHandler getAttachment(String id);
java.util.Map getAttachments();
java.util.Set getAttachmentNames();
void addAttachment(String id, javax.activation.DataHandler content)
// Access the message ID
String getMessageId();
void
setMessageId(String messageId);
For a complete description of the methods in the Message interface, see Section 50.1, “The
Message Interface”.
Lazy creation of bodies, headers, and attachments
Apache Camel supports lazy creation of bodies, headers, and attachments. This means that
the objects that represent a message body, a message header, or a message attachment
are not created until they are needed.
For example, consider the following route that accesses the foo message header from the
In message:
from("SourceURL")
455
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
.filter(header("foo")
.isEqualTo("bar"))
.to("TargetURL");
In this route, if we assume that the component referenced by SourceURL supports lazy
creation, the In message headers are not actually parsed until theheader("foo") call is
executed. At that point, the underlying message implementation parses the headers and
populates the header map. The message body is not parsed until you reach the end of the
route, at the to("TargetURL") call. At that point, the body is converted into the format
required for writing it to the target endpoint, TargetURL.
By waiting until the last possible moment before populating the bodies, headers, and
attachments, you can ensure that unnecessary type conversions are avoided. In some
cases, you can completely avoid parsing. For example, if a route contains no explicit
references to message headers, a message could traverse the route without ever parsing
the headers.
Whether or not lazy creation is implemented in practice depends on the underlying
component implementation. In general, lazy creation is valuable for those cases where
creating a message body, a message header, or a message attachment is expensive. For
details about implementing a message type that supports lazy creation, see Section 50.2,
“Implementing the Message Interface”.
Lazy creation of message IDs
Apache Camel supports lazy creation of message IDs. That is, a message ID is generated
only when you actually call the getMessageId() method. The
DefaultExchange.getExchangeId() implementation of this method delegates ID
generation to the UUID generator that is registered with the CamelContext.
Some endpoint implementations would call the getMessageId() method implicitly, if the
endpoint implements a protocol that requires a unique message ID. In particular, JMS
messages normally include a header containing unique message ID, so the JMS component
automatically calls getMessageId() to obtain the message ID (this is controlled by the
messageIdEnabled option on the JMS endpoint).
For details of how to register UUID generators with the CamelContext, see Section 40.4,
“Built-In UUID Generators”.
Initial message format
The initial format of an In message is determined by the source endpoint, and the initial
format of an Out message is determined by the target endpoint. If lazy creation is
supported by the underlying component, the message remains unparsed until it is accessed
explicitly by the application. Most Apache Camel components create the message body in a
relatively raw form—for example, representing it using types such as byte[], ByteBuffer,
InputStream, or OutputStream. This ensures that the overhead required for creating the
initial message is minimal. Where more elaborate message formats are required
components usually rely on type converters or marshalling processors.
Type converters
It does not matter what the initial format of the message is, because you can easily convert
a message from one format to another using the built-in type converters (see Section 40.3,
“Built-In Type Converters”). There are various methods in the Apache Camel API that
456
CHAPTER 40. UNDERSTANDING MESSAGE FORMATS
expose type conversion functionality. For example, the convertBodyTo(Class type)
method can be inserted into a route to convert the body of an In message, as follows:
from("SourceURL").convertBodyTo(String.class).to("TargetURL");
Where the body of the In message is converted to a java.lang.String. The following
example shows how to append a string to the end of the In message body:
from("SourceURL").setBody(bodyAs(String.class).append("My Special
Signature")).to("TargetURL");
Where the message body is converted to a string format before appending a string to the
end. It is not necessary to convert the message body explicitly in this example. You can
also use:
from("SourceURL").setBody(body().append("My Special
Signature")).to("TargetURL");
Where the append() method automatically converts the message body to a string before
appending its argument.
Type conversion methods in Message
The org.apache.camel.Message interface exposes some methods that perform type
conversion explicitly:
getBody(Class type)—Returns the message body as type,T.
getHeader(String name, Class type)—Returns the named header value as
type, T.
For the complete list of supported conversion types, see Section 40.3, “Built-In Type
Converters”.
Converting to XML
In addition to supporting conversion between simple types (such as byte[], ByteBuffer,
String, and so on), the built-in type converter also supports conversion to XML formats.
For example, you can convert a message body to the org.w3c.dom.Document type. This
conversion is more expensive than the simple conversions, because it involves parsing the
entire message and then creating a tree of nodes to represent the XML document
structure. You can convert to the following XML document types:
org.w3c.dom.Document
javax.xml.transform.sax.SAXSource
XML type conversions have narrower applicability than the simpler conversions. Because
not every message body conforms to an XML structure, you have to remember that this
type conversion might fail. On the other hand, there are many scenarios where a router
deals exclusively with XML message types.
Marshalling and unmarshalling
457
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
Marshalling involves converting a high-level format to a low-level format, andunmarshalling
involves converting a low-level format to a high-level format. The following two processors
are used to perform marshalling or unmarshalling in a route:
marshal()
unmarshal()
For example, to read a serialized Java object from a file and unmarshal it into a Java object,
you could use the route definition shown in Example 40.3, “Unmarshalling a Java Object”.
Example 40.3. Unmarshalling a Java Object
from("file://tmp/appfiles/serialized")
.unmarshal()
.serialization()
.
.to("TargetURL");
Final message format
When an In message reaches the end of a route, the target endpoint must be able to
convert the message body into a format that can be written to the physical endpoint. The
same rule applies to Out messages that arrive back at the source endpoint. This conversion
is usually performed implicitly, using the Apache Camel type converter. Typically, this
involves converting from a low-level format to another low-level format, such as converting
from a byte[] array to an InputStream type.
40.3. BUILT-IN TYPE CONVERTERS
Overview
This section describes the conversions supported by the master type converter. These
conversions are built into the Apache Camel core.
Usually, the type converter is called through convenience functions, such as
Message.getBody(Class type) or Message.getHeader(String name, Class
type). It is also possible to invoke the master type converter directly. For example, if you
have an exchange object, exchange, you could convert a given value to aString as shown
in Example 40.4, “Converting a Value to a String”.
Example 40.4. Converting a Value to a String
org.apache.camel.TypeConverter tc =
exchange.getContext().getTypeConverter();
String str_value = tc.convertTo(String.class, value);
Basic type converters
Apache Camel provides built-in type converters that perform conversions to and from the
following basic types:
458
CHAPTER 40. UNDERSTANDING MESSAGE FORMATS
java.io.File
String
byte[] and java.nio.ByteBuffer
java.io.InputStream and java.io.OutputStream
java.io.Reader and java.io.Writer
java.io.BufferedReader and java.io.BufferedWriter
java.io.StringReader
However, not all of these types are inter-convertible. The built-in converter is mainly
focused on providing conversions from the File and String types. The File type can be
converted to any of the preceding types, except Reader, Writer, and StringReader. The
String type can be converted to File, byte[], ByteBuffer, InputStream, or
StringReader. The conversion from String to File works by interpreting the string as a
file name. The trio of String, byte[], and ByteBuffer are completely inter-convertible.
NOTE
You can explicitly specify which character encoding to use for conversion from
byte[] to String and from String to byte[] by setting the
Exchange.CHARSET_NAME exchange property in the current exchange. For
example, to perform conversions using the UTF-8 character encoding, call
exchange.setProperty("Exchange.CHARSET_NAME", "UTF-8"). The
supported character sets are described in the java.nio.charset.Charset
class.
Collection type converters
Apache Camel provides built-in type converters that perform conversions to and from the
following collection types:
Object[]
java.util.Set
java.util.List
All permutations of conversions between the preceding collection types are supported.
Map type converters
Apache Camel provides built-in type converters that perform conversions to and from the
following map types:
java.util.Map
java.util.HashMap
java.util.Hashtable
java.util.Properties
459
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
The preceding map types can also be converted into a set, of java.util.Set type, where
the set elements are of the MapEntry type.
DOM type converters
You can perform type conversions to the following Document Object Model (DOM) types:
org.w3c.dom.Document—convertible from byte[], String, java.io.File, and
java.io.InputStream.
org.w3c.dom.Node
javax.xml.transform.dom.DOMSource—convertible from String.
javax.xml.transform.Source—convertible from byte[] and String.
All permutations of conversions between the preceding DOM types are supported.
SAX type converters
You can also perform conversions to the javax.xml.transform.sax.SAXSource type,
which supports the SAX event-driven XML parser (see the SAX Web site for details). You
can convert to SAXSource from the following types:
String
InputStream
Source
StreamSource
DOMSource
Custom type converters
Apache Camel also enables you to implement your own custom type converters. For details
on how to implement a custom type converter, see Chapter 42, Type Converters.
40.4. BUILT-IN UUID GENERATORS
Overview
Apache Camel enables you to register a UUID generator in the CamelContext. This UUID
generator is then used whenever Apache Camel needs to generate a unique ID—in
particular, the registered UUID generator is called to generate the IDs returned by the
Exchange.getExchangeId() and the Message.getMessageId() methods.
For example, you might prefer to replace the default UUID generator, if part of your
application does not support IDs with a length of 36 characters (like Websphere MQ). Also,
it can be convenient to generate IDs using a simple counter (see the
SimpleUuidGenerator) for testing purposes.
Provided UUID generators
460
CHAPTER 40. UNDERSTANDING MESSAGE FORMATS
You can configure Apache Camel to use one of the following UUID generators, which are
provided in the core:
org.apache.camel.impl.ActiveMQUuidGenerator—(Default) generates the same
style of ID as is used by Apache ActiveMQ. This implementation might not be
suitable for all applications, because it uses some JDK APIs that are forbidden in the
context of cloud computing (such as the Google App Engine).
org.apache.camel.impl.SimpleUuidGenerator—implements a simple counter ID,
starting at 1. The underlying implementation uses the
java.util.concurrent.atomic.AtomicLong type, so that it is thread-safe.
org.apache.camel.impl.JavaUuidGenerator—implements an ID based on the
java.util.UUID type. Because java.util.UUID is synchronized, this might affect
performance on some highly concurrent systems.
Custom UUID generator
To implement a custom UUID generator, implement the
org.apache.camel.spi.UuidGenerator interface, which is shown in Example 40.5,
“UuidGenerator Interface”. The generateUuid() must be implemented to return a unique
ID string.
Example 40.5. UuidGenerator Interface
// Java
package org.apache.camel.spi;
/**
* Generator to generate UUID strings.
*/
public interface UuidGenerator {
String generateUuid();
}
Specifying the UUID generator using Java
To replace the default UUID generator using Java, call the setUuidGenerator() method on
the current CamelContext object. For example, you can register a SimpleUuidGenerator
instance with the current CamelContext, as follows:
// Java
getContext().setUuidGenerator(new
org.apache.camel.impl.SimpleUuidGenerator());
NOTE
The setUuidGenerator() method should be called during startup,before any
routes are activated.
Specifying the UUID generator using Spring
461
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
To replace the default UUID generator using Spring, all you need to do is to create an
instance of a UUID generator using the Spring bean element. When a camelContext
instance is created, it automatically looks up the Spring registry, searching for a bean that
implements org.apache.camel.spi.UuidGenerator. For example, you can register a
SimpleUuidGenerator instance with the CamelContext as follows:
...
...
[2] If there is no active method the returned value will be null.
462
CHAPTER 41. IMPLEMENTING A PROCESSOR
CHAPTER 41. IMPLEMENTING A PROCESSOR
Abstract
Apache Camel allows you to implement a custom processor. You can then insert the
custom processor into a route to perform operations on exchange objects as they pass
through the route.
41.1. PROCESSING MODEL
Pipelining model
The pipelining model describes the way in which processors are arranged inSection 4.4,
“Pipes and Filters”. Pipelining is the most common way to process a sequence of endpoints
(a producer endpoint is just a special type of processor). When the processors are arranged
in this way, the exchange's In and Out messages are processed as shown inFigure 41.1,
“Pipelining Model”.
Figure 41.1. Pipelining Model
The processors in the pipeline look like services, where the In message is analogous to a
request, and the Out message is analogous to a reply. In fact, in a realistic pipeline, the
nodes in the pipeline are often implemented by Web service endpoints, such as the CXF
component.
For example, Example 41.1, “Java DSL Pipeline” shows a Java DSL pipeline constructed from
a sequence of two processors, ProcessorA, ProcessorB, and a producer endpoint,
TargetURI.
Example 41.1. Java DSL Pipeline
from(SourceURI).pipeline(ProcessorA, ProcessorB, TargetURI);
41.2. IMPLEMENTING A SIMPLE PROCESSOR
Overview
This section describes how to implement a simple processor that executes message
processing logic before delegating the exchange to the next processor in the route.
Processor interface
463
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
Simple processors are created by implementing the org.apache.camel.Processor
interface. As shown in Example 41.2, “Processor Interface”, the interface defines a single
method, process(), which processes an exchange object.
Example 41.2. Processor Interface
package org.apache.camel;
public interface Processor {
void process(Exchange exchange) throws Exception;
}
Implementing the Processor interface
To create a simple processor you must implement the Processor interface and provide the
logic for the process() method. Example 41.3, “Simple Processor Implementation” shows
the outline of a simple processor implementation.
Example 41.3. Simple Processor Implementation
import org.apache.camel.Processor;
public class MyProcessor implements Processor {
public MyProcessor() { }
public void process(Exchange exchange) throws Exception
{
// Insert code that gets executed *before* delegating
// to the next processor in the chain.
...
}
}
All of the code in the process() method gets executed before the exchange object is
delegated to the next processor in the chain.
For examples of how to access the message body and header values inside a simple
processor, see Section 41.3, “Accessing Message Content”.
Inserting the simple processor into a route
Use the process() DSL command to insert a simple processor into a route. Create an
instance of your custom processor and then pass this instance as an argument to the
process() method, as follows:
org.apache.camel.Processor myProc = new MyProcessor();
from("SourceURL").process(myProc).to("TargetURL");
41.3. ACCESSING MESSAGE CONTENT
464
CHAPTER 41. IMPLEMENTING A PROCESSOR
Accessing message headers
Message headers typically contain the most useful message content from the perspective
of a router, because headers are often intended to be processed in a router service. To
access header data, you must first get the message from the exchange object (for
example, using Exchange.getIn()), and then use the Message interface to retrieve the
individual headers (for example, using Message.getHeader()).
Example 41.4, “Accessing an Authorization Header” shows an example of a custom
processor that accesses the value of a header named Authorization. This example uses
the ExchangeHelper.getMandatoryHeader() method, which eliminates the need to test
for a null header value.
Example 41.4. Accessing an Authorization Header
import org.apache.camel.*;
import org.apache.camel.util.ExchangeHelper;
public class MyProcessor implements Processor {
public void process(Exchange exchange) {
String auth = ExchangeHelper.getMandatoryHeader(
exchange,
"Authorization",
String.class
);
// process the authorization string...
// ...
}
}
For full details of the Message interface, see Section 40.2, “Messages”.
Accessing the message body
You can also access the message body. For example, to append a string to the end of the In
message, you can use the processor shown in Example 41.5, “Accessing the Message
Body”.
Example 41.5. Accessing the Message Body
import org.apache.camel.*;
import org.apache.camel.util.ExchangeHelper;
public class MyProcessor implements Processor {
public void process(Exchange exchange) {
Message in = exchange.getIn();
in.setBody(in.getBody(String.class) + " World!");
}
}
Accessing message attachments
465
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
You can access a message's attachments using either the Message.getAttachment()
method or the Message.getAttachments() method. See Example 40.2, “Message
Interface” for more details.
41.4. THE EXCHANGEHELPER CLASS
Overview
The org.apache.camel.util.ExchangeHelper class is a Apache Camel utility class that
provides methods that are useful when implementing a processor.
Resolve an endpoint
The static resolveEndpoint() method is one of the most useful methods in the
ExchangeHelper class. You use it inside a processor to create newEndpoint instances on
the fly.
Example 41.6. The resolveEndpoint() Method
public final class ExchangeHelper {
...
@SuppressWarnings({"unchecked" })
public static Endpoint
resolveEndpoint(Exchange exchange, Object value)
throws NoSuchEndpointException { ... }
...
}
The first argument to resolveEndpoint() is an exchange instance, and the second
argument is usually an endpoint URI string. Example 41.7, “Creating a File Endpoint” shows
how to create a new file endpoint from an exchange instance exchange
Example 41.7. Creating a File Endpoint
Endpoint file_endp = ExchangeHelper.resolveEndpoint(exchange,
"file://tmp/messages/in.xml");
Wrapping the exchange accessors
The ExchangeHelper class provides several static methods of the form
getMandatoryBeanProperty(), which wrap the corresponding getBeanProperty()
methods on the Exchange class. The difference between them is that the original
getBeanProperty() accessors return null, if the corresponding property is unavailable,
and the getMandatoryBeanProperty() wrapper methods throw a Java exception. The
following wrapper methods are implemented in the ExchangeHelper class:
public final class ExchangeHelper {
...
public static T getMandatoryProperty(Exchange exchange, String
propertyName, Class type)
466
CHAPTER 41. IMPLEMENTING A PROCESSOR
throws NoSuchPropertyException { ... }
public static T getMandatoryHeader(Exchange exchange, String
propertyName, Class type)
throws NoSuchHeaderException { ... }
public static Object getMandatoryInBody(Exchange exchange)
throws InvalidPayloadException { ... }
public static T getMandatoryInBody(Exchange exchange, Class
type)
throws InvalidPayloadException { ... }
public static Object getMandatoryOutBody(Exchange exchange)
throws InvalidPayloadException { ... }
public static T getMandatoryOutBody(Exchange exchange, Class
type)
throws InvalidPayloadException { ... }
...
}
Testing the exchange pattern
Several different exchange patterns are compatible with holding an In message. Several
different exchange patterns are also compatible with holding an Out message. To provide a
quick way of checking whether or not an exchange object is capable of holding an In
message or an Out message, the ExchangeHelper class provides the following methods:
public final class ExchangeHelper {
...
public static boolean isInCapable(Exchange exchange) { ... }
public static boolean isOutCapable(Exchange exchange) { ... }
...
}
Get the In message's MIME content type
If you want to find out the MIME content type of the exchange's In message, you can access
it by calling the ExchangeHelper.getContentType(exchange) method. To implement this,
the ExchangeHelper object looks up the value of the In message's Content-Type header—
this method relies on the underlying component to populate the header value).
467
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
CHAPTER 42. TYPE CONVERTERS
Abstract
Apache Camel has a built-in type conversion mechanism, which is used to convert message
bodies and message headers to different types. This chapter explains how to extend the
type conversion mechanism by adding your own custom converter methods.
42.1. TYPE CONVERTER ARCHITECTURE
Overview
This section describes the overall architecture of the type converter mechanism, which you
must understand, if you want to write custom type converters. If you only need to use the
built-in type converters, see Chapter 40, Understanding Message Formats.
Type converter interface
Example 42.1, “TypeConverter Interface” shows the definition of the
org.apache.camel.TypeConverter interface, which all type converters must implement.
Example 42.1. TypeConverter Interface
package org.apache.camel;
public interface TypeConverter {
T convertTo(Class type, Object value);
}
Master type converter
The Apache Camel type converter mechanism follows a master/slave pattern. There are
many slave type converters, which are each capable of performing a limited number of
type conversions, and a single master type converter, which aggregates the type
conversions performed by the slaves. The master type converter acts as a front-end for the
slave type converters. When you request the master to perform a type conversion, it
selects the appropriate slave and delegates the conversion task to that slave.
For users of the type conversion mechanism, the master type converter is the most
important because it provides the entry point for accessing the conversion mechanism.
During start up, Apache Camel automatically associates a master type converter instance
with the CamelContext object. To obtain a reference to the master type converter, you call
the CamelContext.getTypeConverter() method. For example, if you have an exchange
object, exchange, you can obtain a reference to the master type converter as shown in
Example 42.2, “Getting a Master Type Converter”.
Example 42.2. Getting a Master Type Converter
org.apache.camel.TypeConverter tc =
exchange.getContext().getTypeConverter();
468
CHAPTER 42. TYPE CONVERTERS
Type converter loader
The master type converter uses a type converter loader to populate the registry of slave
type converters. A type converter loader is any class that implements the
TypeConverterLoader interface. Apache Camel currently uses only one kind of type
converter loader—the annotation type converter loader (of
AnnotationTypeConverterLoader type).
Type conversion process
Figure 42.1, “Type Conversion Process” gives an overview of the type conversion process,
showing the steps involved in converting a given data value, value, to a specified type,
toType.
Figure 42.1. Type Conversion Process
The type conversion mechanism proceeds as follows:
1. The CamelContext object holds a reference to the masterTypeConverter instance.
The first step in the conversion process is to retrieve the master type converter by
calling CamelContext.getTypeConverter().
469
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
2. Type conversion is initiated by calling the convertTo() method on the master type
converter. This method instructs the type converter to convert the data object,
value, from its original type to the type specified by thetoType argument.
3. Because the master type converter is a front end for many different slave type
converters, it looks up the appropriate slave type converter by checking a registry of
type mappings The registry of type converters is keyed by a type mapping pair
(toType, fromType). If a suitable type converter is found in the registry, the
master type converter calls the slave's convertTo() method and returns the result.
4. If a suitable type converter cannot be found in the registry, the master type
converter loads a new type converter, using the type converter loader.
5. The type converter loader searches the available JAR libraries on the classpath to
find a suitable type converter. Currently, the loader strategy that is used is
implemented by the annotation type converter loader, which attempts to load a
class annotated by the org.apache.camel.Converter annotation. See the section
called “Create a TypeConverter file”.
6. If the type converter loader is successful, a new slave type converter is loaded and
entered into the type converter registry. This type converter is then used to convert
the value argument to the toType type.
7. If the data is successfully converted, the converted data value is returned. If the
conversion does not succeed, null is returned.
42.2. IMPLEMENTING TYPE CONVERTER USING
ANNOTATIONS
Overview
The type conversion mechanism can easily be customized by adding a new slave type
converter. This section describes how to implement a slave type converter and how to
integrate it with Apache Camel, so that it is automatically loaded by the annotation type
converter loader.
How to implement a type converter
To implement a custom type converter, perform the following steps:
1. Implement an annotated converter class.
2. Create a TypeConverter file.
3. Package the type converter.
Implement an annotated converter class
You can implement a custom type converter class using the @Converter annotation. You
must annotate the class itself and each of the static methods intended to perform type
conversion. Each converter method takes an argument that defines the from type,
optionally takes a second Exchange argument, and has a non-void return value that defines
the to type. The type converter loader uses Java reflection to find the annotated methods
and integrate them into the type converter mechanism. Example 42.3, “Example of an
Annotated Converter Class” shows an example of an annotated converter class that defines
470
CHAPTER 42. TYPE CONVERTERS
a converter method for converting from java.io.File to java.io.InputStream and
another converter method (with an Exchange argument) for converting from byte[] to
String.
Example 42.3. Example of an Annotated Converter Class
package com.YourDomain.YourPackageName;
import org.apache.camel.Converter;
import java.io.*;
@Converter
public class IOConverter {
private IOConverter() {
}
@Converter
public static InputStream toInputStream(File file) throws
FileNotFoundException {
return new BufferedInputStream(new FileInputStream(file));
}
@Converter
public static String toString(byte[] data, Exchange exchange) {
if (exchange != null) {
String charsetName =
exchange.getProperty(Exchange.CHARSET_NAME, String.class);
if (charsetName != null) {
try {
return new String(data, charsetName);
} catch (UnsupportedEncodingException e) {
LOG.warn("Can't convert the byte to String with the
charset " + charsetName, e);
}
}
}
return new String(data);
}
}
The toInputStream() method is responsible for performing the conversion from theFile
type to the InputStream type and the toString() method is responsible for performing
the conversion from the byte[] type to the String type.
NOTE
The method name is unimportant, and can be anything you choose. What is
important are the argument type, the return type, and the presence of the
@Converter annotation.
Create a TypeConverter file
To enable the discovery mechanism (which is implemented by the annotation type
471
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
converter loader) for your custom converter, create aTypeConverter file at the following
location:
META-INF/services/org/apache/camel/TypeConverter
The TypeConverter file must contain a comma-separated list of package names identifying
the packages that contain type converter classes. For example, if you want the type
converter loader to search the com.YourDomain.YourPackageName package for annotated
converter classes, the TypeConverter file would have the following contents:
com.YourDomain.YourPackageName
Package the type converter
The type converter is packaged as a JAR file containing the compiled classes of your custom
type converters and the META-INF directory. Put this JAR file on your classpath to make it
available to your Apache Camel application.
Fallback converter method
In addition to defining regular converter methods using the @Converter annotation, you
can optionally define a fallback converter method using the @FallbackConverter
annotation. The fallback converter method will only be tried, if the master type converter
fails to find a regular converter method in the type registry.
The essential difference between a regular converter method and a fallback converter
method is that whereas a regular converter is defined to perform conversion between a
specific pair of types (for example, from byte[] to String), a fallback converter can
potentially perform conversion between any pair of types. It is up to the code in the body of
the fallback converter method to figure out which conversions it is able to perform. At run
time, if a conversion cannot be performed by a regular converter, the master type converter
iterates through every available fallback converter until it finds one that can perform the
conversion.
The method signature of a fallback converter can have either of the following forms:
// 1. Non-generic form of signature
@FallbackConverter
public static Object MethodName(
Class type,
Exchange exchange,
Object value,
TypeConverterRegistry registry
)
// 2. Templating form of signature
@FallbackConverter
public static T MethodName(
Class type,
Exchange exchange,
Object value,
TypeConverterRegistry registry
)
472
CHAPTER 42. TYPE CONVERTERS
Where MethodName is an arbitrary method name for the fallback converter.
For example, the following code extract (taken from the implementation of the File
component) shows a fallback converter that can convert the body of a GenericFile object,
exploiting the type converters already available in the type converter registry:
package org.apache.camel.component.file;
import
import
import
import
import
org.apache.camel.Converter;
org.apache.camel.FallbackConverter;
org.apache.camel.Exchange;
org.apache.camel.TypeConverter;
org.apache.camel.spi.TypeConverterRegistry;
@Converter
public final class GenericFileConverter {
private GenericFileConverter() {
// Helper Class
}
@FallbackConverter
public static T convertTo(Class type, Exchange exchange, Object
value, TypeConverterRegistry registry) {
// use a fallback type converter so we can convert the embedded
body if the value is GenericFile
if (GenericFile.class.isAssignableFrom(value.getClass())) {
GenericFile file = (GenericFile) value;
Class from = file.getBody().getClass();
TypeConverter tc = registry.lookup(type, from);
if (tc != null) {
Object body = file.getBody();
return tc.convertTo(type, exchange, body);
}
}
return null;
}
...
}
42.3. IMPLEMENTING A TYPE CONVERTER DIRECTLY
Overview
Generally, the recommended way to implement a type converter is to use an annotated
class, as described in the previous section, Section 42.2, “Implementing Type Converter
Using Annotations”. But if you want to have complete control over the registration of your
type converter, you can implement a custom slave type converter and add it directly to the
type converter registry, as described here.
Implement the TypeConverter interface
To implement your own type converter class, define a class that implements the
473
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
TypeConverter interface. For example, the following MyOrderTypeConverter class converts
an integer value to a MyOrder object, where the integer value is used to initialize the order
ID in the MyOrder object.
import org.apache.camel.TypeConverter
private class MyOrderTypeConverter implements TypeConverter {
public T convertTo(Class type, Object value) {
// converter from value to the MyOrder bean
MyOrder order = new MyOrder();
order.setId(Integer.parseInt(value.toString()));
return (T) order;
}
public T convertTo(Class type, Exchange exchange, Object value)
{
// this method with the Exchange parameter will be preferd by
Camel to invoke
// this allows you to fetch information from the exchange during
convertions
// such as an encoding parameter or the likes
return convertTo(type, value);
}
public T mandatoryConvertTo(Class type, Object value) {
return convertTo(type, value);
}
public T mandatoryConvertTo(Class type, Exchange exchange,
Object value) {
return convertTo(type, value);
}
}
Add the type converter to the registry
You can add the custom type converter directly to the type converter registry using code
like the following:
// Add the custom type converter to the type converter registry
context.getTypeConverterRegistry().addTypeConverter(MyOrder.class,
String.class, new MyOrderTypeConverter());
Where context is the current org.apache.camel.CamelContext instance. The
addTypeConverter() method registers the MyOrderTypeConverter class against the
specific type conversion, from String.class to MyOrder.class.
474
CHAPTER 43. PRODUCER AND CONSUMER TEMPLATES
CHAPTER 43. PRODUCER AND CONSUMER
TEMPLATES
Abstract
The producer and consumer templates in Apache Camel are modelled after a feature of the
Spring container API, whereby access to a resource is provided through a simplified, easyto-use API known as a template. In the case of Apache Camel, the producer template and
consumer template provide simplified interfaces for sending messages to and receiving
messages from producer endpoints and consumer endpoints.
43.1. USING THE PRODUCER TEMPLATE
43.1.1. Introduction to the Producer Template
Overview
The producer template supports a variety of different approaches to invoking producer
endpoints. There are methods that support different formats for the request message (as
an Exchange object, as a message body, as a message body with a single header setting,
and so on) and there are methods to support both the synchronous and the asynchronous
style of invocation. Overall, producer template methods can be grouped into the following
categories:
the section called “Synchronous invocation”.
the section called “Synchronous invocation with a processor”.
the section called “Asynchronous invocation”.
the section called “Asynchronous invocation with a callback”.
Synchronous invocation
The methods for invoking endpoints synchronously have names of the form sendSuffix()
and requestSuffix(). For example, the methods for invoking an endpoint using either the
default message exchange pattern (MEP) or an explicitly specified MEP are named send(),
sendBody(), and sendBodyAndHeader() (where these methods respectively send an
Exchange object, a message body, or a message body and header value). If you want to
force the MEP to be InOut (request/reply semantics), you can call the request(),
requestBody(), and requestBodyAndHeader() methods instead.
The following example shows how to create a ProducerTemplate instance and use it to
send a message body to the activemq:MyQueue endpoint. The example also shows how to
send a message body and header value using sendBodyAndHeader().
import org.apache.camel.ProducerTemplate
import org.apache.camel.impl.DefaultProducerTemplate
...
ProducerTemplate template = context.createProducerTemplate();
// Send to a specific queue
475
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
template.sendBody("activemq:MyQueue", "world! ");
// Send with a body and header
template.sendBodyAndHeader(
"activemq:MyQueue",
"world! ",
"CustomerRating", "Gold" );
Synchronous invocation with a processor
A special case of synchronous invocation is where you provide the send() method with a
Processor argument instead of an Exchange argument. In this case, the producer template
implicitly asks the specified endpoint to create an Exchange instance (typically, but not
always having the InOnly MEP by default). This default exchange is then passed to the
processor, which initializes the contents of the exchange object.
The following example shows how to send an exchange initialized by the MyProcessor
processor to the activemq:MyQueue endpoint.
import org.apache.camel.ProducerTemplate
import org.apache.camel.impl.DefaultProducerTemplate
...
ProducerTemplate template = context.createProducerTemplate();
// Send to a specific queue, using a processor to initialize
template.send("activemq:MyQueue", new MyProcessor());
The MyProcessor class is implemented as shown in the following example. In addition to
setting the In message body (as shown here), you could also initialize message heades and
exchange properties.
import org.apache.camel.Processor;
import org.apache.camel.Exchange;
...
public class MyProcessor implements Processor {
public MyProcessor() { }
public void process(Exchange ex) {
ex.getIn().setBody("world! ");
}
}
Asynchronous invocation
The methods for invoking endpoints asynchronously have names of the form
asyncSendSuffix() and asyncRequestSuffix(). For example, the methods for invoking an
endpoint using either the default message exchange pattern (MEP) or an explicitly specified
MEP are named asyncSend() and asyncSendBody() (where these methods respectively
send an Exchange object or a message body). If you want to force the MEP to beInOut
(request/reply semantics), you can call the asyncRequestBody(),
asyncRequestBodyAndHeader(), and asyncRequestBodyAndHeaders() methods instead.
The following example shows how to send an exchange asynchronously to the
direct:start endpoint. The asyncSend() method returns a
476
CHAPTER 43. PRODUCER AND CONSUMER TEMPLATES
java.util.concurrent.Future object, which is used to retrieve the invocation result at a
later time.
import java.util.concurrent.Future;
import org.apache.camel.Exchange;
import org.apache.camel.impl.DefaultExchange;
...
Exchange exchange = new DefaultExchange(context);
exchange.getIn().setBody("Hello");
Future future = template.asyncSend("direct:start", exchange);
// You can do other things, whilst waiting for the invocation to complete
...
// Now, retrieve the resulting exchange from the Future
Exchange result = future.get();
The producer template also provides methods to send a message body asynchronously (for
example, using asyncSendBody() or asyncRequestBody()). In this case, you can use one of
the following helper methods to extract the returned message body from the Future
object:
T extractFutureBody(Future future, Class type);
T extractFutureBody(Future future, long timeout, TimeUnit unit,
Class type) throws TimeoutException;
The first version of the extractFutureBody() method blocks until the invocation
completes and the reply message is available. The second version of the
extractFutureBody() method allows you to specify a timeout. Both methods have a type
argument, type, which casts the returned message body to the specified type using a builtin type converter.
The following example shows how to use the asyncRequestBody() method to send a
message body to the direct:start endpoint. The blocking extractFutureBody() method
is then used to retrieve the reply message body from the Future object.
Future future = template.asyncRequestBody("direct:start",
"Hello");
// You can do other things, whilst waiting for the invocation to complete
...
// Now, retrieve the reply message body as a String type
String result = template.extractFutureBody(future, String.class);
Asynchronous invocation with a callback
In the preceding asynchronous examples, the request message is dispatched in a subthread, while the reply is retrieved and processed by the main thread. The producer
template also gives you the option, however, of processing replies in the sub-thread, using
one of the asyncCallback(), asyncCallbackSendBody(), or asyncCallbackRequestBody()
methods. In this case, you supply a callback object (of
org.apache.camel.impl.SynchronizationAdapter type), which automatically gets
invoked in the sub-thread as soon as a reply message arrives.
477
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
The Synchronization callback interface is defined as follows:
package org.apache.camel.spi;
import org.apache.camel.Exchange;
public interface Synchronization {
void onComplete(Exchange exchange);
void onFailure(Exchange exchange);
}
Where the onComplete() method is called on receipt of a normal reply and the
onFailure() method is called on receipt of a fault message reply. Only one of these
methods gets called back, so you must override both of them to ensure that all types of
reply are processed.
The following example shows how to send an exchange to the direct:start endpoint,
where the reply message is processed in the sub-thread by the SynchronizationAdapter
callback object.
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import org.apache.camel.Exchange;
import org.apache.camel.impl.DefaultExchange;
import org.apache.camel.impl.SynchronizationAdapter;
...
Exchange exchange = context.getEndpoint("direct:start").createExchange();
exchange.getIn().setBody("Hello");
Future future = template.asyncCallback("direct:start", exchange,
new SynchronizationAdapter() {
@Override
public void onComplete(Exchange exchange) {
assertEquals("Hello World", exchange.getIn().getBody());
}
});
Where the SynchronizationAdapter class is a default implementation of the
Synchronization interface, which you can override to provide your own definitions of the
onComplete() and onFailure() callback methods.
You still have the option of accessing the reply from the main thread, because the
asyncCallback() method also returns a Future object—for example:
// Retrieve the reply from the main thread, specifying a timeout
Exchange reply = future.get(10, TimeUnit.SECONDS);
43.1.2. Synchronous Send
Overview
The synchronous send methods are a collection of methods that you can use to invoke a
producer endpoint, where the current thread blocks until the method invocation is
478
CHAPTER 43. PRODUCER AND CONSUMER TEMPLATES
complete and the reply (if any) has been received. These methods are compatible with any
kind of message exchange protocol.
Send an exchange
The basic send() method is a general-purpose method that sends the contents of an
Exchange object to an endpoint, using the message exchange pattern (MEP) of the
exchange. The return value is the exchange that you get after it has been processed by
the producer endpoint (possibly containing an Out message, depending on the MEP).
There are three varieties of send() method for sending an exchange that let you specify
the target endpoint in one of the following ways: as the default endpoint, as an endpoint
URI, or as an Endpoint object.
Exchange send(Exchange exchange);
Exchange send(String endpointUri, Exchange exchange);
Exchange send(Endpoint endpoint, Exchange exchange);
Send an exchange populated by a processor
A simple variation of the general send() method is to use a processor to populate a default
exchange, instead of supplying the exchange object explicitly (see the section called
“Synchronous invocation with a processor” for details).
The send() methods for sending an exchange populated by a processor let you specify the
target endpoint in one of the following ways: as the default endpoint, as an endpoint URI,
or as an Endpoint object. In addition, you can optionally specify the exchange's MEP by
supplying the pattern argument, instead of accepting the default.
Exchange send(Processor processor);
Exchange send(String endpointUri, Processor processor);
Exchange send(Endpoint endpoint, Processor processor);
Exchange send(
String endpointUri,
ExchangePattern pattern,
Processor processor
);
Exchange send(
Endpoint endpoint,
ExchangePattern pattern,
Processor processor
);
Send a message body
If you are only concerned with the contents of the message body that you want to send,
you can use the sendBody() methods to provide the message body as an argument and let
the producer template take care of inserting the body into a default exchange object.
The sendBody() methods let you specify the target endpoint in one of the following ways:
as the default endpoint, as an endpoint URI, or as an Endpoint object. In addition, you can
optionally specify the exchange's MEP by supplying the pattern argument, instead of
accepting the default. The methods without a pattern argument return void (even though
479
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
the invocation might give rise to a reply in some cases); and the methods with a pattern
argument return either the body of the Out message (if there is one) or the body of theIn
message (otherwise).
void sendBody(Object body);
void sendBody(String endpointUri, Object body);
void sendBody(Endpoint endpoint, Object body);
Object sendBody(
String endpointUri,
ExchangePattern pattern,
Object body
);
Object sendBody(
Endpoint endpoint,
ExchangePattern pattern,
Object body
);
Send a message body and header(s)
For testing purposes, it is often interesting to try out the effect of a single header setting
and the sendBodyAndHeader() methods are useful for this kind of header testing. You
supply the message body and header setting as arguments to sendBodyAndHeader() and
let the producer template take care of inserting the body and header setting into a default
exchange object.
The sendBodyAndHeader() methods let you specify the target endpoint in one of the
following ways: as the default endpoint, as an endpoint URI, or as an Endpoint object. In
addition, you can optionally specify the exchange's MEP by supplying the pattern
argument, instead of accepting the default. The methods without a pattern argument
return void (even though the invocation might give rise to a reply in some cases); and the
methods with a pattern argument return either the body of theOut message (if there is
one) or the body of the In message (otherwise).
void sendBodyAndHeader(
Object body,
String header,
Object headerValue
);
void sendBodyAndHeader(
String endpointUri,
Object body,
String header,
Object headerValue
);
void sendBodyAndHeader(
Endpoint endpoint,
Object body,
String header,
Object headerValue
);
Object sendBodyAndHeader(
String endpointUri,
ExchangePattern pattern,
Object body,
480
CHAPTER 43. PRODUCER AND CONSUMER TEMPLATES
String header,
Object headerValue
);
Object sendBodyAndHeader(
Endpoint endpoint,
ExchangePattern pattern,
Object body,
String header,
Object headerValue
);
The sendBodyAndHeaders() methods are similar to the sendBodyAndHeader() methods,
except that instead of supplying just a single header setting, these methods allow you to
specify a complete hash map of header settings.
void sendBodyAndHeaders(
Object body,
Map headers
);
void sendBodyAndHeaders(
String endpointUri,
Object body,
Map headers
);
void sendBodyAndHeaders(
Endpoint endpoint,
Object body,
Map headers
);
Object sendBodyAndHeaders(
String endpointUri,
ExchangePattern pattern,
Object body,
Map headers
);
Object sendBodyAndHeaders(
Endpoint endpoint,
ExchangePattern pattern,
Object body,
Map headers
);
Send a message body and exchange property
You can try out the effect of setting a single exchange property using the
sendBodyAndProperty() methods. You supply the message body and property setting as
arguments to sendBodyAndProperty() and let the producer template take care of inserting
the body and exchange property into a default exchange object.
The sendBodyAndProperty() methods let you specify the target endpoint in one of the
following ways: as the default endpoint, as an endpoint URI, or as an Endpoint object. In
addition, you can optionally specify the exchange's MEP by supplying the pattern
argument, instead of accepting the default. The methods without a pattern argument
481
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
return void (even though the invocation might give rise to a reply in some cases); and the
methods with a pattern argument return either the body of theOut message (if there is
one) or the body of the In message (otherwise).
void sendBodyAndProperty(
Object body,
String property,
Object propertyValue
);
void sendBodyAndProperty(
String endpointUri,
Object body,
String property,
Object propertyValue
);
void sendBodyAndProperty(
Endpoint endpoint,
Object body,
String property,
Object propertyValue
);
Object sendBodyAndProperty(
String endpoint,
ExchangePattern pattern,
Object body,
String property,
Object propertyValue
);
Object sendBodyAndProperty(
Endpoint endpoint,
ExchangePattern pattern,
Object body,
String property,
Object propertyValue
);
43.1.3. Synchronous Request with InOut Pattern
Overview
The synchronous request methods are similar to the synchronous send methods, except
that the request methods force the message exchange pattern to be InOut (conforming to
request/reply semantics). Hence, it is generally convenient to use a synchronous request
method, if you expect to receive a reply from the producer endpoint.
Request an exchange populated by a processor
The basic request() method is a general-purpose method that uses a processor to
populate a default exchange and forces the message exchange pattern to be InOut (so that
the invocation obeys request/reply semantics). The return value is the exchange that you
get after it has been processed by the producer endpoint, where the Out message contains
the reply message.
The request() methods for sending an exchange populated by a processor let you specify
the target endpoint in one of the following ways: as an endpoint URI, or as an Endpoint
482
CHAPTER 43. PRODUCER AND CONSUMER TEMPLATES
object.
Exchange request(String endpointUri, Processor processor);
Exchange request(Endpoint endpoint, Processor processor);
Request a message body
If you are only concerned with the contents of the message body in the request and in the
reply, you can use the requestBody() methods to provide the request message body as an
argument and let the producer template take care of inserting the body into a default
exchange object.
The requestBody() methods let you specify the target endpoint in one of the following
ways: as the default endpoint, as an endpoint URI, or as an Endpoint object. The return
value is the body of the reply message (Out message body), which can either be returned
as plain Object or converted to a specific type, T, using the built-in type converters (see
Section 40.3, “Built-In Type Converters”).
Object requestBody(Object body);
T requestBody(Object body, Class type);
Object requestBody(
String endpointUri,
Object body
);
T requestBody(
String endpointUri,
Object body,
Class type
);
Object requestBody(
Endpoint endpoint,
Object body
);
T requestBody(
Endpoint endpoint,
Object body,
Class type
);
Request a message body and header(s)
You can try out the effect of setting a single header value using the
requestBodyAndHeader() methods. You supply the message body and header setting as
arguments to requestBodyAndHeader() and let the producer template take care of
inserting the body and exchange property into a default exchange object.
The requestBodyAndHeader() methods let you specify the target endpoint in one of the
following ways: as an endpoint URI, or as an Endpoint object. The return value is the body
of the reply message (Out message body), which can either be returned as plainObject or
converted to a specific type, T, using the built-in type converters (see Section 40.3, “Built-In
Type Converters”).
Object requestBodyAndHeader(
String endpointUri,
483
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
Object body,
String header,
Object headerValue
);
T requestBodyAndHeader(
String endpointUri,
Object body,
String header,
Object headerValue,
Class type
);
Object requestBodyAndHeader(
Endpoint endpoint,
Object body,
String header,
Object headerValue
);
T requestBodyAndHeader(
Endpoint endpoint,
Object body,
String header,
Object headerValue,
Class type
);
The requestBodyAndHeaders() methods are similar to the requestBodyAndHeader()
methods, except that instead of supplying just a single header setting, these methods allow
you to specify a complete hash map of header settings.
Object requestBodyAndHeaders(
String endpointUri,
Object body,
Map headers
);
T requestBodyAndHeaders(
String endpointUri,
Object body,
Map headers,
Class type
);
Object requestBodyAndHeaders(
Endpoint endpoint,
Object body,
Map headers
);
T requestBodyAndHeaders(
Endpoint endpoint,
Object body,
Map headers,
Class type
);
43.1.4. Asynchronous Send
484
CHAPTER 43. PRODUCER AND CONSUMER TEMPLATES
Overview
The producer template provides a variety of methods for invoking a producer endpoint
asynchronously, so that the main thread does not block while waiting for the invocation to
complete and the reply message can be retrieved at a later time. The asynchronous send
methods described in this section are compatible with any kind of message exchange
protocol.
Send an exchange
The basic asyncSend() method takes an Exchange argument and invokes an endpoint
asynchronously, using the message exchange pattern (MEP) of the specified exchange. The
return value is a java.util.concurrent.Future object, which is a ticket you can use to
collect the reply message at a later time—for details of how to obtain the return value from
the Future object, see the section called “Asynchronous invocation”.
The following asyncSend() methods let you specify the target endpoint in one of the
following ways: as an endpoint URI, or as an Endpoint object.
Future asyncSend(String endpointUri, Exchange exchange);
Future asyncSend(Endpoint endpoint, Exchange exchange);
Send an exchange populated by a processor
A simple variation of the general asyncSend() method is to use a processor to populate a
default exchange, instead of supplying the exchange object explicitly.
The following asyncSend() methods let you specify the target endpoint in one of the
following ways: as an endpoint URI, or as an Endpoint object.
Future asyncSend(String endpointUri, Processor processor);
Future asyncSend(Endpoint endpoint, Processor processor);
Send a message body
If you are only concerned with the contents of the message body that you want to send,
you can use the asyncSendBody() methods to send a message body asynchronously and
let the producer template take care of inserting the body into a default exchange object.
The asyncSendBody() methods let you specify the target endpoint in one of the following
ways: as an endpoint URI, or as an Endpoint object.
Future asyncSendBody(String endpointUri, Object body);
Future asyncSendBody(Endpoint endpoint, Object body);
43.1.5. Asynchronous Request with InOut Pattern
Overview
The asynchronous request methods are similar to the asynchronous send methods, except
that the request methods force the message exchange pattern to be InOut (conforming to
request/reply semantics). Hence, it is generally convenient to use an asynchronous request
method, if you expect to receive a reply from the producer endpoint.
485
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
Request a message body
If you are only concerned with the contents of the message body in the request and in the
reply, you can use the requestBody() methods to provide the request message body as an
argument and let the producer template take care of inserting the body into a default
exchange object.
The asyncRequestBody() methods let you specify the target endpoint in one of the
following ways: as an endpoint URI, or as an Endpoint object. The return value that is
retrievable from the Future object is the body of the reply message (Out message body),
which can be returned either as a plain Object or converted to a specific type, T, using a
built-in type converter (see the section called “Asynchronous invocation”).
Future asyncRequestBody(
String endpointUri,
Object body
);
Future asyncRequestBody(
String endpointUri,
Object body,
Class type
);
Future asyncRequestBody(
Endpoint endpoint,
Object body
);
Future asyncRequestBody(
Endpoint endpoint,
Object body,
Class type
);
Request a message body and header(s)
You can try out the effect of setting a single header value using the
asyncRequestBodyAndHeader() methods. You supply the message body and header
setting as arguments to asyncRequestBodyAndHeader() and let the producer template
take care of inserting the body and exchange property into a default exchange object.
The asyncRequestBodyAndHeader() methods let you specify the target endpoint in one of
the following ways: as an endpoint URI, or as an Endpoint object. The return value that is
retrievable from the Future object is the body of the reply message (Out message body),
which can be returned either as a plain Object or converted to a specific type, T, using a
built-in type converter (see the section called “Asynchronous invocation”).
Future asyncRequestBodyAndHeader(
String endpointUri,
Object body,
String header,
Object headerValue
);
Future asyncRequestBodyAndHeader(
String endpointUri,
Object body,
String header,
486
CHAPTER 43. PRODUCER AND CONSUMER TEMPLATES
Object headerValue,
Class type
);
Future asyncRequestBodyAndHeader(
Endpoint endpoint,
Object body,
String header,
Object headerValue
);
Future asyncRequestBodyAndHeader(
Endpoint endpoint,
Object body,
String header,
Object headerValue,
Class type
);
The asyncRequestBodyAndHeaders() methods are similar to the
asyncRequestBodyAndHeader() methods, except that instead of supplying just a single
header setting, these methods allow you to specify a complete hash map of header
settings.
Future asyncRequestBodyAndHeaders(
String endpointUri,
Object body,
Map headers
);
Future asyncRequestBodyAndHeaders(
String endpointUri,
Object body,
Map headers,
Class type
);
Future asyncRequestBodyAndHeaders(
Endpoint endpoint,
Object body,
Map headers
);
Future asyncRequestBodyAndHeaders(
Endpoint endpoint,
Object body,
Map headers,
Class type
);
43.1.6. Asynchronous Send with Callback
Overview
The producer template also provides the option of processing the reply message in the
same sub-thread that is used to invoke the producer endpoint. In this case, you provide a
callback object, which automatically gets invoked in the sub-thread as soon as the reply
message is received. In other words, the asynchronous send with callback methods enable
487
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
you to initiate an invocation in your main thread and then have all of the associated
processing—invocation of the producer endpoint, waiting for a reply and processing the
reply—occur asynchronously in a sub-thread.
Send an exchange
The basic asyncCallback() method takes an Exchange argument and invokes an endpoint
asynchronously, using the message exchange pattern (MEP) of the specified exchange.
This method is similar to the asyncSend() method for exchanges, except that it takes an
additional org.apache.camel.spi.Synchronization argument, which is a callback
interface with two methods: onComplete() and onFailure(). For details of how to use the
Synchronization callback, see the section called “Asynchronous invocation with a
callback”.
The following asyncCallback() methods let you specify the target endpoint in one of the
following ways: as an endpoint URI, or as an Endpoint object.
Future asyncCallback(
String endpointUri,
Exchange exchange,
Synchronization onCompletion
);
Future asyncCallback(
Endpoint endpoint,
Exchange exchange,
Synchronization onCompletion
);
Send an exchange populated by a processor
The asyncCallback() method for processors calls a processor to populate a default
exchange and forces the message exchange pattern to be InOut (so that the invocation
obeys request/reply semantics).
The following asyncCallback() methods let you specify the target endpoint in one of the
following ways: as an endpoint URI, or as an Endpoint object.
Future asyncCallback(
String endpointUri,
Processor processor,
Synchronization onCompletion
);
Future asyncCallback(
Endpoint endpoint,
Processor processor,
Synchronization onCompletion
);
Send a message body
If you are only concerned with the contents of the message body that you want to send,
you can use the asyncCallbackSendBody() methods to send a message body
asynchronously and let the producer template take care of inserting the body into a default
exchange object.
488
CHAPTER 43. PRODUCER AND CONSUMER TEMPLATES
The asyncCallbackSendBody() methods let you specify the target endpoint in one of the
following ways: as an endpoint URI, or as an Endpoint object.
Future asyncCallbackSendBody(
String endpointUri,
Object body,
Synchronization onCompletion
);
Future asyncCallbackSendBody(
Endpoint endpoint,
Object body,
Synchronization onCompletion
);
Request a message body
If you are only concerned with the contents of the message body in the request and in the
reply, you can use the asyncCallbackRequestBody() methods to provide the request
message body as an argument and let the producer template take care of inserting the
body into a default exchange object.
The asyncCallbackRequestBody() methods let you specify the target endpoint in one of
the following ways: as an endpoint URI, or as an Endpoint object.
Future asyncCallbackRequestBody(
String endpointUri,
Object body,
Synchronization onCompletion
);
Future asyncCallbackRequestBody(
Endpoint endpoint,
Object body,
Synchronization onCompletion
);
43.2. USING THE CONSUMER TEMPLATE
Overview
The consumer template provides methods for polling a consumer endpoint in order to
receive incoming messages. You can choose to receive the incoming message either in the
form of an exchange object or in the form of a message body (where the message body
can be cast to a particular type using a built-in type converter).
Example of polling exchanges
You can use a consumer template to poll a consumer endpoint for exchanges using one of
the following polling methods: blocking receive(); receive() with a timeout; or
receiveNoWait(), which returns immediately. Because a consumer endpoint represents a
service, it is also essential to start the service thread by calling start() before you
attempt to poll for exchanges.
489
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
The following example shows how to poll an exchange from the seda:foo consumer
endpoint using the blocking receive() method:
import org.apache.camel.ProducerTemplate;
import org.apache.camel.ConsumerTemplate;
import org.apache.camel.Exchange;
...
ProducerTemplate template = context.createProducerTemplate();
ConsumerTemplate consumer = context.createConsumerTemplate();
// Start the consumer service
consumer.start();
...
template.sendBody("seda:foo", "Hello");
Exchange out = consumer.receive("seda:foo");
...
// Stop the consumer service
consumer.stop();
Where the consumer template instance, consumer, is instantiated using the
CamelContext.createConsumerTemplate() method and the consumer service thread is
started by calling ConsumerTemplate.start().
Example of polling message bodies
You can also poll a consumer endpoint for incoming message bodies using one of the
following methods: blocking receiveBody(); receiveBody() with a timeout; or
receiveBodyNoWait(), which returns immediately. As in the previous example, it is also
essential to start the service thread by calling start() before you attempt to poll for
exchanges.
The following example shows how to poll an incoming message body from the seda:foo
consumer endpoint using the blocking receiveBody() method:
import org.apache.camel.ProducerTemplate;
import org.apache.camel.ConsumerTemplate;
...
ProducerTemplate template = context.createProducerTemplate();
ConsumerTemplate consumer = context.createConsumerTemplate();
// Start the consumer service
consumer.start();
...
template.sendBody("seda:foo", "Hello");
Object body = consumer.receiveBody("seda:foo");
...
// Stop the consumer service
consumer.stop();
Methods for polling exchanges
There are three basic methods for polling exchanges from a consumer endpoint: receive()
without a timeout blocks indefinitely; receive() with a timeout blocks for the specified
period of milliseconds; and receiveNoWait() is non-blocking. You can specify the consumer
endpoint either as an endpoint URI or as an Endpoint instance.
490
CHAPTER 43. PRODUCER AND CONSUMER TEMPLATES
Exchange receive(String endpointUri);
Exchange receive(String endpointUri, long timeout);
Exchange receiveNoWait(String endpointUri);
Exchange receive(Endpoint endpoint);
Exchange receive(Endpoint endpoint, long timeout);
Exchange receiveNoWait(Endpoint endpoint);
Methods for polling message bodies
There are three basic methods for polling message bodies from a consumer endpoint:
receiveBody() without a timeout blocks indefinitely; receiveBody() with a timeout blocks
for the specified period of milliseconds; and receiveBodyNoWait() is non-blocking. You can
specify the consumer endpoint either as an endpoint URI or as an Endpoint instance.
Moreover, by calling the templating forms of these methods, you can convert the returned
body to a particular type, T, using a built-in type converter.
Object receiveBody(String endpointUri);
Object receiveBody(String endpointUri, long timeout);
Object receiveBodyNoWait(String endpointUri);
Object receiveBody(Endpoint endpoint);
Object receiveBody(Endpoint endpoint, long timeout);
Object receiveBodyNoWait(Endpoint endpoint);
T receiveBody(String endpointUri, Class type);
T receiveBody(String endpointUri, long timeout, Class type);
T receiveBodyNoWait(String endpointUri, Class type);
T receiveBody(Endpoint endpoint, Class type);
T receiveBody(Endpoint endpoint, long timeout, Class type);
T receiveBodyNoWait(Endpoint endpoint, Class type);
491
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
CHAPTER 44. IMPLEMENTING A COMPONENT
Abstract
This chapter provides a general overview of the approaches can be used to implement a
Apache Camel component.
44.1. COMPONENT ARCHITECTURE
44.1.1. Factory Patterns for a Component
Overview
A Apache Camel component consists of a set of classes that are related to each other
through a factory pattern. The primary entry point to a component is the Component object
itself (an instance of org.apache.camel.Component type). You can use theComponent
object as a factory to create Endpoint objects, which in turn act as factories for creating
Consumer, Producer, and Exchange objects. These relationships are summarized in
Figure 44.1, “Component Factory Patterns”
Figure 44.1. Component Factory Patterns
Component
A component implementation is an endpoint factory. The main task of a component
implementor is to implement the Component.createEndpoint() method, which is
responsible for creating new endpoints on demand.
Each kind of component must be associated with a component prefix that appears in an
endpoint URI. For example, the file component is usually associated with the file prefix,
which can be used in an endpoint URI like file://tmp/messages/input. When you install a new
component in Apache Camel, you must define the association between a particular
component prefix and the name of the class that implements the component.
Endpoint
492
CHAPTER 44. IMPLEMENTING A COMPONENT
Each endpoint instance encapsulates a particular endpoint URI. Every time Apache Camel
encounters a new endpoint URI, it creates a new endpoint instance. An endpoint object is
also a factory for creating consumer endpoints and producer endpoints.
Endpoints must implement the org.apache.camel.Endpoint interface. The Endpoint
interface defines the following factory methods:
createConsumer() and createPollingConsumer()—Creates a consumer endpoint,
which represents the source endpoint at the beginning of a route.
createProducer()—Creates a producer endpoint, which represents the target
endpoint at the end of a route.
createExchange()—Creates an exchange object, which encapsulates the messages
passed up and down the route.
Consumer
Consumer endpoints consume requests. They always appear at the start of a route and
they encapsulate the code responsible for receiving incoming requests and dispatching
outgoing replies. From a service-oriented prospective a consumer represents a service.
Consumers must implement the org.apache.camel.Consumer interface. There are a
number of different patterns you can follow when implementing a consumer. These
patterns are described in Section 44.1.3, “Consumer Patterns and Threading”.
Producer
Producer endpoints produce requests. They always appears at the end of a route and they
encapsulate the code responsible for dispatching outgoing requests and receiving incoming
replies. From a service-oriented prospective a producer represents a service consumer.
Producers must implement the org.apache.camel.Producer interface. You can optionally
implement the producer to support an asynchronous style of processing. See
Section 44.1.4, “Asynchronous Processing” for details.
Exchange
Exchange objects encapsulate a related set of messages. For example, one kind of
message exchange is a synchronous invocation, which consists of a request message and
its related reply.
Exchanges must implement the org.apache.camel.Exchange interface. The default
implementation, DefaultExchange, is sufficient for many component implementations.
However, if you want to associated extra data with the exchanges or have the exchanges
preform additional processing, it can be useful to customize the exchange implementation.
Message
There are two different message slots in an Exchange object:
In message—holds the current message.
Out message—temporarily holds a reply message.
493
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
All of the message types are represented by the same Java object,
org.apache.camel.Message. It is not always necessary to customize the message
implementation—the default implementation, DefaultMessage, is usually adequate.
44.1.2. Using a Component in a Route
Overview
A Apache Camel route is essentially a pipeline of processors, of
org.apache.camel.Processor type. Messages are encapsulated in an exchange object,E,
which gets passed from node to node by invoking the process() method. The architecture
of the processor pipeline is illustrated in Figure 44.2, “Consumer and Producer Instances in
a Route”.
Figure 44.2. Consumer and Producer Instances in a Route
Source endpoint
At the start of the route, you have the source endpoint, which is represented by an
org.apache.camel.Consumer object. The source endpoint is responsible for accepting
incoming request messages and dispatching replies. When constructing the route, Apache
Camel creates the appropriate Consumer type based on the component prefix from the
endpoint URI, as described in Section 44.1.1, “Factory Patterns for a Component”.
Processors
Each intermediate node in the pipeline is represented by a processor object (implementing
the org.apache.camel.Processor interface). You can insert either standard processors (for
example, filter, throttler, or delayer) or insert your own custom processor
implementations.
Target endpoint
At the end of the route is the target endpoint, which is represented by an
org.apache.camel.Producer object. Because it comes at the end of a processor pipeline,
the producer is also a processor object (implementing the org.apache.camel.Processor
interface). The target endpoint is responsible for sending outgoing request messages and
receiving incoming replies. When constructing the route, Apache Camel creates the
appropriate Producer type based on the component prefix from the endpoint URI.
44.1.3. Consumer Patterns and Threading
Overview
The pattern used to implement the consumer determines the threading model used in
processing the incoming exchanges. Consumers can be implemented using one of the
following patterns:
494
CHAPTER 44. IMPLEMENTING A COMPONENT
Event-driven pattern—The consumer is driven by an external thread.
Scheduled poll pattern—The consumer is driven by a dedicated thread pool.
Polling pattern—The threading model is left undefined.
Event-driven pattern
In the event-driven pattern, the processing of an incoming request is initiated when
another part of the application (typically a third-party library) calls a method implemented
by the consumer. A good example of an event-driven consumer is the Apache Camel JMX
component, where events are initiated by the JMX library. The JMX library calls the
handleNotification() method to initiate request processing—seeExample 47.4,
“JMXConsumer Implementation” for details.
Figure 44.3, “Event-Driven Consumer” shows an outline of the event-driven consumer
pattern. In this example, it is assumed that processing is triggered by a call to the notify()
method.
Figure 44.3. Event-Driven Consumer
The event-driven consumer processes incoming requests as follows:
1. The consumer must implement a method to receive the incoming event (in
Figure 44.3, “Event-Driven Consumer” this is represented by thenotify() method).
The thread that calls notify() is normally a separate part of the application, so the
consumer's threading policy is externally driven.
For example, in the case of the JMX consumer implementation, the consumer
implements the NotificationListener.handleNotification() method to receive
notifications from JMX. The threads that drive the consumer processing are created
within the JMX layer.
2. In the body of the notify() method, the consumer first converts the incoming
event into an exchange object, E, and then calls process() on the next processor in
the route, passing the exchange object as its argument.
Scheduled poll pattern
In the scheduled poll pattern, the consumer retrieves incoming requests by checking at
regular time intervals whether or not a request has arrived. Checking for requests is
scheduled automatically by a built-in timer class, the scheduled executor service, which is a
standard pattern provided by the java.util.concurrent library. The scheduled executor
service executes a particular task at timed intervals and it also manages a pool of threads,
which are used to run the task instances.
Figure 44.4, “Scheduled Poll Consumer” shows an outline of the scheduled poll consumer
pattern.
495
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
Figure 44.4. Scheduled Poll Consumer
The scheduled poll consumer processes incoming requests as follows:
1. The scheduled executor service has a pool of threads at its disposal, that can be
used to initiate consumer processing. After each scheduled time interval has
elapsed, the scheduled executor service attempts to grab a free thread from its pool
(there are five threads in the pool by default). If a free thread is available, it uses
that thread to call the poll() method on the consumer.
2. The consumer's poll() method is intended to trigger processing of an incoming
request. In the body of the poll() method, the consumer attempts to retrieve an
incoming message. If no request is available, the poll() method returns
immediately.
3. If a request message is available, the consumer inserts it into an exchange object
and then calls process() on the next processor in the route, passing the exchange
object as its argument.
Polling pattern
In the polling pattern, processing of an incoming request is initiated when a third-party calls
one of the consumer's polling methods:
receive()
receiveNoWait()
receive(long timeout)
It is up to the component implementation to define the precise mechanism for initiating
calls on the polling methods. This mechanism is not specified by the polling pattern.
Figure 44.5, “Polling Consumer” shows an outline of the polling consumer pattern.
496
CHAPTER 44. IMPLEMENTING A COMPONENT
Figure 44.5. Polling Consumer
The polling consumer processes incoming requests as follows:
1. Processing of an incoming request is initiated whenever one of the consumer's
polling methods is called. The mechanism for calling these polling methods is
implementation defined.
2. In the body of the receive() method, the consumer attempts to retrieve an
incoming request message. If no message is currently available, the behavior
depends on which receive method was called.
receiveNoWait() returns immediately
receive(long timeout) waits for the specified timeout interval[3] before
returning
receive() waits until a message is received
3. If a request message is available, the consumer inserts it into an exchange object
and then calls process() on the next processor in the route, passing the exchange
object as its argument.
44.1.4. Asynchronous Processing
Overview
Producer endpoints normally follow a synchronous pattern when processing an exchange.
When the preceding processor in a pipeline calls process() on a producer, the process()
method blocks until a reply is received. In this case, the processor's thread remains blocked
until the producer has completed the cycle of sending the request and receiving the reply.
Sometimes, however, you might prefer to decouple the preceding processor from the
producer, so that the processor's thread is released immediately and the process() call
does not block. In this case, you should implement the producer using anasynchronous
pattern, which gives the preceding processor the option of invoking a non-blocking version
of the process() method.
To give you an overview of the different implementation options, this section describes
both the synchronous and the asynchronous patterns for implementing a producer
endpoint.
497
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
Synchronous producer
Figure 44.6, “Synchronous Producer” shows an outline of a synchronous producer, where
the preceding processor blocks until the producer has finished processing the exchange.
Figure 44.6. Synchronous Producer
The synchronous producer processes an exchange as follows:
1. The preceding processor in the pipeline calls the synchronous process() method on
the producer to initiate synchronous processing. The synchronous process()
method takes a single exchange argument.
2. In the body of the process() method, the producer sends the request I(n message)
to the endpoint.
3. If required by the exchange pattern, the producer waits for the reply (Out message)
to arrive from the endpoint. This step can cause the process() method to block
indefinitely. However, if the exchange pattern does not mandate a reply, the
process() method can return immediately after sending the request.
4. When the process() method returns, the exchange object contains the reply from
the synchronous call (an Out message message).
Asynchronous producer
Figure 44.7, “Asynchronous Producer” shows an outline of an asynchronous producer,
where the producer processes the exchange in a sub-thread, and the preceding processor
is not blocked for any significant length of time.
498
CHAPTER 44. IMPLEMENTING A COMPONENT
Figure 44.7. Asynchronous Producer
The asynchronous producer processes an exchange as follows:
1. Before the processor can call the asynchronous process() method, it must create
an asynchronous callback object, which is responsible for processing the exchange
on the return portion of the route. For the asynchronous callback, the processor
must implement a class that inherits from the AsyncCallback interface.
2. The processor calls the asynchronous process() method on the producer to initiate
asynchronous processing. The asynchronous process() method takes two
arguments:
an exchange object
a synchronous callback object
3. In the body of the process() method, the producer creates a Runnable object that
encapsulates the processing code. The producer then delegates the execution of
this Runnable object to a sub-thread.
4. The asynchronous process() method returns, thereby freeing up the processor's
thread. The exchange processing continues in a separate sub-thread.
5. The Runnable object sends the In message to the endpoint.
6. If required by the exchange pattern, the Runnable object waits for the reply (Out or
Fault message) to arrive from the endpoint. TheRunnable object remains blocked
until the reply is received.
7. After the reply arrives, the Runnable object inserts the reply (Out message) into the
exchange object and then calls done() on the asynchronous callback object. The
asynchronous callback is then responsible for processing the reply message
(executed in the sub-thread).
499
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
44.2. HOW TO IMPLEMENT A COMPONENT
Overview
This section gives a brief overview of the steps required to implement a custom Apache
Camel component.
Which interfaces do you need to implement?
When implementing a component, it is usually necessary to implement the following Java
interfaces:
org.apache.camel.Component
org.apache.camel.Endpoint
org.apache.camel.Consumer
org.apache.camel.Producer
In addition, it can also be necessary to implement the following Java interfaces:
org.apache.camel.Exchange
org.apache.camel.Message
Implementation steps
You typically implement a custom component as follows:
1. Implement the Component interface—A component object acts as an endpoint
factory. You extend the DefaultComponent class and implement the
createEndpoint() method.
See Chapter 45, Component Interface.
2. Implement the Endpoint interface—An endpoint represents a resource identified by
a specific URI. The approach taken when implementing an endpoint depends on
whether the consumers follow an event-driven pattern, a scheduled poll pattern, or
a polling pattern.
For an event-driven pattern, implement the endpoint by extending the
DefaultEndpoint class and implementing the following methods:
createProducer()
createConsumer()
For a scheduled poll pattern, implement the endpoint by extending the
ScheduledPollEndpoint class and implementing the following methods:
createProducer()
createConsumer()
500
CHAPTER 44. IMPLEMENTING A COMPONENT
For a polling pattern, implement the endpoint by extending the
DefaultPollingEndpoint class and implementing the following methods:
createProducer()
createPollConsumer()
See Chapter 46, Endpoint Interface.
3. Implement the Consumer interface—There are several different approaches you can
take to implementing a consumer, depending on which pattern you need to
implement (event-driven, scheduled poll, or polling). The consumer implementation
is also crucially important for determining the threading model used for processing
a message exchange.
See Section 47.2, “Implementing the Consumer Interface”.
4. Implement the Producer interface—To implement a producer, you extend the
DefaultProducer class and implement the process() method.
See Chapter 48, Producer Interface.
5. Optionally implement the Exchange or the Message interface—The default
implementations of Exchange and Message can be used directly, but occasionally,
you might find it necessary to customize these types.
See Chapter 49, Exchange Interface and Chapter 50, Message Interface.
Installing and configuring the component
You can install a custom component in one of the following ways:
Add the component directly to the CamelContext—The
CamelContext.addComponent() method adds a component programatically.
Add the component using Spring configuration—The standard Spring bean element
creates a component instance. The bean's id attribute implicitly defines the
component prefix. For details, see Section 44.3.2, “Configuring a Component”.
Configure Apache Camel to auto-discover the component—Auto-discovery, ensures
that Apache Camel automatically loads the component on demand. For details, see
Section 44.3.1, “Setting Up Auto-Discovery”.
44.3. AUTO-DISCOVERY AND CONFIGURATION
44.3.1. Setting Up Auto-Discovery
Overview
Auto-discovery is a mechanism that enables you to dynamically add components to your
Apache Camel application. The component URI prefix is used as a key to load components
on demand. For example, if Apache Camel encounters the endpoint URI,
activemq://MyQName, and the ActiveMQ endpoint is not yet loaded, Apache Camel
searches for the component identified by the activemq prefix and dynamically loads the
component.
501
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
Availability of component classes
Before configuring auto-discovery, you must ensure that your custom component classes
are accessible from your current classpath. Typically, you bundle the custom component
classes into a JAR file, and add the JAR file to your classpath.
Configuring auto-discovery
To enable auto-discovery of your component, create a Java properties file named after the
component prefix, component-prefix, and store that file in the following location:
/META-INF/services/org/apache/camel/component/component-prefix
The component-prefix properties file must contain the following property setting:
class=component-class-name
Where component-class-name is the fully-qualified name of your custom component class.
You can also define additional system property settings in this file.
Example
For example, you can enable auto-discovery for the Apache Camel FTP component by
creating the following Java properties file:
/META-INF/services/org/apache/camel/component/ftp
Which contains the following Java property setting:
class=org.apache.camel.component.file.remote.RemoteFileComponent
NOTE
The Java properties file for the FTP component is already defined in the JAR
file, camel-ftp-Version.jar.
44.3.2. Configuring a Component
Overview
You can add a component by configuring it in the Apache Camel Spring configuration file,
META-INF/spring/camel-context.xml. To find the component, the component's URI prefix
is matched against the ID attribute of a bean element in the Spring configuration. If the
component prefix matches a bean element ID, Apache Camel instantiates the referenced
class and injects the properties specified in the Spring configuration.
NOTE
This mechanism has priority over auto-discovery. If the CamelContext finds a
Spring bean with the requisite ID, it will not attempt to find the component
using auto-discovery.
502
CHAPTER 44. IMPLEMENTING A COMPONENT
Define bean properties on your component class
If there are any properties that you want to inject into your component class, define them
as bean properties. For example:
public class CustomComponent extends
DefaultComponent {
...
PropType getProperty() { ... }
void setProperty(PropType v) { ...
}
}
The getProperty() method and the setProperty() method access the value of property.
Configure the component in Spring
To configure a component in Spring, edit the configuration file, META-INF/spring/camelcontext.xml, as shown inExample 44.1, “Configuring a Component in Spring”.
Example 44.1. Configuring a Component in Spring
RouteBuilderPackage
The bean element with ID component-prefix configures the component-class-name
component. You can inject properties into the component instance using property
elements. For example, the property element in the preceding example would inject the
value, propertyValue, into the property property by calling setProperty() on the
component.
Examples
Example 44.2, “JMS Component Spring Configuration” shows an example of how to
configure the Apache Camel's JMS component by defining a bean element with ID equal to
jms. These settings are added to the Spring configuration file,camel-context.xml.
Example 44.2. JMS Component Spring Configuration
503
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
org.apache.camel.example.spring
1
2 3
4
1
The CamelContext automatically instantiates any RouteBuilder classes that it finds in
the specified Java package, org.apache.camel.example.spring.
2
The bean element with ID, jms, configures the JMS component. The bean ID
corresponds to the component's URI prefix. For example, if a route specifies an
endpoint with the URI, jms://MyQName, Apache Camel automatically loads the JMS
component using the settings from the jms bean element.
3
JMS is just a wrapper for a messaging service. You must specify the concrete
implementation of the messaging system by setting the connectionFactory property
on the JmsComponent class.
4
In this example, the concrete implementation of the JMS messaging service is Apache
ActiveMQ. The brokerURL property initializes a connection to an ActiveMQ broker
instance, where the message broker is embedded in the local Java virtual machine
(JVM). If a broker is not already present in the JVM, ActiveMQ will instantiate it with the
options broker.persistent=false (the broker does not persist messages) and
broker.useJmx=false (the broker does not open a JMX port).
[3] The timeout interval is typically specified in milliseconds.
504
CHAPTER 45. COMPONENT INTERFACE
CHAPTER 45. COMPONENT INTERFACE
Abstract
This chapter describes how to implement the Component interface.
45.1. THE COMPONENT INTERFACE
Overview
To implement a Apache Camel component, you must implement the
org.apache.camel.Component interface. An instance of Component type provides the entry
point into a custom component. That is, all of the other objects in a component are
ultimately accessible through the Component instance. Figure 45.1, “Component Inheritance
Hierarchy” shows the relevant Java interfaces and classes that make up theComponent
inheritance hierarchy.
Figure 45.1. Component Inheritance Hierarchy
The Component interface
Example 45.1, “Component Interface” shows the definition of the
org.apache.camel.Component interface.
Example 45.1. Component Interface
package org.apache.camel;
public interface Component {
CamelContext getCamelContext();
void setCamelContext(CamelContext context);
Endpoint createEndpoint(String uri) throws Exception;
}
Component methods
The Component interface defines the following methods:
505
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
getCamelContext() and setCamelContext()—References the CamelContext to
which this Component belongs. The setCamelContext() method is automatically
called when you add the component to a CamelContext.
createEndpoint()—The factory method that gets called to createEndpoint
instances for this component. The uri parameter is the endpoint URI, which
contains the details required to create the endpoint.
45.2. IMPLEMENTING THE COMPONENT INTERFACE
The DefaultComponent class
You implement a new component by extending the
org.apache.camel.impl.DefaultComponent class, which provides some standard
functionality and default implementations for some of the methods. In particular, the
DefaultComponent class provides support for URI parsing and for creating ascheduled
executor (which is used for the scheduled poll pattern).
URI parsing
The createEndpoint(String uri) method defined in the baseComponent interface takes
a complete, unparsed endpoint URI as its sole argument. The DefaultComponent class, on
the other hand, defines a three-argument version of the createEndpoint() method with
the following signature:
protected abstract Endpoint createEndpoint(
String uri,
String remaining,
Map parameters
)
throws Exception;
uri is the original, unparsed URI; remaining is the part of the URI that remains after
stripping off the component prefix at the start and cutting off the query options at the end;
and parameters contains the parsed query options. It is this version of the
createEndpoint() method that you must override when inheriting from
DefaultComponent. This has the advantage that the endpoint URI is already parsed for you.
The following sample endpoint URI for the file component shows how URI parsing works in
practice:
file:///tmp/messages/foo?delete=true&moveNamePostfix=.old
For this URI, the following arguments are passed to the three-argument version of
createEndpoint():
Argument
Sample Value
uri
file:///tmp/messages/foo?delete=true&moveNamePostfix=.old
remaining
/tmp/messages/foo
506
CHAPTER 45. COMPONENT INTERFACE
Argument
Sample Value
parameters
Two entries are set in java.util.Map:
parameter delete is boolean true
parameter moveNamePostfix has the string value, .old.
Parameter injection
By default, the parameters extracted from the URI query options are injected on the
endpoint's bean properties. The DefaultComponent class automatically injects the
parameters for you.
For example, if you want to define a custom endpoint that supports two URI query options:
delete and moveNamePostfix. All you must do is define the corresponding bean methods
(getters and setters) in the endpoint class:
public class FileEndpoint extends ScheduledPollEndpoint {
...
public boolean isDelete() {
return delete;
}
public void setDelete(boolean delete) {
this.delete = delete;
}
...
public String getMoveNamePostfix() {
return moveNamePostfix;
}
public void setMoveNamePostfix(String moveNamePostfix) {
this.moveNamePostfix = moveNamePostfix;
}
}
It is also possible to inject URI query options into consumer parameters. For details, see the
section called “Consumer parameter injection”.
Disabling endpoint parameter injection
If there are no parameters defined on your Endpoint class, you can optimize the process of
endpoint creation by disabling endpoint parameter injection. To disable parameter injection
on endpoints, override the useIntrospectionOnEndpoint() method and implement it to
return false, as follows:
protected boolean useIntrospectionOnEndpoint() {
return false;
}
507
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
NOTE
The useIntrospectionOnEndpoint() method does not affect the parameter
injection that might be performed on a Consumer class. Parameter injection at
that level is controlled by the Endpoint.configureProperties() method (see
Section 46.2, “Implementing the Endpoint Interface”).
Scheduled executor service
The scheduled executor is used in the scheduled poll pattern, where it is responsible for
driving the periodic polling of a consumer endpoint (a scheduled executor is effectively a
thread pool implementation).
To instantiate a scheduled executor service, use the ExecutorServiceStrategy object that
is returned by the CamelContext.getExecutorServiceStrategy() method. For details of
the Apache Camel threading model, see Section 2.9, “Threading Model”.
NOTE
Prior to Apache Camel 2.3, the DefaultComponent class provided a
getExecutorService() method for creating thread pool instances. Since 2.3,
however, the creation of thread pools is now managed centrally by the
ExecutorServiceStrategy object.
Validating the URI
If you want to validate the URI before creating an endpoint instance, you can override the
validateURI() method from the DefaultComponent class, which has the following
signature:
protected void validateURI(String uri,
String path,
Map parameters)
throws ResolveEndpointFailedException;
If the supplied URI does not have the required format, the implementation of
validateURI() should throw the org.apache.camel.ResolveEndpointFailedException
exception.
Creating an endpoint
Example 45.2, “Implementation of createEndpoint()” outlines how to implement the
DefaultComponent.createEndpoint() method, which is responsible for creating endpoint
instances on demand.
Example 45.2. Implementation of createEndpoint()
1 public class CustomComponent extends DefaultComponent {
...
protected Endpoint createEndpoint(String uri, String remaining, Map
parameters)
throws Exception {
2
CustomEndpoint
result = new CustomEndpoint(uri, this);
3
// ...
return result;
}
}
508
CHAPTER 45. COMPONENT INTERFACE
1
The CustomComponent is the name of your custom component class, which is defined
by extending the DefaultComponent class.
2
When extending DefaultComponent, you must implement the createEndpoint()
method with three arguments (see the section called “URI parsing”).
3
Create an instance of your custom endpoint type, CustomEndpoint, by calling its
constructor. At a minimum, this constructor takes a copy of the original URI string, uri,
and a reference to this component instance, this.
Example
Example 45.3, “FileComponent Implementation” shows a sample implementation of a
FileComponent class.
Example 45.3. FileComponent Implementation
package org.apache.camel.component.file;
import org.apache.camel.CamelContext;
import org.apache.camel.Endpoint;
import org.apache.camel.impl.DefaultComponent;
import java.io.File;
import java.util.Map;
public class FileComponent extends DefaultComponent {
public static final String HEADER_FILE_NAME =
"org.apache.camel.file.name";
1
public FileComponent() {
}
2
public FileComponent(CamelContext context) {
super(context);
}
protected Endpoint createEndpoint(String uri, String remaining, Map
parameters)
throws Exception {
3
File file = new File(remaining);
FileEndpoint result = new FileEndpoint(file, uri, this);
return result;
}
}
1
Always define a no-argument constructor for the component class in order to facilitate
automatic instantiation of the class.
2
A constructor that takes the parent CamelContext instance as an argument is
convenient when creating a component instance by programming.
509
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
3
510
The implementation of the FileComponent.createEndpoint() method follows the
pattern described in Example 45.2, “Implementation of createEndpoint()”. The
implementation creates a FileEndpoint object.
CHAPTER 46. ENDPOINT INTERFACE
CHAPTER 46. ENDPOINT INTERFACE
Abstract
This chapter describes how to implement the Endpoint interface, which is an essential step
in the implementation of a Apache Camel component.
46.1. THE ENDPOINT INTERFACE
Overview
An instance of org.apache.camel.Endpoint type encapsulates an endpoint URI, and it also
serves as a factory for Consumer, Producer, and Exchange objects. There are three different
approaches to implementing an endpoint:
Event-driven
scheduled poll
polling
These endpoint implementation patterns complement the corresponding patterns for
implementing a consumer—see Section 47.2, “Implementing the Consumer Interface”.
Figure 46.1, “Endpoint Inheritance Hierarchy” shows the relevant Java interfaces and
classes that make up the Endpoint inheritance hierarchy.
511
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
Figure 46.1. Endpoint Inheritance Hierarchy
The Endpoint interface
Example 46.1, “Endpoint Interface” shows the definition of the
org.apache.camel.Endpoint interface.
Example 46.1. Endpoint Interface
package org.apache.camel;
public interface Endpoint {
boolean isSingleton();
String getEndpointUri();
String getEndpointKey();
CamelContext getCamelContext();
void setCamelContext(CamelContext context);
void configureProperties(Map options);
boolean isLenientProperties();
512
CHAPTER 46. ENDPOINT INTERFACE
Exchange createExchange();
Exchange createExchange(ExchangePattern pattern);
Exchange createExchange(Exchange exchange);
Producer createProducer() throws Exception;
Consumer createConsumer(Processor processor) throws Exception;
PollingConsumer createPollingConsumer() throws Exception;
}
Endpoint methods
The Endpoint interface defines the following methods:
isSingleton()—Returns true, if you want to ensure that each URI maps to a single
endpoint within a CamelContext. When this property is true, multiple references to
the identical URI within your routes always refer to a single endpoint instance. When
this property is false, on the other hand, multiple references to the same URI within
your routes refer to distinct endpoint instances. Each time you refer to the URI in a
route, a new endpoint instance is created.
getEndpointUri()—Returns the endpoint URI of this endpoint.
getEndpointKey()—Used by org.apache.camel.spi.LifecycleStrategy when
registering the endpoint.
getCamelContext()—return a reference to the CamelContext instance to which this
endpoint belongs.
setCamelContext()—Sets the CamelContext instance to which this endpoint
belongs.
configureProperties()—Stores a copy of the parameter map that is used to inject
parameters when creating a new Consumer instance.
isLenientProperties()—Returns true to indicate that the URI is allowed to
contain unknown parameters (that is, parameters that cannot be injected on the
Endpoint or the Consumer class). Normally, this method should be implemented to
return false.
createExchange()—An overloaded method with the following variants:
Exchange createExchange()—Creates a new exchange instance with a default
exchange pattern setting.
Exchange createExchange(ExchangePattern pattern)—Creates a new
exchange instance with the specified exchange pattern.
Exchange createExchange(Exchange exchange)—Converts the given
exchange argument to the type of exchange needed for this endpoint. If the
given exchange is not already of the correct type, this method copies it into a
new instance of the correct type. A default implementation of this method is
provided in the DefaultEndpoint class.
createProducer()—Factory method used to create newProducer instances.
513
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
createConsumer()—Factory method to create new event-driven consumer
instances. The processor argument is a reference to the first processor in the route.
createPollingConsumer()—Factory method to create new polling consumer
instances.
Endpoint singletons
In order to avoid unnecessary overhead, it is a good idea to create a single endpoint
instance for all endpoints that have the same URI (within a CamelContext). You can enforce
this condition by implementing isSingleton() to return true.
NOTE
In this context, same URI means that two URIs are the same when compared
using string equality. In principle, it is possible to have two URIs that are
equivalent, though represented by different strings. In that case, the URIs
would not be treated as the same.
46.2. IMPLEMENTING THE ENDPOINT INTERFACE
Alternative ways of implementing an endpoint
The following alternative endpoint implementation patterns are supported:
Event-driven endpoint implementation
Scheduled poll endpoint implementation
Polling endpoint implementation
Event-driven endpoint implementation
If your custom endpoint conforms to the event-driven pattern (see Section 44.1.3,
“Consumer Patterns and Threading”), it is implemented by extending the abstract class,
org.apache.camel.impl.DefaultEndpoint, as shown inExample 46.2, “Implementing
DefaultEndpoint”.
Example 46.2. Implementing DefaultEndpoint
import java.util.Map;
import java.util.concurrent.BlockingQueue;
import
import
import
import
import
import
import
org.apache.camel.Component;
org.apache.camel.Consumer;
org.apache.camel.Exchange;
org.apache.camel.Processor;
org.apache.camel.Producer;
org.apache.camel.impl.DefaultEndpoint;
org.apache.camel.impl.DefaultExchange;
1 public class CustomEndpoint extends DefaultEndpoint {
2
514
public CustomEndpoint(String endpointUri, Component component) {
CHAPTER 46. ENDPOINT INTERFACE
super(endpointUri, component);
// Do any other initialization...
}
public Producer createProducer() throws Exception {
return new CustomProducer(this);
3
}
public Consumer createConsumer(Processor processor) throws Exception
4{
return new CustomConsumer(this, processor);
}
public boolean isSingleton() {
return true;
}
// Implement the following methods, only if you need to set exchange
properties.
//
public Exchange createExchange() {
5
return this.createExchange(getExchangePattern());
}
public Exchange createExchange(ExchangePattern pattern) {
Exchange result = new DefaultExchange(getCamelContext(),
pattern);
// Set exchange properties
...
return result;
}
}
1
Implement an event-driven custom endpoint, CustomEndpoint, by extending the
DefaultEndpoint class.
2
You must have at least one constructor that takes the endpoint URI, endpointUri, and
the parent component reference, component, as arguments.
3
Implement the createProducer() factory method to create producer endpoints.
4
Implement the createConsumer() factory method to create event-driven consumer
instances.
IMPORTANT
Do not override the createPollingConsumer() method.
5
In general, it is not necessary to override the createExchange() methods. The
implementations inherited from DefaultEndpoint create a DefaultExchange object by
default, which can be used in any Apache Camel component. If you need to initialize
some exchange properties in the DefaultExchange object, however, it is appropriate
to override the createExchange() methods here in order to add the exchange
property settings.
515
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
The DefaultEndpoint class provides default implementations of the following methods,
which you might find useful when writing your custom endpoint code:
getEndpointUri()—Returns the endpoint URI.
getCamelContext()—Returns a reference to the CamelContext.
getComponent()—Returns a reference to the parent component.
createPollingConsumer()—Creates a polling consumer. The created polling
consumer's functionality is based on the event-driven consumer. If you override the
event-driven consumer method, createConsumer(), you get a polling consumer
implementation for free.
createExchange(Exchange e)—Converts the given exchange object, e, to the type
required for this endpoint. This method creates a new endpoint using the overridden
createExchange() endpoints. This ensures that the method also works for custom
exchange types.
Scheduled poll endpoint implementation
If your custom endpoint conforms to the scheduled poll pattern (see Section 44.1.3,
“Consumer Patterns and Threading”) it is implemented by inheriting from the abstract
class, org.apache.camel.impl.ScheduledPollEndpoint, as shown inExample 46.3,
“ScheduledPollEndpoint Implementation”.
Example 46.3. ScheduledPollEndpoint Implementation
import
import
import
import
import
import
org.apache.camel.Consumer;
org.apache.camel.Processor;
org.apache.camel.Producer;
org.apache.camel.ExchangePattern;
org.apache.camel.Message;
org.apache.camel.impl.ScheduledPollEndpoint;
1 public class CustomEndpoint extends ScheduledPollEndpoint {
protected CustomEndpoint(String endpointUri, CustomComponent
component)
{
2
super(endpointUri, component);
// Do any other initialization...
}
public Producer createProducer() throws Exception {
Producer result = new CustomProducer(this);
return result;
3
}
public Consumer createConsumer(Processor processor) throws Exception
4{
Consumer result = new CustomConsumer(this, processor);
configureConsumer(result);
return result;
5
}
public boolean isSingleton() {
516
CHAPTER 46. ENDPOINT INTERFACE
return true;
}
// Implement the following methods, only if you need to set exchange
properties.
//
public Exchange createExchange() {
6
return this.createExchange(getExchangePattern());
}
public Exchange createExchange(ExchangePattern pattern) {
Exchange result = new DefaultExchange(getCamelContext(),
pattern);
// Set exchange properties
...
return result;
}
}
1
Implement a scheduled poll custom endpoint, CustomEndpoint, by extending the
ScheduledPollEndpoint class.
2
You must to have at least one constructor that takes the endpoint URI, endpointUri,
and the parent component reference, component, as arguments.
3
Implement the createProducer() factory method to create a producer endpoint.
4
Implement the createConsumer() factory method to create a scheduled poll consumer
instance.
IMPORTANT
Do not override the createPollingConsumer() method.
5
The configureConsumer() method, defined in the ScheduledPollEndpoint base
class, is responsible for injecting consumer query options into the consumer. See the
section called “Consumer parameter injection”.
6
In general, it is not necessary to override the createExchange() methods. The
implementations inherited from DefaultEndpoint create a DefaultExchange object by
default, which can be used in any Apache Camel component. If you need to initialize
some exchange properties in the DefaultExchange object, however, it is appropriate
to override the createExchange() methods here in order to add the exchange
property settings.
Polling endpoint implementation
If your custom endpoint conforms to the polling consumer pattern (see Section 44.1.3,
“Consumer Patterns and Threading”), it is implemented by inheriting from the abstract
class, org.apache.camel.impl.DefaultPollingEndpoint, as shown inExample 46.4,
“DefaultPollingEndpoint Implementation”.
Example 46.4. DefaultPollingEndpoint Implementation
517
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
import
import
import
import
import
import
org.apache.camel.Consumer;
org.apache.camel.Processor;
org.apache.camel.Producer;
org.apache.camel.ExchangePattern;
org.apache.camel.Message;
org.apache.camel.impl.DefaultPollingEndpoint;
public class CustomEndpoint extends DefaultPollingEndpoint {
...
public PollingConsumer createPollingConsumer() throws Exception {
PollingConsumer result = new CustomConsumer(this);
configureConsumer(result);
return result;
}
// Do NOT implement createConsumer(). It is already implemented in
DefaultPollingEndpoint.
...
}
Because this CustomEndpoint class is a polling endpoint, you must implement the
createPollingConsumer() method instead of the createConsumer() method. The
consumer instance returned from createPollingConsumer() must inherit from the
PollingConsumer interface. For details of how to implement a polling consumer, seethe
section called “Polling consumer implementation”.
Apart from the implementation of the createPollingConsumer() method, the steps for
implementing a DefaultPollingEndpoint are similar to the steps for implementing a
ScheduledPollEndpoint. See Example 46.3, “ScheduledPollEndpoint Implementation” for
details.
Implementing the BrowsableEndpoint interface
If you want to expose the list of exchange instances that are pending in the current
endpoint, you can implement the org.apache.camel.spi.BrowsableEndpoint interface,
as shown in Example 46.5, “BrowsableEndpoint Interface”. It makes sense to implement
this interface if the endpoint performs some sort of buffering of incoming events. For
example, the Apache Camel SEDA endpoint implements the BrowsableEndpoint interface—
see Example 46.6, “SedaEndpoint Implementation”.
Example 46.5. BrowsableEndpoint Interface
package org.apache.camel.spi;
import java.util.List;
import org.apache.camel.Endpoint;
import org.apache.camel.Exchange;
public interface BrowsableEndpoint extends Endpoint {
List getExchanges();
}
518
CHAPTER 46. ENDPOINT INTERFACE
Example
Example 46.6, “SedaEndpoint Implementation” shows a sample implementation of
SedaEndpoint. The SEDA endpoint is an example of anevent-driven endpoint. Incoming
events are stored in a FIFO queue (an instance of java.util.concurrent.BlockingQueue)
and a SEDA consumer starts up a thread to read and process the events. The events
themselves are represented by org.apache.camel.Exchange objects.
Example 46.6. SedaEndpoint Implementation
package org.apache.camel.component.seda;
import
import
import
import
java.util.ArrayList;
java.util.List;
java.util.Map;
java.util.concurrent.BlockingQueue;
import
import
import
import
import
import
import
org.apache.camel.Component;
org.apache.camel.Consumer;
org.apache.camel.Exchange;
org.apache.camel.Processor;
org.apache.camel.Producer;
org.apache.camel.impl.DefaultEndpoint;
org.apache.camel.spi.BrowsableEndpoint;
public class SedaEndpoint extends DefaultEndpoint implements
1 BrowsableEndpoint {
private BlockingQueue queue;
public SedaEndpoint(String endpointUri, Component component,
2 BlockingQueue queue) {
super(endpointUri, component);
this.queue = queue;
}
public SedaEndpoint(String uri, SedaComponent component, Map
parameters)
{
3
this(uri, component, component.createQueue(uri, parameters));
}
public Producer createProducer() throws Exception {
return new CollectionProducer(this, getQueue());
4
}
public Consumer createConsumer(Processor processor) throws Exception
5{
return new SedaConsumer(this, processor);
}
public BlockingQueue getQueue() {
return queue;
6
}
public boolean isSingleton() {
return true;
7
}
519
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
public List getExchanges() {
return new ArrayList(getQueue());
8
}
}
1
The SedaEndpoint class follows the pattern for implementing an event-driven endpoint
by extending the DefaultEndpoint class. The SedaEndpoint class also implements the
BrowsableEndpoint interface, which provides access to the list of exchange objects in
the queue.
2
Following the usual pattern for an event-driven consumer, SedaEndpoint defines a
constructor that takes an endpoint argument, endpointUri, and a component
reference argument, component.
3
Another constructor is provided, which delegates queue creation to the parent
component instance.
4
The createProducer() factory method creates an instance of CollectionProducer,
which is a producer implementation that adds events to the queue.
5
The createConsumer() factory method creates an instance of SedaConsumer, which is
responsible for pulling events off the queue and processing them.
6
The getQueue() method returns a reference to the queue.
7
The isSingleton() method returns true, indicating that a single endpoint instance
should be created for each unique URI string.
8
The getExchanges() method implements the corresponding abstract method from
BrowsableEndpoint.
520
CHAPTER 47. CONSUMER INTERFACE
CHAPTER 47. CONSUMER INTERFACE
Abstract
This chapter describes how to implement the Consumer interface, which is an essential step
in the implementation of a Apache Camel component.
47.1. THE CONSUMER INTERFACE
Overview
An instance of org.apache.camel.Consumer type represents a source endpoint in a route.
There are several different ways of implementing a consumer (see Section 44.1.3,
“Consumer Patterns and Threading”), and this degree of flexibility is reflected in the
inheritance hierarchy ( see Figure 47.1, “Consumer Inheritance Hierarchy”), which includes
several different base classes for implementing a consumer.
Figure 47.1. Consumer Inheritance Hierarchy
Consumer parameter injection
For consumers that follow the scheduled poll pattern (see the section called “Scheduled
poll pattern”), Apache Camel provides support for injecting parameters into consumer
instances. For example, consider the following endpoint URI for a component identified by
the custom prefix:
custom:destination?consumer.myConsumerParam
521
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
Apache Camel provides support for automatically injecting query options of the form
consumer.*. For the consumer.myConsumerParam parameter, you need to define
corresponding setter and getter methods on the Consumer implementation class as follows:
public class CustomConsumer extends ScheduledPollConsumer {
...
String getMyConsumerParam() { ... }
void setMyConsumerParam(String s) { ... }
...
}
Where the getter and setter methods follow the usual Java bean conventions (including
capitalizing the first letter of the property name).
In addition to defining the bean methods in your Consumer implementation, you must also
remember to call the configureConsumer() method in the implementation of
Endpoint.createConsumer(). See the section called “Scheduled poll endpoint
implementation”). Example 47.1, “FileEndpoint createConsumer() Implementation” shows
an example of a createConsumer() method implementation, taken from the FileEndpoint
class in the file component:
Example 47.1. FileEndpoint createConsumer() Implementation
...
public class FileEndpoint extends ScheduledPollEndpoint {
...
public Consumer createConsumer(Processor processor) throws
Exception {
Consumer result = new FileConsumer(this, processor);
configureConsumer(result);
return result;
}
...
}
At run time, consumer parameter injection works as follows:
1. When the endpoint is created, the default implementation of
DefaultComponent.createEndpoint(String uri) parses the URI to extract the
consumer parameters, and stores them in the endpoint instance by calling
ScheduledPollEndpoint.configureProperties().
2. When createConsumer() is called, the method implementation calls
configureConsumer() to inject the consumer parameters (seeExample 47.1,
“FileEndpoint createConsumer() Implementation”).
3. The configureConsumer() method uses Java reflection to call the setter methods
whose names match the relevant options after the consumer. prefix has been
stripped off.
Scheduled poll parameters
522
CHAPTER 47. CONSUMER INTERFACE
A consumer that follows the scheduled poll pattern automatically supports the consumer
parameters shown in Table 47.1, “Scheduled Poll Parameters” (which can appear as query
options in the endpoint URI).
Table 47.1. Scheduled Poll Parameters
Name
Default
Description
initialDelay
1000
Delay, in milliseconds, before the first poll.
delay
500
Depends on the value of the useFixedDelay flag (time
unit is milliseconds).
useFixedDelay
false
If false, the delay parameter is interpreted as the polling
period. Polls will occur at initialDelay,
initialDelay+delay, initialDelay+2*delay, and so
on.
If true, the delay parameter is interpreted as the time
elapsed between the previous execution and the next
execution. Polls will occur at initialDelay,
initialDelay+[ProcessingTime]+delay, and so on.
Where ProcessingTime is the time taken to process an
exchange object in the current thread.
Converting between event-driven and polling consumers
Apache Camel provides two special consumer implementations which can be used to
convert back and forth between an event-driven consumer and a polling consumer. The
following conversion classes are provided:
org.apache.camel.impl.EventDrivenPollingConsumer—Converts an event-driven
consumer into a polling consumer instance.
org.apache.camel.impl.DefaultScheduledPollConsumer—Converts a polling
consumer into an event-driven consumer instance.
In practice, these classes are used to simplify the task of implementing an Endpoint type.
The Endpoint interface defines the following two methods for creating a consumer
instance:
package org.apache.camel;
public interface Endpoint {
...
Consumer createConsumer(Processor processor) throws Exception;
PollingConsumer createPollingConsumer() throws Exception;
}
createConsumer() returns an event-driven consumer and createPollingConsumer()
returns a polling consumer. You would only implement one these methods. For example, if
you are following the event-driven pattern for your consumer, you would implement the
createConsumer() method provide a method implementation for
createPollingConsumer() that simply raises an exception. With the help of the conversion
classes, however, Apache Camel is able to provide a more useful default implementation.
523
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
For example, if you want to implement your consumer according to the event-driven
pattern, you implement the endpoint by extending DefaultEndpoint and implementing the
createConsumer() method. The implementation of createPollingConsumer() is inherited
from DefaultEndpoint, where it is defined as follows:
public PollingConsumer createPollingConsumer() throws Exception {
return new EventDrivenPollingConsumer(this);
}
The EventDrivenPollingConsumer constructor takes a reference to the event-driven
consumer, this, effectively wrapping it and converting it into a polling consumer. To
implement the conversion, the EventDrivenPollingConsumer instance buffers incoming
events and makes them available on demand through the receive(), the receive(long
timeout), and the receiveNoWait() methods.
Analogously, if you are implementing your consumer according to the polling pattern, you
implement the endpoint by extending DefaultPollingEndpoint and implementing the
createPollingConsumer() method. In this case, the implementation of the
createConsumer() method is inherited from DefaultPollingEndpoint, and the default
implementation returns a DefaultScheduledPollConsumer instance (which converts the
polling consumer into an event-driven consumer).
ShutdownPrepared interface
Consumer classes can optionally implement the
org.apache.camel.spi.ShutdownPrepared interface, which enables your custom
consumer endpoint to receive shutdown notifications.
Example 47.2, “ShutdownPrepared Interface” shows the definition of the
ShutdownPrepared interface.
Example 47.2. ShutdownPrepared Interface
package org.apache.camel.spi;
public interface ShutdownPrepared {
void prepareShutdown(boolean forced);
}
The ShutdownPrepared interface defines the following methods:
prepareShutdown
Receives notifications to shut down the consumer endpoint in one or two phases, as
follows:
1. Graceful shutdown—where the forced argument has the value false. Attempt to
clean up resources gracefully. For example, by stopping threads gracefully.
2. Forced shutdown—where the forced argument has the value true. This means
that the shutdown has timed out, so you must clean up resources more
aggressively. This is the last chance to clean up resources before the process
524
CHAPTER 47. CONSUMER INTERFACE
exits.
ShutdownAware interface
Consumer classes can optionally implement the org.apache.camel.spi.ShutdownAware
interface, which interacts with the graceful shutdown mechanism, enabling a consumer to
ask for extra time to shut down. This is typically needed for components such as SEDA,
which can have pending exchanges stored in an internal queue. Normally, you would want
to process all of the exchanges in the queue before shutting down the SEDA consumer.
Example 47.3, “ShutdownAware Interface” shows the definition of the ShutdownAware
interface.
Example 47.3. ShutdownAware Interface
// Java
package org.apache.camel.spi;
import org.apache.camel.ShutdownRunningTask;
public interface ShutdownAware extends ShutdownPrepared {
boolean deferShutdown(ShutdownRunningTask shutdownRunningTask);
int getPendingExchangesSize();
}
The ShutdownAware interface defines the following methods:
deferShutdown
Return true from this method, if you want to delay shutdown of the consumer. The
shutdownRunningTask argument is an enum which can take either of the following
values:
ShutdownRunningTask.CompleteCurrentTaskOnly—finish processing the
exchanges that are currently being processed by the consumer's thread pool, but
do not attempt to process any more exchanges than that.
ShutdownRunningTask.CompleteAllTasks—process all of the pending
exchanges. For example, in the case of the SEDA component, the consumer
would process all of the exchanges from its incoming queue.
getPendingExchangesSize
Indicates how many exchanges remain to be processed by the consumer. A zero value
indicates that processing is finished and the consumer can be shut down.
For an example of how to define the ShutdownAware methods, see Example 47.7, “Custom
Threading Implementation”.
47.2. IMPLEMENTING THE CONSUMER INTERFACE
525
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
Alternative ways of implementing a consumer
You can implement a consumer in one of the following ways:
Event-driven consumer implementation
Scheduled poll consumer implementation
Polling consumer implementation
Custom threading implementation
Event-driven consumer implementation
In an event-driven consumer, processing is driven explicitly by external events. The events
are received through an event-listener interface, where the listener interface is specific to
the particular event source.
Example 47.4, “JMXConsumer Implementation” shows the implementation of the
JMXConsumer class, which is taken from the Apache Camel JMX component implementation.
The JMXConsumer class is an example of an event-driven consumer, which is implemented
by inheriting from the org.apache.camel.impl.DefaultConsumer class. In the case of the
JMXConsumer example, events are represented by calls on the
NotificationListener.handleNotification() method, which is a standard way of
receiving JMX events. In order to receive these JMX events, it is necessary to implement the
NotificationListener interface and override the handleNotification() method, as
shown in Example 47.4, “JMXConsumer Implementation”.
Example 47.4. JMXConsumer Implementation
package org.apache.camel.component.jmx;
import
import
import
import
javax.management.Notification;
javax.management.NotificationListener;
org.apache.camel.Processor;
org.apache.camel.impl.DefaultConsumer;
public class JMXConsumer extends DefaultConsumer implements
1 NotificationListener {
JMXEndpoint jmxEndpoint;
public JMXConsumer(JMXEndpoint endpoint, Processor processor) {
super(endpoint, processor);
this.jmxEndpoint = endpoint;
2
}
public void handleNotification(Notification notification, Object
handback)
{
3
try {
4 getProcessor().process(jmxEndpoint.createExchange(notification));
} catch (Throwable e) {
handleException(e);
5
}
}
526
CHAPTER 47. CONSUMER INTERFACE
}
1
The JMXConsumer pattern follows the usual pattern for event-driven consumers by
extending the DefaultConsumer class. Additionally, because this consumer is designed
to receive events from JMX (which are represented by JMX notifications), it is necessary
to implement the NotificationListener interface.
2
You must implement at least one constructor that takes a reference to the parent
endpoint, endpoint, and a reference to the next processor in the chain,processor, as
arguments.
3
The handleNotification() method (which is defined inNotificationListener) is
automatically invoked by JMX whenever a JMX notification arrives. The body of this
method should contain the code that performs the consumer's event processing.
Because the handleNotification() call originates from the JMX layer, the consumer's
threading model is implicitly controlled by the JMX layer, not by the JMXConsumer class.
NOTE
The handleNotification() method is specific to the JMX example. When
implementing your own event-driven consumer, you must identify an
analogous event listener method to implement in your custom consumer.
4
This line of code combines two steps. First, the JMX notification object is converted into
an exchange object, which is the generic representation of an event in Apache Camel.
Then the newly created exchange object is passed to the next processor in the route
(invoked synchronously).
5
The handleException() method is implemented by theDefaultConsumer base class.
By default, it handles exceptions using the
org.apache.camel.impl.LoggingExceptionHandler class.
Scheduled poll consumer implementation
In a scheduled poll consumer, polling events are automatically generated by a timer class,
java.util.concurrent.ScheduledExecutorService. To receive the generated polling
events, you must implement the ScheduledPollConsumer.poll() method (see
Section 44.1.3, “Consumer Patterns and Threading”).
Example 47.5, “ScheduledPollConsumer Implementation” shows how to implement a
consumer that follows the scheduled poll pattern, which is implemented by extending the
ScheduledPollConsumer class.
Example 47.5. ScheduledPollConsumer Implementation
import java.util.concurrent.ScheduledExecutorService;
import
import
import
import
import
import
org.apache.camel.Consumer;
org.apache.camel.Endpoint;
org.apache.camel.Exchange;
org.apache.camel.Message;
org.apache.camel.PollingConsumer;
org.apache.camel.Processor;
527
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
import org.apache.camel.impl.ScheduledPollConsumer;
1 public class CustomConsumer extends ScheduledPollConsumer {
private final CustomEndpoint endpoint;
public CustomConsumer(CustomEndpoint endpoint, Processor processor)
2{
super(endpoint, processor);
this.endpoint = endpoint;
}
3
4
5
6
protected void poll() throws Exception {
Exchange exchange = /* Receive exchange object ... */;
// Example of a synchronous processor.
getProcessor().process(exchange);
}
@Override
protected void doStart() throws Exception {
// Pre-Start:
// Place code here to execute just before start of processing.
super.doStart();
// Post-Start:
// Place code here to execute just after start of processing.
}
@Override
protected void doStop() throws Exception {
// Pre-Stop:
// Place code here to execute just before processing stops.
super.doStop();
// Post-Stop:
// Place code here to execute just after processing stops.
}
}
1
Implement a scheduled poll consumer class, CustomConsumer, by extending the
org.apache.camel.impl.ScheduledPollConsumer class.
2
You must implement at least one constructor that takes a reference to the parent
endpoint, endpoint, and a reference to the next processor in the chain,processor, as
arguments.
3
Override the poll() method to receive the scheduled polling events. This is where you
should put the code that retrieves and processes incoming events (represented by
exchange objects).
4
In this example, the event is processed synchronously. If you want to process events
asynchronously, you should use a reference to an asynchronous processor instead, by
calling getAsyncProcessor(). For details of how to process events asynchronously,
see Section 44.1.4, “Asynchronous Processing”.
5
528
CHAPTER 47. CONSUMER INTERFACE
(Optional) If you want some lines of code to execute as the consumer is starting up,
override the doStart() method as shown.
6
(Optional) If you want some lines of code to execute as the consumer is stopping,
override the doStop() method as shown.
Polling consumer implementation
Example 47.6, “PollingConsumerSupport Implementation” outlines how to implement a
consumer that follows the polling pattern, which is implemented by extending the
PollingConsumerSupport class.
Example 47.6. PollingConsumerSupport Implementation
import org.apache.camel.Exchange;
import org.apache.camel.RuntimeCamelException;
import org.apache.camel.impl.PollingConsumerSupport;
1 public class CustomConsumer extends PollingConsumerSupport {
private final CustomEndpoint endpoint;
public CustomConsumer(CustomEndpoint endpoint) {
super(endpoint);
this.endpoint = endpoint;
2
}
public Exchange receiveNoWait() {
Exchange exchange = /* Obtain an exchange object. */;
// Further processing ...
return exchange;
3
}
public Exchange receive() {
// Blocking poll ...
4
}
public Exchange receive(long timeout) {
// Poll with timeout ...
5
}
protected void doStart() throws Exception {
// Code to execute whilst starting up.
6
}
protected void doStop() throws Exception {
// Code to execute whilst shutting down.
}
}
1
Implement your polling consumer class, CustomConsumer, by extending the
org.apache.camel.impl.PollingConsumerSupport class.
2
529
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
You must implement at least one constructor that takes a reference to the parent
endpoint, endpoint, as an argument. A polling consumer does not need a reference to
3
The receiveNoWait() method should implement a non-blocking algorithm for
retrieving an event (exchange object). If no event is available, it should return null.
4
The receive() method should implement a blocking algorithm for retrieving an event.
This method can block indefinitely, if events remain unavailable.
5
The receive(long timeout) method implements an algorithm that can block for as
long as the specified timeout (typically specified in units of milliseconds).
6
If you want to insert code that executes while a consumer is starting up or shutting
down, implement the doStart() method and the doStop() method, respectively.
Custom threading implementation
If the standard consumer patterns are not suitable for your consumer implementation, you
can implement the Consumer interface directly and write the threading code yourself. When
writing the threading code, however, it is important that you comply with the standard
Apache Camel threading model, as described in Section 2.9, “Threading Model”.
For example, the SEDA component from camel-core implements its own consumer
threading, which is consistent with the Apache Camel threading model. Example 47.7,
“Custom Threading Implementation” shows an outline of how the SedaConsumer class
implements its threading.
Example 47.7. Custom Threading Implementation
package org.apache.camel.component.seda;
import
import
import
import
import
java.util.ArrayList;
java.util.List;
java.util.concurrent.BlockingQueue;
java.util.concurrent.ExecutorService;
java.util.concurrent.TimeUnit;
import
import
import
import
import
import
import
import
...
import
import
org.apache.camel.Consumer;
org.apache.camel.Endpoint;
org.apache.camel.Exchange;
org.apache.camel.Processor;
org.apache.camel.ShutdownRunningTask;
org.apache.camel.impl.LoggingExceptionHandler;
org.apache.camel.impl.ServiceSupport;
org.apache.camel.util.ServiceHelper;
org.apache.commons.logging.Log;
org.apache.commons.logging.LogFactory;
/**
* A Consumer for the SEDA component.
*
* @version $Revision: 922485 $
*/
public class SedaConsumer extends ServiceSupport implements Consumer,
530
CHAPTER 47. CONSUMER INTERFACE
1 Runnable, ShutdownAware {
private static final transient Log LOG =
LogFactory.getLog(SedaConsumer.class);
private SedaEndpoint endpoint;
private Processor processor;
private ExecutorService executor;
...
public SedaConsumer(SedaEndpoint endpoint, Processor processor) {
this.endpoint = endpoint;
this.processor = processor;
}
...
public void run() {
BlockingQueue queue = endpoint.getQueue();
// Poll the queue and process exchanges
...
2
}
...
protected void doStart() throws Exception {
3
int poolSize = endpoint.getConcurrentConsumers();
executor =
endpoint.getCamelContext().getExecutorServiceStrategy()
.newFixedThreadPool(this, endpoint.getEndpointUri(),
poolSize);
4
for (int i = 0; i < poolSize; i++) {
5
executor.execute(this);
}
endpoint.onStarted(this);
}
protected void doStop() throws Exception {
endpoint.onStopped(this);
// must shutdown executor on stop to avoid overhead of having
them running
6
endpoint.getCamelContext().getExecutorServiceStrategy().shutdownNow(exec
7 utor);
executor = null;
if (multicast != null) {
ServiceHelper.stopServices(multicast);
}
}
...
//---------// Implementation of ShutdownAware interface
public boolean deferShutdown(ShutdownRunningTask
shutdownRunningTask) {
// deny stopping on shutdown as we want seda consumers to run
in case some other queues
// depend on this consumer to run, so it can complete its
exchanges
531
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
return true;
}
public int getPendingExchangesSize() {
// number of pending messages on the queue
return endpoint.getQueue().size();
}
}
1
The SedaConsumer class is implemented by extending the
org.apache.camel.impl.ServiceSupport class and implementing the Consumer,
Runnable, and ShutdownAware interfaces.
2
Implement the Runnable.run() method to define what the consumer does while it is
running in a thread. In this case, the consumer runs in a loop, polling the queue for
new exchanges and then processing the exchanges in the latter part of the queue.
3
The doStart() method is inherited from ServiceSupport. You override this method in
order to define what the consumer does when it starts up.
4
Instead of creating threads directly, you should create a thread pool using the
ExecutorServiceStrategy object that is registered with theCamelContext. This is
important, because it enables Apache Camel to implement centralized management of
threads and support such features as graceful shutdown.
For details, see Section 2.9, “Threading Model”.
5
Kick off the threads by calling the ExecutorService.execute() method poolSize
times.
6
The doStop() method is inherited from ServiceSupport. You override this method in
order to define what the consumer does when it shuts down.
7
Shut down the thread pool, which is represented by the executor instance.
532
CHAPTER 48. PRODUCER INTERFACE
CHAPTER 48. PRODUCER INTERFACE
Abstract
This chapter describes how to implement the Producer interface, which is an essential step
in the implementation of a Apache Camel component.
48.1. THE PRODUCER INTERFACE
Overview
An instance of org.apache.camel.Producer type represents a target endpoint in a route.
The role of the producer is to send requests (In messages) to a specific physical endpoint
and to receive the corresponding response (Out or Fault message). A Producer object is
essentially a special kind of Processor that appears at the end of a processor chain
(equivalent to a route). Figure 48.1, “Producer Inheritance Hierarchy” shows the
inheritance hierarchy for producers.
Figure 48.1. Producer Inheritance Hierarchy
The Producer interface
Example 48.1, “Producer Interface” shows the definition of the
org.apache.camel.Producer interface.
Example 48.1. Producer Interface
package org.apache.camel;
public interface Producer extends Processor, Service, IsSingleton {
Endpoint getEndpoint();
Exchange createExchange();
Exchange createExchange(ExchangePattern pattern);
Exchange createExchange(E exchange);
}
533
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
Producer methods
The Producer interface defines the following methods:
process() (inherited from Processor)—The most important method. A producer is
essentially a special type of processor that sends a request to an endpoint, instead
of forwarding the exchange object to another processor. By overriding the
process() method, you define how the producer sends and receives messages to
and from the relevant endpoint.
getEndpoint()—Returns a reference to the parent endpoint instance.
createExchange()—These overloaded methods are analogous to the corresponding
methods defined in the Endpoint interface. Normally, these methods delegate to
the corresponding methods defined on the parent Endpoint instance (this is what
the DefaultEndpoint class does by default). Occasionally, you might need to
override these methods.
Asynchronous processing
Processing an exchange object in a producer—which usually involves sending a message to
a remote destination and waiting for a reply—can potentially block for a significant length
of time. If you want to avoid blocking the current thread, you can opt to implement the
producer as an asynchronous processor. The asynchronous processing pattern decouples
the preceding processor from the producer, so that the process() method returns without
delay. See Section 44.1.4, “Asynchronous Processing”.
When implementing a producer, you can support the asynchronous processing model by
implementing the org.apache.camel.AsyncProcessor interface. On its own, this is not
enough to ensure that the asynchronous processing model will be used: it is also necessary
for the preceding processor in the chain to call the asynchronous version of the process()
method. The definition of the AsyncProcessor interface is shown in Example 48.2,
“AsyncProcessor Interface”.
Example 48.2. AsyncProcessor Interface
package org.apache.camel;
public interface AsyncProcessor extends Processor {
boolean process(Exchange exchange, AsyncCallback callback);
}
The asynchronous version of the process() method takes an extra argument,callback, of
org.apache.camel.AsyncCallback type. The corresponding AsyncCallback interface is
defined as shown in Example 48.3, “AsyncCallback Interface”.
Example 48.3. AsyncCallback Interface
package org.apache.camel;
public interface AsyncCallback {
534
CHAPTER 48. PRODUCER INTERFACE
void done(boolean doneSynchronously);
}
The caller of AsyncProcessor.process() must provide an implementation of
AsyncCallback to receive the notification that processing has finished. The
AsyncCallback.done() method takes a boolean argument that indicates whether the
processing was performed synchronously or not. Normally, the flag would be false, to
indicate asynchronous processing. In some cases, however, it can make sense for the
producer not to process asynchronously (in spite of being asked to do so). For example, if
the producer knows that the processing of the exchange will complete rapidly, it could
optimise the processing by doing it synchronously. In this case, the doneSynchronously
flag should be set to true.
ExchangeHelper class
When implementing a producer, you might find it helpful to call some of the methods in the
org.apache.camel.util.ExchangeHelper utility class. For full details of the
ExchangeHelper class, see Section 41.4, “The ExchangeHelper Class”.
48.2. IMPLEMENTING THE PRODUCER INTERFACE
Alternative ways of implementing a producer
You can implement a producer in one of the following ways:
How to implement a synchronous producer.
How to implement an asynchronous producer.
How to implement a synchronous producer
Example 48.4, “DefaultProducer Implementation” outlines how to implement a synchronous
producer. In this case, call to Producer.process() blocks until a reply is received.
Example 48.4. DefaultProducer Implementation
import
import
import
import
org.apache.camel.Endpoint;
org.apache.camel.Exchange;
org.apache.camel.Producer;
org.apache.camel.impl.DefaultProducer;
1 public class CustomProducer extends DefaultProducer {
public CustomProducer(Endpoint endpoint) {
super(endpoint);
// Perform other initialization tasks...
2
}
public void process(Exchange exchange) throws Exception {
// Process exchange synchronously.
// ...
3
}
}
535
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
1
Implement a custom synchronous producer class, CustomProducer, by extending the
org.apache.camel.impl.DefaultProducer class.
2
Implement a constructor that takes a reference to the parent endpoint.
3
The process() method implementation represents the core of the producer code. The
implementation of the process() method is entirely dependent on the type of
component that you are implementing. In outline, the process() method is normally
implemented as follows:
If the exchange contains an In message, and if this is consistent with the
specified exchange pattern, then send the In message to the designated
endpoint.
If the exchange pattern anticipates the receipt of an Out message, then wait
until the Out message has been received. This typically causes theprocess()
method to block for a significant length of time.
When a reply is received, call exchange.setOut() to attach the reply to the
exchange object. If the reply contains a fault message, set the fault flag on the
Out message using Message.setFault(true).
How to implement an asynchronous producer
Example 48.5, “CollectionProducer Implementation” outlines how to implement an
asynchronous producer. In this case, you must implement both a synchronous process()
method and an asynchronous process() method (which takes an additional AsyncCallback
argument).
Example 48.5. CollectionProducer Implementation
import
import
import
import
import
import
org.apache.camel.AsyncCallback;
org.apache.camel.AsyncProcessor;
org.apache.camel.Endpoint;
org.apache.camel.Exchange;
org.apache.camel.Producer;
org.apache.camel.impl.DefaultProducer;
public class CustomProducer extends DefaultProducer implements
1 AsyncProcessor {
public CustomProducer(Endpoint endpoint) {
super(endpoint);
// ...
2
}
public void process(Exchange exchange) throws Exception {
// Process exchange synchronously.
// ...
3
}
public boolean process(Exchange exchange, AsyncCallback callback) {
536
CHAPTER 48. PRODUCER INTERFACE
// Process exchange asynchronously.
CustomProducerTask task = new CustomProducerTask(exchange,
4
callback);
// Process 'task' in a separate thread...
// ...
return false;
5
}
}
6 public class CustomProducerTask implements Runnable {
private Exchange exchange;
private AsyncCallback callback;
public CustomProducerTask(Exchange exchange, AsyncCallback
callback) {
this.exchange = exchange;
this.callback = callback;
}
public void run() {
// Process exchange.
// ...
callback.done(false);
7
}
}
1
Implement a custom asynchronous producer class, CustomProducer, by extending the
org.apache.camel.impl.DefaultProducer class, and implementing the
AsyncProcessor interface.
2
Implement a constructor that takes a reference to the parent endpoint.
3
Implement the synchronous process() method.
4
Implement the asynchronous process() method. You can implement the asynchronous
method in several ways. The approach shown here is to create a java.lang.Runnable
instance, task, that represents the code that runs in a sub-thread. You then use the
Java threading API to run the task in a sub-thread (for example, by creating a new
thread or by allocating the task to an existing thread pool).
5
Normally, you return false from the asynchronous process() method, to indicate that
the exchange was processed asynchronously.
6
The CustomProducerTask class encapsulates the processing code that runs in a subthread. This class must store a copy of the Exchange object, exchange, and the
AsyncCallback object, callback, as private member variables.
7
The run() method contains the code that sends theIn message to the producer
endpoint and waits to receive the reply, if any. After receiving the reply (Out message
or Fault message) and inserting it into the exchange object, you must call
callback.done() to notify the caller that processing is complete.
537
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
CHAPTER 49. EXCHANGE INTERFACE
Abstract
This chapter describes the Exchange interface. Since the refactoring of the camel-core
module performed in Apache Camel 2.0, there is no longer any necessity to define custom
exchange types. The DefaultExchange implementation can now be used in all cases.
49.1. THE EXCHANGE INTERFACE
Overview
An instance of org.apache.camel.Exchange type encapsulates the current message
passing through a route, with additional metadata encoded as exchange properties.
Figure 49.1, “Exchange Inheritance Hierarchy” shows the inheritance hierarchy for the
exchange type. The default implementation, DefaultExchange, is always used.
Figure 49.1. Exchange Inheritance Hierarchy
The Exchange interface
Example 49.1, “Exchange Interface” shows the definition of the
org.apache.camel.Exchange interface.
Example 49.1. Exchange Interface
package org.apache.camel;
import java.util.Map;
import org.apache.camel.spi.Synchronization;
import org.apache.camel.spi.UnitOfWork;
public interface Exchange {
// Exchange property names (string constants)
// (Not shown here)
...
ExchangePattern getPattern();
void setPattern(ExchangePattern pattern);
538
CHAPTER 49. EXCHANGE INTERFACE
Object getProperty(String name);
Object getProperty(String name, Object defaultValue);
T getProperty(String name, Class type);
T getProperty(String name, Object defaultValue, Class
type);
void
setProperty(String name, Object value);
Object removeProperty(String name);
Map getProperties();
boolean hasProperties();
Message getIn();
T
getIn(Class type);
void
setIn(Message in);
Message
T
void
boolean
getOut();
getOut(Class type);
setOut(Message out);
hasOut();
Throwable getException();
T
getException(Class type);
void
setException(Throwable e);
boolean isFailed();
boolean isTransacted();
boolean isRollbackOnly();
CamelContext getContext();
Exchange copy();
Endpoint getFromEndpoint();
void
setFromEndpoint(Endpoint fromEndpoint);
String getFromRouteId();
void
setFromRouteId(String fromRouteId);
UnitOfWork getUnitOfWork();
void setUnitOfWork(UnitOfWork unitOfWork);
String getExchangeId();
void setExchangeId(String id);
void addOnCompletion(Synchronization onCompletion);
void handoverCompletions(Exchange target);
}
Exchange methods
The Exchange interface defines the following methods:
539
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
getPattern(), setPattern()—The exchange pattern can be one of the values
enumerated in org.apache.camel.ExchangePattern. The following exchange
pattern values are supported:
InOnly
RobustInOnly
InOut
InOptionalOut
OutOnly
RobustOutOnly
OutIn
OutOptionalIn
setProperty(), getProperty(), getProperties(), removeProperty(),
hasProperties()—Use the property setter and getter methods to associate named
properties with the exchange instance. The properties consist of miscellaneous
metadata that you might need for your component implementation.
setIn(), getIn()—Setter and getter methods for the In message.
The getIn() implementation provided by theDefaultExchange class implements
lazy creation semantics: if the In message is null when getIn() is called, the
DefaultExchange class creates a default In message.
setOut(), getOut(), hasOut()—Setter and getter methods for the Out message.
The getOut() method implicitly supports lazy creation of anOut message. That is, if
the current Out message is null, a new message instance is automatically created.
setException(), getException()—Getter and setter methods for an exception
object (of Throwable type).
isFailed()—Returns true, if the exchange failed either due to an exception or due
to a fault.
isTransacted()—Returns true, if the exchange is transacted.
isRollback()—Returns true, if the exchange is marked for rollback.
getContext()—Returns a reference to the associated CamelContext instance.
copy()—Creates a new, identical (apart from the exchange ID) copy of the current
custom exchange object. The body and headers of the In message, the Out message
(if any), and the Fault message (if any) are also copied by this operation.
setFromEndpoint(), getFromEndpoint()—Getter and setter methods for the
consumer endpoint that orginated this message (which is typically the endpoint
appearing in the from() DSL command at the start of a route).
540
CHAPTER 49. EXCHANGE INTERFACE
setFromRouteId(), getFromRouteId()—Getters and setters for the route ID that
originated this exchange. The getFromRouteId() method should only be called
internally.
setUnitOfWork(), getUnitOfWork()—Getter and setter methods for the
org.apache.camel.spi.UnitOfWork bean property. This property is only required
for exchanges that can participate in a transaction.
setExchangeId(), getExchangeId()—Getter and setter methods for the exchange
ID. Whether or not a custom component uses and exchange ID is an implementation
detail.
addOnCompletion()—Adds an org.apache.camel.spi.Synchronization callback
object, which gets called when processing of the exchange has completed.
handoverCompletions()—Hands over all of the OnCompletion callback objects to
the specified exchange object.
541
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
CHAPTER 50. MESSAGE INTERFACE
Abstract
This chapter describes how to implement the Message interface, which is an optional step in
the implementation of a Apache Camel component.
50.1. THE MESSAGE INTERFACE
Overview
An instance of org.apache.camel.Message type can represent any kind of message (In or
Out). Figure 50.1, “Message Inheritance Hierarchy” shows the inheritance hierarchy for the
message type. You do not always need to implement a custom message type for a
component. In many cases, the default implementation, DefaultMessage, is adequate.
Figure 50.1. Message Inheritance Hierarchy
The Message interface
Example 50.1, “Message Interface” shows the definition of the org.apache.camel.Message
interface.
Example 50.1. Message Interface
package org.apache.camel;
import java.util.Map;
import java.util.Set;
import javax.activation.DataHandler;
public interface Message {
String getMessageId();
void setMessageId(String messageId);
Exchange getExchange();
boolean isFault();
542
CHAPTER 50. MESSAGE INTERFACE
void
setFault(boolean fault);
Object getHeader(String name);
Object getHeader(String name, Object defaultValue);
T getHeader(String name, Class type);
T getHeader(String name, Object defaultValue, Class type);
Map getHeaders();
void setHeader(String name, Object value);
void setHeaders(Map headers);
Object removeHeader(String name);
boolean removeHeaders(String pattern);
boolean hasHeaders();
Object getBody();
Object getMandatoryBody() throws InvalidPayloadException;
T getBody(Class type);
T getMandatoryBody(Class type) throws
InvalidPayloadException;
void
setBody(Object body);
void setBody(Object body, Class type);
DataHandler getAttachment(String id);
Map getAttachments();
Set getAttachmentNames();
void removeAttachment(String id);
void addAttachment(String id, DataHandler content);
void setAttachments(Map attachments);
boolean hasAttachments();
Message copy();
void copyFrom(Message message);
String createExchangeId();
}
Message methods
The Message interface defines the following methods:
setMessageId(), getMessageId()—Getter and setter methods for the message ID.
Whether or not you need to use a message ID in your custom component is an
implementation detail.
getExchange()—Returns a reference to the parent exchange object.
isFault(), setFault()—Getter and setter methods for the fault flag, which
indicates whether or not this message is a fault message.
getHeader(), getHeaders(), setHeader(), setHeaders(), removeHeader(),
hasHeaders()—Getter and setter methods for the message headers. In general,
these message headers can be used either to store actual header data, or to store
miscellaneous metadata.
543
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
getBody(), getMandatoryBody(), setBody()—Getter and setter methods for the
message body. The getMandatoryBody() accessor guarantees that the returned
body is non-null, otherwise the InvalidPayloadException exception is thrown.
getAttachment(), getAttachments(), getAttachmentNames(),
removeAttachment(), addAttachment(), setAttachments(), hasAttachments()—
Methods to get, set, add, and remove attachments.
copy()—Creates a new, identical (including the message ID) copy of the current
custom message object.
copyFrom()—Copies the complete contents (including the message ID) of the
specified generic message object, message, into the current message instance.
Because this method must be able to copy from any message type, it copies the
generic message properties, but not the custom properties.
createExchangeId()—Returns the unique ID for this exchange, if the message
implementation is capable of providing an ID; otherwise, return null.
50.2. IMPLEMENTING THE MESSAGE INTERFACE
How to implement a custom message
Example 50.2, “Custom Message Implementation” outlines how to implement a message by
extending the DefaultMessage class.
Example 50.2. Custom Message Implementation
import org.apache.camel.Exchange;
import org.apache.camel.impl.DefaultMessage;
1 public class CustomMessage extends DefaultMessage {
public CustomMessage() {
// Create message with default properties...
2
}
544
3
@Override
public String toString() {
// Return a stringified message...
}
4
@Override
public CustomMessage newInstance() {
return new CustomMessage( ... );
}
5
@Override
protected Object createBody() {
// Return message body (lazy creation).
}
6
@Override
protected void populateInitialHeaders(Map map) {
// Initialize headers from underlying message (lazy
INDEX
creation).
}
@Override
protected void populateInitialAttachments(Map
7 map) {
// Initialize attachments from underlying message (lazy
creation).
}
}
1
Implements a custom message class, CustomMessage, by extending the
org.apache.camel.impl.DefaultMessage class.
2
Typically, you need a default constructor that creates a message with default
properties.
3
Override the toString() method to customize message stringification.
4
The newInstance() method is called from inside the MessageSupport.copy() method.
Customization of the newInstance() method should focus on copying all of thecustom
properties of the current message instance into the new message instance. The
MessageSupport.copy() method copies the generic message properties by calling
copyFrom().
5
The createBody() method works in conjunction with theMessageSupport.getBody()
method to implement lazy access to the message body. By default, the message body
is null. It is only when the application code tries to access the body (by calling
getBody()), that the body should be created. TheMessageSupport.getBody()
automatically calls createBody(), when the message body is accessed for the first
time.
6
The populateInitialHeaders() method works in conjunction with the header getter
and setter methods to implement lazy access to the message headers. This method
parses the message to extract any message headers and inserts them into the hash
map, map. The populateInitialHeaders() method is automatically called when a user
attempts to access a header (or headers) for the first time (by calling getHeader(),
getHeaders(), setHeader(), or setHeaders()).
7
The populateInitialAttachments() method works in conjunction with the
attachment getter and setter methods to implement lazy access to the attachments.
This method extracts the message attachments and inserts them into the hash map,
map. The populateInitialAttachments() method is automatically called when a user
attempts to access an attachment (or attachments) for the first time by calling
getAttachment(), getAttachments(), getAttachmentNames(), or addAttachment().
INDEX
Symbols
@Converter, Implement an annotated converter class
A
545
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
AsyncCallback, Asynchronous processing
asynchronous producer
implementing, How to implement an asynchronous producer
AsyncProcessor, Asynchronous processing
auto-discovery
configuration, Configuring auto-discovery
C
Component
createEndpoint(), URI parsing
definition, The Component interface
methods, Component methods
component prefix, Component
components, Component
bean properties, Define bean properties on your component class
configuring, Installing and configuring the component
implementation steps, Implementation steps
installing, Installing and configuring the component
interfaces to implement, Which interfaces do you need to implement?
parameter injection, Parameter injection
Spring configuration, Configure the component in Spring
Consumer, Consumer
consumers, Consumer
event-driven, Event-driven pattern, Implementation steps
polling, Polling pattern, Implementation steps
scheduled, Scheduled poll pattern, Implementation steps
threading, Overview
D
DefaultComponent
createEndpoint(), URI parsing
DefaultEndpoint, Event-driven endpoint implementation
createExchange(), Event-driven endpoint implementation
546
INDEX
createPollingConsumer(), Event-driven endpoint implementation
getCamelConext(), Event-driven endpoint implementation
getComponent(), Event-driven endpoint implementation
getEndpointUri(), Event-driven endpoint implementation
E
Endpoint, Endpoint
createConsumer(), Endpoint methods
createExchange(), Endpoint methods
createPollingConsumer(), Endpoint methods
createProducer(), Endpoint methods
getCamelContext(), Endpoint methods
getEndpointURI(), Endpoint methods
interface definition, The Endpoint interface
isLenientProperties(), Endpoint methods
isSingleton(), Endpoint methods
setCamelContext(), Endpoint methods
endpoint
event-driven, Event-driven endpoint implementation
scheduled, Scheduled poll endpoint implementation
endpoints, Endpoint
Exchange, Exchange, The Exchange interface
copy(), Exchange methods
getExchangeId(), Exchange methods
getIn(), Accessing message headers , Exchange methods
getOut(), Exchange methods
getPattern(), Exchange methods
getProperties(), Exchange methods
getProperty(), Exchange methods
getUnitOfWork(), Exchange methods
removeProperty(), Exchange methods
setExchangeId(), Exchange methods
setIn(), Exchange methods
547
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
setOut(), Exchange methods
setProperty(), Exchange methods
setUnitOfWork(), Exchange methods
exchange
in capable, Testing the exchange pattern
out capable, Testing the exchange pattern
exchange properties
accessing, Wrapping the exchange accessors
ExchangeHelper, The ExchangeHelper Class
getContentType(), Get the In message's MIME content type
getMandatoryHeader(), Accessing message headers , Wrapping the exchange
accessors
getMandatoryInBody(), Wrapping the exchange accessors
getMandatoryOutBody(), Wrapping the exchange accessors
getMandatoryProperty(), Wrapping the exchange accessors
isInCapable(), Testing the exchange pattern
isOutCapable(), Testing the exchange pattern
resolveEndpoint(), Resolve an endpoint
exchanges, Exchange
I
in message
MIME type, Get the In message's MIME content type
M
Message, Message
getHeader(), Accessing message headers
message headers
accessing, Accessing message headers
messages, Message
P
performer, Overview
548
INDEX
pipeline, Pipelining model
Processor, Processor interface
implementing, Implementing the Processor interface
producer, Producer
Producer, Producer
createExchange(), Producer methods
getEndpoint(), Producer methods
process(), Producer methods
producers
asynchronous, Asynchronous producer
synchronous, Synchronous producer
S
ScheduledPollEndpoint, Scheduled poll endpoint implementation
simple processor
implementing, Implementing the Processor interface
synchronous producer
implementing, How to implement a synchronous producer
T
type conversion
runtime process, Type conversion process
type converter
annotating the implementation, Implement an annotated converter class
discovery file, Create a TypeConverter file
implementation steps, How to implement a type converter
mater, Master type converter
packaging, Package the type converter
slave, Master type converter
TypeConverter, Type converter interface
TypeConverterLoader, Type converter loader
U
549
Red Hat JBoss Fuse 6.1 Apache Camel Development Guide
useIntrospectionOnEndpoint(), Disabling endpoint parameter injection
W
wire tap pattern, System Management
550
Source Exif Data:
File Type : PDF File Type Extension : pdf MIME Type : application/pdf PDF Version : 1.4 Linearized : No Title : Red Hat JBoss Fuse 6.1 Apache Camel Development Guide Creator : wkhtmltopdf 0.12.2.1 Producer : Qt 4.8.6 Create Date : 2017:10:12 08:47:55-04:00 Page Count : 554 Page Mode : UseOutlinesEXIF Metadata provided by EXIF.tools