The ServiceMix File component provides JBI integration to the file system. It can be used to read & write files via URI or to periodically poll directories for new files.
The servicemix-file-service-unit archetype creates a File Service Unit which contains both poller and sender endpoints sample:
mvn archetype:create \ -DarchetypeGroupId=org.apache.servicemix.tooling \ -DarchetypeArtifactId=servicemix-file-service-unit \ -DarchetypeVersion=2010.01 \ -DgroupId=your.group.id \ -DartifactId=your.artifact.id \ -Dversion=your-version |
Once you've customized the service unit, simply install the SU:
mvn install |
|
Remember that to be deployable in ServiceMix, the ServiceUnit has to be embedded in a Service Assembly: only the Service Assembly zip file can be deployed in ServiceMix.
|
The File Poller endpoint polls file in a given directory, read it using the marshaler, and send marshaled file content to the NMR.
<file:poller service="test:poller"
endpoint="poller"
targetService="test:receiver"
file="file:target/pollerFiles" />
|
|
Poller generates an InOnly message. |
|
A File Sender endpoint expects messages coming from the NMR, converts it to file content (using the marshaler) and write file in the given directory.
<file:sender service="test:service"
endpoint="endpoint"
directory="file:target/pollerFiles" />
|
|
You can use the familiar file URI to communicate with files
file:foo.xml file://path/to/something |
Filtering of files to use during the polling process can be accomplished by configuring the filter property.
<file:poller service="test:poller" endpoint="poller" file="file:inbox" targetService="test:service" targetEndpoint="endpoint" period="10000" recursive="true"> <property name="filter"> <bean class="org.apache.oro.io.GlobFilenameFilter"> <constructor-arg value="*.xml" /> </bean> </property> </file:poller> |
In the example above, the Apache ORO jar must be in the classpath. See: http://jakarta.apache.org/oro/
<file:poller service="test:poller" endpoint="poller"
file="file:inbox" targetService="test:service"
targetEndpoint="endpoint" period="10000" recursive="true">
<property name="filter">
<bean class="org.apache.commons.io.filefilter.WildcardFilter">
<constructor-arg value="*.csv" />
</bean>
</property>
</file:poller>
|
In the example above it uses Apache Commons IO WildcardFilter as it implements FileFilter. The Apache Commons IO jar file must be in the classpath. See: http://commons.apache.org/io/
You can define an implementation of the java.util.Comparator interface to specifies the process order of the files.
For example, you can process file sorted by file name. You can implement the following comparator:
public class NameComparator implements Comparator<File> {
/*
* (non-Javadoc)
* @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
*/
public int compare(File file1, File file2) {
return file1.getPath().compareTo(file2.getPath());
}
}
|
and use it in the endpoint definition:
<file:poller service="test:poller" endpoint="poller"
file="file:inbox" targetService="test:service"
targetEndpoint="endpoint" period="10000" recursive="true">
<property name="comparator">
<bean class="your.package.NameComparator"/>
</property>
</file:poller>
|
We provide the following comparators:
|
By default, poller endpoints expect the content of the file to be in xml format.
<file:sender service="test:service" endpoint="endpoint" directory="file:target/componentOutput">
<file:marshaler>
<sm:defaultFileMarshaler>
<sm:fileName>
<!-- lets use a header from the message -->
<sm:xpathString xpath="concat($name, '.xml')"/>
</sm:fileName>
</sm:defaultFileMarshaler>
</file:marshaler>
</file:sender>
|
Below is an example of a non-XML file marshaler that moves a file from an inbox to an outbox directory. The binary marshaler sends a message with the file attached. This allows any file to be processed.
The contents below were used in an xbean.xml file for a service unit named filemover
<?xml version="1.0"?>
<beans xmlns:f="http://servicemix.apache.org/file/1.0"
xmlns:proj="http://servicemix.apache.org/samples/filemover"
xmlns:sm="http://servicemix.apache.org/config/1.0">
<f:sender service="proj:fileSender"
endpoint="endpoint"
directory="file:///C:/opensrc/test/myOutbox"
autoCreateDirectory="true">
<property name="marshaler">
<bean class="org.apache.servicemix.components.util.BinaryFileMarshaler" />
</property>
</f:sender>
<f:poller
service="proj:filePoller"
endpoint="poller"
file="file:///C:/opensrc/test/myInbox"
targetService="proj:fileSender"
targetEndpoint="endpoint"
period="60000"
recursive="true"
autoCreateDirectory="true">
<property name="marshaler">
<bean class="org.apache.servicemix.components.util.BinaryFileMarshaler" />
</property>
</f:poller>
</beans>
|