File to XMPP Bridge
This cookbook page shows how to implement a simple file to xmpp forwarder.
Situation
We want to poll files containing simple text messages formatted as xml from a folder and forward those an xmpp buddy.
This is a very simple example showing how to route messages from a file poller endpoint to an xmpp sender endpoint.
Requirements
For this example we need 2 endpoints configured in ServiceMix.
- a file poller endpoint
- an xmpp sender endpoint
You may put the file poller and xmpp sender together into a single service unit. We divided them here for a better overview.
This recipe has been tested with servicemix-xmpp-2010.02-SNAPSHOT only.
The file poller
The file poller will poll a specific folder for xml files containing the message to send and routes them to the xmpp sender endpoint.
<?xml version="1.0"?> <beans xmlns:file="http://servicemix.apache.org/file/1.0" xmlns:ex="http://servicemix.apache.org/example" xmlns:sm="http://servicemix.apache.org/config/1.0"> <file:poller service="ex:filePoller" endpoint="pollEndpoint" autoCreateDirectory="true" file="/home/lhein/xmpp/messages/" targetService="ex:xmppSender" period="20000" deleteFile="true" recursive="true" /> </beans>
The above definition will create a file poller endpoint which polls the given folder every 20 seconds and deleting the polled files after successful sending. The targetService points to the xmpp sender endpoint.
The XMPP sender
The xmpp sender will send the received messages to a given xmpp buddy. You could also post it to chat rooms.
<?xml version="1.0"?> <beans xmlns:xmpp="http://servicemix.apache.org/xmpp/1.0" xmlns:ex="http://servicemix.apache.org/example" xmlns:sm="http://servicemix.apache.org/config/1.0"> <xmpp:sender service="ex:xmppSender" endpoint="senderEndpoint" host="my.jabber.server" port="5222" user="lhein" password="myVerySecretPassword" participant="gnodet@my.jabber.server" createAccount="false" /> </beans>
The above definition will create an xmpp sender endpoint which connects to the given XMPP server and authenticates using the defined user and password. It will send the messages to the given participant. If the given user has no xmpp account on that server we do not want to create one.
How it works
The file poller will poll the specified folder every 20 seconds and grab all available unprocessed message files. Those files will be simply forwarded to the xmpp sender endpoint. if the xmpp sender endpoint will receive those messages then an xmpp message is generated out of the message content and sent to the specified participant. After that is done, the original exchange from the file poller will be set to done state and that will lead to deletion of the polled file. If an error occurs in between, there will be a roll back and the polled file will be polled again in the next cycle.
Test it with ServiceMix 4
Just take the following file, modify the xmpp settings and the folder and then drop it into the deploy folder of your ServiceMix 4 installation.
<?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:xmpp="http://servicemix.apache.org/xmpp/1.0" xmlns:ex="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/xmpp/1.0 http://servicemix.apache.org/xmpp/1.0/servicemix-xmpp.xsd http://servicemix.apache.org/file/1.0 http://servicemix.apache.org/file/1.0/servicemix-file.xsd"> <!-- this bean has to be here always, otherwise no endpoints are recognized within this file --> <bean class="org.apache.servicemix.common.osgi.EndpointExporter" /> <!-- this has to be here because we need to translate system variables --> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" /> <manifest> Bundle-Version = 1.0.0 Bundle-Name = Examples :: Jabber Application Bundle-SymbolicName = org.apache.servicemix.examples.jabber Bundle-Description = An example which reads a file from an input and posts it to jabber Bundle-Vendor = lhein@apache.org Require-Bundle = servicemix-file,servicemix-xmpp </manifest> <!-- file poller endpoint --> <file:poller service="ex:filePoller" endpoint="pollEndpoint" autoCreateDirectory="true" file="/home/lhein/xmpp/messages/" targetService="ex:xmppSender" period="20000" deleteFile="true" recursive="true" /> <xmpp:sender service="ex:xmppSender" endpoint="senderEndpoint" host="my.jabber.server" port="5222" user="lhein" password="myVerySecretPassword" participant="gnodet@my.jabber.server" createAccount="false" /> </beans>
As a test file you can use this one:
<message>Hi, this xmpp message was sent to you via Apache ServiceMix!</message>