Skip to main content

Aggregate multiple JSON responses with WSO2 Aggregate mediator

We have 2 APIs which are passing JSON responses as follows

{
    "name": "Harshana",
    "address": "Warakapola",
    "vehicleNo": "BGP 3417"
}

{
   "name": "madusanka",
   "address": "colombo",
   "vehicleNo": "BEG 8765"
}

Now we need to pass the response by aggregating these responses as a single response.

{
    "response1": {
        "hAddress": "colombo",
        "name": "madusanka",
        "vehicle": "BEG 8765"
    },
    "response2": {
        "hAddress": "Warakapola",
        "name": "Harshana",
        "vehicle": "BGP 3417"
    }
}

Okay, We can do this by using the WSO2 Aggregrate mediator in WSO2 ESB.

The Aggregate mediator implements the Message Aggregator enterprise integration pattern and aggregates the response messages for messages that were split by the Clone or Iterate mediator and sent using the Send mediator. 

The Syntax for Aggregate Mediator in WSO2

<aggregate>
   <correlateOn expression="xpath"/>?
   <completeCondition [timeout="time-in-seconds"]>
     <messageCount min="int-min" max="int-max"/>?
   </completeCondition>?
   <onComplete expression="xpath" [sequence="sequence-ref"]>
     (mediator +)?
   </onComplete>
</aggregate>


So I have created three APIs. Two for the calling endpoints and the third one for aggregate both responses.

API One.

<?xml version="1.0" encoding="UTF-8"?>
<api context="/getResponse1" name="ResponseOneAPI" xmlns="http://ws.apache.org/ns/synapse">
    <resource methods="GET">
        <inSequence>
            <payloadFactory media-type="json">
                <format>
                    {
                     "name" :"Harshana",
                     "address": "Warakapola",
                     "vehicleNo" : "BGP 3417"
                    }
                </format>
                <args/>
            </payloadFactory>
            <respond/>
        </inSequence>
        <outSequence/>
        <faultSequence/>
    </resource>
</api>

API Two.

<?xml version="1.0" encoding="UTF-8"?> <api context="/getReponse2" name="RespnoseTwoApi" xmlns="http://ws.apache.org/ns/synapse"> <resource methods="GET"> <inSequence> <payloadFactory media-type="json"> <format> { "name" : "madusanka", "address": "colombo", "vehicleNo" : "BEG 8765" } </format> <args/> </payloadFactory> <respond/> </inSequence> <outSequence/> <faultSequence/> </resource> </api>

Now here is the sample API for aggregate both responses.

<?xml version="1.0" encoding="UTF-8"?> <api context="/aggregrateApi" name="AggregrateSampleAPI" xmlns="http://ws.apache.org/ns/synapse"> <resource methods="GET"> <inSequence> <clone> <target> <sequence> <call> <endpoint> <http method="get" uri-template="http://192.168.56.1:8280/getResponse1"/> </endpoint> </call> <log> <property expression="json-eval($.)" name="log"/> </log> </sequence> </target> <target> <sequence> <call> <endpoint> <http method="get" uri-template="http://192.168.56.1:8280/getReponse2"/> </endpoint> </call> <log> <property expression="json-eval($.)" name="log"/> </log> </sequence> </target> </clone> <payloadFactory media-type="json"> <format> { "response" : { "name" : "$1", "hAddress" : "$2", "vehicle" : "$3" } } </format> <args> <arg evaluator="json" expression="$.name"/> <arg evaluator="json" expression="$.address"/> <arg evaluator="json" expression="$.vehicleNo"/> </args> </payloadFactory> <loopback/> </inSequence> <outSequence> <property name="response" scope="default"> <Responses xmlns=""/> </property> <aggregate> <completeCondition> <messageCount max="-1" min="-1"/> </completeCondition> <onComplete expression="$body//response" enclosingElementProperty="response"> <payloadFactory media-type="json"> <format> { "response1" : $1, "response2" : $2 } </format> <args> <arg evaluator="json" expression="$.Responses.response[0]"/> <arg evaluator="json" expression="$.Responses.response[1]"/> </args> </payloadFactory> <send/> </onComplete> </aggregate> </outSequence> <faultSequence/> </resource> </api>

Comments

  1. The Best Casinos & Slot Games in Reno, NV
    The Casinos & Slot 강릉 출장안마 Games in Reno, NV · Golden Nugget Hotel · Red 영주 출장마사지 Sands 화성 출장마사지 Hotel & Casino · Planet Hollywood Casino & Spa · SkyCity Hotel 목포 출장안마 & Casino · Planet Hollywood Casino 고양 출장마사지 &

    ReplyDelete

Post a Comment

Popular posts from this blog

How to use WSO2 Class Mediator in WSO2 ESB

The  Class Mediator  creates an instance of a custom-specified class and sets it as a mediator. If any properties are specified, the corresponding setter methods are invoked once on the class during initialization. Use the Class mediator for user-specific, custom developments only when there is no built-in mediator that already provides the required functionality.  The syntax of Class Mediator in ESB < class   name= "class-name" >     <property name= "string"   value= "literal" >     </property> </ class > Creating a Class Mediator lets use the Eclipse  WSO2 Developer Studio Create a New  Mediator project by selecting File --> New --> project --> Mediator Project Now you have class mediator by extending the AbstractMediator class. Then you need to implement the mediate methods Sample class mediator implementation is as follows. package lk.harshana; import org.apache.synapse.Mess

One to Many Mapping using Spring boot

In this blog, I will explain how to use one-to-many mapping in Spring boot Application What you need? JAVA MySql Eclipse IDE ( whatever you like IDE, I'm using Eclipse for this example) Maven ( you can use the Gradle as well) Initial Plan I will create a spring boot application project using the  Spring Initializer  web tool and import the project as a maven project. after configuring the all necessary setting, I will code for one-to-many mapping. Below diagram is the database model diagram which we going to install using the spring boot application. Let's Start to Code. You need to configure the application.properties file for database connections. add the following content to the src/main/resources/application.properties spring.datasource.url=jdbc:mysql://localhost:3306/learning spring.datasource.username=root spring.datasource.password=root spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect spring.jpa.hibernate

When To Use Indexes In MySQL

When deciding when and how to create an index in your MySQL database, it's important to consider how the data is being used. Let's say you have a database of  students . We will create it like this: CREATE TABLE `students` ( `id` int ( 11 ) NOT NULL AUTO_INCREMENT , `first_name` varchar ( 255 ) DEFAULT NULL , `last_name` varchar ( 255 ) DEFAULT NULL , `class` varchar ( 255 ) DEFAULT NULL , PRIMARY KEY ( `id` ) ) ENGINE = InnoDB Indexes are best used on columns that are frequently used in where clauses, and in any kind of sorting, such as "order by". You should also pay attention to whether or not this information will change frequently, because it will slow down your updates and inserts. Since you wont frequently be adding students, you don't have to worry about the inserts Let's say that you will be looking up the students with a web interface and the end user will be typing in the students name to find them, since r