DZone
Integration Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Integration Zone > Consume Multiple Operations of SOAP-Based Web Service With Mulesoft Anypoint Studio: Part 2

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.

Yasir Siraj user avatar by
Yasir Siraj
·
May. 14, 17 · Integration Zone · Tutorial
Like (2)
Save
Tweet
6.20K Views

Join the DZone community and get the full member experience.

Join For Free

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.

Web Service MuleSoft

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • How Low Code Demands More Creativity From Developers
  • MACH Architecture Explained
  • ETL/ELT on Kubernetes With Airbyte
  • Vaadin Apps as Native Executables Using Quarkus Native

Comments

Integration Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo