Routing Requests Based on Message Content
In the Sending a Simple Message to a Service Using the ESB Profile tutorial, we routed a simple message to a single endpoint in the back-end service. In this tutorial, we are building on the same sequence, by creating the mediation artifacts that can route a message to the relevant endpoint, depending on the content of the message payload.
When the client sends the appointment reservation request to the ESB, the message payload of the request contains the name of the hospital where the appointment needs to be confirmed. The HTTP request method that is used for this is POST. Based on the hospital name sent in the request message, the ESB should route the appointment reservation to the relevant hospital's back-end service.
In this tutorial, you will use a Switch mediator to route messages based on the message content to the relevant HTTP Endpoint defined in the ESB. The back-end service used in this example is a micro service stored in the MSF4J profile of WSO2 EI.
For more details on how routing of messages within the ESB is done based on the message content, refer Content-Based Router Enterprise Integration Pattern.
Before you begin,
Download the product installer from here, and run the installer.
Select and download the relevant EI tooling ZIP file based on your operating system from here and then extract the ZIP file.
The path to this folder will be referred to as<EI_TOOLING>throughout this tutorial.If you did not try the Sending a Simple Message to a Service Using the ESB Profile tutorial yet, open the WSO2 EI Tooling environment, click File, and click Import.
Next, expand the WSO2 category and select Existing WSO2 Projects into workspace, click Next and upload the pre-packaged project.
This contains the configurations of the Sending a Simple Message to a Service Using the ESB Profile tutorial so that you do not have to repeat those steps.Download the MSF4J service from here and copy the JAR file to the
<EI_HOME>/wso2/msf4j/deployment/microservicesdirectory. The back-end service is now deployed in the MSF4J profile of WSO2 EI.
Let's get started!
This tutorial contains the following sections:
Connecting to the back-end service
In this tutorial we have three hospital backend services hosted in the MSF4J profile of WSO2 EI:
Grand Oak Community Hospital:
http://localhost:9090/grandoaks/Pine Valley Community Hospital:
http://localhost:9090/pinevalley/
The request method is POST and the format of the request URL expected by the backend services is: http://localhost:9090/grandoaks/categories/{category}/reserve
Let's create three different HTTP endpoints for the above services.
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:
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}/reservePineValleyEP:
http://localhost:9090/pinevalley/categories/{uri.var.category}/reserve
You have now created the three endpoints for the hospital backend services that will be used to make appointment reservations.
You can also create a single endpoint where the differentiation of the hospital name can be handled using a variable in the URI template. See the following tutorial: Exposing Several Services as a Single Service.
Using three different endpoints is advantageous when the back-end services are very different from one another and/or when there is a requirement to configure error handling differently for each of them.
Mediating requests to the back-end service
To implement the routing scenario, let's update the REST API we created in the previous section by adding a new API resource. We will then use a Switch mediator to route the message to the relevant back-end service based on the hospital name that is passed in the payload of the request message.
Let’s update the REST API we created in the previous tutorial using WSO2 EI Tooling.
In the REST API configuration, select API Resource in the API palette and drag it onto the canvas just below the previous API resource that was created.
add a second APIClick the API Resource you just added to access the Properties tab and fill in the following details:
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 fill in the following details:
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.
In this scenario, we are assuming there are three different hospitals, hence there are three cases.Enter 3 for Number of branches and click OK.
With the Switch mediator selected, go to the Properties tab, and fill in the details given below:
Let's add a Log mediator to print a message indicating to which hospital the request message is being routed.
Drag a Log mediator to the first Case box of the Switch mediator, and name it Grand Oak Log.
With the Log mediator selected, access the Properties tab and fill in the information given in the table below:
Add a Send mediator adjoining the Log mediator and add the GrandOakEP endpoint from Defined Endpoints palette to the empty box adjoining the Send mediator.
Add Log mediators in the other two Case boxes in Switch mediator and then enter the same properties as in Step 10.
Make sure to name the two Log mediators asClemency LogandPine Valley Logrespectively.Add Send mediators adjoining these log mediators and respectively add the ClemencyEP and PineValleyEP endpoints from the Defined Endpoints palette.
Add a Log mediator to the Default (the bottom box) of the Switch mediator and configure it the same way you did for the Log mediator above.
The default case of the Switch mediator handles the invalid hospital requests that are sent to the request payload. This logs the message
Invalid hospital - <Hospital Name>" for requests that have the invalid hospital name.Drag a Respond mediator next to the Log mediator you just added.
This ensures that there is no further processing of the current message and returns the request message back to the client.
The In Sequence of the API resource configuration should now look like this:
final in sequence configurationDrag a Send mediator to the Out sequence of the API resource, to send the response back to the client.
Save the updated REST API configuration.