Wednesday, July 22, 2015

Dynamic Address Resolution For Endpoints used in WSO2 ESB

 In real business situations , there are different environments (DEV , QA , PREPROD , PRODUCTION) and endpoints may get changed accordingly.  But it is very hard to change endpoint urls from one environment to another.

We can introduce some workarounds for dynamic address resolution .We can load endpoint urls from registry resources and substitute those values at the run time. Following is an example on how to configure it.

<inSequence>
        <property name="AppServer"
                  expression="get-property('registry','gov:/serverdetails/servers.xml')"
                  scope="default"
                  type="OM"/>
        <property name="AppServerIP"
                  expression="$ctx:AppServer//as/ip/text()"
                  scope="default"
                  type="STRING"/>
        <property name="AppServerPort"
                  expression="$ctx:AppServer//as/port/text()"
                  scope="default"
                  type="STRING"/>
        <property name="service_ep"
                  expression="fn:concat('http://' ,get-property('AppServerIP'), ':' , get-property('AppServerPort') ,'//services/TargetService')"/>
        <header name="To" expression="get-property('service_ep')"/>
        <send>
           <endpoint key="TargetEndpoint"/>
        </send>
     </inSequence>


<endpoint xmlns="http://ws.apache.org/ns/synapse" name="TargetEndpoint">
  <default>
     <suspendOnFailure>
        <progressionFactor>1.0</progressionFactor>
     </suspendOnFailure>
     <markForSuspension>
        <retriesBeforeSuspension>0</retriesBeforeSuspension>
        <retryDelay>0</retryDelay>
     </markForSuspension>
  </default>
</endpoint>

It will read servers.xml file stored in registry and load the values at runtime.