Thursday, September 3, 2015

How to send request in multiple endpoints in WSO2 ESB

If there is a scenario in which we have to send a request to multiple endpoints then this can be achieved by the Recipient List Endpoints. It can contain multiple child endpoints in which we have send request.  It routes cloned copies of messages to each child recipient.

Syntax:

<recipientlist>
    <endpoint .../>+
</recipientlist>

As illustrated below diagram an incoming message is transferred to any number of endpoint defined inside of <recipientlist> tag.  



Example:

In below example we are trying to send same message to three different endpoints which have been defined inside of send mediator.

<!-- 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>
<!--Dynamic Recipient List-->
<endpoint>
<recipientlist>
<endpoint>
<address uri="http://xxxx1:8280/services/GetEmployeeDetailsProxy"/>
</endpoint>
<endpoint>
<address uri="http://xxxx2:8280/services/GetEmployeeDetailsProxy"/>
</endpoint>
<endpoint>
<address uri="http://xxxx3:8280/services/GetEmployeeDetailsProxy"/>
</endpoint>
</recipientlist>
</endpoint>         
</send>
</sequence>
<sequence name="GetEmployeeDetailsProxy_OUT">
<log level="full"/>
<send/>
</sequence>
<!-- End of GetEmployeeDetailsProxy-->

There are 3 endpoint defined and request will send to each endpoint. Endpoint can be increased as required, just need to add endpoint entry in synapse.


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.