Skip to content

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 mediation flow.

Let's get started!

Step 1: Set up the workspace

The following software and configurations are required to proceed with this tutorial:

  • Visual Studio Code (VS Code): with the Micro Integrator extension installed.
  • Java Development Kit (JDK): Version 11 or 17 is required. Ensure the JDK is properly configured in your system's PATH environment variable.
  • Apache Maven: Ensure Apache Maven is installed and its path is correctly set within the system's PATH environment variable.

Info

Follow the Install Micro Integrator for VS Code documentation for a complete installation guide.

Step 2: Develop the integration artifacts

Create an integration project

The Integration project will contain all the required artifacts for the integration solution.

  1. Launch VS Code with the Micro Integrator extension installed.

  2. Click on the Micro Integrator icon on the Activity Bar of the VS Code editor.

    MI VS Code Extension

  3. Click Create New Project on Design View.

    Design View Pane Create New Project

    Next, the Project Creation Form will be opened.

  4. In the Project Creation Form, enter SampleServices as the Project Name.

  5. Provide a location under Select Project Directory.

    create new project

  6. Click Create.

Create a REST API

  1. Go to MI Project Explorer > APIs.

    create new api

  2. Hover over APIs and click the + icon that appears to open the API Form.

    add API

  3. Specify values for the required REST API properties:

    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.

  4. Click Create. This will open the Service Designer interface.

    You can now start configuring the API resource.

  5. Click on the GET API resource under Available resources on the Service Designer.

    You will now see the graphical view of the HealthcareAPI with its default API Resource.

  6. Click the Edit icon to edit the API resource.

    edit icon

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

    Property Description
    URL Style URI_TEMPLATE
    URI Template Enter /categories/{category}/reserve.
    Methods POST
    This defines that the API resource only handles requests where the HTTP method is POST.

    edit API resource

  8. Click Update.

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. Navigate to the MI Project Explorer > Endpoints.

    create new endpoint

  2. Hover over Endpoints and click the + icon that appears.

    Add endpoint

  3. Next, select HTTP Endpoint type from the Create Endpoint Artifact interface.

    Create HTTP Endpoint

  4. In the HTTP Endpoint Form that appears, specify the following values to create the new endpoint.

    Property Value Description
    Endpoint Name GrandOakEP The name of the endpoint represents the Grand Oaks Hospital service.
    URI Template http://localhost:9090/grandoaks/categories/{uri.var.category}/reserve The template for the request URL is expected by the back-end service.
    Method POST Endpoint HTTP REST Method.

  5. Click Create.

  6. 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. Navigate to the MI Project Explorer. Hover over Templates and click the + icon that appears.

    create new sequence template

  2. Next, select the Sequence Template from the appeared template artifact menu.

    select sequence template

  3. In the Template Artifact form that appears, specify the following values to create the new sequence template.

    Property Description
    Template Name HospitalRoutingSeq
    Parameters
    1. Click on + Add Parameter
    2. In the form that appears, specify the following values to add a parameter
      • Parameter: sethospital
      • Is Mandatory: Keep it untick.
      • Default Value: Keep it empty.
    3. Click Save

    Filled sequence template

  4. Click Create.

  5. Navigate to MI Project Explorer > Templates > HospitalRoutingSeq.

  6. Add a Log mediator by clicking on the + in the design view and selecting the Log mediator from the palette. This will print a message indicating to which hospital a requested message is routed.

  7. In the form that appears specify the following values.

    Property Description
    Log Category INFO
    Log Level CUSTOM
    Properties
    1. Click on + Add Parameter
    2. In the form that appears, specify the following values to add a property
      • Property Name: message
      • Property Value: Expression fn:concat('Routing to ', get-property('Hospital'))
    3. Click Save

    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.

  8. Click Submit.

  9. Add a Property mediator by clicking on + sign after the Log mediator, and selecting the Property mediator from the palette. In the form that appears specify the following values.

    Property Description
    Property Name uri.var.hospital
    Property Action Select set
    Property Data Type Select STRING
    Property Value
    1. Click Ex button
    2. Enter $func:sethospital
    Property Scope Select DEFAULT

Update the mediation flow

You can now start configuring the API resource.

  1. Navigate to MI Project Explorer > APIs > HealthcareAPI > /categories/{category}/reserve.

  2. Add a Property mediator by clicking on the + sign in the design view and selecting the Property mediator from the palette. This is used to extract the hospital name that is sent in the request payload. In the form that appears, specify the following values.

    Property Description
    Property Name Enter Hospital.
    Property Action Select set.
    Property Data Type Select STRING.
    Property Scope Select DEFAULT.
    Value

    Follow the steps given below to specify the expression value:

    1. Click the Ex button in the Value field. This specifies the value type as an expression.
    2. 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 by clicking the + sign after the Property mediator, and selecting Switch mediator from the palette. In the form that appears, specify the following values.

    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 Ex button in the Value field towards the end
    2. Enter get-property('Hospital')
    Case Branches

    You can use + Add Parameter button to add case branches

    1. Case 1: Click on + Add Parameter and specify Case Regex as grand oak community hospital
    2. Case 2: Click on + Add Parameter and specify Case Regex as clemency medical center
    3. Case 3: Click on + Add Parameter and specify Case Regex as pine valley community hospital

    Add switch mediator

  4. Add a Call Template mediator to each case branch by clicking the + sign in each case branch, and selecting Call Template mediator from the palette. In the form that appears, specify the following values.

    Case 1 Case 2 Case 3
    Target Template Select HospitalRoutingSeq Select HospitalRoutingSeq Select HospitalRoutingSeq
    Call-Template Parameter Click on + Add Parameter button, and specify the following values.
    • Parameter Name: sethospital
    • Parameter Value: grandoaks
    Click on + Add Parameter button, and specify the following values.
    • Parameter Name: sethospital
    • Parameter Value: clemency
    Click on + Add Parameter button, and specify the following values.
    • Parameter Name: sethospital
    • Parameter Value: pinevalley

  5. Add a Call Endpoint mediator to each case branch by clicking the + sign after the Call Template mediator in each case branch, and selecting Call Endpoint mediator from the palette. In the form that appears, specify the following values.

    Case 1 Case 2 Case 3
    Select Endpoint Select GrandOakEP Select ClemencyEP Select PineValleyEP

  6. Add a Respond mediator by clicking the + sign after the Switch mediator, and selecting the Respond mediator from the palette. It will return the response from the health care service back to the client.

Complete design view

Step 3: Build and run the artifacts

Prerequisites

Before you begin, install Micro Integrator on your machine:

  1. Go to the WSO2 Micro Integrator web page, click Download, provide necessary details, and then click Zip Archive to download the Micro Integrator distribution as a ZIP file.

  2. Extract the ZIP file. The extracted folder will be referred as the <MI_HOME> folder.

Once you have downloaded and set up the Micro Integrator locally, follow the steps given below. Use one of the below two methods.

  1. Click on the Command Palette on the top of the VS Code.

  2. Type > to show the available commands.

  3. Select MI: Add MI server.

  4. Select Add MI server.

  5. Select the folder where <MI_HOME> is located. This wll be set as the current server path.

    Current server path

  6. Run the project.

Use one of the following two options to build and run the project:

Option 1

  1. Click on the Command Palette on the top of the VS Code.
  2. Type > to show the available commands.
  3. Select MI: Build and Run.

Option 2

Click the Build and Run icon located on the top right corner of the VS Code.

Build and run

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 4: Test 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, and navigate to the location where you 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 Postman or any other HTTP Client.

  1. Open the Postman application. If you do not have the application, download it from here : Postman

  2. Add the request information as given below and click the Send button.

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

    Body
    { "patient": { "name": "John Doe", "dob": "1940-03-19", "ssn": "234-23-5253", "address": "California", "phone": "8770586755", "email": "[email protected]" }, "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 named request.json with the following request payload.

    {
        "patient": {
            "name": "John Doe",
            "dob": "1940-03-19",
            "ssn": "234-23-5253",
            "address": "California",
            "phone": "8770586755",
            "email": "[email protected]"
        },
        "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 -X POST -H "Content-Type: application/json" --data @request.json http://localhost:8290/healthcare/categories/surgery/reserve
    

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": "1940-03-19",
        "ssn": "234-23-5253",
        "address": "California",
        "phone": "8770586755",
        "email": "[email protected]"
    },
    "fee": 7000.0,
    "confirmed": false
}

Now check the Console tab and you will see the following message:

INFO {LogMediator} - {api:HealthcareAPI} To: /healthcare/categories/surgery/reserve, MessageID: urn:uuid:cb0ccafb-8880-44cb-9cb8-23c068a893b4, correlation_id: cb0ccafb-8880-44cb-9cb8-23c068a893b4, Direction: request, 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.