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

Java Increment Operations: `n++` vs `n = n + 1` vs `n += 1`

In Java, incrementing a variable by one can be done in several ways: n++ , n = n + 1 , and n += 1 . While these expressions achieve the same end result, they differ slightly in syntax and use cases. Let's explore each one and discuss their performance. 1. n++ Post-Increment Operator : Increments the value of n by 1 after its current value has been used. Common Usage : Typically used in loops and other contexts where the current value needs to be used before incrementing. int n = 5; n++; // n is now 6 2. n = n + 1 Addition Assignment : Explicitly sets n to its current value plus 1. Readability : Straightforward and clear, though slightly more verbose. int n = 5; n = n + 1; // n is now 6 3. n += 1 Compound Assignment Operator : Equivalent to n = n + 1 , but more concise. Usage : Combines addition and assignment into one step. int n = 5; n += 1; // n is

Understanding C1 and C2 Compilers in Java

Understanding C1 and C2 Compilers in Java Understanding C1 and C2 Compilers in Java In Java, the Just-In-Time (JIT) compiler is a part of the Java Virtual Machine (JVM) that improves the performance of Java applications by compiling bytecode into native machine code at runtime. The JIT compiler includes two different compilers, known as the C1 and C2 compilers, each with distinct optimization strategies and purposes. C1 Compiler (Client Compiler) The C1 compiler, also known as the client compiler, is designed for fast startup times and lower memory consumption. It performs lighter and quicker optimizations, which makes it suitable for applications that require quick startup and responsiveness. Key characteristics of the C1 compiler include: Quick Compilation: Prioritizes fast compilation times over deep optimizations. Low Overhead: Consumes less memory and resources during compilation. Profile-Guided Optimization: Ca

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