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¶
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.
- Open WSO2 Integration Studio.
-
Click New Integration Project in the Getting Started tab as shown below.
This will open the New Integration Project dialog box.
-
Enter
SampleServices
as the project name and select the following check boxes to create the required modules.- Create ESB Configs
- Create Composite Exporter
-
Click Finish.
You will now see the projects listed in the Project Explorer.
Create a REST API¶
- In the Project Explorer, right-click SampleServicesConfigs and navigate to New -> REST API.
- Ensure Create A New API Artifact is selected and click Next.
-
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 withhttp://host:port/healthcare
.
Save location SampleServicesConfigs This is the ESB Config module where the artifact will be saved. -
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.
- Right-click SampleServicesConfigs in the Project Explorer and navigate to New -> Endpoint.
- Ensure Create a New Endpoint is selected and click Next.
-
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. -
Click Finish.
- 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
- ClemencyEP:
Create a Sequence Template¶
- Right-click on SampleServicesConfigs and navigate to New -> Template . The New Template Artifact dialog box will open.
- Select the Create a New Template and click Next.
-
Enter the following details and click Finish.
Parameter Description Template Name HospitalRoutingSeq Template Type Sequence Template -
The template artifact will open in the canvas as shown below.
-
Open the Properties tab of the sequence template by clicking on the canvas (outside the sequence box).
-
Click the icon to start adding parameters .
-
In the Template Parameter dialog box that opens, enter 'sethospital' as the parameter name and click Finish.
-
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.
-
Open the Properties tab of the log mediator and specify the following:
Property Description Log Category INFO Log Level CUSTOM -
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.
-
Add a Property mediator just after the Log mediator to store the value for uri.var.hospital.
-
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.
- 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.
-
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:
- Click the Ex button before the Value field. This specifies the value type as expression.
- Now, click the f button to open the Expression Selector dialog box.
- Enter
json-eval($.hospital)
as the expression value.
-
Add a Switch mediator from the Mediator palette just after the Property Mediator.
-
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.
-
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:
- Click the text box of the Source XPath property. This opens the Expression Selector dialog box.
- Select Expression from the list.
- Enter
get-property('Hospital')
to overwrite the default expression. - Click OK.
Case Branches Follow the steps given below to add the case branches:
- Double click each case regex (corresponding to each branch) that is listed. This will open the SwitchCaseBranchOutputConnector dialog box.
-
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
- Click OK .
-
Add a Call Template mediator to the first switch case sequence.
-
Open the Properties tab of the Call Template mediator and select ' HospitalRoutingSeq' from the list of available templates.
-
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 -
Repeat the above steps to add Call Templates for 'Clemency' and 'Pine Valley' hospitals. Add clemency and pinevalley as the respective parameter values.
-
Drag a Call mediators from the Mediators pallette after the Call Template mediators in each switch sequence.
-
Then, add the GrandOakEP, ClemencyEP, and PineValleyEP endpoints from the Defined Endpoints palette to the empty boxes adjoining the Call mediator.
-
Drag a Respond mediator to return the response from the health care service back to the client.
-
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.
- Open the
pom.xml
file in the composite exporter module. -
Ensure that the following artifacts are selected in the POM file.
HealthcareAPI
HospitalRoutingSeq
-
Save the changes.
Step 4: Build and run the artifacts¶
To test the artifacts, deploy the packaged artifacts in the embedded Micro Integrator:
- Right-click the composite exporter module and click Export Project Artifacts and Run.
- In the dialog box that opens, confirm that the required artifacts from the composite exporter module are selected.
- 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¶
- Download the JAR file of the back-end service from here.
- Open a terminal, navigate to the location where your 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 the embedded HTTP Client of WSO2 Integration Studio as follows:
-
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.
-
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": "[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 names
request.json
with the following request payload.{ "name": "John Doe", "dob": "1940-03-19", "ssn": "234-23-525", "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 -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":"[email protected]"},
"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