Tuesday, May 15, 2012

Scatter-Gather EIP with WSO2 ESB


Scatter-Gather or Broadcast Aggregate is a popular Enterprise Integration Pattern (EIP). The example given in the EIP catalog talks about a scenario where you send a quote request to a 3 vendors, and get quotes for certain items, and send back the response from the vendor who provides the best price.

We will show how this can be simulated using the WSO2 ESB, which is the fastestproven & best performing & most flexible open source ESB. For the sake of convenience, we will take the SimpleStockQuoteService as the service provided by the vendors, and return the best quote to the client. 


We will use 3 constructs in the ESB to implement this scenario.
  1. Clone mediator
  2. Aggregate mediator 
  3. Enrich mediator
We will write a simple proxy service called ScatterGatherProxy. The assumption here is that all 3 vendors implement the same service contract. If the service contracts are different, we will first have to do the appropriate transformations before sending the requests to the vendor services, and then transform back the responses. The XSLT mediator can be used to do that. The configuration of this proxy service is shown below.

<?xml version="1.0" encoding="UTF-8"?>
<proxy xmlns="http://ws.apache.org/ns/synapse" name="ScatterGatherProxy" transports="https http" startOnLoad="true" trace="disable">
    <description/>
    <target>
        <inSequence>
            <clone>
                <target>
                    <endpoint name="vendorA">
                        <address uri="http://localhost:9000/services/SimpleStockQuoteService/"/>
                    </endpoint>
                </target>
                <target>
                    <endpoint name="vendorB">
                        <address uri="http://localhost:9001/services/SimpleStockQuoteService/"/>
                    </endpoint>
                </target>
                <target>
                    <endpoint name="vendorC">
                        <address uri="http://localhost:9002/services/SimpleStockQuoteService/"/>
                    </endpoint>
                </target>
            </clone>
        </inSequence>
        <outSequence>
            <log level="full"/>
            <aggregate>
                <completeCondition>
                    <messageCount min="3"/>
                </completeCondition>
                <onComplete xmlns:m1="http://services.samples/xsd" xmlns:m0="http://services.samples" expression="//m0:return">
                    <enrich>
                        <source xmlns:m1="http://services.samples/xsd" clone="true" xpath="//m0:return[not(preceding-sibling::m0:return/m1:last &lt;= m1:last) and not(following-sibling::m0:return/m1:last &lt; m1:last)]"/>
                        <target type="body"/>
                    </enrich>
                    <send/>
                </onComplete>
            </aggregate>
        </outSequence>
    </target>
</proxy>

To be able to run this sample, you will need to deploy the SimpleStockQuoteService on the Sample AxisServer which ships with the WSO2 ESB, and start 3 instances of it, with HTTP ports 9000, 9001 & 9002 respectively.

Next deploy the above proxy service on WSO2 ESB.

Now let's use soapUI to send the following request to the ScatterGatherProxy service.

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ser="http://services.samples">
   <soapenv:Header/>
   <soapenv:Body>
      <ser:getSimpleQuote>
         <ser:symbol>IBM</ser:symbol>
      </ser:getSimpleQuote>
   </soapenv:Body>
</soapenv:Envelope>


Since we have enabled the log mediator inside the outSequence, you will see 3 responses from the 3 vendors. The logs you will see will be similar to the following


In soapUI, you will get the response from the vendor who provided the best quote.


If you carefully compare the logged response messages & the response received by the client, you will notice that the ScatterGatherProxy service returns the best quote to the client (soapUI)


Underneath the Hood

So what is going on in this proxy service?

Let us first look at the inSequence of the ScatterGatherProxy service. In the inSequence, we are simply cloning, or making 3 copies of the request that was sent by the client using the Clone mediator, and forwarding those requests to the 3 vendor services (SimpleStockeQuoteService).

The responses to those 3 requests will be received at the outSequence. Here we first log all the received responses, and then use the Aggregate mediator  to aggregate the responses. The onComplete sequence of the Aggregate mediator   will get called once all 3 responses are received or the specified completion condition is met. We aggregate the responses based on the value of the "return" element in the response. Next we use the Enrich mediator to extract the response which contains the best quote. We use the following XPath 1.0 expression to do this;

//m0:return[not(preceding-sibling::m0:return/m1:last <= m1:last) and not(following-sibling::m0:return/m1:last < m1:last)]

This is a complex way of saying, "pick the response which has the lowest 'last' value". Unfortunately, XPath 1.0, which is the default supported by WSO2 ESB, does not support the XPath 2.0 min function which can reduce the complexity of the above statement by a great deal. Once we find the proper response, we enrich the SOAP body with that response, and then send that response back to the client (soapUI)

Sunday, May 13, 2012

Dead Letter Channel EIP with WSO ESB

Figure 1: Dead Letter Channel in action

Dead Letter Channel (DLC) is an Enterprise Integration Pattern (EIP) that outlines how messaging systems could deal with messages that cannot be delivered to the recipient. Due to system failures, network failures or failures at the recipient, messages may not get delivered to the target recipient. In such a scenario, the messaging system can deliver the message to a DLC. Other mechanisms in the DLC implementation should take care of trying to deliver the dead message to the recipient. This could be done by periodically retrying to send the message to the recipient over a defined time period. Persistence of the dead message could also be another option provided by the DLC implementation, so that even if the messaging system fails, once it is rebooted, the dead messages can be delivered to the receivers.

WSO2 ESB, which is the fastest, proven & best performing & most flexible open source ESB available; implements the DLC pattern using a combination of 3 constructs;


  1. store mediator
  2. message stores
  3. message processors
Figure 2: DLC architecture in WSO2 ESB
As shown in the figure above, the store mediator takes care of storing the dead message in the specified message store. WSO2 ESB contains two message store types, in memory & JMS. In addition, users can provide their own custom message store implementations. The message processor will retrieve the stored messages from the message store associated with it & try to resend those messages to the target receiver. The message store & message processor combination act as the dead letter channel.

Charith has explained in detail how you could configure message stores & message processors  in WSO2 ESB in his article; Implementing Store and Forward Messaging Patterns with WSO2 ESB Part 2

Wednesday, May 09, 2012

Lazy Loading Webapps in a PaaS Deployment

In Lazy Loading Deployment Artifacts in a PaaS Deployments posted last November, I talked about the principles behind lazy loading & the advantages of lazy loading for a PaaS deployment. I concluded the article with the plans we had for implementing lazy loading for webapps. The WSO2 AppServer team has just completed this feature and carried out a performance comparison.

The following graph shows the behavior of the initial response time when the number of deployed webapps increase, with & without ghost deployment (GD). We started with 1 webapp, and gradually increased the number of deployed webapps up to 300. As you can see, without ghost deployment, the initial response time steadily increases, eventually resulting in client timeouts.

This perhaps is the first time webapp lazy loading has been implemented for Tomcat. WSO2 AppServer uses embedded Tomcat. The lazy loading facility is available for the standalone version of the product as well as the multi-tenanted PaaS version of the WSO2 AppServer.

This implementation also includes unloading of webapps which have not been used for sometime, thereby allowing optimal usage of the available resources.

We have written a Tomcat valve which will check whether the real form of the webapp has been loaded, and load it if necessary, and there is an unloader task which periodically checks for inactive webapps & unloads the real webapp & replaces it with the ghostified form of that webapp.

Sunday, May 06, 2012

WSO2 ESB - Service Chaining

This is a follow up to the article titled "WSO2 ESB by Example - Service Chaining" by Supun Kamburugamuva which describes in detail how the following service chaining scenario can be implemented using WSO2 ESB.



Since that article was written, there has been an enhancement to the WSO2 ESB send mediator where the send mediator can direct the response to a named sequence. This simplifies the configuration quite a bit since it is no longer necessary to handle all responses in the outSequence, and decide what to do next depending on properties.

The new proxy service configuration looks like the following:

      <proxy name="CreditProxy" transports="https http" startOnLoad="true" trace="disable">
        <description/>
        <target>
            <inSequence>
                <log>
                    <property name="sequence" value="inSequence - request for CreditProxy"/>
                </log>
                <property xmlns:sam="http://samples.esb.wso2.org" name="ORG_ID" expression="//sam:credit/sam:id"/>
                <property xmlns:sam="http://samples.esb.wso2.org" name="ORG_AMOUNT" expression="//sam:credit/sam:amount"/>
                <enrich>
                    <source type="inline" clone="true">
                        <sam:get xmlns:sam="http://samples.esb.wso2.org">
                            <sam:id>?</sam:id>
                        </sam:get>
                    </source>
                    <target type="body"/>
                </enrich>
                <enrich>
                    <source type="property" clone="true" property="ORG_ID"/>
                    <target xmlns:sam="http://samples.esb.wso2.org" xpath="//sam:get/sam:id"/>
                </enrich>
                <log>
                    <property name="sequence" value="inSequence - request for PersonInfoService"/>
                </log>
                <send receive="personInfoSeq">
                    <endpoint key="PersonInfoEpr"/>
                </send>
            </inSequence>
            <outSequence>
                <drop/>
            </outSequence>
        </target>
        <publishWSDL uri="file:resources/CreditProxy.wsdl"/>
    </proxy>


The two named sequences are shown below:

    <sequence name="personInfoSeq">
        <xslt key="xslt">
            <property name="amount" expression="get-property('ORG_AMOUNT')"/>
        </xslt>
        <send receive="creditSeq">
            <endpoint key="CreditEpr"/>
        </send>
    </sequence>


    <sequence name="creditSeq">
        <log level="full"/>
        <send/>
    </sequence>


The send mediator in the inSequence of the CreditProxy service defines the receiving sequence of the response as personInfoSeq. So the response will be forwarded to the personInfoSeq instead of the outSequence. In the personInfoSeq, we have a send mediator which defines the receiving sequence as creditSeq, which will simply send the response out to the client.

As you will notice, this is a major improvement when it comes to writing a service chaining configuration over the previous method of using a STATE property combined with switch & case mediators.

Thursday, December 01, 2011

6 years at WSO2!

3 years ago, I wrote a blog post titled "3 years at WSO2". So, it's only befitting that I write a similar post at the end of 3 more years. These 3 years passed by very quickly. Time does fly when you are enjoying yourself.

Over the past 3 years, the company has grown from strength to strength while continuing the maintain its culture & values, and emerged as a leader in the middleware space. We've built a comprehensive middleware stack, as well as built the first complete Cloud middleware platform.

Even after 6 long years, I'm still having a lot of fun & always learning something new all the time, which keeps me excited & motivated.


Saturday, November 19, 2011

The birth of Stratos multi-tenancy


On Nov 14th, 2009, I was all by myself & had a LONG & BORING 12hr transit at the Hong Kong airport! I was returning to Sri Lanka after a trip to the US, where I was a speaker at ApacheCon NA, 2009.



Since I had nothing better to do, I started looking at how multi-tenant service deployment & dispatching can be made to work in WSO2 WSAS (since then, this product has been renamed to WSO2 AppServer). I tweeted about what I was planning to do.



I started implementing this, and got very excited when the first request got dispatched to a tenant. So, again I tweeted about this (now historic) event. 



This was the birth of the WSO2 Stratos Cloud middleware platform! The code I wrote during this transit formed the basis for multi-tenancy in Stratos.


During the next month, I got some of the other core stuff working, and like any good Tweep, tweeted about the progress.



My tweets had gotten Sanjiva excited, prompting him to send me the following mail.



The very first bits of the multi-tenancy code was developed in a scratch section of the WSO2 Subversion repo before it became stable enough to be committed to the trunk.

So, it's been 2 years since the first message got dispatched to a tenant at the HKG airport. Time well spent during a long transit. Over the past few years, this code was improved by many people & Stratos  has grown from strength to strength to become the only complete open source Cloud middeware platform. StratoLive, the PaaS based on Stratos, went onto to become the first & only complete PaaS.

P.S. I used SnapBird to search through my old tweets.

Tuesday, November 15, 2011

How to customize WSO2 Carbon UI theme

Chanaka has written an article showing how easy it is to introduce your own custom theme for any WSO2 Carbon based products. The interesting thing is, the theme itself is deployed as an OSGi bundle, so it is just a matter of replacing the OSGi bundle & refreshing it.

 See 'Playing Around With Carbon Product Themes' for more details.