Writing Integrations in YAML
|
The YAML DSL is current in preview support level. |
Defining a route
A route is a sequence of elements, or steps, defined as follow:
from: (1)
uri: "direct:start"
steps: (2)
- filter:
expression:
simple: "${in.header.continue} == true"
steps: (2)
- to:
uri: "log:filtered"
- to:
uri: "log:original"
| 1 | route entry point, by default from and rest are supported |
| 2 | processing steps |
|
Each step is represented by YAML map that has a single entry where the field name is the EIP name |
As general rule each step provide all the parameters the related definition declares but there are some minor differences/enhancements:
-
Output Aware Steps
Some steps such as
filterandsplithave their own pipeline when an exchange matches the filter expression or for the items generated by the split expression, such pipeline can be defined by thestepsfield:filter: expression: simple: "${in.header.continue} == true" steps: - to: uri: "log:filtered"if the
stepsfield is omitted, then each subsequent step is considered part of the filter pipeline. -
Expression Aware Steps
Some EIP such as
filterandsplitsupports the definition of an expression through theexpressionfield:Explicit Expression fieldfilter: expression: simple: "${in.header.continue} == true"To make the DSL less verbose, the
expressionfield can be omitted:Implicit Expression fieldfilter: simple: "${in.header.continue} == true"In general expression can be defined inline like in the examples above but in case you need provide more information, you can 'unroll' the expression definition and configure any single parameter the expression defines.
Full Expression definitionfilter: tokenize: token: "<" end-token: ">" -
Data Format Aware Steps
Some EIP such as
set-bodyandmarshalsupports the definition of data formats through thedata-formatfield:Explicit Data Format fieldset-body: data-format: json: library: GsonTo make the DSL less verbose, the
data-formatfield can be omitted:Implicit Data Format fieldset-body: json: library: GsonIn case you want to use the data-format’s default settings, you need to place an empty block as data format parameters, like
json: {}
Extending the DSL
The DSL is designed to be easily extended so you can provide your own step handler which is discovered at runtime using Camel’s factory finder.
Assuming you want to create a step to simplify the creation of a certain type of endpoints then you need:
-
create a service definition entry in
META-INF/services/org/apache/camel/k/yamlwith content like:class=com.acme.converter.MyConverter -
create the step handler extending
org.apache.camel.k.loader.yaml.parser.ProcessorStepParserpackage com.acme.converter.AcmeConverter import org.apache.camel.k.loader.yaml.parser.ProcessorStepParser; public class AcmeConverter implements ProcessorStepParser { /** * @param context contains a references to the camel context and the current node as raw JsonNode */ @Override public ProcessorDefinition<?> toProcessor(Context context) { // decode the raw json node Definition definition = context.node(Definition.class); // create the definition ToDefinition to = new ToDefinition() to.setUri(String.format("http://%s:%d/fixed/path"), definition.host, definition.port) return to; } /* * Define the data */ public static final class Definition { public String host; @JsonSetter(nulls = Nulls.SKIP) public Integer port = 8080; } }
Assuming the entry in the META-INF/services/org/apache/camel/k/yaml is named acme then you can use it from the YAML DSL like:
from:
uri: "direct:start"
steps:
- acme:
host: acme.com
port: 8081
Supported EIP
-
Aggregate
-
Bean
-
Choice
-
Circuit Breaker
-
Claim Check
-
Convert Body To
-
Delay
-
Dynamic Router
-
Enrich
-
Filter
-
From
-
Idempotent Consumer
-
Load Balance
-
Log
-
Loop
-
Marshal
-
Multicast
-
Pipeline
-
PollEnrich
-
Process
-
Recipient List
-
Remove Header
-
Remove Headers
-
Remove Property
-
Remove Properties
-
Resequence
-
Rest DSL
-
Rollback
-
Routing Slip
-
Saga
-
Sample
-
Script
-
ServiceCall
-
Set Body
-
Set Exchange Pattern
-
Set Header
-
Set Property
-
Sort
-
Split
-
Step
-
Stop
-
Threads
-
Throttle
-
Throw Exception
-
To
-
To Dynamic
-
Transacted
-
Transform
-
Try Catch Finally
-
Unmarshal
-
Validate
-
Wire Tap
The Try Catch Finally EIP currently only support specifying one do-catch clause.