Tuesday, October 27, 2015

How to use Switch Mediator in WSO2 ESB

Whenever in ESB logic a conditional filter is required and based on the value of the filter a particular functionality needs to be executed, switch mediator can be used. This switch mediator is same like as we have for other programming language in java, .net, php etc. so overall concept is same in WSO2 as well.

In WSO2 ESB, switch mediator filters the XPATH or JSON. We can pass an expression value in it to filter the value and based on the value of an expression a case is executed if it is matched with filtered string.


You can add switch mediator by the UI configuration it comes in the Filter category.




There are some parameter you need to pass to use this Switch Mediator like Source Xpath and number of case as per your logic. At the last there is a default mediator option you can use that to execute if no match is found.



Example:

<switch source="get-property('GetServiceName')">
<case regex="GetEmployeeDetail">
<send>
<endpoint>
<address uri="http://localhost:8280/services/getEmployeeDetail"/>
</endpoint>
</send>
</case >
<case regex="GetDepartmentDetail">
<send>
<endpoint>
<address uri="http://localhost:8280/services/getDepartmnetDetail"/>
</endpoint>
</send>
</case>
<case regex="GetITHeadDetail">
<send>
<endpoint>
<address uri="http://localhost:8280/services/getITHeadDetail"/>
</endpoint>
</send>
</case>
<default>
<send/>
<drop/>
</default>
</switch>


In above example we are trying to call the different service based on the ‘'GetServiceName'’ parameter. If the string value of 'GetServiceName' variable is ‘GetEmployeeDetail’ then call ‘getEmployeeDetail’ service and if value is ‘GetDepartmentDetail’ then call ‘getDepartmnetDetail’ service. There is a default case in which we are dropping the response back to the client. If no case is found by the filtered parameter then default case is executed.

No comments:

Post a Comment