Red Hat Training

A Red Hat training course is available for JBoss Enterprise SOA Platform

Chapter 15. Apache Camel Integration

15.1. Apache Camel-Smooks Integration

The Camel-Smooks integration lets you to access all of Smooks' capabilities from within Apache Camel. You can take an existing Smooks configuration and use this in your Camel routes.

15.2. Methods of Using Smooks in Apache Camel

There are three ways in which you can use Smooks in Apache Camel:
  • SmooksComponent
  • SmooksDataformat
  • SmooksProcessor

15.3. SmooksComponent

The SmooksComponent is a Camel module which you can use when you want to process the Camel message body using Smooks.

15.4. Using the SmooksComponent

  1. Add a route to your Camel route configuration as shown below:
    from("file://inputDir?noop=true")
    .to("smooks://smooks-config.xml")
    .to("jms:queue:order")
    
  2. Edit the values in the 'smooks-config-xml' file. You cannot tell what type of output the SmooksComponent is producing just by looking at the route configuration. Instead, this is expressed in the Smooks configuration via the exports element.

    Note

    If you would prefer to configure Smooks programmatically, you can set this using the SmooksProcessor

15.5. SmooksComponent Configuration

An Apache component can use any options that are specified after the Smooks configuration file. Currently only one option is available for the SmooksComponent:
  • reportPath: this is the path (including the file name) to the Smooks Execution Report that is to be generated.

15.6. SmooksDataFormat

SmooksDataFormat is an Apache Camel DataFormat. It is capable of transforming one data format to another and back again. Use this when you are only interested in transforming from one format to another and do not need to use any other Smooks features.

15.7. SmooksDataFormat Example

This example code demonstrates how to use SmooksDataFormat to transform a comma-separated value string into a java.util.List of Customer object instances:
SmooksDataFormat sdf = new SmooksDataFormat("csv-smooks-unmarshal-config.xml");
from("direct:unmarshal")
.unmarshal(sdf)
.convertBodyTo(List.class)
.to("mock:result");

15.8. SmooksProcessor

Using the SmooksProcessor gives you full control over Smooks. (If, for example, you want to programmatically create the underlying Smooks instance you would use this component.) When you are using the SmooksProcessor, you can programmatically configure Smooks and then pass a Smooks instance to its constructor.

15.9. SmooksProcessor Example

This example demonstrates how to use the SmooksProcessor in an Apache Camel route:
Smooks smooks = new Smooks("edi-to-xml-smooks-config.xml");
ExecutionContext context = smooks.createExecutionContext();
...
SmooksProcessor processor = new SmooksProcessor(smooks, context);
 
from("file://input?noop=true")
.process(processor)
.to("mock:result");

Similar to the SmooksComponent we have not specified the result type that Smooks produces (if any that is). Instead this is expressed in the Smooks configuration using the exports element or you can do the same programmatically like this:

Smooks smooks = new Smooks();
ExecutionContext context = smooks.createExecutionContext();
smooks.setExports(new Exports(StringResult.class));
SmooksProcessor processor = new SmooksProcessor(smooks, context);
...
from("file://input?noop=true")
.process(processor)
.to("mock:result");

15.10. Camel Properties

Table 15.1. Camel Properties

Name Description
camel-dataformat
This example illustrates how to use the SmooksDataFormat. (A DataFormat is a class that implements Camel's org.apache.camel.spi.DataFormat.)
camel-integration
This example illustrates how to use the Camel SmooksComponent. ("smooks://file:./configs/smooks-config.xml")
splitting-camel
This example demonstrates how to use Smooks and Apache Camel to process a UN/EDIFACT message interchange, splitting the individual interchange messages out into Java and XML fragments and routing the fragments using Apache Camel.