ServiceMix ESB

Tuesday, July 17, 2007

Simple File Transfer Example

Service to move a file from one directory to another. Deployed using JBI Service Assembly xbean and pom.xml files only.

This example will move any file type by using the BinaryFileMarshaler instead of the DefaultFileMarshaler which depends on XML documents only. The BinaryFileMarshaler will send the file as an attachment to the XML document instead of in the payload.

Using ServiceMix v3.2.x

Based on details from: http://cwiki.apache.org/confluence/display/SM/Creating+a+protocol+bridge


1. Directory Structure

filemover\
pom.xml
filemover-file-su\
...
filemover-sa\



2. Create the Service Unit Using Maven Archetypes
(execute this command on one line removing the trailing backslash)

mvn archetype:create \
-DarchetypeGroupId=org.apache.servicemix.tooling \
-DarchetypeArtifactId=servicemix-file-consumer-service-unit \
-DarchetypeVersion=3.1-incubating \
-DgroupId=org.apache.servicemix.samples.filemover \
-DartifactId=bridge-file-su \
-DremoteRepositories=http://people.apache.org/repo/m2-incubating-repository




3. Create the Service Assembly Using Maven Archetypes
(execute this command on one line removing the trailing backslash)

mvn archetype:create \
-DarchetypeGroupId=org.apache.servicemix.tooling \
-DarchetypeArtifactId=servicemix-service-assembly \
-DarchetypeVersion=3.1-incubating \
-DgroupId=org.apache.servicemix.samples.filemover \
-DartifactId=filemover-sa \
-DremoteRepositories=http://people.apache.org/repo/m2-incubating-repository




4. Edit the main pom.xml (in the filemover directory)

<project>
<modelversion>4.0.0</modelversion>
<groupid>org.apache.servicemix.samples</groupid>
<artifactid>filemover</artifactid>
<version>1.0</version>
<packaging>pom</packaging>
<name>FileMover Demo</name>
<modules>
<module>filemover-file-su</module>
<module>filemover-sa</module>
</modules>
</project>




5. Modify the Service Assembly pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xsi="http://www.w3.org/2001/XMLSchema-instance" schemalocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelversion>4.0.0</modelversion>
<groupid>org.apache.servicemix.samples</groupid>
<artifactid>filemover-sa</artifactid>
<packaging>jbi-service-assembly</packaging>
<version>1.0</version>
<name>FileMover Service Assembly</name>
...
<dependency>
<groupid>org.apache.servicemix.samples.bridge</groupid>
<artifactid>filemover-file-su</artifactid>
<version>1.0</version>
</dependency>
...



6. Edit the File SU xbean.xml
Directory: filemover\filemover-file-su\src\main\resources\xbean.xml
Note that both the poller and the sender are in the same bean and the poller's "targetService" points to the sender's "service".


<beans
f="http://servicemix.apache.org/file/1.0"
proj="http://servicemix.apache.org/samples/filemover"
sm="http://servicemix.apache.org/config/1.0">

<f:sender service="proj:fileSender"
endpoint="endpoint"
directory="file:///C:/opensrc/test/myOutbox2"
autocreatedirectory="true">

<property name="marshaler">
<bean
class="org.apache.servicemix.components.util.BinaryFileMarshaler">
</bean>
</property>
</f:sender>

<f:poller service="proj:filePoller"
endpoint="poller" file="file:///C:/opensrc/test/myInbox2"
targetservice="proj:fileSender"
targetendpoint="endpoint"
period="60000"
recursive="true"
autocreatedirectory="true">

<property name="marshaler">
<bean
class="org.apache.servicemix.components.util.BinaryFileMarshaler">
</bean>
</property>
</f:poller>
</beans>





7. Build the project with Maven from filemover directory
mvn clean install