Reusing Mediation Sequences

What you'll build

In this sample scenario, you will use a Sequence Template and reuse it in multiple places of the medation flow.

Let's get started!

Step 1: Set up the workspace

Download the relevant WSO2 Integration Studio based on your operating system.

Step 2: Develop the integration artifacts

Create an Integration project

An Integration project is a maven multi module project, which will contain all the required modules for the integration solution.

  1. Open WSO2 Integration Studio.
  2. Click New Integration Project in the Getting Started tab as shown below.

    This will open the New Integration Project dialog box.

  3. Enter SampleServices as the project name and select the following check boxes to create the required modules.

    • Create ESB Configs
    • Create Composite Exporter
  4. Click Finish.

You will now see the projects listed in the Project Explorer.

Create a REST API

  1. In the Project Explorer, right-click SampleServicesConfigs and navigate to New -> REST API.
  2. Ensure Create A New API Artifact is selected and click Next.
  3. Enter the details given below to create a new REST API.

    Property Value Description
    Name HealthcareAPI The name of the REST API.
    Context /healthcare Here you are anchoring the API in the /healthcare context. This will become part of the name of the generated URL used by the client when sending requests to the Healthcare service. For example, setting the context to /healthcare means that the API will only handle HTTP requests where the URL path starts with http://host:port/healthcare.
    Save location SampleServicesConfigs This is the ESB Config module where the artifact will be saved.

  4. Click the new API Resource to access the Properties tab and enter the following details:

    Property Description
    Url Style Click in the Value field, click the down arrow, and select URI_TEMPLATE from the list.
    URI-Template Enter /categories/{category}/reserve.
    Methods From the list of methods, select POST.

Create Endpoints

In this tutorial, we have three hospital services hosted as the backend:

  • Grand Oak Community Hospital: http://localhost:9090/grandoaks/
  • Clemency Medical Center: http://localhost:9090/clemency/
  • Pine Valley Community Hospital: http://localhost:9090/pinevalley/

The request method is POST and the format of the request URL expected by the back-end services is http://localhost:9090/grandoaks/categories/{category}/reserve.

Let's create three different HTTP endpoints for the above services.

  1. Right-click SampleServicesConfigs in the Project Explorer and navigate to New -> Endpoint.
  2. Ensure Create a New Endpoint is selected and click Next.
  3. Enter the information given below to create the new endpoint.

    Property Value Description
    Endpoint Name GrandOakEP The name of the endpoint representing the Grand Oaks Hospital service.
    Endpoint Type HTTP Endpoint Indicates that the back-end service is HTTP.
    URI Template http://localhost:9090/grandoaks/categories/{uri.var.category}/reserve The template for the request URL expected by the back-end service.
    Method POST Endpoint HTTP REST Method.
    Static Endpoint
    Select this option because we are going to use this endpoint only in this ESB Config module and will not reuse it in other projects.

    Note: If you need to create a reusable endpoint, save it as a Dynamic Endpoint in either the Configuration or Governance Registry.
    Save Endpoint in SampleServicesConfigs This is the ESB Config module we created in the last section.

  4. Click Finish.

  5. Similarly, create the HTTP endpoints for the other two hospital services using the URI Templates given below:
    • ClemencyEP: http://localhost:9090/clemency/categories/{uri.var.category}/reserve
    • PineValleyEP: http://localhost:9090/pinevalley/categories/{uri.var.category}/reserve

Create a Sequence Template

  1. Right-click on SampleServicesConfigs and navigate to New -> Template . The New Template Artifact dialog box will open.
  2. Select the Create a New Template and click Next.
  3. Enter the following details and click Finish.

    Parameter Description
    Template Name HospitalRoutingSeq
    Template Type Sequence Template

  4. The template artifact will open in the canvas as shown below.

  5. Open the Properties tab of the sequence template by clicking on the canvas (outside the sequence box).

  6. Click the icon to start adding parameters .

  7. In the Template Parameter dialog box that opens, enter 'sethospital' as the parameter name and click Finish.

  8. Add a Log mediator to the sequence template as shown below. This will print a message indicating to which hospital a requested message is routed.

  9. Open the Properties tab of the log mediator and specify the following:

    Property Description
    Log Category INFO
    Log Level CUSTOM

  10. Click the icon to start defining a property. Then add the following details for the property:

    Property Name Description
    Name message
    Type EXPRESSION
    Property Expression fn:concat('Routing to ', get-property('Hospital'))

    We select EXPRESSION because the required properties for the log message must be extracted from the request, which we can do using an XPath expression.

  11. Add a Property mediator just after the Log mediator to store the value for uri.var.hospital.

  12. With the Property mediator selected, access the Properties tab and enter the information given below:

    Property Description
    Property Name Select New Property
    New Property Name uri.var.hospital
    URI Template Select set
    Property Data Type Select STRING
    Value Click on the Ex button in front of the label value and add this $func:sethospital as the expression.
    Description Set Hospital Variable

Define the mediation flow

You can now start configuring the API resource.

  1. Drag a Property mediator from the Mediators palette to the In Sequence of the API resource and name it Get Hospital. This is used to extract the hospital name that is sent in the request payload.
  2. With the Property mediator selected, access the Properties tab and give the following details:

    Property Description
    Property Name Enter New Property....
    New Property Name Enter Hospital.
    Property Action Enter set.
    Property Scope Enter default.
    Value

    Follow the steps given below to specify the expression value:

    1. Click the Ex button before the Value field. This specifies the value type as expression.
    2. Now, click the f button to open the Expression Selector dialog box.
    3. Enter json-eval($.hospital) as the expression value.
    Note: This is the JSONPath expression that will extract the hospital from the request payload.

  3. Add a Switch mediator from the Mediator palette just after the Property Mediator.

  4. Right-click the Switch mediator you just added and select Add/Remove Case to add the number of cases you want to specify.

    We have three different hospital endpoints, which corresponds to three switch cases. Enter 3 for Number of branches and click OK.

  5. With the Switch mediator selected, go to the Properties tab and give the following details:

    Property Description
    Source XPath

    The Source XPath field is where we specify the XPath expression, which obtains the value of the Hospital that we stored in the Property mediator.

    Follow the steps given below to specify the expression:

    1. Click the text box of the Source XPath property. This opens the Expression Selector dialog box.
    2. Select Expression from the list.
    3. Enter get-property('Hospital') to overwrite the default expression.
    4. Click OK.
    Case Branches

    Follow the steps given below to add the case branches:

    1. Double click each case regex (corresponding to each branch) that is listed. This will open the SwitchCaseBranchOutputConnector dialog box.
    2. Change the RegEx values for the switch cases as follows:
      • Case 1: grand oak community hospital
      • Case 2: clemency medical center
      • Case 3: pine valley community hospital
    3. Click OK .

  6. Add a Call Template mediator to the first switch case sequence.

  7. Open the Properties tab of the Call Template mediator and select ' HospitalRoutingSeq' from the list of available templates.

  8. Click the icon to start adding parameters. Enter the following parameter details and click Finish .

    Parameter Description
    Parameter Name sethospital
    Parameter Type value
    Value/Expression grandoaks

  9. Repeat the above steps to add Call Templates for 'Clemency' and 'Pine Valley' hospitals. Add clemency and pinevalley as the respective parameter values.

  10. Drag a Call mediators from the Mediators pallete after the Call Template mediators in each switch sequence.

  11. Then, add the GrandOakEP, ClemencyEP, and PineValleyEP endpoints from the Defined Endpoints palette to the empty boxes adjoining the Call mediator.

  12. Drag a Respond mediator to return the response from the health care service back to the client.

  13. Save the configuration.

Step 3: Package the artifacts

Package the artifacts in your composite exporter module (SampleServicesCompositeExporter) to be able to deploy the artifacts in the server.

  1. Open the pom.xml file in the composite exporter module.
  2. Ensure that the following artifacts are selected in the POM file.

    • HealthcareAPI
    • HospitalRoutingSeq
  3. Save the changes.

Step 4: Build and run the artifacts

To test the artifacts, deploy the packaged artifacts in the embedded Micro Integrator:

  1. Right-click the composite exporter module and click Export Project Artifacts and Run.
  2. In the dialog box that opens, confirm that the required artifacts from the composite exporter module are selected.
  3. Click Finish.

The artifacts will be deployed in the embedded Micro Integrator and the server will start.

  • See the startup log in the Console tab.
  • See the URLs of the deployed services and APIs in the Runtime Services tab.

Step 5: Testing the use case

Let's test the use case by sending a simple client request that invokes the service.

Start the back-end service

  1. Download the JAR file of the back-end service from here.
  2. Open a terminal, navigate to the location where your saved the back-end service.
  3. Execute the following command to start the service:

    java -jar Hospital-Service-JDK11-2.0.0.jar

Send the client request

Let's send a simple request to invoke the service. You can use the embedded HTTP Client of WSO2 Integration Studio as follows:

  1. Open the HTTP Client of WSO2 Integration Studio.

    Tip

    If you don't see the HTTP Client pane, go to Window -> Show View - Other and select HTTP Client to enable the client pane.

  2. Enter the request information as given below and click the Send icon ().

    Method POST
    Headers Content-Type=application/json
    URL http://localhost:8290/healthcare/categories/surgery/reserve

    Body
    { "name": "John Doe", "dob": "1940-03-19", "ssn": "234-23-525", "address": "California", "phone": "8770586755", "email": "johndoe@gmail.com", "doctor": "thomas collins", "hospital_id": "grandoaks", "hospital": "grand oak community hospital", "cardNo": "7844481124110331", "appointment_date": "2025-04-02" }

    • This JSON payload contains details of the appointment reservation, which includes patient details, doctor, hospital, and data of appointment.

If you want to send the client request from your terminal:

  1. Install and set up cURL as your REST client.
  2. Create a JSON file names request.json with the following request payload.

    {
     "name": "John Doe",
     "dob": "1940-03-19",
     "ssn": "234-23-525",
     "address": "California",
     "phone": "8770586755",
     "email": "johndoe@gmail.com",
     "doctor": "thomas collins",
     "hospital_id": "grandoaks",
     "hospital": "grand oak community hospital",
     "cardNo": "7844481124110331",
     "appointment_date": "2025-04-02"
    }
  3. Open a command line terminal and execute the following command from the location where the request.json file you created is saved:

    curl -v -X POST --data @request.json http://localhost:8290/healthcare/categories/surgery/reserve --header "Content-Type:application/json"

Analyze the response

You will see the following response received to your HTTP Client:

{"appointmentNumber":1,
    "doctor":
         {"name":"thomas collins",
          "hospital":"grand oak community hospital",
          "category":"surgery","availability":"9.00 a.m - 11.00 a.m",
          "fee":7000.0},
    "patient":
        {"name":"John Doe",
         "dob":"1990-03-19",
         "ssn":"234-23-525",
         "address":"California",
         "phone":"8770586755",
         "email":"johndoe@gmail.com"},
    "fee":7000.0,
"confirmed":false,
"appointmentDate":"2025-04-02"}

Now check the Console tab of WSO2 Integration Studio and you will see the following message: INFO - LogMediator message = Routing to grand oak community hospital

This is the message printed by the Log mediator when the message from the client is routed to the relevant endpoint in the Switch mediator.

Top