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 '' 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="" name="Id" expression="//man:Id/text()"/>
        <property xmlns:man="" name="Name" expression="//man:Name/text()"/>
        <property xmlns:man="" name="Address" expression="//man:Address/text()"/>
        <property xmlns:man="" name="NoOfDepartment" expression="count(//man:Departments/man:DepartmentDetails)"/>
        <iterate xmlns:man="" id="ITR_AGG" expression="//man:Departments/man:DepartmentDetails">
                    <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"/>
                        <endpoint key="DSS_ManageManagerDetails_EPR"/>
                                  <sequence name="ManageManagerService_OUT">
        <log level="full"/>
        <aggregate id="ITR_AGG">
                <messageCount min="-1" max="-1"/>
            <onComplete xmlns:s12="" xmlns:s11="" expression="s11:Body/child::*[position()=1] | s12:Body/child::*[position()=1]">
                <property xmlns:man="" name="ResultCode" expression="//man:ManageManagerDetailsResponse/man:ResultCode/text()"/>
                <property xmlns:man="" name="ResultDescription" expression="//man:ManageManagerDetailsResponse/man:ResultDescription/text()"/>
                <filter xpath="contains(get-property('ResultCode'),'1')">
                        <property xmlns:man="" name="RESCULT_CODE" expression="//man:ManageManagerDetailsResponse[man:ResultCode='1'][1]/man:ResultCode/text()"/>
                        <property xmlns:man="" name="RESCULT_DESC" expression="//man:ManageManagerDetailsResponse[man:ResultCode='1'][1]/man:ResultDescription/text()"/>
                        <property name="RESCULT_CODE" value="0"/>
                        <property name="RESCULT_DESC" value="Success"/>
                        <man:ManagerDetailsResponse xmlns:man="">
                        <arg expression="get-property('RESCULT_CODE')"/>
                        <arg expression="get-property('RESCULT_DESC')"/>
                <property name="RESPONSE" value="true"/>
                <header name="To" action="remove"/>
                <log level="full"/>

For more detail regarding above test case and Synapse code please visit my previous blog