Monday, November 16, 2015

How to use Aggregate Mediator in WSO2 ESB

Aggregate Mediator merges the incoming messages into one message and sends it as a response message.  Aggregate Mediator is required when we use Iterator or Clone Mediator. It keeps collecting the incoming message until completion condition is satisfied. It invokes On Complete sequence once aggregation is completed. You can get the merge message by using XPATH expression.


To use Aggregate Mediator, you can follow 'http://shriwithjava.blogspot.co.nz/2015/11/how-to-use-iterator-mediator-in-wso2-esb.html' blog test case. In this blog I have also put the coding content for Aggregation and Iterator Mediator.

Aggregate mediator is can be added inside the Advance list (After editing the sequence in which Aggregate mediator needs to be defined).


Once you click on the Aggregate mediator you will get the below screen in which you need to pass some parameter.

  • Aggregation ID: However this is the optional parameter but used when Iterator/Clone split messages needs to be aggregated. This Aggregation id must be the same as we define in the Iterator/Clone mediator.
  • Aggregation Expression: An XPATH expression which specify that which message needs to be aggregated.
  • Completion Timeout: We can define the completion timeout; once this timeout is completed Aggregation is completed but this also check the condition of Completion-Max Message. If ‘Completion-Max Message’ is reached to its max value then it does not check the completion time even if there is some time left.
  • Completion Max Message: The number of message it can hold to aggregate once defined value is reached aggregation is completed.
  • Completion Min-Message: This is the minimum message it has to hold for aggregation, it also checks if Completion timeout condition, Aggregation is completed once time is finished even it did not receive even a single message.
  • Correlation Expression: This is an XPATH expression which provides the basis on which response messages should be selected for aggregation.
  • Enclosing Element Property: This parameter is used to accumulate the aggregated messages inside a single property. The name of the relevant property is entered in this field.
  • On Complete: This defines that which sequence has to run once aggregation is completed you can define from the registry and also you can define a new mediator (for example send mediator to send message) as children to send message.

Synapse Code:

<sequence name="ManageManagerService_IN">
        <property xmlns:man="http://shriwithjava.blogspot.co.nz/managerdetails/" name="Id" expression="//man:Id/text()"/>
        <property xmlns:man="http://shriwithjava.blogspot.co.nz/managerdetails/" name="Name" expression="//man:Name/text()"/>
        <property xmlns:man="http://shriwithjava.blogspot.co.nz/managerdetails/" name="Address" expression="//man:Address/text()"/>
        <property xmlns:man="http://shriwithjava.blogspot.co.nz/managerdetails/" name="NoOfDepartment" expression="count(//man:Departments/man:DepartmentDetails)"/>
        <iterate xmlns:man="http://shriwithjava.blogspot.co.nz/managerdetails/" id="ITR_AGG" expression="//man:Departments/man:DepartmentDetails">
            <target>
                <sequence>
                    <property name="DepartmentName" expression="//man:DepartmentName/text()"/>
                    <property name="NoOfPeople" expression="//man:NoOfPeople/text()"/>
                    <property name="OfficeLocation" expression="//man:OfficeLocation/text()"/>
                    <script language="js" key="ManageManagerServiceScript" function="ManageManagerDetailsServiceRequest"/>
                    <send>
                        <endpoint key="DSS_ManageManagerDetails_EPR"/>
                    </send>
                </sequence>
            </target>
        </iterate>
    </sequence>
                                  <sequence name="ManageManagerService_OUT">
        <log level="full"/>
        <aggregate id="ITR_AGG">
            <completeCondition>
                <messageCount min="-1" max="-1"/>
            </completeCondition>
            <onComplete xmlns:s12="http://www.w3.org/2003/05/soap-envelope" xmlns:s11="http://schemas.xmlsoap.org/soap/envelope/" expression="s11:Body/child::*[position()=1] | s12:Body/child::*[position()=1]">
                <property xmlns:man="http://shriwithjava.blogspot.co.nz/managemanagerdetails" name="ResultCode" expression="//man:ManageManagerDetailsResponse/man:ResultCode/text()"/>
                <property xmlns:man="http://shriwithjava.blogspot.co.nz/managemanagerdetails" name="ResultDescription" expression="//man:ManageManagerDetailsResponse/man:ResultDescription/text()"/>
                <filter xpath="contains(get-property('ResultCode'),'1')">
                    <then>
                        <property xmlns:man="http://shriwithjava.blogspot.co.nz/managemanagerdetails" name="RESCULT_CODE" expression="//man:ManageManagerDetailsResponse[man:ResultCode='1'][1]/man:ResultCode/text()"/>
                        <property xmlns:man="http://shriwithjava.blogspot.co.nz/managemanagerdetails" name="RESCULT_DESC" expression="//man:ManageManagerDetailsResponse[man:ResultCode='1'][1]/man:ResultDescription/text()"/>
                    </then>
                    <else>
                        <property name="RESCULT_CODE" value="0"/>
                        <property name="RESCULT_DESC" value="Success"/>
                    </else>
                </filter>
                <payloadFactory>
                    <format>
                        <man:ManagerDetailsResponse xmlns:man="http://shriwithjava.blogspot.co.nz/managerdetails/">
                            <man:ResponseCode>$1</man:ResponseCode>
                            <man:ResponseDescription>$2</man:ResponseDescription>
                        </man:ManagerDetailsResponse>
                    </format>
                    <args>
                        <arg expression="get-property('RESCULT_CODE')"/>
                        <arg expression="get-property('RESCULT_DESC')"/>
                    </args>
                </payloadFactory>
                <property name="RESPONSE" value="true"/>
                <header name="To" action="remove"/>
                <log level="full"/>
                <send/>
                <drop/>
            </onComplete>
        </aggregate>
    </sequence>

For more detail regarding above test case and Synapse code please visit my previous blog http://shriwithjava.blogspot.co.nz/2015/11/how-to-use-iterator-mediator-in-wso2-esb.html.