For this session we are going to do a small application reading a file from some input folder and send it to some output folder. Because we are lazy guys we do not want to use the complicated JBI packaging but will go for the new Spring Deployment of ServiceMix 4.
Requirements:
- working ServiceMix 4 installation (download it here)
- some fancy text editor
- some time
Ok, let's start...
Open up your favourite xml editor and create a new file called for example filemover.xml. First of all we need to define some namespaces. We are using the servicemix-file component so we have to add it's namespace definition and also some own namespace for this example. (see below)
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:osgi="http://www.springframework.org/schema/osgi" xmlns:file="http://servicemix.apache.org/file/1.0" xmlns:example="http://servicemix.apache.org/examples/1.0" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/osgi http://www.springframework.org/schema/osgi/spring-osgi.xsd http://servicemix.apache.org/file/1.0 http://servicemix.apache.org/file/1.0/servicemix-file.xsd">That's it for the namespaces. Now we are going to create the endpoints for polling and sending a file. For that you can simply refer to the servicemix-file component wiki.
<!-- file poller endpoint -->
<file:poller service="example:file"
endpoint="pollEndpoint"
autoCreateDirectory="true"
file="${user.home}/smxtest/input/"
targetService="example:file"
period="20000"
delay="40000"
deleteFile="true"
recursive="true">
<property name="marshaler">
<bean class="org.apache.servicemix.components.util.BinaryFileMarshaler" />
</property>
</file:poller>
<!-- file writer endpoint -->
<file:sender service="example:file"
endpoint="sendEndpoint"
append="false"
autoCreateDirectory="true"
directory="${user.home}/smxtest/output/">
<property name="marshaler">
<bean class="org.apache.servicemix.components.util.BinaryFileMarshaler" />
</property>
</file:sender>
You maybe noticed that I am using an environment variable called ${user.home} in the endpoints. Usually that would not work but you can simply add a small bean definition in your xml and this variable gets resolved correctly:
<!-- this has to be here because we need to translate system variables --> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" />
Well...we are almost finished. Let's do some final steps now.
If you would put the xml file now into the deploy folder of your Apache ServiceMix 4 folder then the system wouldn't recognize relevant endpoints in there. To export the endpoints to be available inside ServiceMix you need to add another small bean definition to your xml:
<!-- this bean has to be here always, otherwise no endpoints are recognized within this file --> <bean class="org.apache.servicemix.common.osgi.EndpointExporter" />
Now your endpoints will be made available inside ServiceMix. The small application should work now. You can try to drop it into your deploy folder and watch it booting up. When dropping a file into the input folder it should be moved to the output folder in time.
When doing osgi/list inside your ServiceMix shell you will recognize that your application is listed there with the pure file name. That's somehow not so nice and also you have no information about the bundle vendor and maybe required other bundles inside your application so it would try to start even if some dependencies are not met. To fix that we need to implement some kind of manifest inside your xml file to hold that information.
Doing that is really easy and straight forward as you can specify here all stuff which can be specified in an OSGi bundles manifest file. For our purposes we only specify some basic stuff.
<manifest> Bundle-Version = 1.0.0 Bundle-Name = Examples :: File Mover Application Bundle-SymbolicName = org.apache.servicemix.examples.filemover Bundle-Description = An example which reads a file from an input folder and writes it to an output folder Bundle-Vendor = lhein@apache.org Require-Bundle = servicemix-file </manifest>You can see that we specified some bundle version which will be listed by osgi/list as well as the bundle name. Via the Require-Bundle key you can define the bundle dependencies for your bundle. For our application servicemix-file is enough. You can put there also more than one value by separating the values with a comma.
Now put the changed version of the xml file into deploy folder and try osgi/list afterwards. You should have some nice bundle name and version now.
Thank you very much for reading this small hands-on tutorial. The complete filemover.xml is available for download here.
4 comments:
I am using jms component in servicemix4.
I am getting strange issue.
The following is NOT working
But following is working
Why the 'jms:consumer' is not working and 'jms:endpoint' is working.
I have checked with 'servicemix-ftp.xsd', it says 'jms:consumer' is spring based end point, 'jms:endpoint' is NON-Spring endpoint.
Even I have included following at the begining of spring file.
Similary '<ftp:poller' & 'ftp:sender' is not working.
In cosole of servicemix4, it is showing like this
[ 229] [Active ] [Failed ] [ 60] osgi-sample (1.0.0.SNAPSHOT)
Here Spring part is failed!
Could you please let me know the reason & how to make it work?
Thanks in advance
Well...hard to say because your code seems to be missing.
Hi,
Now problem solved.
http://cwiki.apache.org/SM/discussion-forums.html#nabble-td27767991|a27769048
Thanks
-Prakash
Great way to deploy configurations. The example is straight forward and easy to follow.
My only problem is that I fail to deploy my own marshaler - I tried to build a special marshaler but have no clue how to deploy this JAR to be used in another spring deployed filemover.
Can anyone give me a hint, please?
Post a Comment