Wednesday, September 2, 2015

How to set time delay between two mediators or delay in request in WSO2 ESB

There are some scenarios when we have requirement like to put delay between the request and response. ESB provides facility to add java script mediator in it. So below is the function of java script, can be used to put delay between two mediators.

Syntax:

<script language="js">java.lang.Thread.sleep(12000);</script>

Example:

Below is the pass-through proxy and in this example we want ESB to wait 12 second before sending the request to end point.

<!-- Start of GetEmployeeDetailsProxy-->

<proxy name="GetEmployeeDetailsProxy" transports="https http" startOnLoad="true" trace="disable" statistics="enable">
<target inSequence="GetEmployeeDetailsProxy_IN" outSequence="GetEmployeeDetailsProxy_OUT" faultSequence="CommonFaultHandler"/>
<publishWSDL key="GetEmployeeDetailsProxy_wsdl"/>
</proxy>

<localEntry key="GetEmployeeDetailsProxy_wsdl" src="file:repository/conf/employee/resources/proxy/employeeDetails.wsdl"/>

<sequence name="GetEmployeeDetailsProxy_IN">
<log level="full"/>
<script language="js">java.lang.Thread.sleep(25000);</script>
<send>
<endpoint key="GetEmployeeDetailsProxy_EPR"/>
</send>
</sequence>


<endpoint name="GetEmployeeDetailsProxy_EPR">  
<address uri="http://xxxx:8280/services/GetEmployeeDetailsProxy">      
<timeout>
<duration>30000</duration>
<responseAction>fault</responseAction>
</timeout>
<suspendOnFailure>
<errorCodes>101500,101501,101506,101507,101508</errorCodes>
<initialDuration>3000</initialDuration>
<progressionFactor>1.0</progressionFactor>
</suspendOnFailure>
<markForSuspension>
<errorCodes>101504,101505</errorCodes>
<retriesBeforeSuspension>3</retriesBeforeSuspension>
<retryDelay>1</retryDelay>
</markForSuspension>
</address>
</endpoint>

<sequence name="GetEmployeeDetailsProxy_OUT">
<log level="full"/>
<send/>
</sequence>

<!-- End of GetEmployeeDetailsProxy-->


In above example you can see that in IN sequence we have put java script sleep function before sending request and the sleep time is 12 second. To ESB will wait for 12 second before sending request to end point.