...
DSL
Camel uses a Java Domain Specific Language or DSL for creating Enterprise Integration Patterns or Routes. Camel also supports a Spring based XML configuration as well as a Scala DSL.The benefits of using the Java DSL is that your IDE can smart complete your code as you start typing, rather than having to mess around with buckets of XML. The Java DSL is also very expressive as you can mix and match your own code within the language for Expression or Predicate evaluations or easily add a custom Processor in a variety of domain-specific languages (DSL) as listed below.
- Java DSL - A Java based DSL using the fluent builder style.
- Spring XML - A XML based DSL in Spring XML files
- Blueprint XML - A XML based DSL in OSGi Blueprint XML files
- Rest DSL - A DSL to define REST services using a REST style in either Java or XML.
- Groovy DSL - A Groovy based DSL using Groovy programming language
- Scala DSL - A Scala based DSL using Scala programming language
- Annotation DSL - Use annotations in Java beans.
- Kotlin DSL - Work in progress - Currently developed outside ASF, but will we included later in Camel when Kotlin and the DSL is ready.
The main entry points for the DSL are
- CamelContext for creating a Camel routing rulebase
- RouteBuilder for creating a collection of routes using the routing DSL
Handling errors
You can handle errors in a number of ways such as:
- by specifying an Error Handler
- using the Exception Clause via the exception() method
Camel uses a strategy to resolve how exceptions should be handled.
Using try ... catch ... finally
Using Interceptors
You can register interceptors on a RouteBuilder so that they are inherited by all child routes. You can also use the DSL itself to write interceptors...
Code Block |
---|
// lets log all steps in all routes
intercept().to("log:foo").proceed();
from("seda:foo").to("seda:bar");
|
You can also add a predicate to the intercept() method so that your interceptor will only be invoked if the predicate is true.
Code Block |
---|
// lets log messages from gold customers
intercept(xpath("/customer[@type='gold']").to("log:customer").proceed();
from("seda:foo").to("seda:bar");
|
Changes in Camel 1.4
In Camel 1.4 proceed()
is now default, so the examples above can be written as:
Code Block |
---|
// lets log all steps in all routes
intercept().to("log:foo");
from("seda:foo").to("seda:bar");
|
Proceed means that Camel will continue the normal route path after the interception. All the examples shown is logging examples where the routing is supposed to continue by its normal path. Camel is so powerful that you can also use interceptors to alter the route path and change the cause of action.
The example below will intercept all exchanges that has an istest flag set to true and move the exchange to a test queue. We append the stop()
to indicate that the interceptor should not proceed - thus any istest message is intercepted and does not route in the normal route path, but is moved to the test queue.
...
See Also
For more examples of the DSL in action see
...