Month: November 2012

Basic Alert System With WSO2 ESB

Introduction

Recently I got a chance to come across different service enterprise integration patterns. Under that Test Message pattern I have read, made my mind to do this type of work. In here I’m using WSO2 ESB, which has become familiar to me. So I suppose that you are also familiar with that.

What I’m going to do

In here I’m going to check the availability of a backend service periodically/at a given time, and if the service is not available at that time corresponding user will get an email. In the following example I’m explaining about a basic scenario, but this can be expanded to scenarios with various transports, various messaging formats, etc.

For this example I’m using latest WSO2 ESB version 4.5.1. Backend service can be your own or a service provided with samples. For more information look here.

The design of the ESB configuration can be shown as follows. You can see that injecting a test message will not affect the normal flow of the messages. For injecting Test message main sequence must be used.

Scheduled Tasks

Scheduled Tasks in WSO2 ESB is used here to produce Test massages. You can set scheduled tasks to inject messages periodically by setting interval parameter or at a particular time by setting a corn expression. I’m not going to discuss on more about Scheduled Tasks here, but you here for more information.

Axis2 Configuration

To enable sending emails, you have to enable smtp transport in axis2 configuration in WSO2 ESB. (resides in /wso2esb-4.5.1/repository/conf/axis2 folder). So it should be look like follow.


<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">your_sending_email@gmail.com</parameter>
 <parameter name="mail.smtp.password">your_sending_email_password</parameter>
 <parameter name="mail.smtp.from">your_sending_email@gmail.com</parameter>
 </transportSender>

Then you need to enable “text/html” message formatter.


<messageFormatter contentType="text/html" class="org.wso2.carbon.relay.ExpandingMessageFormatter"/>

Setting Configuration

Thereafter you have  to add the following configuration to Synapse. Explanations are done along with the configuration

<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns="http://ws.apache.org/ns/synapse">
 <proxy name="ServiceProxy" transports="https http" startOnLoad="true" trace="disable">
  <target inSequence="sendSeq"/>
 </proxy>

<!-- Scheduled task to send messages -->
 <task name="Testing" group="synapse.simple.quartz">
 <!-- produce messages every 25 seconds, this can be replaced by a corn expression -->
 <trigger interval="25"/>

<!-- Create the payload for test message -->
 <property xmlns:task="http://www.wso2.org/products/wso2commons/tasks" name="message">
 <m0:getQuote xmlns:m0="http://services.samples">
 <m0:request>
 <m0:symbol>TEST</m0:symbol>
 </m0:request>
 </m0:getQuote>
 </property>

 <property xmlns:task="http://www.wso2.org/products/wso2commons/tasks" name="soapAction" value="urn:getQuote"/>
 <property xmlns:task="http://www.wso2.org/products/wso2commons/tasks" name="to" value="http://localhost:9000/services/SimpleStockQuoteService"/>
 </task>

<!-- For injecting test messages in to normal message flow -->
 <sequence name="main">
 <in>
 <sequence key="sendSeq"/>
 </in>
 <out>
 <send/>
 </out>
 </sequence>

<!-- sending messages to backend , if available respond will be send to "receiveSeq"-->
 <sequence name="sendSeq">
 <send receive="receiveSeq">
 <endpoint>
 <address uri="http://localhost:9000/services/SimpleStockQuoteService"/>
 </endpoint>
 </send>
 </sequence>

<!-- If there's any fault condition messages are directed to here -->
 <sequence name="fault">

<!-- filter out the failed Test Messages -->
 <filter xmlns:ns="http://org.apache.synapse/xsd" xmlns:m0="http://services.samples" source="//m0:getQuote/m0:request/m0:symbol" regex="TEST">
 <then>
 <property name="Subject" value="Alert Message From WSO2 ESB - Service Down !!!" scope="transport" type="STRING"/>
 <property name="MessageType" value="text/html" scope="axis2" type="STRING"/>
 <property name="ContentType" value="text/html" scope="axis2" type="STRING"/>
 <property name="OUT_ONLY" value="true" scope="default" type="STRING"/>

 <send>
 <endpoint>
<!-- should be changed according to your scenario -->
 <address uri="mailto:your_own_email@gmail.com"/>
 </endpoint>
 </send>

 </then>
 <else/>
 </filter>
 <drop/>
 </sequence>

<!-- sequence which receives replies from back end service, if available-->
 <sequence name="receiveSeq">
 <filter xmlns:ax21="http://services.samples/xsd" xmlns:ns="http://org.apache.synapse/xsd" source="//ax21:symbol" regex="TEST">
 <then>
 <log>
 <property name="TEST PASSED" value="*** Test Message Passed ***"/>
 <property name="TESTED SERVICE" value="*** localhost:9000/services/SimpleStockQuoteService ***"/>
 </log>
 <drop/>
 </then>
 <else>
 <send/>
 </else>
 </filter>
 </sequence>

</definitions>

Now you can start WSO2 ESB alone, so since you test message can’t send to back-end service you will get an email as follows.

References

[1] WSO2 ESB: wso2.com/products/enterprise-service-bus/

[2] Sending Emails with WSO2 ESB: http://wso2.org/project/esb/java/2.1.1/docs/samples/transport_samples.html#Sample256

[3] Setting mail  transport in WSO2 ESB: http://wso2.org/project/esb/java/2.1.1/docs/samples_setup_guide.html#mailsender

[4] WSO2 ESB Scheduled Tasks: http://docs.wso2.org/wiki/display/ESB451/Adding+and+Scheduling+Tasks

[5] Test Message EIP: http://www.eaipatterns.com/TestMessage.html