Exposing Several Services as a Single Service
In WSO2 ESB this is commonly referred to as Service Chaining where several services are integrated based on some business logic and exposed as a single, aggregated service.
In this tutorial, you send a message through the ESB to the back-end service using the Call mediator, instead of the Send mediator. Using the Call mediator, you can build a service chaining scenario as it allows you to specify all service invocations one after the other within a single sequence.
You then use the PayloadFactory mediator to take the response from one back-end service and change it to the format that is accepted by the other back-end service.
The diagram below depicts the sample scenario:
See the following topics for a description of the concepts that you need to know when creating ESB artifacts:
Before you begin,
Install Oracle Java SE Development Kit (JDK) version 1.8.* and set the JAVA_HOME environment variable.
Go to http://wso2.com/products/enterprise-service-bus/, click DOWNLOAD to download the ESB runtime ZIP file, and then extract the ZIP file.
The path to this folder will be referred to as<ESB_HOME>throughout the tutorials.Go to http://wso2.com/products/enterprise-service-bus/, click Tooling to select and download the relevant ESB tooling ZIP file, and then extract the ZIP file.
The path to this folder will be referred to as<TOOLING_HOME>throughout the tutorials.For more detailed installation instructions, see the Installing WSO2 ESB Tooling.
Open the ESB Tooling environment and click File -> Import. Then, select Existing WSO2 Projects into workspace under the WSO2 category, click Next and upload the pre-packaged C-App project. This C-App contains the configurations of the previous tutorial so that you do not have to repeat those steps.
Let's get started!
Connecting to the back-end services
Let's create HTTP endpoints to the back-end services that you need to connect to check channeling fee and settle payment.
Right click SampleServices in the Project Explorer and navigate to New -> Endpoint. Ensure Create a New Endpoint is selected and click Next.
Fill in the information as in the following table:
Field
Value
Endpoint Name
ChannelingFeeEP
Endpoint Type
HTTP Endpoint
URI Template
Method
GET
Static Endpoint
(Select this option because we are going to use this endpoint in this ESB project only and will not re-use it in other projects.)
Save Endpoint in
SampleServices
Click Finish.
Create another endpoint for Settle Payment and fill in the information as in the following table:
Field
Value
Endpoint Name
SettlePaymentEP
Endpoint Type
HTTP Endpoint
URI Template
http://wso2training-restsamples.wso2apps.com/healthcare/payments
Method
POST
Static Endpoint
(Select this option because we are going to use this endpoint in this ESB project only and will not re-use it in other projects.)
Save Endpoint in
SampleServices
Click Finish.
You have now created the additional endpoints that are required for this tutorial.
Creating the deployable artifacts
In WSO2 ESB Tooling, add a Property mediator just after the Get Hospital Property mediator in the In sequence of the API resource to retrieve and store the card number that is sent in the request payload. With the Property mediator selected, access the Properties tab and fill in the information as in the following table:
Field
Value
Property Name
Select New Property
New Property Name
card_number
URI Template
Select set
Value Type
Select Expression
Value Expression
json-eval($.cardNo)
Description
Get Card Number
For detailed instructions on adding a Properymediator, see Mediating requests to the back-end service.
Go to the first case box of the Switch mediator. Add a Property mediator just after the Log mediator to store the value for
uri.var.hospitalvariable that will be used when sending requests to ChannelingFeeEP.
With the Property mediator selected, access the Properties tab and fill in the information as in the following table:Field
Value
Property Name
Select New Property
New Property Name
uri.var.hospital
URI Template
Select set
Value Type
Select LITERAL
Property Data Type
Select STRING
Value
grandoaks
Description
Set Hospital Variable
Similarly, add property mediators in the other two case boxes in the Switch mediator. Change only the Value field as follows:
Case 2 : clemency
Case 3: pinevalley
Delete the Send mediator by right clicking on the mediator and selecting Delete from Model. Replace this with a Call mediator from the Mediators palette and add GrandOakEP from Defined Endpoints palette to the empty box adjoining the Call mediator.
Replace the Send mediators in the following two case boxes as well and add ClemencyEP and PineValleyEP to the respective boxes adjoining the Call mediators.
Replacing with a Call mediator allows us to define other service invocations following this mediator.
Let's use Property mediators to retrieve and store the values that you get from the response you receive from GrandOakEP, ClemencyEP or PineValleyEP.
Next to the Call mediator box, add a Property mediator to retrieve and store the value sent as
appointmentNumber.With the Property mediator selected, access the Properties tab and fill in the information as in the following table:Field
Value
Property Name
Select New Property
New Property Name
uri.var.appointment_id
(This value is used when invoking ChannelingFeeEP)URI Template
Select set
Value Type
Select EXPRESSION
Value Expression
json-eval($.appointmentNumber)Description
Get Appointment Number
You derive the Value Expression in the above table from the following response that is received from GrandOakEP, ClemencyEP or PineValleyEP:
{"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":"johndoe@gmail.com"}, "fee":7000.0, "confirmed":false}Similarly, add two more Property mediators as follows. They retrieve and store the
doctordetails andpatientdetails respectively, from the response that is received from GrandOakEP, ClemencyEP or PineValleyEP.Field
Value
Property Name
Select New Property
New Property Name
doctor_detailsURI Template
Select set
Value Type
Select EXPRESSION
Value Expression
json-eval($.doctor)Description
Get Doctor Details
Property Name
Select New Property
New Property Name
patient_detailsURI Template
Select set
Value Type
Select EXPRESSION
Value Expression
json-eval($.patient)Description
Get Patient Details
Add a Call mediator and add ChannelingFeeEP from Defined Endpoints palette to the empty box adjoining the Call mediator.
Add a Property mediator adjoining the Call mediator box to retrieve and store the value sent as
actualFee.Access the Properties tab of the mediator and fill in the information as in the following table:Field
Value
Property Name
Select New Property
New Property Name
actual_fee(This value is used when invoking SettlePaymentEP )URI Template
Select set
Value Type
Select EXPRESSION
Value Expression
json-eval($.actualFee)Description
Get Actual Fee
You derive the Value Expression in the above table from the following response that is received from ChannelingFeeEP:
{"patientName":" John Doe ", "doctorName":"thomas collins", "actualFee":"7000.0"}Let's use the PayloadFactory mediator to construct the following message payload for the request sent to SettlePaymentEP.
{"appointmentNumber":2, "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":"johndoe@gmail.com" }, "Fee":7000.0, "Confirmed":false, "card_number":"1234567890" }Next to the Property mediator, add a PayloadFactory mediator from the mediators palette to construct the above message payload. With the Payloadfactory mediator selected, access the properties tab of the mediator and fill in the information as in the following table:
Field
Value
Description
Payload Format
Select Inline
-
Payload
{
"appointmentNumber":$1,
"doctor":$2,
"patient":$3,
"fee":$4,
"confirmed":"false",
"card_number":"$5"
}The message payload to send with the request to SettlePaymentEP. In this payload,
$1,$2,$3,$4and$5indicate variables.Media Type
Select json
-
We will look at adding the value for the field Args in the following steps.
To avoid getting an error message, first select the Media Type before providing the Payload.
To add the Args field for the PayloadFactory mediator, click the Browse (...) icon in the Args field. Click on the Add button and enter the following information as in the table below. It provides the argument that defines the actual value of the first variable used in the format definition in the previous step.
Field
Value
Description
Type
Select Expression
-
Value
$ctx:uri.var.appointment_id
The value for the first variable ($1) in the message payload format.
Evaluator
Select xml
Indicates that the expression provided is in XML.
The
$ctxmethod is similar to using theget-propertymethod. This method checks in the message context. For more details on using this method, refer the documentation.Similarly, click Add and add more arguments to define the other variables that are used in the message payload format definition. Use the following as the Value for each of them:
$ctx:doctor_details
$ctx:patient_details
$ctx:actual_fee
$ctx:card_number
Add a Call mediator and add SettlePaymentEP from Defined Endpoints palette to the empty box adjoining the Call mediator.
Add a Respond mediator to send the response to the client.
You should now have a completed configuration that looks like this:
Save the updated REST API configuration.