Content-Based Message Routing¶
Overview¶
In this tutorial, you'll create a service that allows users to reserve appointments at various hospitals. Requests will be directed to the appropriate hospital based on the request payload's content. To accomplish this, you’ll build a REST service with a single resource in WSO2 Integrator: BI extension. The resource will handle user requests, identify the hospital endpoint based on the hospital ID, forward the request to the specified hospital service to make the reservation, and return the reservation details.
Here’s an overview of the process flow.
-
Receive a request with a JSON payload similar to the following.
ReservationRequest.json2. Extract the{ "patient": { "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", "hospital_id": "grandoak", "appointment_date": "2023-10-02" }hospital_idfield and select the corresponding hospital service endpoint.- grandoak ->
http://localhost:9090/grandoak/categories - clemency ->
http://localhost:9090/clemency/categories - pinevalley ->
http://localhost:9090/pinevalley/categories
- grandoak ->
-
Forward the request to the selected hospital service and retrieve the response which will be similar to the following.
ReservationResponse.json{ "appointmentNumber": 8, "doctor": { "name": "thomas collins", "hospital": "grand oak community hospital", "category": "surgery", "availability": "9.00 a.m - 11.00 a.m", "fee": 7000.0 }, "patientName": "John Doe", "hospital": "grand oak community hospital", "confirmed": false, "appointmentDate": "2023-10-02" }
Prerequisites¶
- Docker installed on your machine.
Step 1: Create a new integration project¶
- Click on the BI icon on the sidebar.
- Click on the Create New Integration button.
- Enter the project name as
MessageRouting. - Select Project Directory and click on the Select Location button.
- Click on the Create New Integration button to create the integration project.
Step 2: Create an HTTP service¶
- In the design view, click on the Add Artifact button.
- Select HTTP Service under the Integration as API category.
- Select the + Listeners option from the Listeners dropdown to add a new listener.
- Add the service base path as
/healthcareand select the Design from Scratch option as the The contract of the service. - Enter the listener name as
healthListener,8290as the port in Advanced Configurations. -
Click on the Save button to create the new service with the specified configurations.
Step 3: Define types¶
- Click on the Add Artifacts button and select Type in the Other Artifacts section.
- Click on + Add Type to add a new type
- Add the Name as
ReservationRequestand paste the following JSON payload. Click on the Import button.{ "patient": { "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", "hospital_id": "grandoak", "appointment_date": "2023-10-02" } - Repeat the above steps to add a new type named
ReservationResponsewith the following JSON payload.{ "appointmentNumber": 8, "doctor": { "name": "thomas collins", "hospital": "grand oak community hospital", "category": "surgery", "availability": "9.00 a.m - 11.00 a.m", "fee": 7000.0 }, "patientName": "John Doe", "hospital": "grand oak community hospital", "confirmed": false, "appointmentDate": "2023-10-02" } -
The final Type diagram will look like below.
Step 4: Add connectors¶
- Navigate to design view and click on the Add Artifacts button and select Connection in the Other Artifacts section.
- Search and select the HTTP Client connector.
- Enter the connector name as
grandOakEp, URL as"http://localhost:9090/grandoak/categories". -
Click on the Save button to create the new connector with the specified configurations.
5. Repeat the above steps to add connectors for the clemencyandpinevalleyhospitals with the following configurations.Connector Name URL clemencyEp "http://localhost:9090/clemency/categories"pineValleyEp "http://localhost:9090/pinevalley/categories" -
The final connectors will look like below.
HTTP Connector
To learn more about HTTP client, see Ballerina HTTP Client. See supported advanced client configurations in the HTTP Client Configurations.
Step 5: Add a resource method¶
- Click Add Resource and select POST method.
- Set the resource path as
categories/[string category]/reserve. - Define the payload type as
ReservationRequest. - Change the 201 response return type to
ReservationStatus. - Add a new response of type HttpNotFound under the responses.
-
Click on the Save button to save the resource.
Step 6: Add the routing logic¶
- Click on the
categories/[string category]/reserveresource to navigate to the resource implementation designer view. - Delete the default
Returnaction from the resource. - Hover to the arrow after start and click the ➕ button to add a new action to the resource.
- Select Declare Variable from the node panel on the left. This variable will be used to store the request payload for the hospital service.
-
Change the variable name to
hospitalRequest, type asjsonand expression as below and click Save.{ patient: payload.patient.toJson(), doctor: payload.doctor, hospital: payload.hospital, appointment_date: payload.appointment_date } -
Add If from the node panel after
hospitalRequestvariable. Enter the conditions as If Else If blocks as below for each hospital.- grandOak ->
payload.hospital_id == "grandoak" - clemency ->
payload.hospital_id == "clemency" - pineValley ->
payload.hospital_id == "pinevalley"
- grandOak ->
-
Select the
grandOakEPcondition true path ➕ sign and select grandOakEP connector from the node panel and select post from the dropdown. Then, fill in the required fields with the values given below.Field Value Variable Name oakEPResponseVariable Type ReservationResponsePath string `/${category}/reserve`Message hospitalRequest -
Click Save.
-
Click on the ➕ sign again and select Return from the node panel. Select the
oakEPResponsevariable from the dropdown and click Save. -
The steps above will add the routing logic for the
grandoakhospital. A variable namedoakEPResponsewill store the response from thegrandoakhospital service. The response will be returned to the client. -
Repeat the 7,8,9 steps for the
clemencyandpinevalleyhospitals with the following configurations.clemency:
Field Value Variable Name clemencyEPResponseVariable Type ReserveResponsePath /${category}/reserveMessage hospitalRequestpinevalley:
Field Value Variable Name pineValleyEPResponseVariable Type ReserveResponsePath /${category}/reserveMessage hospitalRequest -
For the else condition, click on the
IfconditionElsepath ➕ sign and add a Return from the node panel. Enterhttp:NOT_FOUNDas the value and click Save. -
The final design will look like below.
Step 7: Run the service¶
- Start the backend service by executing the following command in a terminal.
docker run --name hospital-backend -p 9090:9090 -d anuruddhal/kola-hospital-backend - Click on the Run on the run button in the top right corner to run the service.
- The service will start and the service will be available at
http://localhost:8290/healthcare/categories/[category]/reserve. - Click on the Try it button to open the embedded HTTP client.
-
Replace the {category} with
surgeryin the resource path and enter the following JSON payload in the request body and click on the ▶️ button to send the request.{ "patient":{ "name": "John Doe", "dob": "1940-03-19", "ssn": "234-23-525", "address": "California", "phone": "8770586755", "email": "[email protected]" }, "doctor": "thomas collins", "hospital_id": "grandoak", "hospital": "grand oak community hospital", "appointment_date": "2023-10-02" }
-
The response will be similar to the following.
{ "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-525", "address": "California", "phone": "8770586755", "email": "[email protected]" }, "hospital": "grand oak community hospital", "confirmed": false, "appointmentDate": "2023-10-02" } - Optionally, you can test the service using curl command as below.
curl -X POST "http://localhost:8290/healthcare/categories/surgery/reserve" \ -H "Content-Type: application/json" \ -d '{ "patient": { "name": "John Doe", "dob": "1940-03-19", "ssn": "234-23-525", "address": "California", "phone": "8770586755", "email": "[email protected]" }, "doctor": "thomas collins", "hospital_id": "grandoak", "hospital": "grand oak community hospital", "appointment_date": "2023-10-02" }'
Step 8: Stop the integration¶
- Click on the Stop button to stop the integration.
- Stop the hospital backend server by running the following command:
docker stop hospital-backend









