How to Reuse 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¶ You need Visual Studio Code (VS Code) with the WSO2 Integrator: MI for VS Code extension installed. Info See the Install WSO2 Integrator: MI for VS Code documentation to learn how to install WSO2 Integrator: MI for VS Code. Step 2: Develop the integration artifacts¶ Create an integration project¶ The Integration project will contain all the required artifacts for the integration solution. Launch VS Code with the WSO2 Integrator: MI extension installed. Click on the WSO2 Integrator: MI icon on the Activity Bar of the VS Code editor. Click Create New Project on Design View. Next, the Project Creation Form will be opened. In the Project Creation Form, enter UseTemplateTutorial as the Project Name. Provide a location under Select Project Directory. Click Create. Create a REST API¶ Go to MI Project Overview > Add Artifact. Select API under Create an Integration. 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. Click Create. This will open the Service Designer interface. You can now start configuring the API resource. 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. Click the Edit icon to edit the API resource. Click the new API Resource to access the Properties tab and enter the following details: Property Description Resource Path /categories/{category}/reserve Methods POST This defines that the API resource only handles requests where the HTTP method is POST. Click Update. Create HTTP connections¶ 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 as below. http://localhost:9090/grandoaks/categories/{category}/reserve Let's create three different HTTP connections for the above services. Navigate to the Project Overview page. Click on Add artifact. Click + View More under Create an Integration. Select Connections under Other Artifacts to open the Connector Store Form. Select Http. You need to add dependencies to the project, if not added yet. In the Add New Connection form, specify the following values to create the new HTTP connection. Property Value Description Connection Name GrandOakConn The name of the connection represents the Grand Oaks Hospital service. Base URL http://localhost:9090/grandoaks The base of the request URL for the back-end service. Click Create. Similarly, create the HTTP connections for the other two hospital services using the URI Templates given below: ClemencyConn: http://localhost:9090/clemency PineValleyConn: http://localhost:9090/pinevalley Create a sequence template¶ Navigate to the Project Overview page. Click on the Add artifact. Click + View More under Create an Integration. Select Template under Other Artifacts. Next, select the Sequence Template from the appeared template artifact menu. In the Template Artifact form that appears, specify the following values to create the new sequence template. Property Description Template Name HospitalRoutingSeq Parameters Click on + Add Parameter 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. Click Save Click Create. Navigate to MI Project Explorer > Templates > HospitalRoutingSeq. 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. In the form that appears specify the following values. Property Description Log Category INFO Message Routing to ${params.functionParams.sethospital} Click Add. Update the mediation flow¶ You can now start configuring the API resource. Navigate to MI Project Explorer > APIs > HealthcareAPI > /categories/{category}/reserve. Add a Variable mediator by clicking on the + sign in the design view and selecting the Variable 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 Name Enter Hospital. Action Select set. Data Type Select STRING. Value Follow the steps given below to specify the expression value: Click the Ex button in the Value field. This specifies the value type as an expression. Enter payload.hospital as the expression value. Note: This is the synapse expression that will extract the hospital from the request payload. Add a Switch mediator by clicking the + sign after the Variable mediator, and selecting Switch mediator from the palette. In the form that appears, specify the following values. Property Description Expression The Expression field is where we specify the synapse expression, which obtains the value of the Hospital that we stored in the Variable mediator. Follow the steps given below to specify the expression: Click Ex button in the Value field towards the end Enter vars.Hospital Cases You can use + Add new case button to add case branches Case 1: Click on + Add new case and specify Case Regex as grand oak community hospital Case 2: Click on + Add new case and specify Case Regex as clemency medical center Case 3: Click on + Add new case and specify Case Regex as pine valley community hospital 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 Add an HTTP POST operation by clicking the + sign after the Call Template mediator in each case branch. In the form that appears, specify the following values. Case 1 Case 2 Case 3 Connection GrandOakConn ClemencyConn PineValleyConn Relative Path Enter /categories/${params.uriParams.category}/reserve Enter /categories/${params.uriParams.category}/reserve Enter /categories/${params.uriParams.category}/reserve Content Type Select JSON Select JSON Select JSON Request Body Enter ${payload} Enter ${payload} Enter ${payload} Add a Respond mediator by clicking the + sign after the Switch mediator. It will return the response from the health care service back to the client. Step 3: Build and run the artifacts¶ Click the Build and Run icon located in the top right corner of the VS Code. The artifacts will be deployed in the embedded WSO2 Integrator: MI 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¶ Download the JAR file of the back-end service from here. Open a terminal, and navigate to the location where you saved the back-end service. 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. Open the Postman application. If you do not have the application, download it from here : Postman 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: Install and set up cURL as your REST client. 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" } 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.