Monday, 18 May 2009

Aapche ServiceMix: an example with Camel

After the Open Community Day in Rotterdam, last week I need to rewirte a piece of our workflow with Camel. As James suggested me I could use Camel whenever I don't need to have each single state of the workflow persisted into the database. And this is exactly our case... 
So  here we go... and here some step on doing this.

Problem: 
As in the Order File Processing Workflow in the Service Mix documentation I'll try to create a simple workflow that manages an xml file with an order, picking the file up from a given directory, send it to the Camel endpoint that will process it and write it back to another given dir.

To do this I need 3 endpoints: 
  • the Poller endpoint: it will poll on the given dir to check if there is a file
  • the Process endpoint: managed by Camel, it will receive the xml of the order and it manages the changes on the order xml
  • the Writer endpoint: another file endpoint that is in charge of writing the file on the given destination directory.
So let start creating the service unit that will contain the configuration and some code.
I need 3 SU (SMIX ServiceUnit), even if the poller and the sender could be configured in the same SU. But to enable more reuse of an endpoint is better configure 3 SUs:

I'll use a dir in which I'll create all 3 SUs. This dir will be called project_home, later on I'll add a pom.xml in there to group all the SU and the SA (Service Assembly). In this way is possible to manage in one shot the whole project. here ther references to all possible Maven archetypes
I refer to this directory as $project_home.

Create the File-Poller-Su:
mvn archetype:create \
 -DarchetypeGroupId=org.apache.servicemix.tooling \
 -DarchetypeArtifactId=servicemix-file-poller-service-unit \
 -DartifactId=tutorial-file-poller-su
Create the Camel-Su:
  mvn archetype:create \      -DarchetypeGroupId=org.apache.servicemix.tooling \      -DarchetypeArtifactId=servicemix-camel-service-unit \      -DartifactId=tutorial-process-su
Create the File-Writer-Su:
mvn archetype:create \
 -DarchetypeGroupId=org.apache.servicemix.tooling \
 -DarchetypeArtifactId=servicemix-file-sender-service-unit \
 -DartifactId=tutorial-file-sender-su
Now we can define the configuration of the poller, the sender and the Camel process.
Modify the xbean.xml under the ...-file-poller-su
Modify the camel-context.xml under the ...-process-su
Modify the xbean.xml