Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Consume Multiple Operations of SOAP-Based Web Service With Mulesoft Anypoint Studio: Part 2

DZone's Guide to

Consume Multiple Operations of SOAP-Based Web Service With Mulesoft Anypoint Studio: Part 2

In this tutorial, we learn how to consume multiple operations of a SOAP-based service in a single flow using Mule scatter-gather flow control.

· Integration Zone
Free Resource

The Integration Zone is brought to you in partnership with Cloud Elements.  What’s below the surface of an API integration? Download The Definitive Guide to API Integrations to start building an API strategy.

In my previous article, we saw how to consume multiple operations of a SOAP-based web service with Mulesoft Anypoint studio in a single flow using Mule Choice flow control. In this article, we will see how to consume multiple operations of a SOAP-based service in a single flow using Mule scatter-gather flow control. Scatter-Gather sends a request message to multiple targets concurrently. It collects the responses from all routes, and aggregates them into a single message.

Consuming a SOAP-based web service is fairly simple in Mule Anypoint Studio. First, create a new Mule Project, then follow the following steps.

1. HTTP Listener

Use the HTTP Listener connector and drag it into your flow.

Image title

The HTTP listener configuration can be set to the default values as shown below:

Image title

2. Variable

Next, add a Variable to the flow. In our case, we will name it as "IP." For this article, we are using a public SOAP service which has two operations. The WSDL is available here. One operation (GetGeoIP) returns the country name and country code of the IP address, which is supplied in the request. The second operation (GetGeoIPContext) returns your country name and country code, the consumer. The country code is in ISO Alpha-3 format.

Image title

The next step will do the trick of invoking all operations (whether the same SOAP service or different) in the same flow. This is achieved by introducing a 'Scatter-Gather' flow control after the variable. The scatter-gather control will allow the consumer to call multiple SOAP service operations. 

Image title

We have also defined a custom aggregator strategy to combine the results. This is achieved by implementing the org.mule.routing.AggregationStrategy interface.

package examples.mule.aggregator;

import org.mule.DefaultMuleEvent;
import org.mule.api.MuleEvent;
import org.mule.api.MuleException;
import org.mule.api.routing.AggregationContext;
import org.mule.routing.AggregationStrategy;


public class CustomAggregator implements AggregationStrategy  {


 @Override
     public MuleEvent aggregate(AggregationContext context) throws MuleException {
        StringBuilder responseBuilder = new StringBuilder();
        MuleEvent result = null;
        for (MuleEvent event: context.collectEventsWithoutExceptions()) {
              responseBuilder.append(event.transformMessageToString());
              result = DefaultMuleEvent.copy(event);
        }
        result.getMessage().setPayload(responseBuilder.toString());
        if (result != null) {
           return result;
        }
        throw new RuntimeException("no response obtained");
     }


}


3. Web Service Consumer

The next step is to add the web service consumer. While adding the consumer, we will set up the service configuration as shown below:

Image title

For consumer 2 (GetDetailsForIP), we will set up a 'Transform Message" step. This requires a value to passed across in the "IP" parameter when hitting the service.

Image title

Next, we configure our two consumers:

a. GetGeoIPContext

Image title

b. GetGeoIP

Image title

As the last step, we will add XML to JSON to convert the final XML message into JSON for both service consumers.

4. Testing

We can test the service after deploying the application in Anypoint Studio. You can use a browser or Postman to hit the service.

Request:

http://localhost:8081/ipservice?ip=8.8.8.8

Response:

{
"GetGeoIPContextResponse" : {
"@xmlns:xsd" : "http://www.w3.org/2001/XMLSchema",
"@xmlns:xsi" : "http://www.w3.org/2001/XMLSchema-instance",
"@xmlns:xmlns" : "http://www.webservicex.net/",
"GetGeoIPContextResult" : {
"ReturnCode" : "1",
"IP" : "88.55.44.88",
"ReturnCodeDetails" : "Success",
"CountryName" : "Germany",
"CountryCode" : "DE"
}
}
}{
"GetGeoIPResponse" : {
"@xmlns:xsd" : "http://www.w3.org/2001/XMLSchema",
"@xmlns:xsi" : "http://www.w3.org/2001/XMLSchema-instance",
"@xmlns:xmlns" : "http://www.webservicex.net/",
"GetGeoIPResult" : {
"ReturnCode" : "1",
"IP" : "8.8.8.8",
"ReturnCodeDetails" : "Success",
"CountryName" : "United States",
"CountryCode" : "USA"
}
}
}

I hope this article helps you understand how to consume a SOAP service which has more than one operation in Mule Anypoint studio and combine their response in a single flow.

The State of API Integration Report provides data from the Cloud Elements platform and will help all developers navigate the recent explosion of APIs and the implications of API integrations to work more efficiently in 2017 and beyond.

Topics:
mulesoft ,scatter-gather ,soap ,anypoint studio ,integration ,tutorial

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}