Panel |
---|
All files for this example are in sample.zip. |
Objective: To pick up a file in a
Table of Contents |
---|
Introduction
This sample demonstrates how to pick a file from a directory and process it within the ESB. In this sample scenario you pick a file from the local directory, insert the records from in the file into to a database, send an email with data from the file content, trace and write the log and finally move the file to another directory.
The following scheme visually diagram displays the processes running entities involved in this example.
Info | ||
---|---|---|
| ||
This example works with the MySQL database. |
...
Building the sample
...
maxLevel | 3 |
---|---|
minLevel | 3 |
style | border:1 |
location | top |
type | flat |
separator | pipe |
...
Panel |
---|
All files required for this sample are in sample.zip. |
Follow the steps given below to build this sample.
Set up the database
- Manually set up the database.
Create a table named "
info
" in in your schema. You can use run the commands as shown belowfollowing commands to do this.Code Block delimiter $$ CREATE TABLE `info` ( `name` varchar(45) DEFAULT '', `surname` varchar(45) DEFAULT NULL, `phone` varchar(45) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8$$
Make sure the "
info
" table has been is created and that it contains the following columns:- name
- surname
- phone
...
The result of the query
...
should be as follows when you query to view the records in the test.info
table.
...
You will see that there is no data in the table.
...
Create the main and fault sequences
- Find the
main.xml
andfault.xml
files in the supplied archiveattachedsample.zip
archive. You can find them the files in the\conf\<SAMPLE_HOME>/conf/synapse-config\/sequences
folder directory. Save Copy the files to
<ESB_HOME>/repository/deployment/server/synapse-configs/default/sequences
folder.Info title Note The "
main
" and "fault
" sequences are created and preconfigured automatically while installing when you install WSO2 ESB.
...
Configure the ESB
You will now need to configure the ESB to use the VFS transport for processing the files, and the MailTo transportfor sending the email message. The scenario requires some modifications of the axis2.xml
(ESB configuration file) located in <ESBYou also need to configure the message formatter as specified.
Edit the
<ESB_HOME>/repository/conf/axis2/axis2.
...
Enabling VFS transport
...
xml
file and uncomment the VFS listener and the VFS sender as follows:Code Block language html/xml <transportreceiver name="vfs" class="org.apache.synapse.transport.vfs.VFSTransportListener"/>
...
To enable the VFS transport sender, uncomment the following string.
...
language | html/xml |
---|
... <transportSender name="vfs" class="org.apache.synapse.transport.vfs.VFSTransportSender"/>
Configuring the MailTo Transport
This transport can be used to send e-mail messages.
...
Edit the
<ESB_HOME>/repository/conf/axis2/axis2.xml
file and configure the mailto transport sender as follows to use a mailbox for sending the messages
...
:
Code Block language html/xml <transportSender name="mailto" class="org.apache.axis2.transport.mail.MailTransportSender"> <parameter name="mail.smtp.host">smtp.gmail.com</parameter> <parameter name="mail.smtp.port">587</parameter> <parameter name="mail.smtp.starttls.enable">true</parameter> <parameter name="mail.smtp.auth">true</parameter> <parameter name="mail.smtp.user">username</parameter> <parameter name="mail.smtp.password">userpassword</parameter> <parameter name="mail.smtp.from">username@gmail.com</parameter> </transportSender>
Info title
...
Note In this sample,
...
you will not retrieve mails from a mailbox.
...
Therefore, you do not need to enable the
...
mailto transport receiver.
Configuring Message Builders/Formatters
When a message comes thorough the wire, it first goes through a message builder and message builder is responsible for converting the message into a SOAP message. Message formatters determine the outgoing wire message format of a SOAP message inside the ESB.
...
Code Block | ||
---|---|---|
| ||
<messageFormatters></messageFormatters> |
Add the org.apache.axis2.transport.http.ApplicationXMLFormatter
under the text/html
content type.
Code Block | ||
---|---|---|
| ||
<messageFormatter contentType="text/html" class="org.apache.axis2.transport.http.ApplicationXMLFormatter"/> |
Step 4. Add Database Drivers
...
Add the following message formatter in the
<ESB_HOME>/repository/conf/Axis2/axis2.xml
file under theMessage Formatters
section:Code Block language html/xml <messageFormatter contentType="text/html" class="org.apache.axis2.transport.http.ApplicationXMLFormatter"/>
...
Add database drivers
- Find the MySQL database driver
mysql-connector-java-5.1.10-bin.jar
in the attachedsample.zip
archive. You can find the file in the<SAMPLE_HOME>/lib
directory. - Copy the driver to the
<WSO2ESB_HOME>/repository/components/lib
directory.
...
Add smooks libraries
This example uses a CSV smooks library.
- You can find the CSV smooks library
milyn-smooks-csv-1.2.4.jar
in the attachedsample.zip
archive. You can find the file in the<SAMPLE_HOME>/lib
directory. Copy the library to the
<WSO2ESB_HOME>/repository/components/lib
directory.
Step 5. Add Smooks Libraries
This example uses a CSV smooks library.
- Find the CSV smooks library
milyn-smooks-csv-1.2.4.jar
in the supplied archive. It is located in thelib
folder. Copy the library to the
<WSO2ESB_HOME>/repository/components/lib
directory.Note These configuration changes make system-wide changes to the ESB and the ESB has totitle Notice Note title Note These configuration changes make system-wide changes to the ESB and the ESB has to be restarted for these changes to take effect.
Configure a local entry as follows. This will be used to refer to the smooks configuration saved in For information on how to add a local entry, see Adding a Local Entry. This local entry will be used to refer to the smooks configuration saved in the
<SAMPLE_HOME>/resources/smooks-config.xmlxml
file.Code Block language xml <localEntry key="smooks" src="file:resources/smooks-config.xml"/>
See Adding a Local Entry for further information.
...
Create and
...
configure FileProxy
Find You can find the
FileProxy.xml
in the supplied archive file in the attachedsample.zip
archive. It is located in the\conf\<SAMPLE_HOME>/conf/synapse-config\sequences
folder/proxy-services
directory.
The XML code of the sequence is provided below.as follows:Code Block language html/xml <proxy xmlns="http://ws.apache.org/ns/synapse" name="FileProxy" transports="vfs" startOnLoad="true" trace="disable"> <target> <inSequence> <log level="full"/> <clone> <target sequence="fileWriteSequence"/> <target sequence="sendMailSequence"/> <target sequence="databaseSequence"/> </clone> </inSequence> </target> <parameter name="transport.vfs.ActionAfterProcess">MOVE</parameter> <parameter name="transport.PollInterval">15</parameter> <parameter name="transport.vfs.MoveAfterProcess">file:///home/username/test/original</parameter> <parameter name="transport.vfs.FileURI">file:///home/username/test/in</parameter> <parameter name="transport.vfs.MoveAfterFailure">file:///home/username/test/failure</parameter> <parameter name="transport.vfs.FileNamePattern">.*.txt</parameter> <parameter name="transport.vfs.ContentType">text/plain</parameter> <parameter name="transport.vfs.ActionAfterFailure">MOVE</parameter> </proxy>
In the Edit the FileProxy.xml file, and define the folder where directory to which the original file is should be moved after processing.
Define the folder where the inputCode Block language html/xml <parameter name="transport.vfs.MoveAfterProcess">[file:///home/]<username>/test/original</parameter>
Anchor FileProxyStep FileProxyStep Edit the FileProxy.xml file, and define where the input file should be placed.
Code Block language html/xml <parameter name="transport.vfs.FileURI">[file:///home/]<username>/test/in</parameter>
Define the folder where the file is Edit the FileProxy.xml file, and define the directory to which the file should be moved if an error occurs:.
Code Block language html/xml <parameter name="transport.vfs.MoveAfterFailure">[file:///home/]<username>/test/failure</parameter>
- Save the file to:
<WSO2ESBFileProxy.xml file to the<ESB_HOME>/repository/deployment/server/synapse-configs/default/proxy-services
/FileProxy directory.xml.
...
Create and
...
configure databaseSequence
Follow the instructions below to create a sequence that can be used to connect to the database and to insert the data there.
- Find the
You can find the
the supplied archivedatabaseSequence.xml
file inthe attached
\conf\sample.zip
archive. It is located in the
\<SAMPLE_HOME>/conf/synapse-config
folder/sequences
directory.
provided below
The XML code of the database sequence isas follows.
Specify your database password, username and URL in the <pool> section of the sequence.
Save the file to: <dbreport> <connection> <pool> <password>databasepassword</password> <user>databaseuser</user> <url>jdbc:mysql://localhost:3306/info</url> <driver>com.mysql.jdbc.Driver</driver> </pool> </connection> <statement> <sql>insert into INFO values (?, ?, ?)</sql>Code Block Code Block <sequence xmlns="http://ws.apache.org/ns/synapse" name="databaseSequence"> <log level="full"> <property name="sequence" value="before-smooks"/> </log> <smooks config-key="smooks"> <input type="text"/> <output type="xml"/> </smooks> <log level="full"> <property name="sequence" value="after-smooks"/> </log> <iterate xmlns:ns2="http://org.apache.synapse/xsd" xmlns:ns="http://org.apache.synapse/xsd" xmlns:sec="http://secservice.samples.esb.wso2.org" expression="//csv-records/csv-record"> <target> <sequence> <dbreport> <connection> <pool> <password>db-password</password> <user>db-username</user> <url>jdbc:mysql://localhost:3306/test</url> <driver>com.mysql.jdbc.Driver</driver> </pool> </connection> <statement> <sql>insert into userdetails values (?, ?, ?, ?, ?)</sql> <parameter expression="//csv-record/name/text()" type="VARCHAR"/> <parameter expression="//csv-record/namesurname/text()" type="VARCHAR"/> )" type="VARCHAR"/> <parameter expression="//csv-record/surnamephone/text()" type="VARCHAR"/> <parameter expression="//csv-record/phone/text()" type="VARCHAR"/> </statement> </dbreport>
<WSO2ESB</statement> </dbreport> </sequence> </target> </iterate> </sequence>
- Specify your database username, password, and URL in the
<pool>
section of the sequence. - Save the
databaseSequence.xml
file to the<ESB_HOME>/repository/deployment/server/synapse-configs/default/sequences
/databaseSequence.xml
directory.
...
Create and Configure fileWriteSequence
Find the You can find the
fileWriteSequence.xml
in the supplied archiveattachedsample.zip
archive. It is located in the \conf\the<SAMPLE_HOME>/conf/synapse-config\/sequences
folder directory.
The XML code of the sequence is provided below.as follows:Code Block language html/xml <sequence xmlns="http://ws.apache.org/ns/synapse" name="fileWriteSequence"> <log level="custom"> <property name="sequence" value="fileWriteSequence"/> </log> <property xmlns:ns2="http://org.apache.synapse/xsd" name="transport.vfs.ReplyFileName" expression="fn:concat(fn:substring-after(get-property('MessageID'), 'urn:uuid:'), '.txt')" scope="transport"/> <property name="OUT_ONLY" value="true"/> <send> <endpoint name="FileEpr"> <address uri="vfs:file:///home/username/test/out"/> </endpoint> </send> </sequence>
- In the XML code, define the folder to move the file to.
- Save the file to:
<WSO2ESBEdit the
fileWriteSequence.xml
file, and define the directory to which the file should be moved. - Save the
fileWriteSequence.xml
file to the<ESB_HOME>/repository/deployment/server/synapse-configs/default/sequences
/fileWriteSequence.xml directory.
...
Create and
...
configure sendMailSequence
Find the You can find the
sendMailSequence.xml
file in the supplied archive. You can find it in the \conf\the attachedsample.zip
archive. It is located in the<SAMPLE_HOME>/conf/synapse-config\/sequences
folder directory.
The XML code of the sequence is provided below.as follows:Code Block language html/xml <sequence xmlns="http://ws.apache.org/ns/synapse" name="sendMailSequence"> <log level="custom"> <property name="sequence" value="sendMailSequence"/> </log> <property name="messageType" value="text/html" scope="axis2"/> <property name="ContentType" value="text/html" scope="axis2"/> <property name="Subject" value="File Received" scope="transport"/> <property name="OUT_ONLY" value="true"/> <send> <endpoint name="FileEpr"> <address uri="mailto:username@gmail.com"/> </endpoint> </send> </sequence>
- In the XML code, define Edit the
sendMailSequence.xml
file, and define the e-mail address to which the notification will should be sent. - Save the file to:
<WSO2ESBthesendMailSequence.xml
file to the<ESB_HOME>/repository/deployment/server/synapse-configs/default/sequences
/sendMailSequence directory.xml.
...
Create the input file
Create a text file in the following format.
Code Block name_1, surname_1, phone_1 name_2, surname_2, phone_2
...
Executing the sample
- Save the file in
...
Step 11. Monitor Results
...
- the
.txt
format to the
...
in
directory that you specified in step 3, under the create and configureFileProxy
section.
...
Analyzing the output
In this sample, the ESB listens on a local file system directory. When a file is dropped into the in
directory, the ESB picks this file.
- Make sure the file has appeared appears in the
out
directory. - The ESB inserts the records from the text file to the database. Make sure the data is in the info table. The screen-shot below following screenshot displays the content of the
test.info
table containing with the data from the file.
- Make sure the original file has been is moved to the
/home/<username>/test/original
directory. Make sure the e-mail notification has been is sent to the email address that is specified email. The message should contain the file data. The following screen-shot screenshot displays a notification received.
Excerpt hidden true This example scenario displays how to pick up a file in a local directory, insert the records from the file into a database, send an email with data from the file, trace and write the log and finally move the file to another directory.