wso2wso2-integration-studiowso2-micro-integratoraggregator

Why the following aggregator mediator is not working?


I am creating a simple Composite Application using WSO2 Integration Studio. What I am trying to do is use a switch mediator to route the incoming requests and get the response after that need to call 2 other endpoints and the JSON response coming through from those two endpoints (CarTyreEndpoint, CarEngineEndpoint) needs to aggregate and send to the client. So far everything works fine except this aggregator part. It is not working and even nothing is printing. Can anybody help me to solve this issue? Thanks in advance.

<?xml version="1.0" encoding="UTF-8"?>
<api context="/cars" name="Cars"
    xmlns="http://ws.apache.org/ns/synapse">
    <resource methods="PUT" uri-template="/car">
        <inSequence>
            <property expression="$trp:Model" name="model" scope="default" type="STRING"/>
            <switch source="get-property('model')">
                <case regex="some|models|...">
                    <call>
                        <endpoint key="CarEndpoint"/>
                    </call>
                </case>
                <default>
                    <respond/>
                </default>
            </switch>
            <property expression="json-eval($.CarEndpointResult.Car)" name="car" scope="default" type="STRING"/>
            <header expression="get-property('car')" name="Car" scope="transport"
                xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"/>
                <call>
                    <endpoint key="CarTyreEndpoint"/>
                </call>
                <call>
                    <endpoint key="CarEngineEndpoint"/>
                </call>
                <aggregate>
                    <completeCondition>
                        <messageCount max="-1" min="-1"/>
                    </completeCondition>
                    <onComplete aggregateElementType="root" expression="$body">
                        <log level="full">
                            <property expression="$body" name="Response Body"/>
                        </log>
                        <respond/>
                    </onComplete>
                </aggregate>
            </inSequence>
            <outSequence/>
            <faultSequence/>
        </resource>
    </api>


Solution

  • Aggregate Mediator is coupld with either Iterate or Clone mediator. Basically, when you branch out the mediation flow using the aforementioned mediators you can use the aggregate mediator to combine the responses in each branch.

    What you are trying to do is something like service orchestration. So what you need to do is call each endpoint save the Payloads from each endpoint to a property and then combine them together at the end using something like PayloadFactory Mediator. Take a look at this example.

    Some reference code for your usecase.

    <property expression="json-eval($.CarEndpointResult.Car)" name="car" scope="default" type="STRING"/>
    <header expression="get-property('car')" name="Car" scope="transport"
        xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"/>
    <call>
        <endpoint key="CarTyreEndpoint"/>
    </call>
    <property expression="json-eval($)" name="carType" scope="default" type="STRING"/>
    <call>
        <endpoint key="CarEngineEndpoint"/>
    </call>
    <property expression="json-eval($)" name="carEngine" scope="default" type="STRING"/>
    
    <payloadFactory media-type="json">
        <format>
            {
                "Type": $1,
                "Engine": $2,
            }
        </format>
        <args>
           <arg expression="$ctx:carType" />
           <arg expression="$ctx:carEngine"/>
        </args>
    </payloadFactory>