Chapter 5. XML IO DSL
The xml-io-dsl
is the Camel optimized XML DSL with a very fast and low overhead XML parser. It is a source code generated parser that is Camel specific and can only parse Camel .xml
route files (not classic Spring <beans>
XML files).
We recommend that you use xml-io-dsl
instead of xml-jaxb-dsl
for Camel XML DSL. It works with all Camel runtimes.
When you are using XML IO DSL, the camel-spring-boot
application will by default look for xml files in src/main/resources/camel/*.xml
.
You can configure this behavior by providing a different path in the camel.springboot.routes-include-pattern
property:
camel.springboot.routes-include-pattern=/path/to/*.xml
5.1. Example Copy linkLink copied to clipboard!
The following my-route.xml
source file can be loaded and run with Camel CLI or Camel K:
my-route.xml
You can omit the xmlns
namespace.
If there is only a single route, you can use <route>
as the root XML tag instead of <routes>
.
Running with Camel K
kamel run my-route.xml
kamel run my-route.xml
Running with Camel CLI
camel run my-route.xml
camel run my-route.xml
You can use xml-io-dsl
to declare some beans to be bound to the Camel Registry.
You can declare and Beans define their properties (including nested
properties) in XML. For example:
Bean declaration and definition
While keeping all the benefits of fast XML parser used by xml-io-dsl
, Camel can also process XML elements declared in other XML namespaces and process them separately. With this mechanism it is possible to include XML elements using Spring’s http://www.springframework.org/schema/beans
namespace.
This brings the flexibility of Spring Beans into Camel main without actually running any Spring Application Context (or Spring Boot).
When elements from Spring namespace are found, they are used to populate and configure an instance of org.springframework.beans.factory.support.DefaultListableBeanFactory
and leverage Spring dependency injection to wire the beans together.
These beans are then exposed through normal Camel Registry and may be used by Camel routes.
Here’s an example camel.xml
file, which defines both the routes and beans used (referred to) by the route definition:
camel.xml
A my-route
route is referring to greeter
bean which is defined using Spring <bean>
element.
More examples can be found on the Apache Camel JBang page.
5.2. Using beans with constructors Copy linkLink copied to clipboard!
When you want to create beans with constructor arguments, from Camel 4.1 onwards you can add them as XML tags. For example:
Camel 4.1+: Beans with constructor
tags
If you use Camel 4.0, you must put then constructor arguments in the type
attribute:
Camel 4.0: Beans with constructor
arguments in the type
attribute
5.3. Creating beans from factory method Copy linkLink copied to clipboard!
A bean can also be created from a public static
factory method:
Factory method XML
When you use a factoryMethod
, you must provide constructor
tags for the arguments.
For example, this means that the class com.acme.MyBean
should be as follows:
Factory method
You must make the factory method public static
in the created class.
5.4. Creating beans from builder classes Copy linkLink copied to clipboard!
You can create a bean created from another builder class as shown below:
Builder XML
You must make the builder class public
with a no-arg default constructor.
You can then use the builder class to create the actual bean by using fluent builder style configuration.
Set the properties on the builder class, and create the bean by invoking the builderMethod
at the end.
You invocate this method via Java reflection.
5.5. Creating beans from factory bean Copy linkLink copied to clipboard!
You can create a bean from a factory bean as shown below:
Factory XML
You can also use factoryBean
to refer to an existing bean by bean id instead of the FQN classname.
When you use a factoryBean
the, you must provide arguments as constructor
tags.
For example, the class com.acme.MyHelper
should be as follows:
Factory bean
You must make the factory method public static
.
5.6. Creating beans using script language Copy linkLink copied to clipboard!
If you have advanced use-cases, you can inline a script language, such as groovy, java, javascript, and so on, to create the bean.
With scripting, you can be more flexible and use a bit of programming to create and configure the bean:
Scripting
When you use script
, the constructors, factory bean, and factory method are not used.
5.7. Using init and destroy methods on beans Copy linkLink copied to clipboard!
If you need to do initialization and cleanup work before you use a bean, you can use the initMethod
and destroyMethod
which are triggered as appropriate by Camel.
Those methods must be public void
and have no arguments, as shown below:
Initialization and cleanup methods
You also have to declare those methods in the XML DSL as follows:
Initialization and cleanup XML
Both initMethod
and destroyMethod
are optional, so a bean does not have to have both.
5.8. REST and routes in the same XML IO DSL file Copy linkLink copied to clipboard!
You can have both REST and routes in the same DSL file:
REST and routes in the same XML IO DSL file