2017-01-26 3 views
0

Ich versuche, zwei verschiedene Datenquellen mit WSO2 ESB zu einem einzigen Datenfeed zu kombinieren. Da die verschiedenen Quellen unterschiedliche Datenformate haben, bestand mein Ansatz darin, für jeden Endpunkt einen Proxy zu erstellen, der sich mit der Autorisierung und der Nutzlastformatierung befasst, so dass beide ein JSON-Array zurückgeben. Basierend auf der Forschung war meine Annahme, dass ich dann den Aggregator-Mediator verwenden könnte, um die Ergebnisse zu kombinieren. Nachdem ich viele Beispiele durchgearbeitet habe, habe ich die beiden Arrays erfolgreich kombiniert, aber eines der Arrays ist immer dupliziert. Kann jemand sehen, was ich falsch mache, oder hat jemand andere Vorschläge für einen besseren Weg, die Kombination von zwei JSON-Arrays zu erreichen?Mehrere JSON-Arrays mit WSO2-Aggregator kombinieren

Ich habe die folgenden Codebeispiele setzen bis zu 2 Feeds zu simulieren und dann mit meinem Code kombinieren:

<?xml version="1.0" encoding="UTF-8"?> 
<proxy name="testb_url1" startOnLoad="true" trace="disable" 
    transports="https http" xmlns="http://ws.apache.org/ns/synapse"> 
    <target> 
    <inSequence> 
      <payloadFactory media-type="json"> 
     <format>[ 
      {"id": "1", 
      "type": "object", 
      "name": "first"}, 
      {"id": "2", 
      "type": "object", 
      "name": "second"} 
      ] 
      </format> 
     <args/> 
     </payloadFactory> 
     <log level="full"/> 
     <loopback/> 
    </inSequence> 
    <outSequence> 
     <send/> 
    </outSequence> 
    <faultSequence/> 
    </target> 
</proxy> 

URL Nummer 2

<?xml version="1.0" encoding="UTF-8"?> 
<proxy name="testb_url2" startOnLoad="true" trace="disable" 
    transports="https http" xmlns="http://ws.apache.org/ns/synapse"> 
    <target> 
    <inSequence> 
      <payloadFactory media-type="json"> 
     <format>[ 
      {"id": "10", 
      "type": "object", 
      "name": "ten"}, 
      {"id": "11", 
      "type": "object", 
      "name": "eleven"} 
      ] 
      </format> 
     <args/> 
     </payloadFactory> 
     <log level="full"/> 
     <loopback/> 
    </inSequence> 
    <outSequence> 
     <send/> 
    </outSequence> 
    <faultSequence/> 
    </target> 
</proxy> 

-Code zu kombinieren:

<?xml version="1.0" encoding="UTF-8"?> 
<proxy name="testb_combine" startOnLoad="true" trace="disable" 
    transports="https http" xmlns="http://ws.apache.org/ns/synapse"> 
    <target> 
    <inSequence> 
     <property name="enclosing_element" scope="default"> 
      <jsonArray xmlns=""/> 
     </property> 
     <send> 
      <endpoint> 
       <recipientlist> 
        <endpoint> 
        <address uri="http://localhost:8280/services/testb_url1/" trace="disable"/> 
        </endpoint> 
        <endpoint> 
        <address uri="http://localhost:8280/services/testb_url2/" trace="disable"/> 
        </endpoint> 
       </recipientlist> 
      </endpoint> 
     </send>  
    </inSequence> 
    <outSequence> 
     <enrich> 
      <source clone="true" xpath="$body/jsonArray/jsonElement"/> 
      <target action="child" xpath="$ctx:enclosing_element"/> 
     </enrich> 
     <aggregate> 
      <completeCondition> 
       <messageCount min="-1" max="-1"/> 
      </completeCondition> 
      <onComplete expression="$body/jsonArray/jsonElement" 
         enclosingElementProperty="enclosing_element"> 
       <send/> 
      </onComplete> 
     </aggregate> 
     </outSequence> 
    <faultSequence/> 
    </target> 
</proxy> 

Und zurückgegebene Ergebnisse zeigen Daten von URL 1 dupliziert:

[ 
    { 
    "id": 1, 
    "type": "object", 
    "name": "first" 
    }, 
    { 
    "id": 2, 
    "type": "object", 
    "name": "second" 
    }, 
    { 
    "id": 1, 
    "type": "object", 
    "name": "first" 
    }, 
    { 
    "id": 2, 
    "type": "object", 
    "name": "second" 
    }, 
    { 
    "id": 10, 
    "type": "object", 
    "name": "ten" 
    }, 
    { 
    "id": 11, 
    "type": "object", 
    "name": "eleven" 
    } 
] 

Antwort

3

den Anreicherungs-Vermittler im outsequence entfernen und es arbeitet wie Sie wollen: Proxy-Dienst:

<?xml version="1.0" encoding="UTF-8"?> 
<proxy xmlns="http://ws.apache.org/ns/synapse" 
     name="testb_combine" 
     transports="https http" 
     startOnLoad="true"> 
    <target> 
     <inSequence> 
     <property name="enclosing_element" scope="default"> 
      <jsonArray xmlns=""/> 
     </property> 
     <send> 
      <endpoint> 
       <recipientlist> 
        <endpoint> 
        <address uri="http://localhost:8283/services/testb_url1/"/> 
        </endpoint> 
        <endpoint> 
        <address uri="http://localhost:8283/services/testb_url2/"/> 
        </endpoint> 
       </recipientlist> 
      </endpoint> 
     </send> 
     </inSequence> 
     <outSequence> 
     <aggregate> 
      <completeCondition> 
       <messageCount min="-1" max="-1"/> 
      </completeCondition> 
      <onComplete expression="$body/jsonArray/jsonElement" 
         enclosingElementProperty="enclosing_element"> 
       <send/> 
      </onComplete> 
     </aggregate> 
     </outSequence> 
     <faultSequence/> 
    </target> 
</proxy> 

Antwort:

[ 
    { 
     "id": 1, 
     "type": "object", 
     "name": "first" 
    }, 
    { 
     "id": 2, 
     "type": "object", 
     "name": "second" 
    }, 
    { 
     "id": 10, 
     "type": "object", 
     "name": "ten" 
    }, 
    { 
     "id": 11, 
     "type": "object", 
     "name": "eleven" 
    } 
] 
Verwandte Themen