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. You can reuse the mediation flow that was defined in the Service Orchestration tutorial and then replace its sections with the sequence template. See Creating Templates for details on how to work with templates using WSO2 Integration Studio.
Let's get started!¶
Step 1: Set up the workspace¶
Set up WSO2 Integration Studio as follows:
- Download the relevant WSO2 Integration Studio based on your operating system.
-
Set up the project from the Service Orchestration tutorial:
Note
This tutorial is a continuation of the Service Orchestration tutorial.
- Download the pre-packaged project.
- Open WSO2 Integration Studio and go to File -> Import.
- Select Existing WSO2 Projects into workspace under the WSO2 category, click Next, and then upload the prepackaged project.
Step 2: Develop the integration artifacts¶
Create a Sequence Template¶
-
Once you have exported the integration project as described in above, the project directory will appear with the artifacts as shown below. Note the 'HealthcareAPI' that is already included.
-
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
Update the mediation flow¶
-
Open the design view of the HealthcareAPI.xml and delete 'GrandOak' Log mediator by right clicking the mediator and selecting Delete from Model .
-
Delete the 'Set Hospital Variable' Property mediator.
-
Add a Call Template mediator to the sequence as shown below.
-
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.
-
Save the configuration.
After completion, your API will be similar to this.
<?xml version="1.0" encoding="UTF-8"?> <api context="/healthcare" name="HealthcareAPI" xmlns="http://ws.apache.org/ns/synapse"> <resource methods="GET" uri-template="/querydoctor/{category}"> <inSequence> <log description="Request Log" level="custom"> <property name="Log Property message" value=""Welcome to HealthcareService""/> </log> <send> <endpoint key="QueryDoctorEP"/> </send> </inSequence> <outSequence> <send/> </outSequence> <faultSequence/> </resource> <resource methods="POST" uri-template="/categories/{category}/reserve"> <inSequence> <property description="Get Hospital" expression="json-eval($.hospital)" name="Hospital" scope="default" type="STRING"/> <property description="Get Card Number" expression="json-eval($.cardNo)" name="card_number" scope="default" type="STRING"/> <datamapper config="gov:datamapper/RequestMapping.dmc" inputSchema="gov:datamapper/RequestMapping_inputSchema.json" inputType="JSON" outputSchema="gov:datamapper/RequestMapping_outputSchema.json" outputType="JSON" xsltStyleSheet="gov:datamapper/RequestMapping_xsltStyleSheet.xml"/> <switch source="get-property('Hospital')"> <case regex="grand oak community hospital"> <call-template target="HospitalRoutingSeq"> <with-param name="sethospital" value="grandoaks"/> </call-template> <call> <endpoint key="GrandOakEP"/> </call> </case> <case regex="clemency medical center"> <call-template target="HospitalRoutingSeq"> <with-param name="sethospital" value="Clemency"/> </call-template> <call> <endpoint key="ClemencyEP"/> </call> </case> <case regex="pine valley community hospital"> <call-template target="HospitalRoutingSeq"> <with-param name="sethospital" value="Pine Valley"/> </call-template> <call> <endpoint key="PineValleyEP"/> </call> </case> <default> <log description="Fault Log" level="custom"> <property expression="fn:concat('Invalid hospital - ', get-property('Hospital'))" name="message"/> </log> <respond/> </default> </switch> <property description="Get Appointment Number" expression="json-eval($.appointmentNumber)" name="uri.var.appointment_id" scope="default" type="STRING"/> <property description="Get Doctor Details" expression="json-eval($.doctor)" name="doctor_details" scope="default" type="STRING"/> <property description="Get Patient Details" expression="json-eval($.patient)" name="patient_details" scope="default" type="STRING"/> <call> <endpoint key="ChannelingFeeEP"/> </call> <property description="Get Actual Fee" expression="json-eval($.actualFee)" name="actual_fee" scope="default" type="STRING"/> <payloadFactory media-type="json"> <format>{"appointmentNumber":$1, "doctor":$2, "patient":$3, "fee":$4, "confirmed":"false", "card_number":"$5"}</format> <args> <arg evaluator="xml" expression="$ctx:uri.var.appointment_id"/> <arg evaluator="xml" expression="$ctx:doctor_details"/> <arg evaluator="xml" expression="$ctx:patient_details"/> <arg evaluator="xml" expression="$ctx:actual_fee"/> <arg evaluator="xml" expression="$ctx:card_number"/> </args> </payloadFactory> <call> <endpoint key="SettlePaymentEP"/> </call> <respond/> </inSequence> <outSequence/> <faultSequence/> </resource> </api>
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": "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": "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 response received to your HTTP Client:
{"appointmentNo":2,"doctorName":"thomas collins","patient":"John Doe","actualFee":7000.0,"discount":20,"discounted":5600.0,"paymentID":"cc7e4c23-a66d-4d60-8b72-2cf9143c6335","status":"Settled"}
Top