Chapter 129. Smooks component
Since Camel 4.7
Only producer is supported
The Camel Smooks component uses Smooks to break up the structured data (EDI, CSV, POJO, and so on) of a Camel message body into fragments. These fragments can be processed independently of one another from within Smooks.
Common applications of Smooks include:
- transformation: EDI to CSV, POJO to EDI, POJO to XML, and so on.
- scalable processing: process huge payloads while keeping a small memory footprint.
- split, transform, and route fragments to destinations such as JMS queues, file systems, and databases.
- enrichment: enrich fragments with data from a database or other data sources.
- Java binding: populate POJOs from a source such as CSV, EDI, XML, and other POJOs.
Use the Smooks Data Format instead of this component when you are primarily interested in transformation and binding. This should be used for other Smooks features, like routing.
129.1. Dependencies Copy linkLink copied to clipboard!
When using camel-smooks with Red Hat build of Camel Spring Boot, add the following Maven dependency to your pom.xml to have support for auto configuration:
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-smooks-starter</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-smooks-starter</artifactId>
</dependency>
129.2. URI Format Copy linkLink copied to clipboard!
smooks://smooks-config-path[?options]
smooks://smooks-config-path[?options]
129.3. Configuring Options Copy linkLink copied to clipboard!
Camel components are configured on two separate levels:
- component level
- endpoint level
129.3.1. Configuring component options Copy linkLink copied to clipboard!
The component level is the highest level which holds general and common configurations that are inherited by the endpoints. For example a component may have security settings, credentials for authentication, urls for network connection and so forth.
Some components only have a few options, and others may have many. Because components typically have pre configured defaults that are commonly used, then you may often only need to configure a few options on a component; or none at all.
Configuring components can be done with the Component DSL, in a configuration file (application.properties|yaml), or directly with Java code.
129.3.2. Configuring endpoint options Copy linkLink copied to clipboard!
Where you find yourself configuring the most is on endpoints, as endpoints often have many options, which allows you to configure what you need the endpoint to do. The options are also categorized into whether the endpoint is used as consumer (from) or as a producer (to), or used for both.
Configuring endpoints is most often done directly in the endpoint URI as path and query parameters. You can also use the Endpoint DSL and DataFormat DSL as a type safe way of configuring endpoints and data formats in Java.
A good practice when configuring options is to use Property Placeholders, which allows to not hardcode urls, port numbers, sensitive information, and other settings. In other words placeholders allows to externalize the configuration from your code, and gives more flexibility and reuse.
The following two sections lists all the options, firstly for the component followed by the endpoint.
129.4. Component Options Copy linkLink copied to clipboard!
The Smooks component supports 3 options, which are listed below.
| Name | Description | Default | Type |
|---|---|---|---|
| lazyStartProducer (producer) | Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel’s routing error handlers. Beware that when the first message is processed then creating and starting the producer may take a little time and prolong the total processing time of the processing. | false | boolean |
| autowiredEnabled (advanced) | Whether autowiring is enabled. This is used for automatic autowiring options (the option must be marked as autowired) by looking up in the registry to find if there is a single instance of matching type, which then gets configured on the component. This can be used for automatic configuring JDBC data sources, JMS connection factories, AWS Clients, etc. | true | boolean |
| smooksFactory (advanced) | Autowired To use a custom factory for creating Smooks. | SmooksFactory |
129.5. Endpoint Options Copy linkLink copied to clipboard!
The Smooks endpoint is configured using URI syntax:
smooks:smooksConfig
smooks:smooksConfig
With the following path and query parameters:
129.5.1. Path Parameters (1 parameters) Copy linkLink copied to clipboard!
| Name | Description | Default | Type |
|---|---|---|---|
| smooksConfig (producer) | Required Path to the Smooks configuration file. | String |
129.5.2. Query Parameters (3 parameters) Copy linkLink copied to clipboard!
| Name | Description | Default | Type |
|---|---|---|---|
| reportPath (producer) | File path to place the generated HTML execution report. The report is a useful tool in the developers arsenal for diagnosing issues or comprehending a transformation. Do not set in production since this is a major performance drain. | String | |
| lazyStartProducer (producer (advanced)) | Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel’s routing error handlers. Beware that when the first message is processed then creating and starting the producer may take a little time and prolong the total processing time of the processing. | false | boolean |
| allowExecutionContextFromHeader (advanced) | Allow execution context to be set from the CamelSmooksExecutionContext header. | false | Boolean |
129.6. Message Headers Copy linkLink copied to clipboard!
The Smooks component supports 1 message header(s), which is/are listed below:
| Name | Description | Default | Type |
|---|---|---|---|
| CamelSmooksExecutionContext (advanced)
Constant: | The Smooks execution context. | ExecutionContext |
129.7. Usage Copy linkLink copied to clipboard!
Using the Smooks component lets you leverage all the features of Smooks, such as transformation and fragment-driven routing, from within Camel. You can take an existing Smooks configuration and reference it from your Camel routes as shown below:
Java
from("file:inputDir?noop=true")
.to("smooks:smooks-config.xml")
.to("jms:queue:order");
from("file:inputDir?noop=true")
.to("smooks:smooks-config.xml")
.to("jms:queue:order");
YAML
- from:
uri: file:inputDir?noop=true
steps:
- to: smooks:smooks-config.xml
- to: jms:queue:order
- from:
uri: file:inputDir?noop=true
steps:
- to: smooks:smooks-config.xml
- to: jms:queue:order
The Smooks component is configured with a mandatory configuration file, which is smooks-config.xml in the example above. It is not clear what type of output the component is producing from looking at the above route. By default, the message body output is a stream but the type of output can be changed by configuring {https://www.smooks.org/xsd/smooks/smooks-core-1.6.xsd}exports in the Smooks configuration as shown next:
smooks-config.xml
The {https://www.smooks.org/xsd/smooks/smooks-core-1.6.xsd}exports element in this example configures Smooks to export the execution result to Camel as a string. Keep in mind that exporting the result as string means that the whole result will be kept in-memory which could cause unexpected performance issues for large payloads.
129.7.1. Bean routing Copy linkLink copied to clipboard!
Smooks is capable of routing fragments to Camel endpoints using the {https://www.smooks.org/xsd/smooks/camel-1.5.xsd}route element from the Smooks configuration. As an example, you can route to an endpoint by declaring the following in your Smooks configuration:
smooks-config.xml
The file above configures Smooks to route the Java bean orderItem in the bean context to the endpoints direct:slow and direct:express, depending on whether the priority field of the orderItem instance is equal to Normal or High. It is possible to route based on an event selector rather than a condition thanks to the routeOnElement XML attribute:
smooks-config.xml
Instead of routing complex objects to Camel, a pipeline allows you to have a template (e.g., FreeMarker) reference the beans and then route the evaluated template as string (e.g., XML, CSV, etc.) to Camel.