WSO2 EI – Content Based Routing

Wednesday December 8, 2021

Content-based routing is one of the common patterns in many integration platforms. Here we will discuss how we are going to achieve it using WSO2 Enterprise Integrator (EI). In simple terms, Content-Based Routing means checking the message content and routing it to the intended flow of execution based on the content. This simplifies the integration development when it is needed to execute different flows depending on variations of the message content while enhancing the code reusability. It is not that difficult to achieve using the WSO2 EI.

There are a few ways to do this. In this article, we will be using an API service with the Switch Mediator which is a built-in synapse mediator packed inside the WSO-EI. The switch mediator facilitates checking message payload and allows us to direct the message in multiple flows.

So where do we begin?

Let’s assume that the client sends the following JSON payload and the value of the ‘symbol’ field varies as ‘IBM’, ‘MSFT’, and ‘Oracle’.

{
   "getQuote":
   {
      "request":{"company":"IBM" }
   }
}

We can use the below switch mediator configuration to check the value of ‘symbol’ and route the request to the corresponding path.

<switch source="json-eval($.getQuote.request.company)">
   <case regex="IBM">
      <send>
         <endpoint>
            <http uri-template="http://run.mocky.io/v3/016870a2-622f-4bb3-9d96-39c02a89d9c0" />
         </endpoint>
      </send>
   </case>
   <case regex="MSFT">
      <send>
         <endpoint>
            <http uri-template="http://run.mocky.io/v3/8477318d-5035-4e2c-b436-fdde88c901aa" />
         </endpoint>
      </send>
   </case>
   <case regex="Oracle">
      <send>
         <endpoint>
            <http uri-template="http://run.mocky.io/v3/e6b3ea9b-d52b-421c-abd0-d980c65caa09" />
         </endpoint>
      </send>
   </case>
   <default>
      <payloadFactory media-type="json">
         <format>{"Error":"Invalid company name. Please set a valid value"}</format>
         <args />
      </payloadFactory>
   </default>
</switch>

If the switch mediator found the value of ‘symbol’ matches with any of the cases, it will select that particular flow of execution and invoke the endpoint. If none of the cases match, it will execute the default flow and return the error message.

Here is the complete API service with the above switch mediator logic.

<?xml version="1.0" encoding="UTF-8"?>
<api xmlns="http://ws.apache.org/ns/synapse" name="TestAPI" context="/routing">
   <resource methods="POST">
      <inSequence>
         <switch source="json-eval($.getQuote.request.company)">
            <case regex="IBM">
               <send>
                  <endpoint>
                     <http uri-template="http://run.mocky.io/v3/016870a2-622f-4bb3-9d96-39c02a89d9c0" />
                  </endpoint>
               </send>
            </case>
            <case regex="MSFT">
               <send>
                  <endpoint>
                     <http uri-template="http://run.mocky.io/v3/8477318d-5035-4e2c-b436-fdde88c901aa" />
                  </endpoint>
               </send>
            </case>
            <case regex="Oracle">
               <send>
                  <endpoint>
                     <http uri-template="http://run.mocky.io/v3/e6b3ea9b-d52b-421c-abd0-d980c65caa09" />
                  </endpoint>
               </send>
            </case>
            <default>
               <payloadFactory media-type="json">
                  <format>{"Error":"Invalid company name. Please set a valid value"}</format>
                  <args />
               </payloadFactory>
            </default>
         </switch>
         <respond />
      </inSequence>
   </resource>
</api>

After adding the above API to the EI, you can test it by setting different values to the given request payload.

This is a simple API service to test the concept of content-based routing in EI. You can achieve more advanced use cases by configuring the above flow according to your requirements. We hope this was a good read.

– Dilhara Hewamaddumage

// OUR KNOWLEDGEBASE Find More Interesting Reads

WSO2 EI – Scatter and Gather);

WSO2 EI – Scatter and Gather

4Ps of ERP Support);

4Ps of ERP Support

From ESG to EESG);

From ESG to EESG

want to start your project?

Out experts specialise in Integrations, ERP Services, BA/BI Requirements

Sri Lanka Office
Denmark Office