...
Here's what we'll try to accomplish in this tutorial:
- Create a Maven build for the project
- Get sample files for the customer Excel, CSV, and XML input
- Get a sample file for the canonical XML format that Acme's accounting system uses
- Create an XSD for the canonical XML format
- Create JAXB POJOs corresponding to the canonical XSD
- Create an XSLT stylesheet to convert the Customer 1 (XML over FTP) messages to the canonical format
- Create a unit test to ensure that a simple Camel route invoking the XSLT stylesheet works
- Create a POJO that converts a
List<List<String>>
to the above JAXB POJOs- Note that Camel can automatically convert CSV input to a List of Lists of Strings representing the rows and columns of the CSV, so we'll use this POJO to handle Customer 2 (CSV over HTTP)
- Create a unit test to ensure that a simple Camel route invoking the CSV processing works
- Create a POJO that converts a Customer 3 Excel file to the above JAXB POJOs (using POI to read Excel)
- Create a unit test to ensure that a simple Camel route invoking the Excel processing works
- Create a POJO that reads an input message, takes an attachment off the message, and replaces the body of the message with the attachment
- This is assuming for Customer 3 (Excel over e-mail) that the e-mail contains a single Excel file as an attachment, and the actual e-mail body is throwaway
- Build a set of Camel routes to handle the entire input (Customer -> Acme) side of the scenario.
- Build unit tests for the Camel input.
- TODO: Tasks for the output (Acme -> Customer) side of the scenario
Let's Get Started!
Step 1: Initial Maven build
We'll use Maven for this project as there will eventually be quite a few dependencies and it's nice to have Maven handle them for us. You should have a current version of Maven (e.g. 2.0.9) installed.
You can start with a pretty empty project directory and a Maven POM file, or use a simple JAR archetype to create one.
Here's a sample POM. We've added a dependency on camel-core, and set the compile version to 1.5 (so we can use annotations):
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0">
<modelVersion>4.0.0</modelVersion>
<groupId>org.apache.camel.tutorial</groupId>
<artifactId>trading-partners</artifactId>
<version>1.0</version>
<name>Camel Trading Partners Tutorial</name>
<dependencies>
<dependency>
<artifactId>camel-core</artifactId>
<groupId>org.apache.camel</groupId>
<version>1.4.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
|
Step 2: Get Sample Files
You can make up your own if you like, but here are the "off the shelf" ones:
...
If you look at these files, you'll see that the different input formats use different field names and/or ordering, because of course the sales guys were totally OK with that. Sigh.
Step 3: XSD and JAXB Beans for the Canonical XML Format
Here's the sample of the canonical XML file:
Code Block | ||||
---|---|---|---|---|
| ||||
<?xml version="1.0" encoding="UTF-8"?> <invoice xmlns="http://activemq.apache.org/camel/tutorial/partners/invoice"> <partner-id>2</partner-id> <date-received>9/12/2008</date-received> <line-item> <product-id>134</product-id> <description>A widget</description> <quantity>3</quantity> <item-price>10.45</item-price> <order-date>6/5/2008</order-date> </line-item> ...<!-- // more line items-item elements here --> <order-total>218.82</order-total> </invoice> |
If you're ambitions, you can write your own XSD (XML Schema) for files that look like this. If not, you can download mine. In any case, save it to src/main/xsd.
Generating JAXB Beans
Now we need to update the Maven POM to generate JAXB beans from the XSD file.
We need a dependency:
Code Block | ||||
---|---|---|---|---|
| ||||
<dependency>
<artifactId>camel-jaxb</artifactId>
<groupId>org.apache.camel</groupId>
<version>1.4.0</version>
</dependency>
|
And a plugin configured:
Code Block | ||||
---|---|---|---|---|
| ||||
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>xjc</goal>
</goals>
</execution>
</executions>
</plugin>
|
That should do it (it automatically looks for XML Schemas in src/main/xsd
to generate beans for). Run mvn install and it should emit the beans into target/generated-sources/jaxb
.